project_config.tree module

Cached files tree used by the linter when using checker commands.

class project_config.tree.Tree(rootdir: str)[source]

Bases: object

Files cache used by the linter in checking processes.

It represents the tree of files and directories starting at the root directory of the project.

Instances of project_config.tree.Tree can be iterated with:

for fpath, fcontent in tree.files:
    if fcontent is None:
         # file does not exist
         ...
    elif not isinstance(fcontent, str):
         # file is a directory
         #
         # so `fcontent` is another Tree instance here
         for nested_fpath, nested_fcontent in fcontent.files:
             ...

If you want to get the serialialized version of the file you can use the method project_config.tree.Tree.serialize_file():

instance = tree.serialize_file(fpath)

If you are not inside a context were you have the content of the files (a common scenario for conditional actions) you can get them calling the method project_config.tree.Tree.get_file_content():

fcontent = tree.get_file_content(fpath)

This class caches the files contents along with their serialized versions, so subsequent access to the same files in the project tree are fast.

Parameters

rootdir (str) – Root directory of the project.

_cache_file(fpath: str) str[source]

Cache a file normalizing its path.

Parameters

fpath (str) – Relative path from root directory.

Returns

Normalized absolute path.

Return type

str

_generator(fpaths: Union[Iterator[str], List[str]]) Iterator[Tuple[str, Union[str, Iterator[str]]]][source]
cache_files(fpaths: Union[Iterator[str], List[str]]) None[source]

Cache a set of files given their paths.

Parameters

fpaths (list) – Paths to the files to store in cache.

get_file_content(fpath: str) Union[str, Iterator[str]][source]

Returns the content of a file given his relative path.

This method is tipically used by if plugin action conditionals to get the content of the files that are not defined in files subject rules fields.

Parameters

fpath (str) – Path to the file relative to the root directory.

rootdir: str
serialize_file(fpath: str) Any[source]

Returns the object-serialized version of a file.

This method is a convenient cache wrapper for project_config.serializers.serialize_for_url(). Is used by plugin actions which need an object-serialized version of files to perform operations against them, like the jmespath one.

Parameters

fpath (str) – Path to the file to serialize.