This module implements the core of the command interpreter and any part essential for running it
Interpret and execute the command.
See The interpreter section in the documentation for more details
All the custom errors are defined in vdat.command_interpreter.exceptions. The ones raised in the constructor are derived from from CIValidationError,
Parameters: | command : string
command_config : dict
selected : list-like, optional
|
---|---|
Raises: | CINoExeError
CIParseError
CIKeywordError
CIKeywordTypeError
|
Collect the files, expand and run the required command
All the custom errors raised here derive from CIRunError.
Raises: | CICommandFmtError
|
---|
If the command configuration has the is_alias_of replace the executable name
Check that the executable can be found and replace it with the full path returned by distutils.spawn.find_executable()
Check that all the mandatory keywords are provided.
If mandatory is not found, return without doing anything
Scan the keys and check that the interpreter knows how to deal with them.
Parameters: | keys : list of strings
|
---|---|
Returns: | primary_func : callable
keyword_map : dictionary
|
Raises: | CIKeywordTypeError
|
Get the value of key from the configuration. If it’s a string, convert it to a dictionary with two entries:
and re-add it in the configuration dictionary
Parameters: | key : string
|
---|---|
Returns: | value : dictionary
|
Raises: | CIKeywordError
|
Look for the existence of the filter_selected option and check that it is of the correct type and that selected is of the correct type
Returns: | filter_func : function
|
---|
Look for the existence of the execute option in the configuration and check that it is of the correct type.
Returns: | execute_func : function
|
---|
Helper function and classes.
These functionalities are not essential for the interpreter, but can help the to setup things
Define enumerate-like classes that allows to map from keys to key types and to the functions that needs to be called to deal with any of them.
It uses pkg_resources and entry points to make the framework extendible
Get all the entry points for the group and load them.
Parameters: | group : string
|
---|---|
Returns: | entry_points : dictionary
|
Base class for the types. It shouldn’t be used directly.
If a type loop exists, it can be accessed as instance.loop or instance['loop']
Attributes
known_types() | return the list of known types |
Fill the type<–>function mapping using the vdat.cit.primary entry point.
Fill the type<–>function mapping using the vdat.cit.keyword entry point.
Fill the type<–>function mapping using the vdat.cit.execute entry point.
Template for a function that deals with a primary keyword.
It collects the files from the target_dir according to the instructions in key_val, if any and either yield a value or return an iterable.
Parameters: | target_dir : string
key_val : dictionary
|
---|---|
Returns: | yield a string or iterable of strings |
Raises: | CIPrimaryError
|
Template for a function that deals with a non-primary keyword.
A keyword has a value either statically stored in key_val or its value need to be extracted from the value of the primary file(s).
Parameters: | primary : string
key_val : dictionary
|
---|---|
Returns: | string
|
Raises: | CIKeywordError
|
For each of the primary entry, this function is called to decide whether to execute or skip the command.
Parameters: | primary : string
config : dictionary
|
---|---|
Returns: | bool
|
Get all the files in target_dir matching the string in key_val['value']
Parameters: | target_dir : string
key_val : dictionary
|
---|---|
Returns: | iterator
|
Make a nested loop over the set of given keys, in each step of the loop construct the value using python format string syntax and then get all the files matching it.
If any of the steps doesn’t produce any file, no value is yielded.
Parameters: | target_dir : string
key_val : dictionary
|
---|---|
Returns: | yields a string of space separated file names |
Loop over all the files matching the value entry. For each one, create a list of file names replacing the regex in pattern with the elements of replace.
Parameters: | target_dir : string
key_val : dictionary
|
---|---|
Returns: | yields a string of space separated file names |
Returns the value contained in the keyword
Parameters: | primary : string
key_val : dictionary
|
---|---|
Returns: | string
|
Extract and parse an fits header keyword from the first file.
Extract the value keyword from the header. If extract is in the configuration, it instruct hos to build a variable out of the extracted header value.
Parameters: | primary : string
key_val : dictionary
|
---|---|
Returns: | string
|
Create a new string formatting value according to the provided keys.
The keys are substituted using format string syntax.
The value of keys is a map between values to substitute in value and keyword types used to extract them from the primary file name. Strings are interpreted as of type plain.
Parameters: | primary : string
key_val : dictionary
|
---|---|
Returns: | string
|
Extract a string from the primary using regular expression substitution
Parameters: | primary : string
key_val : dictionary
|
---|---|
Returns: | string
|
For each of the primary entry, it constructs a string using the keyword type defined by subtype. If that string corresponds to something existing in the file system, returns False.
Parameters: | primary : string
config : dictionary
|
---|---|
Returns: | bool
|
Command interpreter exceptions
Bases: exceptions.Exception
Generic exception. It’s the parent of all the other exceptions defined here
Bases: vdat.command_interpreter.exceptions.CIError
Exception raised when validating the command in the constructor
Bases: vdat.command_interpreter.exceptions.CIValidationError
Raised when the executable name is not found
Bases: vdat.command_interpreter.exceptions.CIValidationError
Failed parsing of the command
Bases: vdat.command_interpreter.exceptions.CIValidationError
Raised when the keyword validation fails
Bases: vdat.command_interpreter.exceptions.CIKeywordValidationError
Raised when the keyword doesn’t have a type key or it’s type is not known
Bases: vdat.command_interpreter.exceptions.CIError
Raised when running the command
Bases: vdat.command_interpreter.exceptions.CIRunError
Raised if something bad happens when handling a primary keyword
Bases: vdat.command_interpreter.exceptions.CIRunError
Raised if something bad happens when handling a secondary keyword
Bases: vdat.command_interpreter.exceptions.CIRunError
Raised when the replacement of the keyword fails
Except for the logging mechanism, the CommandInterpreter uses relays-like objects to communicate with the external word. This module defines a few classes with an emit method, that mimic PyQt signals
The names of the emit method arguments are the type of the parameter followed by an underscore and optionally by an explanatory name.
Available relays:
command_string: accept an int and a string
Default implementation: print the string_ for when int_ == 0
progress: accept three numbers, the total expected number, the number of successes and of failures;
Default implementation: Print the percentages of finished, successful and failed jobs, overwriting the line
logger: accept a two strings
Default implementation: print int_level: string_msg
The int_level values are chosen to be the standard logging levels and are converted to strings accordingly
The idea behind this relay is to bind it to the main logger if needed. We decided not to do this directly as we don’t know the name of the logger. We implement it in this way to have a more coherent interface.
Replace the emit method in the relay instance called name with new_emit. The original class is not modified.
Parameters: | name : string
new_emit : callable
|
---|