-
Couldn't load subscription status.
- Fork 72
Add hachoir-list tool #102
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cc27b8d
add initial outline of hachoir-list tool (non-functional yet)
oliver d81a702
hachoir-list: implement basic functionality
oliver b2355cb
hachoir-list: allow passing multiple file names
oliver 819de10
hachoir-list: implement --hide-value and --hide-size
oliver 509c7b1
hachoir-list: also display field description
oliver 275b757
hachoir-list: add --description parameter
oliver 43bf852
hachoir-list: add --indent-width parameter
oliver b271c45
hachoir-list: improve formatting of field size
oliver 7b14808
hachoir-list: print file name only if multiple files are opened
oliver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #!/usr/bin/env python3 | ||
| from hachoir.listtool import main | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| # | ||
| # Tool for parsing a file and writing all fields to stdout. | ||
| # | ||
|
|
||
| from hachoir.core.cmd_line import getHachoirOptions, configureHachoir | ||
| from hachoir.core.cmd_line import displayVersion | ||
| from hachoir.stream import InputStreamError, FileInputStream | ||
| from hachoir.parser import guessParser, HachoirParserList | ||
| from optparse import OptionGroup, OptionParser | ||
| import sys | ||
|
|
||
|
|
||
| def format_size(num_bits): | ||
| if num_bits % 8 == 0: | ||
| return f"{num_bits // 8}B" | ||
| else: | ||
| return f"{num_bits}b" | ||
|
|
||
|
|
||
| def printFieldSet(field_set, args, options={}, indent=0): | ||
| indent_string = " " * options["indent_width"] * indent | ||
| for field in field_set: | ||
| value_display = "" | ||
| if field.value is not None and options["display_value"]: | ||
| value_display = f": {field.display}" | ||
| size_display = "" | ||
| if options["display_size"]: | ||
| size_display = f", {format_size(field.size)}" | ||
| description_display = "" | ||
| if options["display_description"]: | ||
| description_display = f" ({field.description})" | ||
| print(f"{indent_string}{field.name} <{field.__class__.__name__}{size_display}>{description_display}{value_display}") | ||
|
|
||
| if field.is_field_set: | ||
| printFieldSet(field, args, options, indent + 1) | ||
|
|
||
|
|
||
| def displayParserList(*args): | ||
| HachoirParserList().print_() | ||
| sys.exit(0) | ||
|
|
||
|
|
||
| def parseOptions(): | ||
| parser = OptionParser(usage="%prog [options] filename [filenames...]") | ||
|
|
||
| common = OptionGroup(parser, "List Tool", "Options of list tool") | ||
| common.add_option("--parser", help="Use the specified parser (use its identifier)", | ||
| type="str", action="store", default=None) | ||
| common.add_option("--offset", help="Skip first bytes of input file", | ||
| type="long", action="store", default=0) | ||
| common.add_option("--parser-list", help="List all parsers then exit", | ||
| action="callback", callback=displayParserList) | ||
| common.add_option("--size", help="Maximum size of bytes of input file", | ||
| type="long", action="store", default=None) | ||
| common.add_option("--description", dest="display_description", help="Display description", | ||
| action="store_true", default=False) | ||
| common.add_option("--hide-value", dest="display_value", help="Don't display value", | ||
| action="store_false", default=True) | ||
| common.add_option("--hide-size", dest="display_size", help="Don't display size", | ||
| action="store_false", default=True) | ||
| common.add_option("--indent-width", dest="indent_width", help="Indentation width", | ||
| type="long", action="store", default=2) | ||
| common.add_option("--version", help="Display version and exit", | ||
| action="callback", callback=displayVersion) | ||
| parser.add_option_group(common) | ||
|
|
||
| hachoir = getHachoirOptions(parser) | ||
| parser.add_option_group(hachoir) | ||
|
|
||
| values, arguments = parser.parse_args() | ||
| if len(arguments) < 1: | ||
| parser.print_help() | ||
| sys.exit(1) | ||
| return values, arguments | ||
|
|
||
|
|
||
| def openParser(parser_id, filename, offset, size): | ||
| tags = [] | ||
| if parser_id: | ||
| tags += [("id", parser_id), None] | ||
| try: | ||
| stream = FileInputStream(filename, | ||
| offset=offset, size=size, tags=tags) | ||
| except InputStreamError as err: | ||
| return None, "Unable to open file: %s" % err | ||
| parser = guessParser(stream) | ||
| if not parser: | ||
| return None, "Unable to parse file: %s" % filename | ||
| return parser, None | ||
|
|
||
|
|
||
| def main(): | ||
| # Parse options and initialize Hachoir | ||
| values, filenames = parseOptions() | ||
| configureHachoir(values) | ||
|
|
||
| # Open file and create parser | ||
| showing_multiple_files = len(filenames) > 1 | ||
| i = 0 | ||
| for filename in filenames: | ||
|
|
||
| i += 1 | ||
| if i > 1: | ||
| print() | ||
| if showing_multiple_files: | ||
| print(f"File: {filename}") | ||
|
|
||
| parser, err = openParser(values.parser, filename, | ||
| values.offset, values.size) | ||
| if err: | ||
| print(err) | ||
| sys.exit(1) | ||
|
|
||
| # Explore file | ||
| with parser: | ||
| printFieldSet(parser, values, { | ||
| "display_description": values.display_description, | ||
| "display_size": values.display_size, | ||
| "display_value": values.display_value, | ||
| "indent_width": values.indent_width, | ||
| }) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is more than one file, it would be nice to have a newline at the end for readability.