ronin.utils

ronin.utils.argparse

class ronin.utils.argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, version=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True)

Bases: argparse.ArgumentParser

Enhanced argument parser.

  • Support for flag arguments.
  • Applied patch to fix this issue.
add_flag_argument(name, help_true=None, help_false=None, default=False)

Adds a flag argument as two arguments: --my-flag and --no-my-flag.

ronin.utils.collections

class ronin.utils.collections.StrictDict(items=None, key_type=None, value_type=None, wrapper_function=None, unwrapper_function=None)

Bases: collections.OrderedDict

An ordered dict that raises TypeError exceptions when keys or values of the wrong type are used.

Parameters:
  • items (dict) – initial dict
  • key_type (type or str or (type or str)) – type(s) required for dict keys
  • value_type (type or str or (type or str)) – type(s) required for dict values
  • wrapper_function (FunctionType) – calls this optional function on all values before added to the list
  • unwrapper_function (FunctionType) – calls this optional function on all values when retrieved from the list
class ronin.utils.collections.StrictList(items=None, value_type=None, wrapper_function=None, unwrapper_function=None)

Bases: list

A list that raises TypeError exceptions when objects of the wrong type are inserted.

Parameters:
  • items (list) – initial list
  • value_type (type or str or (type or str)) – type(s) required for list values
  • wrapper_function (FunctionType) – calls this optional function on all values before added to the list
  • unwrapper_function (FunctionType) – calls this optional function on all values when retrieved from the list
append(value)
extend(values)
insert(index, value)
ronin.utils.collections.dedup(values)

Removes duplicate items from a list. Note that it does not change the original list.

Parameters:values (list) – list
Returns:de-duped list
Return type:list

ronin.utils.messages

ronin.utils.messages.announce(message, prefix=u'r\u014dnin', color=u'green')

Writes a message to the terminal with a colored prefix.

Parameters:
  • message (str) – message
  • color (str) – color name
ronin.utils.messages.error(message)

Writes an error message to the terminal with a red prefix.

Parameters:message (str or BaseException subclass instance) – message or exception
ronin.utils.messages.warning(message)

Writes a warning message to the terminal with a yellow prefix.

Parameters:message (str) – message

ronin.utils.paths

ronin.utils.paths.base_path(path)

Returns the real base path string of a file.

Parameters:path (str|FunctionType) – path; calls ronin.utils.strings.stringify() on it
Returns:base path of path
Return type:str
ronin.utils.paths.change_extension(path, new_extension)

Changes the file extension to a new one.

The extension is defined as the segment following the last “.” in the path.

Parameters:
Returns:

path with new extension

Return type:

str

ronin.utils.paths.glob(pattern, path=None, hidden=False, dirs=False)

Returns a list of real path strings matching the pattern. If path is not specified, the pattern is implicitly joined to the context’s paths.input.

Use “?” to match a single character, “*” to match zero or more characters, and “**” to match zero or more path segments.

Note that this implementation improves on Python’s standard glob.glob() by supporting “**” correctly.

Parameters:
Returns:

zero or more full paths to files (and optionally directories) matching the pattern

Return type:

[str]

ronin.utils.paths.input_path(*segments)

Joins the path segments to the context’s paths.input.

See join_path().

Parameters:segments ([str|FunctionType|None]) – path segments; calls ronin.utils.strings.stringify() on each
Returns:path joined to paths.input
Return type:str
ronin.utils.paths.join_path(*segments)

Joins the path segments into a single path tring. No attempt is made to make it an absolute path, nor to check that it exists on the filesystem.

Null segments are skipped. Also note that unlike os.path.join, segments beginning with a path separator character will not cause the path to reset.

Parameters:segments ([str|FunctionType|None]) – path segments; calls ronin.utils.strings.stringify() on each
Returns:joined path
Return type:str
ronin.utils.paths.join_path_later(*segments)

Like join_path(), but deferred.

Parameters:segments ([str|FunctionType|None]) – path segments; calls ronin.utils.strings.stringify() on each
Returns:function that calls join_path()
Return type:FunctionType

ronin.utils.platform

exception ronin.utils.platform.WhichException(message=None)

Bases: exceptions.Exception

which() could not find the command.

ronin.utils.platform.configure_platform(prefixes=None, which_command=None)

Configures the current context’s platform support.

Parameters:
  • prefixes ({str: str|FunctionType}) – overrides for the default platform prefixes; unspecified keys will remain unchanged from their defaults
  • which_command (str|FunctionType) – absolute path to which() command; defaults to “/usr/bin/which”
ronin.utils.platform.host_bits()

The bits (64 or 32) for the host machine on which we are running.

Returns:bits
Return type:integer
ronin.utils.platform.host_operating_system_prefix()

The operating system prefix for the host machine on which we are running.

Returns:operating system
Return type:str
ronin.utils.platform.host_platform()

The platform for the host machine on which we are running. A combination of host_operating_system_prefix() and host_bits().

Returns:host platform
Return type:str
ronin.utils.platform.platform_command(command, platform)

The command prefixed for the platform, from platform_prefixes().

Parameters:
  • command (str|FunctionType) – command
  • platform (str|FunctionType) – platform
Returns:

prefixed command

Return type:

str

ronin.utils.platform.platform_executable_extension(platform)

The executable extension for the platform, e.g. exe for windows and None for other platforms.

Parameters:platform (str|FunctionType) – platform
Returns:executable extension or None
Return type:str
ronin.utils.platform.platform_prefix(platform)

The prefix for the platform, from platform_prefixes().

Parameters:platform (str|FunctionType) – platform
Returns:platform prefixes or ‘’
Return type:str
ronin.utils.platform.platform_prefixes()

The current context’s platform.prefixes or the defaults. See also configure_platform().

Returns:platform prefixes
Return type:{str: str|FunctionType}
ronin.utils.platform.platform_shared_library_extension(platform)

The shared library extension for the platform, e.g. dll for windows and so for other platforms.

Parameters:platform (str|FunctionType) – platform
Returns:shared library extension or None
Return type:str
ronin.utils.platform.platform_shared_library_prefix(platform)

The shared library extension for the platform, e.g. lib for *nix and None for Windows.

Parameters:platform (str|FunctionType) – platform
Returns:shared library prefix or None
Return type:str
ronin.utils.platform.which(command, exception=True)

Finds the absolute path to a command on this host machine.

Works by invoking the operating system’s which command, which configured via the context’s platform.which_command. See also configure_which().

Parameters:
  • command (str|FunctionType) – command
  • exception (bool) – set to False in order to return None upon failure, instead of raising an exception
Returns:

absolute path to command

Return type:

str

Raises:

WhichException – if exception is True and could not find command

ronin.utils.strings

ronin.utils.strings.bool_stringify(value)

Like stringify(), except checks if the return value equals, ignoring case, to true.

Parameters:value (str|FunctionType) – value
Returns:True if the stringified value is true
Return type:bool
ronin.utils.strings.format_later(the_format, *args, **kwargs)

Creates a lambda that calls stringify_list() and stringify_dict() on the arguments and then .format their results on the_format.

Parameters:
  • the_format (str|FunctionType) – format string
  • values ([]) – values
Returns:

lambda returning the formatted string

Return type:

FunctionType

ronin.utils.strings.join_later(values, separator=u' ')

Creates a lambda that calls stringify_list() and joins the results on separator.

Parameters:
  • values ([]) – values
  • separator (str|FunctionType) – separator
Returns:

lambda returning the joined string

Return type:

FunctionType

ronin.utils.strings.stringify(value)

Casts the value to a Unicode string. If the value is a function, calls it using ronin.contexts.current_context() as its only argument, and recurses until a non-FunctionType value is returned.

None values are preserved, whether None is directly sent to this function or is the return value of function argument.

This function is the heart of Rōnin’s deferred value capability, as it allows lambdas to be passed around instead of strings.

Parameters:value (str|FunctionType) – value or None
Returns:stringified value or None
Return type:str
ronin.utils.strings.stringify_dict(values)

Calls stringify() on all dict values. Return values of None are preserved.

Parameters:values ({}) – values
Returns:values
Return type:{object: str}
ronin.utils.strings.stringify_list(values)

Calls stringify() on all elements. Return values of None are preserved.

Parameters:values ([]) – values
Returns:values
Return type:[str]

ronin.utils.types

ronin.utils.types.import_symbol(name)

Imports a symbol based on its fully qualified name.

Parameters:

name (str) – symbol name

Returns:

symbol

Raises:
  • ImportError – if could not import the module
  • AttributeError – if could not find the symbol in the module
ronin.utils.types.type_name(the_type)

Human-readable name of type(s). Built-in types will avoid the “__builtin__” prefix.

Tuples are always handled as a join of “|”.

Parameters:the_type (type|(type)) – type(s)
Returns:name of type(s)
Return type:str
ronin.utils.types.verify_subclass(value, the_type)

Raises TypeError if the value is not a subclass of the type.

Parameters:
  • value – value
  • the_type (type|str) – type or type name
Raises:
  • TypeError – if value is not a subclass of the_type
  • ValueError – if the_type is invalid
  • ImportError – if could not import the module
  • AttributeError – if could not find the symbol in the module
ronin.utils.types.verify_type(value, the_type)

Raises TypeError if the value is not an instance of the type.

Parameters:
  • value – value
  • the_type (type|str) – type or type name
Raises:
  • TypeError – if value is not an instance of the_type
  • ValueError – if the_type is invalid
  • ImportError – if could not import the module
  • AttributeError – if could not find the symbol in the module
ronin.utils.types.verify_type_or_subclass(value, the_type)

Raises TypeError if the value is not an instance or subclass of the type.

Parameters:
  • value – value
  • the_type (type|str) – type or type name
Raises:
  • TypeError – if value is not an instance or subclass of the_type
  • ValueError – if the_type is invalid
  • ImportError – if could not import the module
  • AttributeError – if could not find the symbol in the module