4.14. foundations.parsers

parsers.py

Platform:
Windows, Linux, Mac Os X.
Description:
Defines the SectionsFileParser class, PlistFileParser class and others parsing related objects.
Others:
Portions of the code from Fredrik Lundh: http://effbot.org/zone/element-iterparse.htm

4.14.1. Module Attributes

foundations.parsers.LOGGER

4.14.2. Functions

foundations.parsers.getAttributeCompound(attribute, value=None, splitter=u'|', bindingIdentifier=u'@')[source]

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:
  • attribute (unicode) – Attribute.
  • value (object) – Attribute value.
  • splitter (unicode) – Splitter.
  • bindingIdentifier (unicode) – Binding identifier.
Returns:

Attribute compound.

Return type:

AttributeCompound

4.14.3. Classes

class foundations.parsers.AttributeCompound(**kwargs)[source]

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.
class foundations.parsers.SectionsFileParser(file=None, splitters=(u'=', u':'), namespaceSplitter=u'|', commentLimiters=(u';', u'#'), commentMarker=u'#', quotationMarkers=(u'"', u"'", u'`'), rawSectionContentIdentifier=u'__raw__', defaultsSection=u'_defaults', preserveOrder=True)[source]

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:
  • file (unicode) – Current file path.
  • splitters (tuple or list) – Splitter characters.
  • namespaceSplitter (unicode) – Namespace splitters character.
  • commentLimiters (tuple or list) – Comment limiters characters.
  • commentMarker (unicode) – Character use to prefix extracted comments idientifiers.
  • quotationMarkers (tuple or list) – Quotation markers characters.
  • rawSectionContentIdentifier (unicode) – Raw section content identifier.
  • defaultsSection (unicode) – Default section name.
  • preserveOrder (bool) – Data order is preserved.
splitters[source]

Property for self.__splitters attribute.

Returns:self.__splitters.
Return type:tuple or list
namespaceSplitter[source]

Property for self.__namespaceSplitter attribute.

Returns:self.__namespaceSplitter.
Return type:unicode
commentLimiters[source]

Property for self.__commentLimiters attribute.

Returns:self.__commentLimiters.
Return type:tuple or list
commentMarker[source]

Property for self.__commentMarker attribute.

Returns:self.__commentMarker.
Return type:unicode
quotationMarkers[source]

Property for self.__quotationMarkers attribute.

Returns:self.__quotationMarkers.
Return type:tuple or list
rawSectionContentIdentifier[source]

Property for self. __rawSectionContentIdentifier attribute.

Returns:self.__rawSectionContentIdentifier.
Return type:unicode
defaultsSection[source]

Property for self.__defaultsSection attribute.

Returns:self.__defaultsSection.
Return type:unicode
sections[source]

Property for self.__sections attribute.

Returns:self.__sections.
Return type:OrderedDict or dict
comments[source]

Property for self.__comments attribute.

Returns:self.__comments.
Return type:OrderedDict or dict
parsingErrors[source]

Property for self.__parsingErrors attribute.

Returns:self.__parsingErrors.
Return type:list
preserveOrder[source]

Property for self.__preserveOrder attribute.

Returns:self.__preserveOrder.
Return type:bool
parse(rawSections=None, namespaces=True, stripComments=True, stripWhitespaces=True, stripQuotationMarkers=True, raiseParsingErrors=True)[source]
Process the file content and extracts the sections / attributes
as nested collections.OrderedDict dictionaries or dictionaries.

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:
  • rawSections (tuple or list) – Ignored raw sections.
  • namespaces (bool) – Attributes and comments are namespaced.
  • stripComments (bool) – Comments are stripped.
  • stripWhitespaces (bool) – Whitespaces are stripped.
  • stripQuotationMarkers (bool) – Attributes values quotation markers are stripped.
  • raiseParsingErrors (bool) – Raise parsing errors.
Returns:

SectionFileParser instance.

Return type:

SectionFileParser

sectionExists(section)[source]

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
attributeExists(attribute, section)[source]

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:
  • attribute (unicode) – Attribute to check existence.
  • section (unicode) – Section to search attribute into.
Returns:

Attribute existence.

Return type:

bool

getAttributes(section, stripNamespaces=False)[source]

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:
  • section (unicode) – Section containing the requested attributes.
  • stripNamespaces (bool) – Strip namespaces while retrieving attributes.
Returns:

Attributes.

Return type:

OrderedDict or dict

getAllAttributes()[source]

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
getValue(attribute, section, default=u'')[source]

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:
  • attribute (unicode) – Attribute name.
  • section (unicode) – Section containing the searched attribute.
  • default (object) – Default return value.
Returns:

Attribute value.

Return type:

unicode

setValue(attribute, section, value)[source]

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:
  • attribute (unicode) – Attribute name.
  • section (unicode) – Section containing the searched attribute.
  • value (object) – Attribute value.
Returns:

Definition success.

Return type:

bool

write(namespaces=False, splitter=u'=', commentLimiter=u';', spacesAroundSplitter=True, spaceAfterCommentLimiter=True)[source]
Writes defined file using SectionsFileParser.sections and
SectionsFileParser.comments class properties content.

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:
  • namespaces (bool) – Attributes are namespaced.
  • splitter (unicode) – Splitter character.
  • commentLimiter (unicode) – Comment limiter character.
  • spacesAroundSplitter (bool) – Spaces around attributes and value splitters.
  • spaceAfterCommentLimiter (bool) – Space after comments limiter.
Returns:

Method success.

Return type:

bool

class foundations.parsers.PlistFileParser(file=None)[source]

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.
elements[source]

Property for self.__elements attribute.

Returns:self.__elements.
Return type:OrderedDict or dict
parsingErrors[source]

Property for self.__parsingErrors attribute.

Returns:self.__parsingErrors.
Return type:list
unserializers[source]

Property for self.__unserializers attribute.

Returns:self.__unserializers.
Return type:dict
parse(raiseParsingErrors=True)[source]

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
elementExists(element)[source]

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
filterValues(pattern, flags=0)[source]
Filters the PlistFileParser.elements() class property elements using given pattern.
Will return a list of matching elements values, if you want to get only one element value, use the PlistFileParser.getValue() method instead.

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:
  • pattern (unicode) – Regex filtering pattern.
  • flags (int) – Regex flags.
Returns:

Values.

Return type:

list

getValue(element)[source]
Returns the given element value.
If multiple elements with the same name exists, only the first encountered will be returned.

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