1
0
mirror of synced 2024-11-22 09:14:23 +01:00

Do a pylint pass

This commit is contained in:
jvoisin 2018-05-16 22:36:59 +02:00
parent 0354c3b7e3
commit fa7d18784c
9 changed files with 89 additions and 79 deletions

View File

@ -48,6 +48,7 @@ def show_meta(filename:str):
if p is None: if p is None:
print("[-] %s's format (%s) is not supported" % (filename, mtype)) print("[-] %s's format (%s) is not supported" % (filename, mtype))
return return
print("[+] Metadata for %s:" % filename) print("[+] Metadata for %s:" % filename)
for k, v in p.get_meta().items(): for k, v in p.get_meta().items():
try: # FIXME this is ugly. try: # FIXME this is ugly.
@ -55,11 +56,10 @@ def show_meta(filename:str):
except UnicodeEncodeError: except UnicodeEncodeError:
print(" %s: harmful content" % k) print(" %s: harmful content" % k)
def clean_meta(params: Tuple[str, bool]) -> bool: def clean_meta(params: Tuple[str, bool]) -> bool:
filename, is_lightweigth = params filename, is_lightweigth = params
if not __check_file(filename, os.R_OK|os.W_OK): if not __check_file(filename, os.R_OK|os.W_OK):
return return False
p, mtype = parser_factory.get_parser(filename) p, mtype = parser_factory.get_parser(filename)
if p is None: if p is None:
@ -102,12 +102,12 @@ def main():
if not args.list: if not args.list:
return arg_parser.print_help() return arg_parser.print_help()
show_parsers() show_parsers()
return return 0
elif args.show: elif args.show:
for f in __get_files_recursively(args.files): for f in __get_files_recursively(args.files):
show_meta(f) show_meta(f)
return return 0
else: else:
p = multiprocessing.Pool() p = multiprocessing.Pool()

View File

@ -2,4 +2,5 @@
# A set of extension that aren't supported, despite matching a supported mimetype # A set of extension that aren't supported, despite matching a supported mimetype
unsupported_extensions = set(['bat', 'c', 'h', 'ksh', 'pl', 'txt', 'asc', unsupported_extensions = set(['bat', 'c', 'h', 'ksh', 'pl', 'txt', 'asc',
'text', 'pot', 'brf', 'srt', 'rdf', 'wsdl', 'xpdl', 'xsl', 'xsd']) 'text', 'pot', 'brf', 'srt', 'rdf', 'wsdl',
'xpdl', 'xsl', 'xsd'])

View File

@ -6,6 +6,7 @@ class HarmlessParser(abstract.AbstractParser):
mimetypes = {'application/xml', 'text/plain'} mimetypes = {'application/xml', 'text/plain'}
def __init__(self, filename: str): def __init__(self, filename: str):
super().__init__(filename)
self.filename = filename self.filename = filename
self.output_filename = filename self.output_filename = filename

View File

@ -14,11 +14,12 @@ from . import abstract
class PNGParser(abstract.AbstractParser): class PNGParser(abstract.AbstractParser):
mimetypes = {'image/png', } mimetypes = {'image/png', }
meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName',
'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate', 'Directory', 'FileSize', 'FileModifyDate',
"FileInodeChangeDate", 'FilePermissions', 'FileType', 'FileAccessDate', 'FileInodeChangeDate',
'FileTypeExtension', 'MIMEType', 'ImageWidth', 'BitDepth', 'ColorType', 'FilePermissions', 'FileType', 'FileTypeExtension',
'Compression', 'Filter', 'Interlace', 'BackgroundColor', 'ImageSize', 'MIMEType', 'ImageWidth', 'BitDepth', 'ColorType',
'Megapixels', 'ImageHeight'} 'Compression', 'Filter', 'Interlace', 'BackgroundColor',
'ImageSize', 'Megapixels', 'ImageHeight'}
def __init__(self, filename): def __init__(self, filename):
super().__init__(filename) super().__init__(filename)
@ -63,24 +64,26 @@ class GdkPixbufAbstractParser(abstract.AbstractParser):
class JPGParser(GdkPixbufAbstractParser): class JPGParser(GdkPixbufAbstractParser):
mimetypes = {'image/jpeg'} mimetypes = {'image/jpeg'}
meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName',
'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate', 'Directory', 'FileSize', 'FileModifyDate',
"FileInodeChangeDate", 'FilePermissions', 'FileType', 'FileAccessDate', "FileInodeChangeDate",
'FileTypeExtension', 'MIMEType', 'ImageWidth', 'FilePermissions', 'FileType', 'FileTypeExtension',
'ImageSize', 'BitsPerSample', 'ColorComponents', 'EncodingProcess', 'MIMEType', 'ImageWidth', 'ImageSize', 'BitsPerSample',
'JFIFVersion', 'ResolutionUnit', 'XResolution', 'YCbCrSubSampling', 'ColorComponents', 'EncodingProcess', 'JFIFVersion',
'ResolutionUnit', 'XResolution', 'YCbCrSubSampling',
'YResolution', 'Megapixels', 'ImageHeight'} 'YResolution', 'Megapixels', 'ImageHeight'}
class TiffParser(GdkPixbufAbstractParser): class TiffParser(GdkPixbufAbstractParser):
mimetypes = {'image/tiff'} mimetypes = {'image/tiff'}
meta_whitelist = {'Compression', 'ExifByteOrder', 'ExtraSamples', meta_whitelist = {'Compression', 'ExifByteOrder', 'ExtraSamples',
'FillOrder', 'PhotometricInterpretation', 'PlanarConfiguration', 'FillOrder', 'PhotometricInterpretation',
'RowsPerStrip', 'SamplesPerPixel', 'StripByteCounts', 'PlanarConfiguration', 'RowsPerStrip', 'SamplesPerPixel',
'StripOffsets', 'BitsPerSample', 'Directory', 'ExifToolVersion', 'StripByteCounts', 'StripOffsets', 'BitsPerSample',
'FileAccessDate', 'FileInodeChangeDate', 'FileModifyDate', 'Directory', 'ExifToolVersion', 'FileAccessDate',
'FileName', 'FilePermissions', 'FileSize', 'FileType', 'FileInodeChangeDate', 'FileModifyDate', 'FileName',
'FileTypeExtension', 'ImageHeight', 'ImageSize', 'ImageWidth', 'FilePermissions', 'FileSize', 'FileType',
'MIMEType', 'Megapixels', 'SourceFile'} 'FileTypeExtension', 'ImageHeight', 'ImageSize',
'ImageWidth', 'MIMEType', 'Megapixels', 'SourceFile'}
class BMPParser(GdkPixbufAbstractParser): class BMPParser(GdkPixbufAbstractParser):
@ -88,11 +91,11 @@ class BMPParser(GdkPixbufAbstractParser):
meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', 'Directory', meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', 'Directory',
'FileSize', 'FileModifyDate', 'FileAccessDate', 'FileSize', 'FileModifyDate', 'FileAccessDate',
'FileInodeChangeDate', 'FilePermissions', 'FileType', 'FileInodeChangeDate', 'FilePermissions', 'FileType',
'FileTypeExtension', 'MIMEType', 'BMPVersion', 'ImageWidth', 'FileTypeExtension', 'MIMEType', 'BMPVersion',
'ImageHeight', 'Planes', 'BitDepth', 'Compression', 'ImageLength', 'ImageWidth', 'ImageHeight', 'Planes', 'BitDepth',
'PixelsPerMeterX', 'PixelsPerMeterY', 'NumColors', 'Compression', 'ImageLength', 'PixelsPerMeterX',
'NumImportantColors', 'RedMask', 'GreenMask', 'BlueMask', 'PixelsPerMeterY', 'NumColors', 'NumImportantColors',
'AlphaMask', 'ColorSpace', 'RedEndpoint', 'GreenEndpoint', 'RedMask', 'GreenMask', 'BlueMask', 'AlphaMask',
'BlueEndpoint', 'GammaRed', 'GammaGreen', 'GammaBlue', 'ImageSize', 'ColorSpace', 'RedEndpoint', 'GreenEndpoint',
'Megapixels'} 'BlueEndpoint', 'GammaRed', 'GammaGreen', 'GammaBlue',
'ImageSize', 'Megapixels'}

View File

@ -35,7 +35,8 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
return metadata return metadata
def _clean_internal_file(self, item:zipfile.ZipInfo, temp_folder:str, zin:zipfile.ZipFile, zout:zipfile.ZipFile): def _clean_internal_file(self, item: zipfile.ZipInfo, temp_folder: str,
zin: zipfile.ZipFile, zout: zipfile.ZipFile):
zin.extract(member=item, path=temp_folder) zin.extract(member=item, path=temp_folder)
tmp_parser, mtype = parser_factory.get_parser(os.path.join(temp_folder, item.filename)) tmp_parser, mtype = parser_factory.get_parser(os.path.join(temp_folder, item.filename))
if not tmp_parser: if not tmp_parser:

View File

@ -2,10 +2,10 @@ import os
import mimetypes import mimetypes
import importlib import importlib
import pkgutil import pkgutil
from typing import TypeVar
from . import abstract, unsupported_extensions from . import abstract, unsupported_extensions
from typing import TypeVar
T = TypeVar('T', bound='abstract.AbstractParser') T = TypeVar('T', bound='abstract.AbstractParser')

View File

@ -103,7 +103,8 @@ class PDFParser(abstract.AbstractParser):
return True return True
def __remove_superficial_meta(self, in_file:str, out_file: str) -> bool: @staticmethod
def __remove_superficial_meta(in_file: str, out_file: str) -> bool:
document = Poppler.Document.new_from_file('file://' + in_file) document = Poppler.Document.new_from_file('file://' + in_file)
document.set_producer('') document.set_producer('')
document.set_creator('') document.set_creator('')
@ -112,7 +113,8 @@ class PDFParser(abstract.AbstractParser):
return True return True
def __parse_metadata_field(self, data:str) -> dict: @staticmethod
def __parse_metadata_field(data: str) -> dict:
metadata = {} metadata = {}
for (_, key, value) in re.findall(r"<(xmp|pdfx|pdf|xmpMM):(.+)>(.+)</\1:\2>", data, re.I): for (_, key, value) in re.findall(r"<(xmp|pdfx|pdf|xmpMM):(.+)>(.+)</\1:\2>", data, re.I):
metadata[key] = value metadata[key] = value

View File

@ -53,7 +53,8 @@ class _BencodeHandler(object):
list: self.__encode_list, list: self.__encode_list,
} }
def __decode_int(self, s:str) -> (int, str): @staticmethod
def __decode_int(s: str) -> (int, str):
s = s[1:] s = s[1:]
next_idx = s.index(b'e') next_idx = s.index(b'e')
if s.startswith(b'-0'): if s.startswith(b'-0'):
@ -62,7 +63,8 @@ class _BencodeHandler(object):
raise ValueError # no leading zero except for zero itself raise ValueError # no leading zero except for zero itself
return int(s[:next_idx]), s[next_idx+1:] return int(s[:next_idx]), s[next_idx+1:]
def __decode_string(self, s:str) -> (str, str): @staticmethod
def __decode_string(s: str) -> (str, str):
sep = s.index(b':') sep = s.index(b':')
str_len = int(s[:sep]) str_len = int(s[:sep])
if str_len < 0: if str_len < 0: