Miscellaneous
plateforme.core.patterns
This module provides utilities to interact with Python patterns such as regular expressions within the Plateforme framework.
CHAR_MARK
module-attribute
A special character used as a placeholder for string transformations.
CHAR_SEP
module-attribute
A special character used as a separator for string transformations.
EMAIL_MAX_LENGTH
module-attribute
The maximum accepted length for an email address.
BracketType
module-attribute
BracketType = Literal['all', 'curly', 'round', 'square']
A type alias used for bracket removal options.
RegexPattern
Bases: StrEnum
A enumeration of regex patterns used within the Plateforme framework.
ALIAS
class-attribute
instance-attribute
Pattern for validating aliases.
It should start with a lowercase letter, followed by lowercase letters or digits. Underscores can be used to separate words within the name.
ALIAS_EXP
class-attribute
instance-attribute
Pattern for validating alias expressions.
It is the same as the ALIAS
pattern but allows an optional expression
spread symbol character '*'
at the end of the name, which is used to
indicate list expansion.
ASSIGNMENT
class-attribute
instance-attribute
Pattern for validating a selector assignment.
It can be prefixed with a case-insensitive version of an assignment alias
matching the ALIAS
pattern followed by an equal character. The
assignment value can contain any character other than newline. Optionally,
the value can be a question mark '?'
to represent a deferred value that
should be inferred when resolving the selection.
It captures the selection assignment in two groups:
1. alias
(optional): The selection assignment alias.
2. value
: The selection assignment value, or '?'
for deferred one.
EMAIL_FORMATTED
class-attribute
instance-attribute
Pattern for validating formatted email addresses.
It captures the email address in three groups:
1. unquoted_name
(optional): The unquoted email address name.
2. quoted_name
(optional): The quoted email address name.
3. email
: The email address in angle brackets without leading and
trailing spaces.
EMAIL_PLAIN
class-attribute
instance-attribute
Pattern for validating plain email addresses.
It captures the email address in two groups:
1. username
: The email address username.
2. domain
: The email address domain.
ENGINE_SCHEME
class-attribute
instance-attribute
Pattern for validating database engine URL schemes.
It captures and validates the scheme part of the connection string and with the dialect and driver parts separated in groups. The capturing groups are as follows:
-
scheme
: The full scheme string including both dialect and driver parts (e.g.mysql+aiomysql
wheremysql
is the dialect part andaiomysql
is the driver part). -
dialect
: The dialect part of the database scheme (e.g.mysql
inmysql+aiomysql
). -
driver
(optional): The driver part of the database scheme, if present (e.g.aiomysql
inmysql+aiomysql
).
ENGINE_ADDRESS
class-attribute
instance-attribute
Pattern for validating database engine URL addresses.
It captures and validates the address part of the connection string with the username, password, host, port, and database separated in groups. The capturing groups are as follows:
-
username
(optional): The username for database authentication (e.g.user
inuser:password@localhost:5432/dbname
). -
password
(optional): The password for database authentication (e.g.password
inuser:password@localhost:5432/dbname
). -
host
(optional): The hostname or IP address of the database server (e.g.localhost
inuser:password@localhost:5432/dbname
). -
port
(optional): The port number for the database server, if specified (e.g.5432
inuser:password@localhost:5432/dbname
). -
database
: The name or path of the database to connect to (e.g.dbname
inuser:password@localhost:5432/dbname
).
ENGINE
class-attribute
instance-attribute
Pattern for validating database engine URLs.
It captures and validates the whole connection string and each of its parts in separate groups. The capturing groups are as follows:
-
scheme
: The full scheme string including both dialect and driver parts (e.g.mysql+aiomysql
wheremysql
is the dialect part andaiomysql
is the driver part). -
dialect
: The dialect part of the database scheme (e.g.mysql
inmysql+aiomysql
). -
driver
(optional): The driver part of the database scheme, if present (e.g.aiomysql
inmysql+aiomysql
). -
username
(optional): The username for database authentication (e.g.user
inuser:password@localhost:5432/dbname
). -
password
(optional): The password for database authentication (e.g.password
inuser:password@localhost:5432/dbname
). -
host
(optional): The hostname or IP address of the database server (e.g.localhost
inuser:password@localhost:5432/dbname
). -
port
(optional): The port number for the database server, if specified (e.g.5432
inuser:password@localhost:5432/dbname
). -
database
: The name or path of the database to connect to (e.g.dbname
inuser:password@localhost:5432/dbname
).
LANGUAGE
class-attribute
instance-attribute
Pattern for validating language codes.
It should start with two letters, followed by a hyphen and two more letters (optional). It enforces the language code to follow the ISO 639-1 standard.
MODULE
class-attribute
instance-attribute
Pattern for validating module names.
It should start with a letter or an underscore, followed by letters, digits or underscores. Module segments can be separated by dots while underscores can be used within each segment to separate words.
NAME
class-attribute
instance-attribute
Pattern for validating names.
It should start with a lowercase letter, followed by lowercase letters or digits. Segments of the name can be separated by dots while underscores can be used within each segment to separate words.
NAME_EXP
class-attribute
instance-attribute
Pattern for validating name expressions.
It is the same as the NAME
pattern but allows an optional expression
spread symbol character '*'
at the end of each segment, which is used
to indicate list expansion.
PATH
class-attribute
instance-attribute
PATH = "^\\/[a-z][a-z\\d]*(?:-[a-z\\d]+)*(?:\\/(?:[a-z][a-z\\d]*(?:-[a-z\\d]+)*|\\{[a-z_][a-z\\d]*(?:_[a-z\\d]+)*\\}))*$"
Pattern for validating paths.
It should start with a slash and then for each segment a lowercase letter, followed by lowercase letters or digits. Path segments can be separated by slashes while hyphens can be used within each segment to separate words. Optionally segments can be enclosed in curly braces for path parameters, where the parameter name should start with a lowercase letter or an underscore, followed by letters, digits or underscores.
SLUG
class-attribute
instance-attribute
Pattern for validating slugs.
It should start with a lowercase letter, followed by lowercase letters or digits. Hyphens can be used to separate words within the slug.
TITLE
class-attribute
instance-attribute
Pattern for validating titles.
It should start with an uppercase letter, followed by any alphanumeric characters that can be separated by a hyphen. Additional words are separated by spaces, where hyphens can be used to separate parts of the title. More information can be found for the title case format standard at https://en.wikipedia.org/wiki/title_case.
VERSION
class-attribute
instance-attribute
Pattern for validating version numbers.
It should start with a number, followed by a dot and another number (optional), followed by a hyphen and a string of lowercase letters and zero or more digits (optional).
It captures the version information in four groups:
major
: The major version number.minor
(optional): The minor version number.patch
(optional): The patch version number.tag
(optional): The version tag, e.g.alpha1
,beta2
, etc.
match_any_pattern
Check if the input string matches any of the provided patterns.
This function iterates through a list of patterns and checks if the input string matches any of them from start to end. Each pattern is anchored to the start and end of the string to ensure a full match.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The string to be checked against the patterns. |
required |
*patterns
|
str
|
Variable length argument list of string patterns. |
()
|
Returns:
Type | Description |
---|---|
bool
|
|
bool
|
otherwise. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/patterns.py
parse_email
Parse a string containing a formatted email address into a tuple.
It parses an email address that is either plain or formatted as a name
followed by an email address in angle brackets. The email address is
validated against the EMAIL_FORMATTED
and EMAIL_PLAIN
patterns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The string containing the email address to parse. |
required |
Returns:
Type | Description |
---|---|
tuple[str | None, str]
|
A tuple containing the parsed email address name and email address. |
Note
Spaces are striped from the beginning and end of addresses.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/patterns.py
parse_selection_assignment
Parse a string containing a selection assignment into a tuple.
It parses a selection assignment that is formatted either as an alias
followed by an equal character and a value alias=value
, or just a
value. The assignment alias is case-insensitive and parsed as a lowercase
string, while the value can be any string other than a newline character.
Optionally, the value can be a question mark '?'
to represent a
deferred value that should be inferred when resolving the selection, it is
replaced with a Deferred
object.
In addition, the alias is optional and can be omitted, in which case the
wildcard character *
is returned as the alias.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The string containing the selection assignment to parse. |
required |
Returns:
Type | Description |
---|---|
tuple[str, Any]
|
A tuple containing the parsed selection assignment alias and value. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/patterns.py
parse_selection
Parse a string containing selection assignments into a dictionary.
It parses a composite selection that is formatted as a list of selection
assignments separated by a specified separator (by default a semicolons
';'
). Each selection assignment alias and value are parsed using the
regular expression pattern ASSIGNMENT
(see the function
parse_selection_assignment
for more details). Only one wildcard selection
assignment *
, i.e. a selection assignment without an alias, is allowed
in the selection assignments string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The string containing the selection assignments to parse. |
required |
separator
|
str
|
The separator used to split the selection assignments.
Defaults to a semicolon |
';'
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
A base selection dictionary containing the parsed selection assignments |
dict[str, Any]
|
where a wildcard alias |
dict[str, Any]
|
and values |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/patterns.py
pluralize
Convert a singular string to its plural form.
It's a simple pluralization function that handles common English pluralization patterns. The function is not perfect and may not cover all edge cases, but it should work well for most common words.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The singular string to pluralize. |
required |
Returns:
Type | Description |
---|---|
str
|
The pluralized form of the input string. |
Examples:
>>> pluralize('user') # -> users
>>> pluralize('box') # -> boxes
>>> pluralize('category') # -> categories
>>> pluralize('day') # -> days
>>> pluralize('leaf') # -> leaves
Source code in .venv/lib/python3.12/site-packages/plateforme/core/patterns.py
to_camel_case
Convert a string to camel case.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The string to convert. |
required |
Returns:
Type | Description |
---|---|
str
|
The converted string to camel case. |
Examples:
Source code in .venv/lib/python3.12/site-packages/plateforme/core/patterns.py
to_kebab_case
Convert a string to kebab case.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The string to convert. |
required |
Returns:
Type | Description |
---|---|
str
|
The converted string to kebab case. |
Examples:
Source code in .venv/lib/python3.12/site-packages/plateforme/core/patterns.py
to_pascal_case
Convert a string to pascal case.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The string to convert. |
required |
Returns:
Type | Description |
---|---|
str
|
The converted string to pascal case. |
Examples:
Source code in .venv/lib/python3.12/site-packages/plateforme/core/patterns.py
to_snake_case
Convert a string to snake case.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The string to convert. |
required |
Returns:
Type | Description |
---|---|
str
|
The converted string to snake case. |
Examples:
Source code in .venv/lib/python3.12/site-packages/plateforme/core/patterns.py
to_title_case
Convert a string to title case.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The string to convert. |
required |
Returns:
Type | Description |
---|---|
str
|
The converted string to title case. |
Examples:
Source code in .venv/lib/python3.12/site-packages/plateforme/core/patterns.py
to_name_case
to_name_case(
string: str,
*brackets_handlers: tuple[
BracketType | set[BracketType],
Callable[[str], str] | None,
],
) -> str
Convert a string to name case.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The string to convert. |
required |
*brackets_handlers
|
tuple[BracketType | set[BracketType], Callable[[str], str] | None]
|
List of brackets handler tuples containing the type
of brackets for which the enclosed content should be processed and
the processor function to apply on the enclosed content. The
options for brackets are |
()
|
Returns:
Type | Description |
---|---|
str
|
The converted string to name case. |
Examples:
>>> to_name_case('name-case/example')
'name_case.example'
>>> to_name_case('NameCase[Example]', ('square', None))
'name_case'
Source code in .venv/lib/python3.12/site-packages/plateforme/core/patterns.py
to_path_case
to_path_case(
string: str,
*brackets_handlers: tuple[
BracketType | set[BracketType],
Callable[[str], str] | None,
],
) -> str
Convert a string to path case.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The string to convert. |
required |
*brackets_handlers
|
tuple[BracketType | set[BracketType], Callable[[str], str] | None]
|
List of brackets handler tuples containing the type
of brackets for which the enclosed content should be processed and
the processor function to apply on the enclosed content. The
options for brackets are |
()
|
Returns:
Type | Description |
---|---|
str
|
The converted string to path case. |
Examples:
>>> to_path_case('path_case.example')
'path-case/example'
>>> to_path_case('PathCase[Example]', ('square', None))
Source code in .venv/lib/python3.12/site-packages/plateforme/core/patterns.py
plateforme.core.representations
This module defines classes for enhanced representation of objects within the Plateforme framework. It provides more debugging and logging readability.
The Representation
class is a mixin providing customized __str__
,
__repr__
, __pretty__
, and __rich_repr__
methods for detailed and
formatted object display, compatible with devtools and rich libraries.
The PlainRepresentation
class is a string subclass where the representation
excludes quotes, ideal for creating Python-compatible string outputs.
ReprArgs
module-attribute
A type alias for arguments representation.
ReprRich
module-attribute
A type alias for rich representation.
Representation
Representation(**kwargs: Any)
Initialize the instance dictionary with the provided keyword arguments.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/representations.py
PlainRepresentation
Bases: str
A plain representation of a string.
String class where representation doesn't include quotes. Useful with
Representation
when you want to return a string representation of an
object that is valid (or pseudo-valid) python.