Skip to content

Add script to simplify adding a missing type. #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions add_filetype.j2
Original file line number Diff line number Diff line change
@@ -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%02x' % i}}{% if loop.index < signature|length %}, {% endif%}{% endfor %}])

72 changes: 72 additions & 0 deletions add_filetype.py
Original file line number Diff line number Diff line change
@@ -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())
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
jinja2
pytest
flake8