project¶
panel_flowdash.project
¶
Read and write the project file, flowdash.yaml.
The project file is one YAML 1.2 file of named sections that configure a
FlowDash app, e.g. the admin settings or an extension's metrics:. Every
key and value keeps the line and column it is written at, so a problem is
reported where it is.
flowdash serve reads flowdash.yaml (PROJECT_FILE), given with
--project or found in the served directory (find_project). Each
top-level section is read by the loader registered for it
(register_section); a section no one registered is ignored with a
warning, so a file written for a newer app or for an extension that is not
installed still loads.
A ProjectSection is the unit an extension registers, programmatically
or through the panel_flowdash.project_sections entry point group: the
section's loader, its dump for writing it back and, optionally, the
factory of its admin settings tab.
Dumper = Callable[[Any], Any]
module-attribute
¶
ENTRY_POINT_GROUP = 'panel_flowdash.project_sections'
module-attribute
¶
FILE_LOCK = threading.RLock()
module-attribute
¶
KEY = object()
module-attribute
¶
Loader = Callable[['Section'], Any]
module-attribute
¶
PROJECT_FILE = 'flowdash.yaml'
module-attribute
¶
SettingsView = Callable[['Project', bool], Any]
module-attribute
¶
WizardView = Callable[['Project'], Any]
module-attribute
¶
_AMBIGUOUS = re.compile('^(?:y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF|~|null|Null|NULL|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\\.[0-9_]*)?)$')
module-attribute
¶
_COMMENT = re.compile('(?:^|\\s)(#.*)$', re.MULTILINE)
module-attribute
¶
_ENV_REFERENCE = re.compile('\\$\\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\\}')
module-attribute
¶
_FILE_LOCK = FILE_LOCK
module-attribute
¶
_NOT_A_MAPPING = 'A project file is a mapping of sections, e.g. `metrics:`.'
module-attribute
¶
_REGISTRY_LOCK = threading.RLock()
module-attribute
¶
_SECTIONS = {}
module-attribute
¶
_SHARING_RETRY_SECONDS = 2.0
module-attribute
¶
__all__ = ['ENTRY_POINT_GROUP', 'FILE_LOCK', 'KEY', 'PROJECT_FILE', 'Problem', 'Project', 'ProjectError', 'ProjectSection', 'Section', 'SectionError', 'current_raw_section', 'current_section', 'expand_env', 'find_project', 'load_entry_points', 'load_project', 'parse_project', 'parse_project_data', 'register_section', 'sections', 'unregister_section']
module-attribute
¶
_entry_points_loaded = False
module-attribute
¶
_entry_points_loading = False
module-attribute
¶
_parse = parse_project_data
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Problem
dataclass
¶
A problem in a project file.
path, line and column say where it is written; section
and keys say which value it concerns, e.g. "metrics" and
("capacity", "table"), so a settings form can show it at its field.
Project
dataclass
¶
A loaded project file.
It holds the raw data, where each key and value is written, by path
(e.g. ("metrics", "capacity", "table")), the value each registered
loader made of its section, the warnings found while loading and the
context the loaders were given.
context = field(default_factory=dict)
class-attribute
instance-attribute
¶
data = field(default_factory=dict)
class-attribute
instance-attribute
¶
directory
property
¶
Return the directory the file is in, against which paths it names resolve.
path = None
class-attribute
instance-attribute
¶
positions = field(default_factory=dict)
class-attribute
instance-attribute
¶
warnings = field(default_factory=list)
class-attribute
instance-attribute
¶
get(name, default=None)
¶
Return the value of section name, or default if the file has none.
The value is what the section's loader returned. A section whose loader
was registered after the file was loaded is loaded now (raising
ProjectError); a section no loader is registered for gives its
raw data.
load_all()
¶
Load every section a loader is registered for, warning about the others.
Raises ProjectError with every problem the loaders found.
position(path)
¶
Return the 1-based line and column of the value at path.
A path that is not in the file gives the position of its nearest enclosing value.
problem(path, message, key=False)
¶
Return a problem at the value at path (with key, at the key naming it).
raw(name, default=None)
¶
Return the raw YAML data of section name.
set(name, value)
¶
Set section name to value and write it to the project's file.
The value goes through the section's dump and the result through
its loader, so a value the loader rejects raises ProjectError,
positioned where it would be written, and the file is left alone.
Only the lines of this section change in the file; comments inside it
are not kept. Text a YAML 1.1 reader would take for something else,
such as no or 1:20, is quoted.
update(name, change)
¶
Change section name from its raw data as the file has it now, and write it.
change is called with a copy of the section's raw YAML data, read
again from the file (None when the file has no such section), and
returns the new value, which is written as set writes it. The read,
change and the write happen under FILE_LOCK, so no other write
from this process comes in between. An exception from change
leaves the file alone. Returns the value written.
ProjectError
¶
Bases: ValueError
The problems of a project file, each with the file, line and column it concerns.
problems = list(problems)
instance-attribute
¶
ProjectSection
dataclass
¶
A top-level section of the project file, as an extension registers it.
loader turns the section's raw YAML data (Section.value) into
the typed value Project.get(name) gives, reporting problems by
raising SectionError (one problem at a path in the section) or
ProjectError (several, made with Section.problem) and warnings
with Section.warn; a ValueError or TypeError is reported at
the section's key. dump turns a typed value back into plain YAML
data for Project.set; without it the value is written as it is.
settings_view(project, editable), if given, builds the section's page
in the admin view, listed under the heading group, e.g. "Data".
wizard_view(project), if given, builds the section's step of the setup
wizard (a panel_flowdash.admin.wizard.WizardStep or a SettingsForm);
a "Data" section's step takes the place of the wizard's Data step,
any other comes after People. The wizard writes nothing until its Review.
description = ''
class-attribute
instance-attribute
¶
dump = None
class-attribute
instance-attribute
¶
group = 'Extensions'
class-attribute
instance-attribute
¶
icon = None
class-attribute
instance-attribute
¶
label
property
¶
Return the title, or one made from the name.
loader
instance-attribute
¶
name
instance-attribute
¶
settings_view = None
class-attribute
instance-attribute
¶
title = ''
class-attribute
instance-attribute
¶
wizard_view = None
class-attribute
instance-attribute
¶
Section
dataclass
¶
One top-level section of a project file, as a loader receives it.
context
property
¶
Return what the app loaded the project with, e.g. its data sources.
name
instance-attribute
¶
project
instance-attribute
¶
value
instance-attribute
¶
position(path=())
¶
Return the line and column of the value at path in this section.
problem(path, message, key=False)
¶
Return a problem at the value at path in this section (with key, at its key).
warn(path, message, key=False)
¶
Report a problem at path that does not stop the section from loading.
SectionError
¶
Bases: ValueError
A section loader's problem at path inside its section.
path is e.g. ("capacity", "table"). With key=True the problem
is reported where the last key of path is written instead of its value.
_anchor_owners(node, owner, owners, shared)
¶
Record the top-level key that first holds each node under node.
A node met again under another key is an alias of an anchor defined
earlier; it is added to shared with its anchor and both keys.
_build(yaml, node, path, positions, problems, source)
¶
_check_anchors(text, name, source)
¶
Refuse to replace section name when another key aliases an anchor defined in it.
_check_written(name, value, old, new, source)
¶
_dump(document)
¶
_mark(node)
¶
_plain(value, quote=False)
¶
Return value with tuples as lists and, with quote, ambiguous text quoted.
_problem(message, source, line, column, path)
¶
_qualname(loader)
¶
_read(path)
¶
_register_entry_point(entry_point)
¶
_replace_section(text, data, positions, name, value, source=None)
¶
Return text with section name set to value and its other lines untouched.
_retry_sharing(operation)
¶
Run operation, retrying a Windows sharing violation for a bounded time.
_rewrite(text, data, name, value, source)
¶
Return text written whole with section name set to value.
ruamel's round trip keeps the comments and merge keys it can; whatever it cannot keep, or a file it does not read back the same, is logged.
_write_atomically(target, text)
¶
Replace target with text through a temporary file in its directory.
current_raw_section(project, name, default=None)
¶
Return a copy of the raw YAML data of section name as the project file has it now.
The file is read again under FILE_LOCK, so what another process wrote
since the project was loaded counts; without a file it is the project's
own data. default when the section is not there. Raises
ProjectError when the file cannot be read or parsed.
current_section(project, name)
¶
Return section name as the project file has it now, loaded by its loader.
The file is read again, so what another process wrote since the project was loaded counts; without a file, or if it cannot be read, the loaded value is returned. None when the section is not in the file.
error_text(exc)
¶
Return an exception's message with its credentials masked, to show a user or an agent.
expand_env(value, environ=None)
¶
Replace ${NAME} and ${NAME:-default} references with environment values.
Text in value, or in the lists and mappings it holds, is expanded;
other values are returned as they are. Raises ValueError naming a
variable that is not set and has no default. A loader keeps the
reference and expands it where it is used, so dump writes the
reference back and never the secret.
find_project(places=(), cwd=None)
¶
Find the project file an app reads when none is given.
It is PROJECT_FILE in the first of places that has one (a local
directory, or a local file whose directory is used; URLs and other
values are skipped), else in cwd (the working directory by default).
None if there is none.
load_entry_points(force=False)
¶
Register the sections installed packages declare as entry points, once.
The group is panel_flowdash.project_sections. Each entry point names
a ProjectSection, a sequence of them, or a callable returning either.
One that fails to load is skipped with a warning. Concurrent callers
wait until every entry point is registered.
load_project(path, context=None, *, lazy=False)
¶
Read and parse the project file at path, see parse_project.
parse_project(text, path=None, context=None, *, lazy=False)
¶
Parse the project file in text and load each registered section.
context is handed to the loaders as Section.context. Raises
ProjectError with every problem found: invalid YAML, a file that is
not a mapping, a key given twice or a section its loader rejects.
Unknown sections and loader warnings are logged and kept in
Project.warnings.
With lazy, no section is loaded now: each is loaded when first asked
for with Project.get, so a section the file gets wrong is reported
where it is used rather than stopping whoever reads the file.
parse_project_data(text, source=None)
¶
Parse the project file in text into its raw data and the positions of its values.
Nothing is loaded: the data is the YAML as written, and the positions map
each value's path (see Project.positions) to its 1-based line and
column. source names the file in problems. Raises ProjectError
for invalid YAML, a file that is not a mapping or a key given twice.
register_section(section, loader=None, **fields)
¶
Register a section of the project file and return it.
The section is a ProjectSection, or its name with its loader and
other ProjectSection fields. Registering a name again replaces it,
so a module that is reloaded can register again.
sections()
¶
Return the registered sections by name, including those of installed extensions.
unregister_section(name)
¶
Remove the section name from the registry, if it is registered.