parsers.py
Returns an attribute compound.
Usage:
>>> data = "@Link | Value | Boolean | Link Parameter"
>>> attributeCompound = foundations.parsers.getAttributeCompound("Attribute Compound", data)
>>> attributeCompound.name
u'Attribute Compound'
>>> attributeCompound.value
u'Value'
>>> attributeCompound.link
u'@Link'
>>> attributeCompound.type
u'Boolean'
>>> attributeCompound.alias
u'Link Parameter'
Parameters: | |
---|---|
Returns: | Attribute compound. |
Return type: | AttributeCompound |
Bases: foundations.dataStructures.Structure
Defines a storage object for attributes compounds usually encountered in sIBL_GUI Templates files.
Some attributes compounds:
- Name = @Name | Standard | String | Template Name
- Background|BGfile = @BGfile
- showCamerasDialog = @showCamerasDialog | 0 | Boolean | Cameras Selection Dialog
Initializes the class.
Usage:
AttributeCompound(name="showCamerasDialog",
value="0",
link="@showCamerasDialog",
type="Boolean",
alias="Cameras Selection Dialog")
Parameters: | **kwargs (dict) – name, value, link, type, alias. |
---|
Bases: foundations.io.File
Defines methods to parse sections file format files, an alternative configuration file parser is available directly with Python: ConfigParser.ConfigParser.
The parser given by this class has some major differences with Python ConfigParser.ConfigParser:
Sections and attributes are stored in their appearance order by default. ( Using Python collections.OrderedDict ) A default section ( _default ) will store orphans attributes ( Attributes appearing before any declared section ).File comments are stored inside the SectionsFileParser.comments class property.
Sections, attributes and values are whitespaces stripped by default but can also be stored with their leading and trailing whitespaces. Values are quotations markers stripped by default but can also be stored with their leading and trailing quotations markers.Attributes are namespaced by default allowing sections merge without keys collisions.
Initializes the class.
Usage:
>>> content = ["[Section A]\n", "; Comment.\n", "Attribute 1 = \"Value A\"\n", "\n", "[Section B]\n", "Attribute 2 = \"Value B\"\n"]
>>> sectionsFileParser = SectionsFileParser()
>>> sectionsFileParser.content = content
>>> sectionsFileParser.parse(stripComments=False)
<foundations.parsers.SectionsFileParser object at 0x293892011>
>>> sectionsFileParser.sections.keys()
[u'Section A', u'Section B']
>>> sectionsFileParser.comments
OrderedDict([(u'Section A|#0', {u'content': u'Comment.', u'id': 0})])
Parameters: |
|
---|
Property for self.__splitters attribute.
Returns: | self.__splitters. |
---|---|
Return type: | tuple or list |
Property for self.__namespaceSplitter attribute.
Returns: | self.__namespaceSplitter. |
---|---|
Return type: | unicode |
Property for self.__commentLimiters attribute.
Returns: | self.__commentLimiters. |
---|---|
Return type: | tuple or list |
Property for self.__commentMarker attribute.
Returns: | self.__commentMarker. |
---|---|
Return type: | unicode |
Property for self.__quotationMarkers attribute.
Returns: | self.__quotationMarkers. |
---|---|
Return type: | tuple or list |
Property for self. __rawSectionContentIdentifier attribute.
Returns: | self.__rawSectionContentIdentifier. |
---|---|
Return type: | unicode |
Property for self.__defaultsSection attribute.
Returns: | self.__defaultsSection. |
---|---|
Return type: | unicode |
Property for self.__sections attribute.
Returns: | self.__sections. |
---|---|
Return type: | OrderedDict or dict |
Property for self.__comments attribute.
Returns: | self.__comments. |
---|---|
Return type: | OrderedDict or dict |
Property for self.__parsingErrors attribute.
Returns: | self.__parsingErrors. |
---|---|
Return type: | list |
Property for self.__preserveOrder attribute.
Returns: | self.__preserveOrder. |
---|---|
Return type: | bool |
Usage:
>>> content = ["; Comment.\n", "Attribute 1 = \"Value A\"\n", "Attribute 2 = \"Value B\"\n"]
>>> sectionsFileParser = SectionsFileParser()
>>> sectionsFileParser.content = content
>>> sectionsFileParser.parse(stripComments=False)
<foundations.parsers.SectionsFileParser object at 0x860323123>
>>> sectionsFileParser.sections.keys()
[u'_defaults']
>>> sectionsFileParser.sections["_defaults"].values()
[u'Value A', u'Value B']
>>> sectionsFileParser.parse(stripComments=False, stripQuotationMarkers=False)
<foundations.parsers.SectionsFileParser object at 0x860323123>
>>> sectionsFileParser.sections["_defaults"].values()
[u'"Value A"', u'"Value B"']
>>> sectionsFileParser.comments
OrderedDict([(u'_defaults|#0', {u'content': u'Comment.', u'id': 0})])
>>> sectionsFileParser.parse()
<foundations.parsers.SectionsFileParser object at 0x860323123>
>>> sectionsFileParser.sections["_defaults"]
OrderedDict([(u'_defaults|Attribute 1', u'Value A'), (u'_defaults|Attribute 2', u'Value B')])
>>> sectionsFileParser.parse(namespaces=False)
<foundations.parsers.SectionsFileParser object at 0x860323123>
>>> sectionsFileParser.sections["_defaults"]
OrderedDict([(u'Attribute 1', u'Value A'), (u'Attribute 2', u'Value B')])
Parameters: |
|
---|---|
Returns: | SectionFileParser instance. |
Return type: | SectionFileParser |
Checks if given section exists.
Usage:
>>> content = ["[Section A]\n", "; Comment.\n", "Attribute 1 = \"Value A\"\n", "\n", "[Section B]\n", "Attribute 2 = \"Value B\"\n"]
>>> sectionsFileParser = SectionsFileParser()
>>> sectionsFileParser.content = content
>>> sectionsFileParser.parse()
<foundations.parsers.SectionsFileParser object at 0x845683844>
>>> sectionsFileParser.sectionExists("Section A")
True
>>> sectionsFileParser.sectionExists("Section C")
False
Parameters: | section (unicode) – Section to check existence. |
---|---|
Returns: | Section existence. |
Return type: | bool |
Checks if given attribute exists.
Usage:
>>> content = ["[Section A]\n", "; Comment.\n", "Attribute 1 = \"Value A\"\n", "\n", "[Section B]\n", "Attribute 2 = \"Value B\"\n"]
>>> sectionsFileParser = SectionsFileParser()
>>> sectionsFileParser.content = content
>>> sectionsFileParser.parse()
<foundations.parsers.SectionsFileParser object at 0x234564563>
>>> sectionsFileParser.attributeExists("Attribute 1", "Section A")
True
>>> sectionsFileParser.attributeExists("Attribute 2", "Section A")
False
Parameters: | |
---|---|
Returns: | Attribute existence. |
Return type: | bool |
Returns given section attributes.
Usage:
>>> content = ["[Section A]\n", "; Comment.\n", "Attribute 1 = \"Value A\"\n", "\n", "[Section B]\n", "Attribute 2 = \"Value B\"\n"]
>>> sectionsFileParser = SectionsFileParser()
>>> sectionsFileParser.content = content
>>> sectionsFileParser.parse()
<foundations.parsers.SectionsFileParser object at 0x125698322>
>>> sectionsFileParser.getAttributes("Section A")
OrderedDict([(u'Section A|Attribute 1', u'Value A')])
>>> sectionsFileParser.preserveOrder=False
>>> sectionsFileParser.getAttributes("Section A")
{u'Section A|Attribute 1': u'Value A'}
>>> sectionsFileParser.preserveOrder=True
>>> sectionsFileParser.getAttributes("Section A", stripNamespaces=True)
OrderedDict([(u'Attribute 1', u'Value A')])
Parameters: | |
---|---|
Returns: | Attributes. |
Return type: | OrderedDict or dict |
Returns all sections attributes.
Usage:
>>> content = ["[Section A]\n", "; Comment.\n", "Attribute 1 = \"Value A\"\n", "\n", "[Section B]\n", "Attribute 2 = \"Value B\"\n"]
>>> sectionsFileParser = SectionsFileParser()
>>> sectionsFileParser.content = content
>>> sectionsFileParser.parse()
<foundations.parsers.SectionsFileParser object at 0x845683844>
>>> sectionsFileParser.getAllAttributes()
OrderedDict([(u'Section A|Attribute 1', u'Value A'), (u'Section B|Attribute 2', u'Value B')])
>>> sectionsFileParser.preserveOrder=False
>>> sectionsFileParser.getAllAttributes()
{u'Section B|Attribute 2': u'Value B', u'Section A|Attribute 1': u'Value A'}
Returns: | All sections / files attributes. |
---|---|
Return type: | OrderedDict or dict |
Returns requested attribute value.
Usage:
>>> content = ["[Section A]\n", "; Comment.\n", "Attribute 1 = \"Value A\"\n", "\n", "[Section B]\n", "Attribute 2 = \"Value B\"\n"]
>>> sectionsFileParser = SectionsFileParser()
>>> sectionsFileParser.content = content
>>> sectionsFileParser.parse()
<foundations.parsers.SectionsFileParser object at 0x679302423>
>>> sectionsFileParser.getValue("Attribute 1", "Section A")
u'Value A'
Parameters: | |
---|---|
Returns: | Attribute value. |
Return type: | unicode |
Sets requested attribute value.
Usage:
>>> content = ["[Section A]\n", "; Comment.\n", "Attribute 1 = \"Value A\"\n", "\n", "[Section B]\n", "Attribute 2 = \"Value B\"\n"]
>>> sectionsFileParser = SectionsFileParser()
>>> sectionsFileParser.content = content
>>> sectionsFileParser.parse()
<foundations.parsers.SectionsFileParser object at 0x109304209>
>>> sectionsFileParser.setValue("Attribute 3", "Section C", "Value C")
True
Parameters: | |
---|---|
Returns: | Definition success. |
Return type: | bool |
Usage:
>>> sections = {"Section A": {"Section A|Attribute 1": "Value A"}, "Section B": {"Section B|Attribute 2": "Value B"}}
>>> sectionsFileParser = SectionsFileParser("SectionsFile.rc")
>>> sectionsFileParser.sections = sections
>>> sectionsFileParser.write()
True
>>> sectionsFileParser.read()
u'[Section A]\nAttribute 1 = Value A\n\n[Section B]\nAttribute 2 = Value B\n'
Parameters: | |
---|---|
Returns: | Method success. |
Return type: | bool |
Bases: foundations.io.File
Defines methods to parse plist files.
Initializes the class.
Usage:
>>> plistFileParser = PlistFileParser("standard.plist")
>>> plistFileParser.parse()
True
>>> plistFileParser.elements.keys()
[u'Dictionary A', u'Number A', u'Array A', u'String A', u'Date A', u'Boolean A', u'Data A']
>>> plistFileParser.elements["Dictionary A"]
{u'String C': u'My Value C', u'String B': u'My Value B'}
Parameters: | file (unicode) – Current file path. |
---|
Property for self.__elements attribute.
Returns: | self.__elements. |
---|---|
Return type: | OrderedDict or dict |
Property for self.__parsingErrors attribute.
Returns: | self.__parsingErrors. |
---|---|
Return type: | list |
Property for self.__unserializers attribute.
Returns: | self.__unserializers. |
---|---|
Return type: | dict |
Process the file content.
Usage:
>>> plistFileParser = PlistFileParser("standard.plist")
>>> plistFileParser.parse()
True
>>> plistFileParser.elements.keys()
[u'Dictionary A', u'Number A', u'Array A', u'String A', u'Date A', u'Boolean A', u'Data A']
Parameters: | raiseParsingErrors (bool) – Raise parsing errors. |
---|---|
Returns: | Method success. |
Return type: | bool |
Checks if given element exists.
Usage:
>>> plistFileParser = PlistFileParser("standard.plist")
>>> plistFileParser.parse()
True
>>> plistFileParser.elementExists("String A")
True
>>> plistFileParser.elementExists("String Nemo")
False
Parameters: | element (unicode) – Element to check existence. |
---|---|
Returns: | Element existence. |
Return type: | bool |
Usage:
>>> plistFileParser = PlistFileParser("standard.plist")
>>> plistFileParser.parse()
True
>>> plistFileParser.filterValues(r"String A")
[u'My Value A']
>>> plistFileParser.filterValues(r"String.*")
[u'My Value C', u'My Value B', u'My Value A']
Parameters: | |
---|---|
Returns: | Values. |
Return type: | list |
Usage:
>>> plistFileParser = PlistFileParser("standard.plist")
>>> plistFileParser.parse()
True
>>> plistFileParser.getValue("String A")
u'My Value A'
Parameters: | element (unicode) – Element to get the value. |
---|---|
Returns: | Element value. |
Return type: | object |