Name and Name-Path
Naming the individual elements in a configuration is done using name-paths. The API provides the classes NamePath
and Name
for this purpose.
The NamePath
has a built-in mini-parser, that parsers a string in the name-path format defined by the Erbsland Configuration Language into a NamePath
object. This parser also generates sytnax errors, like the configuraton parser does. It is a safe way to use name-paths in string format in your code.
Usage
import erbsland.conf as elcl
doc = elcl.load("configuration.elcl")
# Implicit parsed name-path
port = doc.get_int("main.server.port")
# Pre-parsed name-path
NP_PORT = elcl.NamePath.from_text("main.server.port")
for server in doc["main.server"]:
port = server.get_int(NP_PORT)
path1 = elcl.NamePath.from_text("main.server")
path2 = elcl.NamePath.from_text("filter.keywords")
# Path operations
path3 = path1 / path2
path1.append(path2)
# Assemble a path from Name instances.
assembled_path = elcl.NamePath([
elcl.Name.create_regular("translations"),
elcl.Name.create_text("Welcome!"),
])
Interface
- class Name(_type: NameType, _value: str | int)
Represent a single name.
A regular name is always converted into its normalized lower-case form.
A text name is kept as is.
An index name is neither normalized nor range checked.
- classmethod create_regular(name: str) Name
Create a regular name from
name
.- Parameters:
name – The raw name text.
- Returns:
A
Name
instance of typeREGULAR
.- Raises:
ConfSyntaxError – If
name
violates the regular name rules.
- classmethod create_text(text: str) Name
Create a text name from
text
.- Parameters:
text – The text of the name.
- Returns:
A
Name
instance of typeTEXT
.
- property type: NameType
Return the
NameType
of this name.
- as_text() str
Return the underlying string value.
- Returns:
The name text.
- Raises:
TypeError – If the name does not store text.
- as_index() int
Return the underlying integer value.
- Returns:
The index value.
- Raises:
TypeError – If the name does not store an index.
- classmethod from_document(raw_text: str) Name
Parse a name captured from a document.
- Parameters:
raw_text – The raw text captured from the document.
- Returns:
The parsed name.
- Raises:
ConfSyntaxError – If
raw_text
contains invalid syntax.
- class NamePath(path: Name | list[Name] | None = None)
Represent a name path pointing to a value.
- classmethod from_text(text: str) NamePath
Parse a name path from
text
.A path is a sequence of segments separated by dots. Segments may be regular names (
foo
), numeric indices ([0]
), quoted text names ("foo"
) or text indices (""[0]
). Spacing around segments is ignored. Quoted names use standard backslash escapes such as"
,\n
or\u
.- Parameters:
text – The text to parse.
- Returns:
The parsed path.
- Raises:
ConfSyntaxError – If
text
contains invalid syntax.