Source documentation¶
Warning
Python API is far from being “frozen”, use it with zero backwards-compatibility in mind. You are welcome to report suggestions to bug tracker.
mrbob – Main package¶
mrbob.configurator – Machinery to figure out configuration¶
- class mrbob.configurator.Configurator(template, target_directory, bobconfig=None, variables=None, defaults=None)[source]¶
Bases: object
Controller that figures out settings, asks questions and renders the directory structure.
Parameters: - template – Template name
- target_directory – Filesystem path to a output directory
- bobconfig – Configuration for mr.bob behaviour
- variables – Given variables to questions
- defaults – Overriden defaults of the questions
Additional to above settings, Configurator exposes following attributes:
- template_dir is root directory of the template
- is_tempdir if template directory is temporary (when using zipfile)
- templateconfig dictionary parsed from template section
- questions ordered list of `Question instances to be asked
- bobconfig dictionary parsed from mrbob section of the config
- render()[source]¶
Render file structure given instance configuration. Basically calls mrbob.rendering.render_structure().
- class mrbob.configurator.Question(name, question, default=None, required=False, command_prompt=<built-in function raw_input>, pre_ask_question='', post_ask_question='', help='', **extra)[source]¶
Bases: object
Question configuration. Parameters are used to configure questioning and possible validation of the answer.
Parameters: - name – Unique, namespaced name of the question
- question – Question to be asked
- default – Default value of the question
- required (bool) – Is question required?
- command_prompt – Function to executed to ask the question given question text
- help – Optional help message
- pre_ask_question – Space limited functions in dotted notation to ask before the question is asked
- post_ask_question – Space limited functions in dotted notation to ask aster the question is asked
- **extra –
Any extra parameters stored for possible extending of Question functionality
Any of above parameters can be accessed as an attribute of Question instance.
- ask(configurator)[source]¶
Eventually, ask the question.
Parameters: configurator – mrbob.configurator.Configurator instance
mrbob.bobexceptions – Exceptions¶
mr.bob exceptions module.
- exception mrbob.bobexceptions.ConfigurationError[source]¶
Bases: mrbob.bobexceptions.MrBobError
Raised during configuration phase
- exception mrbob.bobexceptions.SkipQuestion[source]¶
Bases: mrbob.bobexceptions.MrBobError
Raised during pre_ask_question if we should skip it
- exception mrbob.bobexceptions.TemplateConfigurationError[source]¶
Bases: mrbob.bobexceptions.ConfigurationError
Raised reading template configuration
- exception mrbob.bobexceptions.ValidationError[source]¶
Bases: mrbob.bobexceptions.MrBobError
Raised during question validation
mrbob.parsing – Parsing .ini files¶
mrbob.rendering – Everything related to rendering templates and directory structure¶
- mrbob.rendering.render_structure(fs_source_root, fs_target_root, variables, verbose, renderer, ignored_files, ignored_directories)[source]¶
Recursively copies the given filesystem path fs_source_root_ to a target directory `fs_target_root.
Any files ending in .bob are rendered as templates using the given renderer using the variables dictionary, thereby losing the .bob suffix.
strings wrapped in + signs in file- or directory names will be replaced with values from the variables, i.e. a file named +name+.py.bob given a dictionary {‘name’: ‘bar’} would be rendered as bar.py.
mrbob.hooks – Included hooks¶
Use any of hooks below or write your own. You are welcome to contribute them!
- mrbob.hooks.set_current_datetime(configurator, question)[source]¶
If you want to set the default answer of a question to the current date and time, use this function as Pre question hook:
[questions] project.question = What year was the project started? project.pre_ask_question = mrbob.hooks:set_current_datetime project.datetime_format = %%Y
The datetime_format property should be of the standard Python strftime format. It defaults to YYYY-MM-DD if not specified.
Please note that you’ll have to escape the % character (by using %%) due to the fact it’s a special character in INI files.
See the following URL for more information: http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
- mrbob.hooks.show_message(configurator)[source]¶
If you want to display a message to the user when rendering is complete, you can use this function as Post render hook:
[template] post_render = mrbob.hooks:show_message message = Well done, %(author.name)s, your code is ready!
As shown above, you can use standard Python formatting in post_render_msg.
- mrbob.hooks.to_boolean(configurator, question, answer)[source]¶
If you want to convert an answer to Python boolean, you can use this function as Post question hook:
[questions] idiot.question = Are you young? idiot.post_ask_question = mrbob.hooks:to_boolean
Following variables can be converted to a boolean: y, n, yes, no, true, false, 1, 0
- mrbob.hooks.to_decimal(configurator, question, answer)[source]¶
If you want to convert an answer to Python float, you can use this function as Post question hook:
[questions] daemon.question = What interval should the daemon poll for data? daemon.post_ask_question = mrbob.hooks:to_decimal
- mrbob.hooks.to_integer(configurator, question, answer)[source]¶
If you want to convert an answer to Python integer, you can use this function as Post question hook:
[questions] owner.question = What's your age? owner.post_ask_question = mrbob.hooks:to_integer
- mrbob.hooks.validate_choices(configurator, question, answer)[source]¶
If you want to validate an answer using a limited set of choices, you can use this function as Post question hook:
[questions] license.question = What license would you like to use? license.post_ask_question = mrbob.hooks:validate_choices license.choices = MIT|BSD|Apache 2.0|Other license.choices_case_sensitive = yes license.choices_delimiter = |
Currently choices are split using whitespace by default. If you wish to have whitespace within each choice, you may specify a custom delimiter which will be used to split the choices.
This hook may be set to validate the choices in a case sensitive manner. However, this behaviour is disabled by default.
- mrbob.hooks.validate_datetime(configurator, question, answer)[source]¶
If you want to validate a date using a chosen date format, you can use this function as Post question hook:
[questions] project.question = What year was the project started? project.post_ask_question = mrbob.hooks:validate_datetime project.datetime_format = %%Y
The datetime_format property should be of the standard Python strftime format. It defaults to YYYY-MM-DD if not specified.
Please note that you’ll have to escape the % character (by using %%) due to the fact it’s a special character in INI files.
See the following URL for more information: http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
- mrbob.hooks.validate_regex(configurator, question, answer)[source]¶
If you want to validate an answer using a regular expression, you can use this function as Post question hook:
[questions] project.question = Please specify a name (only lowercase characters)? project.post_ask_question = mrbob.hooks:validate_regex project.regex = ^[a-z]+$