fsl.utils.filetree.utils
- class fsl.utils.filetree.utils.Part[source]
Bases:
object
Individual part of a template
3 subclasses are defined:
Literal
: piece of textRequired
: required variable to fill in (between curly brackets)Optional
: part of text containing optional variables (between square brackets)
- contains_optionals(variables: Set[Part] | None = None)[source]
Returns True if this part contains the optional variables
- append_variables(variables: List[str])[source]
Appends the variables in this part to the provided list in order
- __dict__ = mappingproxy({'__module__': 'fsl.utils.filetree.utils', '__doc__': '\n Individual part of a template\n\n 3 subclasses are defined:\n\n - :class:`Literal`: piece of text\n - :class:`Required`: required variable to fill in (between curly brackets)\n - :class:`Optional`: part of text containing optional variables (between square brackets)\n ', 'fill_known': <function Part.fill_known>, 'optional_variables': <function Part.optional_variables>, 'required_variables': <function Part.required_variables>, 'contains_optionals': <function Part.contains_optionals>, 'append_variables': <function Part.append_variables>, '__dict__': <attribute '__dict__' of 'Part' objects>, '__weakref__': <attribute '__weakref__' of 'Part' objects>, '__annotations__': {}})
- __module__ = 'fsl.utils.filetree.utils'
- __weakref__
list of weak references to the object (if defined)
- class fsl.utils.filetree.utils.Literal(text: str)[source]
Bases:
Part
- __init__(text: str)[source]
Literal part is defined purely by the text it contains
- Parameters:
text – part of the template
- __annotations__ = {}
- __module__ = 'fsl.utils.filetree.utils'
- class fsl.utils.filetree.utils.Required(var_name, var_formatting=None)[source]
Bases:
Part
- __init__(var_name, var_formatting=None)[source]
Required part of template (between curly brackets)
Required variable part of template is defined by variable name and its format
- Parameters:
var_name – name of variable
var_formatting – how to format the variable
- append_variables(variables)[source]
Appends the variables in this part to the provided list in order
- __annotations__ = {}
- __module__ = 'fsl.utils.filetree.utils'
- class fsl.utils.filetree.utils.Optional(sub_template: Template)[source]
Bases:
Part
- __init__(sub_template: Template)[source]
Optional part of template (between square brackets)
Optional part can contain literal and required parts
- Parameters:
sub_template – part of the template within square brackets
- contains_optionals(variables=None)[source]
Returns True if this part contains the optional variables
- append_variables(variables)[source]
Appends the variables in this part to the provided list in order
- __annotations__ = {}
- __module__ = 'fsl.utils.filetree.utils'
- class fsl.utils.filetree.utils.Template(parts: Sequence[Part])[source]
Bases:
object
Splits a template into its constituent parts
- classmethod parse(text: str) Template [source]
Parses a text template into its constituent parts
- Parameters:
text – input template as string
- Returns:
same template split into its parts
- ordered_variables() Tuple[str] [source]
Sequence of all variables in order (can contain duplicates)
- fill_known(variables) Template [source]
Fill in the known variables
Any optional parts, where all variables have been filled will be automatically replaced
- remove_optionals(optionals=None) Template [source]
Removes any optionals containing the provided variables (default: remove all)
- resolve(variables) str [source]
Resolves the template given a set of variables
- Parameters:
variables – mapping of variable names to values
- Returns:
cleaned string
- get_all(variables, glob_vars=()) Tuple[Dict[str, str]] [source]
Gets all variables for files on disk matching the templates
- Parameters:
variables – (incomplete) mapping of variable names to values
glob_vars – sequence of undefined variables that can take any possible values when looking for matches on the disk
- optional_subsets() Iterator[Template] [source]
Yields template sub-sets with every combination optional variables
- extract_variables(filename, known_vars=None)[source]
Extracts the variable values from the filename
- Parameters:
filename – filename
known_vars – already known variables
- Returns:
dictionary from variable names to string representations (unused variables set to None)
- __dict__ = mappingproxy({'__module__': 'fsl.utils.filetree.utils', '__doc__': '\n Splits a template into its constituent parts\n ', '__init__': <function Template.__init__>, 'parse': <classmethod(<function Template.parse>)>, '__str__': <function Template.__str__>, 'optional_variables': <function Template.optional_variables>, 'required_variables': <function Template.required_variables>, 'ordered_variables': <function Template.ordered_variables>, 'fill_known': <function Template.fill_known>, '_fill_known_single': <function Template._fill_known_single>, 'remove_optionals': <function Template.remove_optionals>, 'resolve': <function Template.resolve>, 'get_all': <function Template.get_all>, '_get_all_helper': <function Template._get_all_helper>, 'optional_subsets': <function Template.optional_subsets>, 'extract_variables': <function Template.extract_variables>, '__dict__': <attribute '__dict__' of 'Template' objects>, '__weakref__': <attribute '__weakref__' of 'Template' objects>, '__annotations__': {}})
- __module__ = 'fsl.utils.filetree.utils'
- __weakref__
list of weak references to the object (if defined)
- fsl.utils.filetree.utils.resolve(template, variables)[source]
Resolves the template given a set of variables
- Parameters:
template – template
variables – mapping of variable names to values
- Returns:
cleaned string
- fsl.utils.filetree.utils.get_all(template, variables, glob_vars=())[source]
Gets all variables matching the templates given the variables
- Parameters:
template – template
variables – (incomplete) mapping of variable names to values
glob_vars – sequence of undefined variables that can take any possible values when looking for matches on the disk If glob_vars contains any defined variables, it will be ignored.
- Returns:
sequence of variables
- fsl.utils.filetree.utils.find_variables(template)[source]
Finds all the variables in the template
- Parameters:
template – full template
- Returns:
sequence of variables
- fsl.utils.filetree.utils.optional_variables(template)[source]
Finds the variables that can be skipped
- Parameters:
template – full template
- Returns:
set of variables that are only present in optional parts of the string
- fsl.utils.filetree.utils.extract_variables(template, filename, known_vars=None)[source]
Extracts the variable values from the filename
- Parameters:
template – template matching the given filename
filename – filename
known_vars – already known variables
- Returns:
dictionary from variable names to string representations (unused variables set to None)