command_tree package¶
Submodules¶
command_tree.argument module¶
-
class
command_tree.argument.
AddArgumentHandler
[source]¶ Bases:
command_tree.argument.AddArgumentHandlerBase
Add argument by the default way.
-
class
command_tree.argument.
AddArgumentHandlerBase
[source]¶ Bases:
object
Interface for override the default way to use
argparser.ArgumenParser.add_argument()
function.Typical usage of this is the MutuallyExclusiveGroup and the ArgumentGroup.
-
add
(parser, *args, **kwargs)[source]¶ Add argument to parser.
Parameters: - parser (argparse.ArgumentParser) – the parent parser instance
- args (list) – all pos args for ArgumentParser.add_argument
- kwargs (dict) – all keyword args for ArgumentParser.add_argument
Returns: the result of the ArgumentParser.add_argument
Return type:
-
-
class
command_tree.argument.
Argument
(identifier, args=None, kwargs=None, name_generator=None)[source]¶ Bases:
object
Descriptor for an
argparser.ArgumentParser
argument.Parameters: - identifier (str) – name of the the handler function’s parameter which is declared in the code
- args (list) – all positional arguments for
argparser.ArgumenParser.add_argument()
- kwargs (dict) – all keywird arguments for
argparser.ArgumenParser.add_argument()
- name_generator (callable) – will be used for the automatic name generation but only if the name not specified explicitly first parameter will be the object name
-
add_to_parser
(parser)[source]¶ Add this argument to a parser.
Parameters: parser (argparse.ArgumentParser) –
-
item
¶
command_tree.command_tree module¶
-
class
command_tree.command_tree.
CommandTree
(config=None)[source]¶ Bases:
object
Define the main API for build a tree with
argparse
.It defines decorators and other functions to it.
Parameters: config (Config) – config -
add_argument
(obj, *args, **kwargs)[source]¶ Decorator for argument creation.
Parameters: - obj – class or function handler
- kwargs (args,) – All other arguments will passed to the ArgumentParser.add_argument function
Returns: the argument descriptor instance
Return type:
-
add_leaf
(func, name=None, **kwargs)[source]¶ Add leaf to the tree.
Parameters: - func (function) – the handler function
- name (str) – the node name
- items (list) – explicit list of all the sub nodes
- kwargs – all other keyword arguments will passed to the ArgumentParser.add_subparsers().add_parser function
Returns: the item descriptor instance
Return type:
-
add_node
(cls, name=None, items=None, **kwargs)[source]¶ Add node to the tree.
Parameters: Returns: the item descriptor instance
Return type:
-
add_root
(cls, items=None, **kwargs)[source]¶ Add root node to the tree.
Parameters: - cls (type) – the handler class
- items (list) – explicit list of all the sub nodes
- kwargs – all other keyword arguments will passed to the
argparser.ArgumentParser
constructor
Returns: the item descriptor instance
Return type:
-
argument
(*args, **kwargs)[source]¶ Decorator for argument creation.
All arguments will passed to the
argparse.ArgumentParser.add_argument()
functionReturns: wrapper Return type: function
-
build
(parser=None)[source]¶ Build the parser tree.
Parameters: parser (argparse.ArgumentParser) – external parser to build Returns: the builded parser Return type: argparse.ArgumentParser
-
children
¶
-
common_argument
(*args, **kwargs)[source]¶ Decorator for argument creation.
All arguments will passed to the
argparse.ArgumentParser.add_argument()
functionReturns: wrapper Return type: function
-
execute
(parser=None, args=None)[source]¶ Build the parsers and execute it.
It will be run the handler function chosed by the user.
Parameters: - parser (argparse.ArgumentParser) – external parser built by the CommandTree.build
- args (list) – external arguments to parse by the argument parser
Returns: The return value of the leaf handler function
-
items
¶
-
leaf
(name=None, **kwargs)[source]¶ Decorator for leaf creation.
Parameters: - name (str) – the node name
- kwargs – all other keyword arguments will passed to the ArgumentParser.add_subparsers().add_parser function
Returns: wrapper
Return type: function
-
node
(name=None, items=None, **kwargs)[source]¶ Decorator for node creation.
Parameters: - name (str) – the node name
- items (list) – explicit list of all the sub nodes
- kwargs – all other keyword arguments will passed to the ArgumentParser.add_subparsers().add_parser function
Returns: wrapper
Return type: function
-
command_tree.config module¶
-
class
command_tree.config.
Config
(docstring_parser=<command_tree.doc_string_parser.GoogleParser object>, get_default_from_function_param=True, get_argument_type_from_function_default_value_type=True, change_underscores_to_hyphens_in_names=False, prepend_double_hyphen_prefix_if_arg_has_default=False, generate_simple_hyphen_name=False)[source]¶ Bases:
object
Config holder class for the CommandTree.
Parameters: - docstring_parser (ParserBase) – If there is doc declared in the class or function some data from the docstring will be parsed out. At this time only the help string is used, but if you want to disable the docstring parsing, set None to this value.
- get_default_from_function_param (bool) – If provided the default value in the leaf decorated function declaration and there is not set already by the decorator, the builder will take that value and add to the ArgumentParser.add_argument’s parameters.
- get_argument_type_from_function_default_value_type (bool) – If the default value provided somehow (by the decorator or the function declaration), and the type is not set already by the decorator, the type of the default value will be used as argument type.
- change_underscores_to_hyphens_in_names (bool) – If the argument, leaf or node name contains any underscore, and there is no explicit name specified, the name generator will replace any underscores (‘_’) to hyphens (‘-‘). So if the function or argument name is ‘command_name’ the generate name will be ‘command-name’ if this setting is enabled.
- prepend_double_hyphen_prefix_if_arg_has_default (bool) – If the name not defined explicit, and there is a default value for the argument add hyphen prefix to the generated argument name.
- generate_simple_hyphen_name (dict or bool) – If there is no explicit name set and this value is not None, the CommandTree try to generate a name whith a simple hyphen prefix. So if the argument name is ‘arg1’, the generated name will be ‘-a’. But the config value is a dict, for override this method with a custom mapping. If this value is {“arg1”: “a1”}, the generated argument name will be ‘-a1’.
command_tree.doc_string_parser module¶
-
class
command_tree.doc_string_parser.
DocStringInfo
(description=None)[source]¶ Bases:
object
Structured info about the object’s docstring.
Parameters: description (str) – the object description, typically the first line of the docstring -
class
Argument
(name, help=None)[source]¶ Bases:
object
Structured info about one argument.
Parameters:
-
add_argument_info
(arginfo)[source]¶ Add an argument info instance.
Parameters: arginfo (DocStringInfo.Argument) – the info instance
-
class
-
class
command_tree.doc_string_parser.
GoogleParser
[source]¶ Bases:
command_tree.doc_string_parser.ParserBase
Parser class for Google style doc string comment format.
See: https://google.github.io/styleguide/pyguide.html#Comments
command_tree.exceptions module¶
command_tree.groups module¶
-
class
command_tree.groups.
ArgumentGroup
(command_tree, title=None, description=None)[source]¶ Bases:
command_tree.groups.GroupBase
Group for create MutuallyExclusiveGroup as described as
argparse.ArgumentParser.add_argument_group()
.
-
class
command_tree.groups.
GroupBase
(command_tree, handler)[source]¶ Bases:
object
Interface class to define group handlers.
Parameters: - command_tree (CommandTree) – a CommandTree instance
- handler (AddArgumentHandlerBase) –
command_tree.argument.AddArgumentHandlerBase
based class instance
-
argument
(*args, **kwargs)[source]¶ Decorator for argument creation.
All arguments will passed to the ArgumentParser.add_argument function
Returns: wrapper function Return type: function
-
common_argument
(*args, **kwargs)[source]¶ Decorator for argument creation.
All arguments will passed to the ArgumentParser.add_argument function
Returns: wrapper function Return type: function
-
handler
¶
-
class
command_tree.groups.
MutuallyExclusiveGroup
(command_tree, required=False, argument_group=None)[source]¶ Bases:
command_tree.groups.GroupBase
Group for create MutuallyExclusiveGroup as described as
argparse.ArgumentParser.add_mutually_exclusive_group()
.
command_tree.item module¶
-
class
command_tree.item.
Item
(name, obj, id, arguments, parser_args=None, docstring_parser=None, name_generator=None)[source]¶ Bases:
object
Represents an
argparse.ArgumentParser
object.Parameters: - name (str) – the name of the item
- obj (type) – the function or class handler type
- id (int) – unique id for the item, will be use in the ordering
- arguments (OrderedDict) – argument name (str) -> argument descriptor dict (Arguments)
- parser_args (dict) – arguments for
argparse.ArgumentParser
constructor - docstring_parser (ParserBase) – a ParserBase derived class instance
- name_generator (callable) – will be used for the automatic name generation but only if the name not specified explicitly first parameter will be the object name
-
arguments
¶ Getter for argument list.
- Returns
- OrderedDict: str -> Argument instances
-
build
(parser)[source]¶ Build the argument parser tree.
Parameters: parser (argparse.ArgumentParser) – a parser instance to build
-
build_arguments
(parser)[source]¶ Add arguments to a parser.
Parameters: parser (argparse.ArgumentParser) – the parent parser
-
id
¶ Getter for id
-
name
¶ Getter for name.
-
obj
¶ Getter for the obj – class or function.
-
parser_args
¶ Getter for parser args.
command_tree.leaf module¶
-
class
command_tree.leaf.
Leaf
(name, func, id, arguments, parser_args=None, docstring_parser=None, name_generator=None)[source]¶ Bases:
command_tree.item.Item
An item what will not has any sub items, only arguments.
For other args see
command_tree.item.Item
Parameters: func (function) – the handler function -
obj_name
¶
-
command_tree.node module¶
-
class
command_tree.node.
Node
(name, cls, id, arguments, items=None, parser_args=None, docstring_parser=None, name_generator=None)[source]¶ Bases:
command_tree.item.Item
An item what may have sub nodes or leafs.
Holding a class type and instance.
For other args see
command_tree.item.Item
Parameters: - cls (type) – the class handler type
- items (list) – list of
command_tree.item.Item
decorated objects
-
fetch
()[source]¶ Iterate throught the class attributes (classes or functions) and search for sub items.
It is assumes that the sub items has been decorated already.
-
get_item
(name)[source]¶ Get the specfified sub item by name.
Parameters: name (str) – the sub item’s name Returns: the sub item or None Return type: Item
-
has_handler
¶
-
instance
¶ Getter for instance.
-
instance_arguments
¶ Getter for the instance arguments.
Returns: the handler constructor arguments Return type: dict
-
items
¶ Getter for the list of the sub items.
Returns: Item based instances Return type: list
-
obj_name
¶
-
parent
¶
-
required
¶
-
set_subparser_arguments
(kwargs)[source]¶ Set subparser arguments
Parameters: kwargs (dict) – key-value pairs for ArgumentParser.add_subparsers()
command_tree.types module¶
-
command_tree.types.
valid_dir
(value)[source]¶ Function what is can be used as argument type to check the file is exists or not.
Example
>>> from argparse import ArgumentParser >>> from command_tree import valid_dir >>> parser = ArgumentParser() >>> parser.add_argument("dir", type = valid_dir) >>> print(parser.parse_args()) Namespace(dir='/')
-
command_tree.types.
valid_file
(value)[source]¶ Function what is can be used as argument type to check the file is exists or not.
Example
>>> from argparse import ArgumentParser >>> from command_tree import valid_file >>> parser = ArgumentParser() >>> parser.add_argument("file", type = valid_file) >>> print(parser.parse_args()) Namespace(file='setup.py')