From 589c50d2e147d2aeb2a5aba27a62c7f8f711414a Mon Sep 17 00:00:00 2001 From: dosas Date: Sun, 27 Dec 2020 14:01:13 +0100 Subject: [PATCH 1/2] Add script to simplify adding a missing type. --- add_filetype.j2 | 15 +++++++++ add_filetype.py | 72 ++++++++++++++++++++++++++++++++++++++++++++ requirements-dev.txt | 1 + 3 files changed, 88 insertions(+) create mode 100644 add_filetype.j2 create mode 100644 add_filetype.py diff --git a/add_filetype.j2 b/add_filetype.j2 new file mode 100644 index 0000000..93a06e9 --- /dev/null +++ b/add_filetype.j2 @@ -0,0 +1,15 @@ +class {{classname}}(Type): + """Implements the {{classname}} image type matcher.""" + + MIME = '{{mime}}' + EXTENSION = '{{extension}}' + + def __init__(self): + super({{classname}}, self).__init__( + mime={{classname}}.MIME, + extension={{classname}}.EXTENSION + ) + + def match(self, buf): + return buf[:{{signature|length}}] == bytearray([{% for i in signature %}{{'0x%2x' % i}}{% if loop.index < signature|length %}, {% endif%}{% endfor %}]) + diff --git a/add_filetype.py b/add_filetype.py new file mode 100644 index 0000000..10c6e83 --- /dev/null +++ b/add_filetype.py @@ -0,0 +1,72 @@ +from __future__ import absolute_import + +import argparse +import os +import sys +from contextlib import contextmanager + +from jinja2 import Template + + +@contextmanager +def replace(path): + with open(path) as f: + content = f.readlines() + + yield content + + with open(path, 'w') as f: + f.writelines(content) + + +def main(): + parser = argparse.ArgumentParser(description='Add a filetype to the library.') # noqa + parser.add_argument('-m', '--mime', + required=True, + help='The mime type.') + parser.add_argument('-e', '--extension', + required=True, + help='The extension name.') + parser.add_argument('-s', '--signature', + nargs='+', + required=True, + help='The byte signature.') + parser.add_argument('-t', '--type', + help='The type.') + args = parser.parse_args() + + mime = args.mime + extension = args.extension + signature = [int(i, 16) for i in args.signature] + type_ = args.type if args.type else mime.split('/')[0] + classname = extension.capitalize() + + with open('add_filetype.j2') as f: + template = Template(f.read()) + + rendered = template.render(signature=signature, + mime=mime, + extension=extension, + classname=classname) + + path = os.path.join('filetype', 'types', '%s.py' % type_) + if not os.path.exists(path): + raise ValueError("Unknown type: '%s' try -t option." % type_) + + with open(path, 'a') as f: + f.write('\n\n') + f.write(rendered) + + with replace(os.path.join('filetype', 'types', '__init__.py')) as content: + index = content.index('%s = (\n' % type_.upper()) + content.insert(index + 1, ' %s.%s(),\n' % (type_, classname)) + + with replace('README.rst') as content: + index = content.index('%s\n' % type_.capitalize()) + content.insert(index + 3, '- **%s** - ``%s``\n' % (extension, mime)) + + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/requirements-dev.txt b/requirements-dev.txt index cb64874..093db39 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,2 +1,3 @@ +jinja2 pytest flake8 From 91ce4b8ed076203f723ae97d8c81255737fe9c13 Mon Sep 17 00:00:00 2001 From: dosas Date: Sun, 27 Dec 2020 21:07:05 +0100 Subject: [PATCH 2/2] Fix hex format string. --- add_filetype.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/add_filetype.j2 b/add_filetype.j2 index 93a06e9..31d45c1 100644 --- a/add_filetype.j2 +++ b/add_filetype.j2 @@ -11,5 +11,5 @@ class {{classname}}(Type): ) def match(self, buf): - return buf[:{{signature|length}}] == bytearray([{% for i in signature %}{{'0x%2x' % i}}{% if loop.index < signature|length %}, {% endif%}{% endfor %}]) + return buf[:{{signature|length}}] == bytearray([{% for i in signature %}{{'0x%02x' % i}}{% if loop.index < signature|length %}, {% endif%}{% endfor %}])