Package pyffi :: Module object_models :: Class FileFormat
[hide private]
[frames] | no frames]

Class FileFormat

source code

object --+
         |
        FileFormat
Known Subclasses:

This class is the base class for all file formats. It implements a number of useful functions such as walking over directory trees (walkData) and a default attribute naming function (name_attribute). It also implements the base class for representing file data (FileFormat.Data).
Nested Classes [hide private]
  Data
Base class for representing data in a particular format.
Instance Methods [hide private]

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __init__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Methods [hide private]
 
name_parts(cls, name)
Intelligently split a name into parts:
source code
 
name_attribute(cls, name)
Converts an attribute name, as in the description file, into a name usable by python.
source code
 
name_class(cls, name)
Converts a class name, as in the xsd file, into a name usable by python.
source code
 
walkData(cls, top, topdown=True, mode='rb')
A generator which yields the data of all files in directory top whose filename matches the regular expression RE_FILENAME.
source code
 
walk(cls, top, topdown=True, mode='rb')
A generator which yields all files in directory top whose filename matches the regular expression RE_FILENAME.
source code
Static Methods [hide private]
 
version_number(version_str)
Converts version string into an integer.
source code
Class Variables [hide private]
  RE_FILENAME = None
Override this with a regular expression (the result of a re.compile call) for the file extension of the format you are implementing.
  ARCHIVE_CLASSES = []
Override this with a list of archive formats that may contain files of the format.
  _RE_NAME_SEP = re.compile(r'[_\W]+')
Matches seperators for splitting names.
  _RE_NAME_DIGITS = re.compile(r'([0-9]+)|([a-zA-Z]+)')
Matches digits or characters for splitting names.
  _RE_NAME_CAMEL = re.compile(r'([A-Z][a-z]*)|([a-z]+)')
Finds components of camelCase and CamelCase names.
  _RE_NAME_LC = re.compile(r'[a-z]')
Matches a lower case character.
  _RE_NAME_UC = re.compile(r'[A-Z]')
Matches an upper case character.
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

version_number(version_str)
Static Method

source code 

Converts version string into an integer. This default implementation simply returns zero at all times, and works for formats that are not versioned.

Override for versioned formats.

Parameters:
  • version_str (str) - The version string.
Returns:
A version integer.

name_parts(cls, name)
Class Method

source code 

Intelligently split a name into parts:

  • first, split at non-alphanumeric characters
  • next, seperate digits from characters
  • finally, if some part has mixed case, it must be camel case so split it further at upper case characters
>>> FileFormat.name_parts("hello_world")
['hello', 'world']
>>> FileFormat.name_parts("HELLO_WORLD")
['HELLO', 'WORLD']
>>> FileFormat.name_parts("HelloWorld")
['Hello', 'World']
>>> FileFormat.name_parts("helloWorld")
['hello', 'World']
>>> FileFormat.name_parts("xs:NMTOKEN")
['xs', 'NMTOKEN']
>>> FileFormat.name_parts("xs:NCName")
['xs', 'N', 'C', 'Name']
>>> FileFormat.name_parts('this IS a sillyNAME')
['this', 'IS', 'a', 'silly', 'N', 'A', 'M', 'E']
>>> FileFormat.name_parts('tHis is A Silly naME')
['t', 'His', 'is', 'A', 'Silly', 'na', 'M', 'E']

name_attribute(cls, name)
Class Method

source code 

Converts an attribute name, as in the description file, into a name usable by python.

>>> FileFormat.name_attribute('tHis is A Silly naME')
't_his_is_a_silly_na_m_e'
>>> FileFormat.name_attribute('Test:Something')
'test_something'
>>> FileFormat.name_attribute('unknown?')
'unknown'
Parameters:
  • name (str) - The attribute name.
Returns:
Reformatted attribute name, useable by python.

name_class(cls, name)
Class Method

source code 

Converts a class name, as in the xsd file, into a name usable by python.

>>> FileFormat.name_class('this IS a sillyNAME')
'ThisIsASillyNAME'
Parameters:
  • name (str) - The class name.
Returns:
Reformatted class name, useable by python.

walkData(cls, top, topdown=True, mode='rb')
Class Method

source code 

A generator which yields the data of all files in directory top whose filename matches the regular expression RE_FILENAME. The argument top can also be a file instead of a directory. Errors coming from os.walk are ignored.

Note that the caller is not responsible for closing the stream.

This function is for instance used by pyffi.spells to implement modifying a file after reading and parsing.

Parameters:
  • top (str) - The top folder.
  • topdown (bool) - Determines whether subdirectories should be iterated over first.
  • mode (str) - The mode in which to open files.

walk(cls, top, topdown=True, mode='rb')
Class Method

source code 

A generator which yields all files in directory top whose filename matches the regular expression RE_FILENAME. The argument top can also be a file instead of a directory. Errors coming from os.walk are ignored.

Note that the caller is not responsible for closing the stream.

This function is for instance used by pyffi.spells to implement modifying a file after reading and parsing.

Parameters:
  • top (str) - The top folder.
  • topdown (bool) - Determines whether subdirectories should be iterated over first.
  • mode (str) - The mode in which to open files.