Commit 53c2cb29 by devttys0

Updated pydoc

parent 26d6fab4
...@@ -64,6 +64,9 @@ class Kwarg(object): ...@@ -64,6 +64,9 @@ class Kwarg(object):
self.description = description self.description = description
class Dependency(object): class Dependency(object):
'''
A container class for declaring module dependencies.
'''
def __init__(self, attribute="", name="", kwargs={}): def __init__(self, attribute="", name="", kwargs={}):
self.attribute = attribute self.attribute = attribute
...@@ -89,6 +92,7 @@ class Result(object): ...@@ -89,6 +92,7 @@ class Result(object):
@display - Set to True to display the result to the user, False to hide it. @display - Set to True to display the result to the user, False to hide it.
@extract - Set to True to flag this result for extraction. @extract - Set to True to flag this result for extraction.
@plot - Set to Flase to exclude this result from entropy plots. @plot - Set to Flase to exclude this result from entropy plots.
@name - Name of the result found (None if not applicable or unknown).
Provide additional kwargs as necessary. Provide additional kwargs as necessary.
Returns None. Returns None.
...@@ -130,13 +134,15 @@ class Module(object): ...@@ -130,13 +134,15 @@ class Module(object):
# The module title, as displayed in help output # The module title, as displayed in help output
TITLE = "" TITLE = ""
# A list of binwalk.core.module.ModuleOption command line options # A list of binwalk.core.module.Option command line options
CLI = [] CLI = []
# A list of binwalk.core.module.ModuleKwargs accepted by __init__ # A list of binwalk.core.module.Kwargs accepted by __init__
KWARGS = [] KWARGS = []
# A dictionary of module dependencies; all modules depend on binwalk.modules.general.General # A dictionary of module dependencies; all modules depend on the General and Extractor modules.
# Note that if overriding these default DEPENDS for a module, you MUST include these default
# dependencies, with the same attribute values.
DEPENDS = [ DEPENDS = [
Dependency(name='General', Dependency(name='General',
attribute='config'), attribute='config'),
...@@ -144,27 +150,37 @@ class Module(object): ...@@ -144,27 +150,37 @@ class Module(object):
attribute='extractor'), attribute='extractor'),
] ]
# Format string for printing the header during a scan # Format string for printing the header during a scan.
# Must be set prior to calling self.header.
HEADER_FORMAT = "%-12s %-12s %s\n" HEADER_FORMAT = "%-12s %-12s %s\n"
# Format string for printing each result during a scan # Format string for printing each result during a scan.
# Must be set prior to calling self.result.
RESULT_FORMAT = "%-12d 0x%-12X %s\n" RESULT_FORMAT = "%-12d 0x%-12X %s\n"
# Format string for printing custom information in the verbose header output.
# Must be set prior to calling self.header.
VERBOSE_FORMAT = ""
# The header to print during a scan. # The header to print during a scan.
# Set to None to not print a header. # Set to None to not print a header.
# Note that this will be formatted per the HEADER_FORMAT format string. # Note that this will be formatted per the HEADER_FORMAT format string.
# Must be set prior to calling self.header.
HEADER = ["DECIMAL", "HEX", "DESCRIPTION"] HEADER = ["DECIMAL", "HEX", "DESCRIPTION"]
# The attribute names to print during a scan, as provided to the self.results method. # The Result attribute names to print during a scan, as provided to the self.results method.
# Set to None to not print any results. # Set to None to not print any results.
# Note that these will be formatted per the RESULT_FORMAT format string. # Note that these will be formatted per the RESULT_FORMAT format string.
# Must be set prior to calling self.result.
RESULT = ["offset", "offset", "description"] RESULT = ["offset", "offset", "description"]
VERBOSE_HEADER_FORMAT = "" # The custom data to print in the verbose header output.
VERBOSE_HEADER_ARGS = [] # Note that these will be formatted per the VERBOSE_FORMAT format string.
# Must be set prior to calling self.header.
VERBOSE = []
# If set to True, the progress status will be automatically updated for each result # If set to True, the progress status will be automatically updated for each result
# containing a valid file attribute. # containing valid file and offset attributes.
AUTO_UPDATE_STATUS = True AUTO_UPDATE_STATUS = True
# Modules with higher priorities are executed first # Modules with higher priorities are executed first
...@@ -173,7 +189,7 @@ class Module(object): ...@@ -173,7 +189,7 @@ class Module(object):
# Modules with a higher order are displayed first in help output # Modules with a higher order are displayed first in help output
ORDER = 5 ORDER = 5
# Set to False if this is not a primary module # Set to False if this is not a primary module (e.g., General, Extractor modules)
PRIMARY = True PRIMARY = True
def __init__(self, **kwargs): def __init__(self, **kwargs):
...@@ -291,6 +307,7 @@ class Module(object): ...@@ -291,6 +307,7 @@ class Module(object):
''' '''
Gets the next file to be scanned (including pending extracted files, if applicable). Gets the next file to be scanned (including pending extracted files, if applicable).
Also re/initializes self.status. Also re/initializes self.status.
All modules should access the target file list through this method.
''' '''
fp = None fp = None
...@@ -357,6 +374,7 @@ class Module(object): ...@@ -357,6 +374,7 @@ class Module(object):
if r.display: if r.display:
display_args = self._build_display_args(r) display_args = self._build_display_args(r)
if display_args: if display_args:
self.config.display.format_strings(self.HEADER_FORMAT, self.RESULT_FORMAT)
self.config.display.result(*display_args) self.config.display.result(*display_args)
return r return r
...@@ -386,7 +404,7 @@ class Module(object): ...@@ -386,7 +404,7 @@ class Module(object):
def header(self): def header(self):
self.config.display.format_strings(self.HEADER_FORMAT, self.RESULT_FORMAT) self.config.display.format_strings(self.HEADER_FORMAT, self.RESULT_FORMAT)
self.config.display.add_custom_header(self.VERBOSE_HEADER_FORMAT, self.VERBOSE_HEADER_ARGS) self.config.display.add_custom_header(self.VERBOSE_FORMAT, self.VERBOSE)
if type(self.HEADER) == type([]): if type(self.HEADER) == type([]):
self.config.display.header(*self.HEADER, file_name=self.current_target_file_name) self.config.display.header(*self.HEADER, file_name=self.current_target_file_name)
...@@ -454,6 +472,10 @@ class Status(object): ...@@ -454,6 +472,10 @@ class Status(object):
setattr(self, k, v) setattr(self, k, v)
class ModuleException(Exception): class ModuleException(Exception):
'''
Module exception class.
Nothing special here except the name.
'''
pass pass
class Modules(object): class Modules(object):
...@@ -518,6 +540,11 @@ class Modules(object): ...@@ -518,6 +540,11 @@ class Modules(object):
return sorted(modules, key=modules.get, reverse=True) return sorted(modules, key=modules.get, reverse=True)
def help(self): def help(self):
'''
Generates formatted help output.
Returns the help string.
'''
modules = {} modules = {}
help_string = "\nBinwalk v%s\nCraig Heffner, http://www.binwalk.org\n" % binwalk.core.settings.Settings.VERSION help_string = "\nBinwalk v%s\nCraig Heffner, http://www.binwalk.org\n" % binwalk.core.settings.Settings.VERSION
...@@ -550,6 +577,11 @@ class Modules(object): ...@@ -550,6 +577,11 @@ class Modules(object):
return help_string + "\n" return help_string + "\n"
def execute(self, *args, **kwargs): def execute(self, *args, **kwargs):
'''
Executes all appropriate modules according to the options specified in args/kwargs.
Returns a list of executed module objects.
'''
run_modules = [] run_modules = []
orig_arguments = self.arguments orig_arguments = self.arguments
...@@ -571,6 +603,9 @@ class Modules(object): ...@@ -571,6 +603,9 @@ class Modules(object):
return run_modules return run_modules
def run(self, module, dependency=False, kwargs={}): def run(self, module, dependency=False, kwargs={}):
'''
Runs a specific module.
'''
obj = self.load(module, kwargs) obj = self.load(module, kwargs)
if isinstance(obj, binwalk.core.module.Module) and obj.enabled: if isinstance(obj, binwalk.core.module.Module) and obj.enabled:
...@@ -700,29 +735,29 @@ class Modules(object): ...@@ -700,29 +735,29 @@ class Modules(object):
return kwargs return kwargs
def kwargs(self, module, kwargs): def kwargs(self, obj, kwargs):
''' '''
Processes a module's kwargs. All modules should use this for kwarg processing. Processes a module's kwargs. All modules should use this for kwarg processing.
@module - An instance of the module (e.g., self) @obj - An instance of the module (e.g., self)
@kwargs - The kwargs passed to the module @kwargs - The kwargs passed to the module
Returns None. Returns None.
''' '''
if hasattr(module, "KWARGS"): if hasattr(obj, "KWARGS"):
for module_argument in module.KWARGS: for module_argument in obj.KWARGS:
if has_key(kwargs, module_argument.name): if has_key(kwargs, module_argument.name):
arg_value = kwargs[module_argument.name] arg_value = kwargs[module_argument.name]
else: else:
arg_value = module_argument.default arg_value = module_argument.default
setattr(module, module_argument.name, arg_value) setattr(obj, module_argument.name, arg_value)
for (k, v) in iterator(kwargs): for (k, v) in iterator(kwargs):
if not hasattr(module, k): if not hasattr(obj, k):
setattr(module, k, v) setattr(obj, k, v)
else: else:
raise Exception("binwalk.core.module.Modules.process_kwargs: %s has no attribute 'KWARGS'" % str(module)) raise Exception("binwalk.core.module.Modules.process_kwargs: %s has no attribute 'KWARGS'" % str(obj))
def process_kwargs(obj, kwargs): def process_kwargs(obj, kwargs):
......
...@@ -184,7 +184,3 @@ class General(Module): ...@@ -184,7 +184,3 @@ class General(Module):
except Exception as e: except Exception as e:
self.error(description="Cannot open file : %s" % str(e)) self.error(description="Cannot open file : %s" % str(e))
# If no files could be opened, quit permaturely
#if len(self.target_files) == 0:
# raise Exception("Failed to open any files for scanning")
...@@ -45,7 +45,7 @@ class Signature(Module): ...@@ -45,7 +45,7 @@ class Signature(Module):
MAGIC_FLAGS = magic.MAGIC_NO_CHECK_TEXT | magic.MAGIC_NO_CHECK_ENCODING | magic.MAGIC_NO_CHECK_APPTYPE | magic.MAGIC_NO_CHECK_TOKENS MAGIC_FLAGS = magic.MAGIC_NO_CHECK_TEXT | magic.MAGIC_NO_CHECK_ENCODING | magic.MAGIC_NO_CHECK_APPTYPE | magic.MAGIC_NO_CHECK_TOKENS
VERBOSE_HEADER_FORMAT = "%s %d" VERBOSE_FORMAT = "%s %d"
def init(self): def init(self):
# Create Signature and MagicParser class instances. These are mostly for internal use. # Create Signature and MagicParser class instances. These are mostly for internal use.
...@@ -78,7 +78,7 @@ class Signature(Module): ...@@ -78,7 +78,7 @@ class Signature(Module):
# Once the temporary magic files are loaded into libmagic, we don't need them anymore; delete the temp files # Once the temporary magic files are loaded into libmagic, we don't need them anymore; delete the temp files
self.parser.rm_magic_files() self.parser.rm_magic_files()
self.VERBOSE_HEADER_ARGS = ["Signatures:", self.parser.signature_count] self.VERBOSE = ["Signatures:", self.parser.signature_count]
def validate(self, r): def validate(self, r):
''' '''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment