PNG support and automatic import!
This commit is contained in:
parent
47c5d8b486
commit
d48dfc8e21
13
main.py
13
main.py
@ -2,7 +2,6 @@ import sys
|
||||
from shutil import copyfile
|
||||
import argparse
|
||||
|
||||
from src.parsers import pdf
|
||||
from src import parser_factory
|
||||
|
||||
|
||||
@ -20,7 +19,7 @@ def create_arg_parser():
|
||||
return parser
|
||||
|
||||
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():
|
||||
print("%s: %s" % (k, v))
|
||||
|
||||
@ -32,14 +31,10 @@ def main():
|
||||
for f in args.files:
|
||||
show_meta(f)
|
||||
return 0
|
||||
elif not args.files:
|
||||
return argparser.show_help()
|
||||
|
||||
#p = pdf.PDFParser(sys.argv[1])
|
||||
p = parser_factory.get_parser(sys.argv[1])
|
||||
p.remove_all()
|
||||
p = pdf.PDFParser('OUT_clean.pdf')
|
||||
print("ok")
|
||||
for f in args.files:
|
||||
p = parser_factory.get_parser(sys.argv[1])
|
||||
p.remove_all()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -1,10 +1,19 @@
|
||||
import mimetypes
|
||||
import importlib
|
||||
import pkgutil
|
||||
|
||||
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):
|
||||
mtype, _ = mimetypes.guess_type(filename)
|
||||
for c in abstract.AbstractParser.__subclasses__():
|
||||
if mtype in c.mimetypes:
|
||||
return c(filename)
|
||||
print('Nope')
|
||||
|
@ -1,9 +1,10 @@
|
||||
class AbstractParser(object):
|
||||
meta_list = set()
|
||||
mimetypes = set()
|
||||
|
||||
def __init__(self, filename: str):
|
||||
self.filename = filename
|
||||
self.output_filename = filename + '.cleaned'
|
||||
self.meta_list = set()
|
||||
self.mimetypes = set()
|
||||
|
||||
def get_meta(self):
|
||||
raise NotImplementedError
|
||||
|
18
src/parsers/png.py
Normal file
18
src/parsers/png.py
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user