scd.files

All classes and routines related to files.

class scd.files.File(name, data, config)

This is a wrapper for a file on FS which should be managed by scd.

The same story as for scd.config.Config: this wrapper is used for purposes of conveience mostly. Also, it is required when one need to emit a list of SearchReplace instances for a file.

Parameters:
  • name (str) – The name of the file from config (as is, not absolute one)
  • config (scd.config.Config) – Instance of used config.
  • data (list) – A contents of search/replacement parts of the config.
all_replacements

Mapping of all known replacements for a file.

This mapping includes default replacements and those, defined in config file.

Key is the name of the replacement, value is an instance of jinja2.Template.

Returns:Mapping of replacements.
Return type:dict[str, str]
all_search_patterns

Mapping of all search patterns for a file.

This mapping includes default patterns and those, defined in config file.

Key is the name of the replacement, value is compiled regular expression.

Returns:Mapping of patterns.
Return type:dict[str, str]
default_replace_pattern

Property, returns default replacement template from config.

Returns:Default replacement pattern
Return type:jinja2.Template
default_replacements

Mapping of default replacements for a file.

Key is the name of the replacement, value is an instance of jinja2.Template.

Returns:Mapping of replacements.
Return type:dict[str, str]
default_search_pattern

Property, returns default search pattern from config.

Returns:Default search pattern
Return type:Regular expression
default_search_patterns

Mapping of default search patterns for a file.

Key is the name of the replacement, value is compiled regular expression.

Returns:Mapping of patterns.
Return type:dict[str, str]
filename

Relative filename of the file.

The most cool part about this property is that such name is platform independent: on Windows it might be docsconf.py, on Linux: docs/conf.py. That cool.

Returns:Native platform filename
Return type:str
path

Absolute path to the file for current platform.

Returns:Native platform absolute path.
Return type:str
patterns

A list of search/replacements for a file, based on config.

Returns:List of instances for file management.
Return type:list[SearchReplace]
class scd.files.SearchReplace(search, replace)

Class, which presents a pair of single search and replacement.

Parameters:
  • search (regexp) – Search regular expression.
  • replace (jinja2.Template) – Replacement template
static get_replacement(replace, version)

Return rendered template, taken context from version.

Parameters:
  • replace (jinja2.Template) – Template for replacement.
  • version (scd.version.Version) – Version instance, where template takes context.
Returns:

Rendered template, ready to insert.

Raises:

ValueError – if there is no enough context to render template.

Return type:

str

process(version, text)

Process text according to given version.

This does what is expected: search in text (as a rule, line from file) and inserts replacement where required.

Parameters:
Returns:

Processed line, after inserting replacement if needed. Return original line otherwise.

Return type:

str

scd.files.make_pattern(base_pattern, config)

Function, which creates regular expression based on given pattern.

Also, it injects all predefined search regexps like pep440 etc.

Parameters:base_pattern (str) – Pattern to transform to regular expression instance.
Returns:Regular expression pattern
Return type:regexp
Raises:ValueError – if pattern cannot be parsed.
scd.files.make_template(template)

Function for creating template instance from text template.

Parameters:template (str) – Text template to process.
Returns:Correct template instance, based on given text.
Return type:jinja2.Template
scd.files.validate_access(files)

Function, which validates access to the files.

Parameters:files (list[scd.files.File]) – A list of files to check
Returns:Is all files are accessible or not
Return type:bool