fireman.load#

Feed data in through resources and sources.

exception fireman.load.DuplicateKeyError(messages)#

A mapping repeats a key.

messages#

One line per repeat, e.g. line 12: duplicate key 'rules'.

class fireman.load.Extracted(items, problems)#

Result of Resource.extract.

items#

Surviving (index, item) pairs, with their original positions.

Type:

tuple[tuple[int, object], …]

problems#

One entry per rejected item or error.

Type:

tuple[Problem, …]

exception fireman.load.LoadError(problems)#

Raised when input cannot be loaded.

problems#

Everything that went wrong.

exception fireman.load.NestingError#

The input is nested too deeply to parse.

class fireman.load.Resource(name, fn, schema=None, maps=(), firewall=None, policy=None, paths=())#

Items of one kind, checked against an optional schema and reshaped by converters.

A missing or wrong scope is allowed here, so scoped can fix it later; Pipeline.load reports what is left through scope_problem.

name#

What the items are.

Type:

ResourceNameT

fn#

Yields the raw items.

Type:

Callable[[], Iterable[object]]

schema#

Describes the raw data, if given. It is the user’s, not fireman’s.

Type:

type[BaseModel] | None

maps#

Converters applied in order after the schema.

Type:

tuple[Callable[[Any], Any], …]

firewall#

Firewall the items belong to, or None for global objects.

Type:

NameT | None

policy#

Policy the rules belong to. Rules need one; objects never have one.

Type:

NameT | None

paths#

Files the items come from. fireman load watches them for changes.

Type:

tuple[Path, …]

scope_problem()#

What is wrong with the scope, or None: rules need a firewall and a policy, objects no policy.

property scope: str#

Short label, such as global, edge or edge/inbound.

add_map(fn)#

Return a new resource with fn added after the existing converters.

scoped(*, firewall=None, policy=None)#

Return a new resource with this firewall and policy, for scopes found in the data.

The result is not checked until Pipeline.load.

reads(*paths)#

Return a new resource that also lists paths as files it reads (repeats are dropped).

extract()#

Run every raw item through the schema and converters.

Bad items are skipped and reported. If the generator itself fails (a missing file, say), what it yielded so far is kept and one problem without an index is recorded. Other exceptions propagate, since they are bugs.

class fireman.load.Source(resources)#

Resources that belong together. Several may share a name; all are kept, in order.

resources#

The resources of this source.

Type:

tuple[Resource, …]

property paths: tuple[Path, ...]#

Every file its resources read, each once.

fireman.load.csv_file(path, name, *, schema=None, firewall=None, policy=None, delimiter=',', encoding='utf-8-sig')#

Yield the rows of a CSV file as dict[str, str], read lazily.

Cells are strings, so a schema or converter is expected. The default utf-8-sig reads plain UTF-8 and drops the byte order mark Excel adds. A missing or unreadable file becomes a problem when the resource is extracted. The file is listed in paths, so a later change to it is noticed.

fireman.load.file_source(path)#

Read a YAML or JSON file, chosen by its suffix.

fireman.load.json_file(path)#

Read a JSON file, laid out like the YAML one.

fireman.load.load_json(text)#

Parse JSON, rejecting repeated keys (JSON gives no line for them).

Raises:
fireman.load.load_yaml(text)#

Parse YAML like yaml.safe_load, but reject repeated keys.

Raises:
  • DuplicateKeyError – With every repeated key and its line.

  • NestingError – If the text is nested too deeply.

  • yaml.YAMLError – If the text is not valid YAML.

fireman.load.resource(name, *, schema=None, firewall=None, policy=None)#

Turn a function yielding items into one that returns a Resource.

fireman.load.source(f)#

Turn a function returning resources into one that returns a Source.

fireman.load.yaml_file(path)#

Read a YAML file: global objects plus firewalls with their own objects and policies.

fireman.load.resource.ResourceNameT#

Type alias.

Type aliases are created through the type statement:

type Alias = int

In this example, Alias and int will be treated equivalently by static type checkers.

At runtime, Alias is an instance of TypeAliasType. The __name__ attribute holds the name of the type alias. The value of the type alias is stored in the __value__ attribute. It is evaluated lazily, so the value is computed only if the attribute is accessed.

Type aliases can also be generic:

type ListOrSet[T] = list[T] | set[T]

In this case, the type parameters of the alias are stored in the __type_params__ attribute.

See PEP 695 for more information.