Serializer¶
-
class
py_css_styleguide.serializer.
ManifestSerializer
[source]¶ Serialize parsed CSS to data suitable to Manifest.
Raises: SerializerError
– When there is an invalid syntax in parsed manifest.-
_metas
¶ Buffer to store serialized metas from parsed source. Default is an empty dict which reseted and filled from
serialize
method.Type: collections.OrderedDict
-
_DEFAULT_SPLITTER
¶ Default value splitter used for some structure kinds.
Type: string
-
value_splitter
(reference, prop, value, mode)[source]¶ Split a string into a list items.
Default behavior is to split on white spaces.
Parameters: - reference (string) – Reference name used when raising possible error.
- prop (string) – Property name used when raising possible error.
- value (string) – Property value to split.
- mode (string) –
Splitter mode. Default should come from
ManifestSerializer._DEFAULT_SPLITTER
.Available splitter are:
white-space
: Simply split a string on white spaces;json-list
: Assume the string is a JSON list to parse;
Returns: Return type: list
-
serialize_to_json
(name, datas)[source]¶ Serialize given datas to any object from assumed JSON string.
Parameters: - name (string) – Name only used inside possible exception message.
- datas (dict) – Datas to serialize.
Returns: Object depending from JSON content.
Return type: object
-
serialize_to_nested
(name, datas)[source]¶ Serialize given datas to a nested structure where each key create an item and each other variable is stored as a subitem with corresponding value (according to key index position).
Parameters: - name (string) – Name only used inside possible exception message.
- datas (dict) – Datas to serialize.
Returns: Nested dictionnary of serialized reference datas.
Return type: dict
-
serialize_to_flat
(name, datas)[source]¶ Serialize given datas to a flat structure
KEY:VALUE
whereKEY
comes fromkeys
variable andVALUE
comes fromvalues
variable.This means both
keys
andvalues
are required variable to be correctly filled (each one is a string of item separated with an empty space). Both resulting list must be the same length.Parameters: - name (string) – Name only used inside possible exception message.
- datas (dict) – Datas to serialize.
Returns: Flat dictionnay of serialized reference datas.
Return type: dict
-
serialize_to_list
(name, datas)[source]¶ Serialize given datas to a list structure.
List structure is very simple and only require a variable
--items
which is a string of values separated with an empty space. Every other properties are ignored.Parameters: - name (string) – Name only used inside possible exception message.
- datas (dict) – Datas to serialize.
Returns: List of serialized reference datas.
Return type: list
-
serialize_to_string
(name, datas)[source]¶ Serialize given datas to a string.
Simply return the value from required variable``value``.
Parameters: - name (string) – Name only used inside possible exception message.
- datas (dict) – Datas to serialize.
Returns: Value.
Return type: string
-
get_meta_references
(datas)[source]¶ Get manifest enabled references declaration
This required declaration is readed from
styleguide-metas-references
rule that require either a--names
or--auto
variable, each one define the mode to enable reference:- Manually
Using
--names
which define a list of names to enable, every other non enabled rule will be ignored.Section name (and so Reference name also) must not contains special character nor
-
so they still be valid variable name for almost any languages. For word separator inside name, use_
.- Automatic
Using
--auto
variable every reference rules will be enabled. The value of this variable is not important as long as it is not empty.In this mode, another variable is watched for, it is
excludes
which is a list of reference names to ignore.
If both of these variables are defined, only the manual enable mode “–names” is used.
Parameters: datas (dict) – Data where to search for meta references declaration. This is commonly the fully parsed manifest. Returns: A list of reference names. Return type: list
-
get_reference
(datas, name)[source]¶ Get serialized reference datas
Because every reference is turned to a dict (that stands on
keys
variable that is a list of key names), every variables must have the same exact length of word than the key name list.A reference name starts with ‘styleguide-reference-’ followed by name for reference.
A reference can contains variable
--structure
setted to"flat"
,"list"
or"string"
to define serialization structure.Parameters: - datas (dict) – Data where to search for reference declaration. This is commonly the fully parsed manifest.
- name (string) – Reference name to get and serialize.
Returns: Serialized reference datas.
Return type: collections.OrderedDict
-
get_available_references
(datas)[source]¶ Get available manifest reference names.
Every rules starting with prefix from
nomenclature.RULE_REFERENCE
are available references.Only name validation is performed on these references.
Parameters: datas (dict) – Data where to search for reference declarations. Returns: - List of every available reference names. This is the real
- name unprefixed.
Return type: list
-
get_enabled_references
(datas, meta_references)[source]¶ Get enabled manifest references declarations.
Enabled references are defined through meta references declaration, every other references are ignored.
Parameters: - datas (dict) – Data where to search for reference declarations. This is commonly the fully parsed manifest.
- meta_references (list) – List of enabled reference names.
Returns: Serialized enabled references datas.
Return type: collections.OrderedDict
-
serialize
(datas)[source]¶ Serialize datas to manifest structure with metas and references.
Only references are returned, metas are assigned to attribute
ManifestSerializer._metas
.Parameters: datas (dict) – Data where to search for reference declarations. This is commonly the fully parsed manifest. Returns: Serialized enabled references datas. Return type: collections.OrderedDict
-