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

PNG support and automatic import!

This commit is contained in:
jvoisin 2018-03-20 01:20:11 +01:00
parent 47c5d8b486
commit d48dfc8e21
4 changed files with 35 additions and 12 deletions

13
main.py
View File

@ -2,7 +2,6 @@ import sys
from shutil import copyfile from shutil import copyfile
import argparse import argparse
from src.parsers import pdf
from src import parser_factory from src import parser_factory
@ -20,7 +19,7 @@ def create_arg_parser():
return parser return parser
def show_meta(file_name:str): def show_meta(file_name:str):
p = parser_factory(file_name) p = parser_factory.get_parser(file_name)
for k,v in p.get_meta().items(): for k,v in p.get_meta().items():
print("%s: %s" % (k, v)) print("%s: %s" % (k, v))
@ -32,14 +31,10 @@ def main():
for f in args.files: for f in args.files:
show_meta(f) show_meta(f)
return 0 return 0
elif not args.files:
return argparser.show_help()
#p = pdf.PDFParser(sys.argv[1]) for f in args.files:
p = parser_factory.get_parser(sys.argv[1]) p = parser_factory.get_parser(sys.argv[1])
p.remove_all() p.remove_all()
p = pdf.PDFParser('OUT_clean.pdf')
print("ok")
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,10 +1,19 @@
import mimetypes import mimetypes
import importlib
import pkgutil
from .parsers import abstract from .parsers import abstract
from .parsers import *
for module_loader, name, ispkg in pkgutil.walk_packages('.src.parsers'):
if not name.startswith('src.parsers.'):
continue
elif name == 'src.parsers.abstract':
continue
importlib.import_module(name)
def get_parser(filename: str): def get_parser(filename: str):
mtype, _ = mimetypes.guess_type(filename) mtype, _ = mimetypes.guess_type(filename)
for c in abstract.AbstractParser.__subclasses__(): for c in abstract.AbstractParser.__subclasses__():
if mtype in c.mimetypes: if mtype in c.mimetypes:
return c(filename) return c(filename)
print('Nope')

View File

@ -1,9 +1,10 @@
class AbstractParser(object): class AbstractParser(object):
meta_list = set()
mimetypes = set()
def __init__(self, filename: str): def __init__(self, filename: str):
self.filename = filename self.filename = filename
self.output_filename = filename + '.cleaned' self.output_filename = filename + '.cleaned'
self.meta_list = set()
self.mimetypes = set()
def get_meta(self): def get_meta(self):
raise NotImplementedError raise NotImplementedError

18
src/parsers/png.py Normal file
View File

@ -0,0 +1,18 @@
import subprocess
import json
import cairo
from . import abstract
class PNGParser(abstract.AbstractParser):
mimetypes = {'image/png', }
meta_list = set()
def get_meta(self):
out = subprocess.check_output(['exiftool', '-json', self.filename])
return json.loads(out)[0]
def remove_all(self):
surface = cairo.ImageSurface.create_from_png(self.filename)
surface.write_to_png(self.output_filename)