Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# -- Project information -----------------------------------------------------

Expand Down Expand Up @@ -58,7 +58,6 @@
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
Expand Down
1 change: 1 addition & 0 deletions docs/pyffi/object_models/any_type.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.any_type
1 change: 1 addition & 0 deletions docs/pyffi/object_models/array_type.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.array_type
1 change: 1 addition & 0 deletions docs/pyffi/object_models/basic.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.basic
1 change: 1 addition & 0 deletions docs/pyffi/object_models/binary_type.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.binary_type
1 change: 1 addition & 0 deletions docs/pyffi/object_models/common.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.common
1 change: 1 addition & 0 deletions docs/pyffi/object_models/editable.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.editable
1 change: 1 addition & 0 deletions docs/pyffi/object_models/expression.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.expression
File renamed without changes.
1 change: 1 addition & 0 deletions docs/pyffi/object_models/mex/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.mex
1 change: 1 addition & 0 deletions docs/pyffi/object_models/niftoolsxml/array.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.niftoolsxml.array
1 change: 1 addition & 0 deletions docs/pyffi/object_models/niftoolsxml/bit_struct.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.niftoolsxml.bit_struct
1 change: 1 addition & 0 deletions docs/pyffi/object_models/niftoolsxml/enum.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.niftoolsxml.enum
1 change: 1 addition & 0 deletions docs/pyffi/object_models/niftoolsxml/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.niftoolsxml
1 change: 1 addition & 0 deletions docs/pyffi/object_models/niftoolsxml/struct.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.niftoolsxml.struct_
1 change: 1 addition & 0 deletions docs/pyffi/object_models/simple_type.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.simple_type
1 change: 1 addition & 0 deletions docs/pyffi/object_models/xml/array.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.xml.array
1 change: 1 addition & 0 deletions docs/pyffi/object_models/xml/bit_struct.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.xml.bit_struct
1 change: 1 addition & 0 deletions docs/pyffi/object_models/xml/enum.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.xml.enum
1 change: 1 addition & 0 deletions docs/pyffi/object_models/xml/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.xml
1 change: 1 addition & 0 deletions docs/pyffi/object_models/xml/struct.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.xml.struct_
1 change: 1 addition & 0 deletions docs/pyffi/object_models/xsd/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodule:: pyffi.object_models.xsd
16 changes: 10 additions & 6 deletions examples/metaclasses/howto_generate_class_from_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

DEBUG = False


# This metaclass checks for the presence of an _attrs and __doc__ attribute.
# Used as metaclass of _Block.
class _MetaBlock(type):
Expand All @@ -21,6 +22,7 @@ def __init__(cls, name, bases, dct):
if '__doc__' not in dct:
raise TypeError(str(cls) + ': missing __doc__ attribute')


class _Block(object, metaclass=_MetaBlock):
"""Base class from which all file block types are derived.

Expand All @@ -31,6 +33,7 @@ class _Block(object, metaclass=_MetaBlock):
interface, see MetaFileFormat.__init__ for an example.
"""
_attrs = ()

# initialize all _attrs attributes
def __init__(self):
self._initAttributes(self.__class__)
Expand Down Expand Up @@ -65,6 +68,7 @@ def _strAttributes(self, cls):
s += str(name) + ' : ' + str(getattr(self, name)) + '\n'
return s


# The MetaFileFormat class transforms the XML description of a file format
# into a bunch of classes via the "type(name, bases, dct)" factory.
# Because its base is type, MetaFileFormat is a metaclass: each file format
Expand All @@ -87,28 +91,27 @@ def __init__(cls, name, bases, dct):
# of course we should read the data from file dct['xml_file_name']
# the code below is only a proof of concept
block_name = 'NiObjectNET'
block_ancestor = _Block # base of all block classes
block_ancestor = _Block # base of all block classes
block_dct = {}
# add docstring
block_dct['__doc__'] = 'Some nif object.'
# add class variable <block_name>._attrs, which
# is a tuple containing all attributes: their name, default, and so on
# (to be extended! probably have a tuple of Attribute instances
# instead of a tuple of tuples)
block_dct['_attrs'] = ( ('name', 'noname'), )
block_dct['_attrs'] = (('name', 'noname'),)
# create class cls.<block_name>
setattr(cls, block_name, type(block_name, (block_ancestor,), block_dct))
if DEBUG: print('cls.NiObjectNET: ', dir(cls.NiObjectNET)) # debug
if DEBUG: print('cls.NiObjectNET: ', dir(cls.NiObjectNET)) # debug

# do another one
block_name = 'NiNode'
block_ancestor = getattr(cls, 'NiObjectNET')
block_dct = {}
block_dct['__doc__'] = 'Basic node.'
block_dct['_attrs'] = ( ('translateX', 0.0), ('translateY', 0.0), ('translateZ', 0.0) )
block_dct['_attrs'] = (('translateX', 0.0), ('translateY', 0.0), ('translateZ', 0.0))
setattr(cls, block_name, type(block_name, (block_ancestor,), block_dct))
if DEBUG: print('cls.NiNode: ', dir(cls.NiNode)) # debug

if DEBUG: print('cls.NiNode: ', dir(cls.NiNode)) # debug


# The NifFormat class simply processes nif.xml via MetaFileFormat
Expand All @@ -117,6 +120,7 @@ def __init__(cls, name, bases, dct):
class NifFormat(object, metaclass=MetaFileFormat):
xml_file_name = "nif.xml"


# For example, NifFormat.NiNode is now a class representing the NiNode block
# type! The _Block class, from which NifFormat.NiNode is derived, takes care
# of initialization of all attributes, and printing them.
Expand Down
8 changes: 5 additions & 3 deletions examples/simple/simple.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os
import pyffi.object_models.xml

import pyffi.object_models.common
import pyffi.object_models.xml


class SimpleFormat(pyffi.object_models.xml.FileFormat):
xml_file_name = 'simple.xml'
xml_file_path = [ os.path.dirname(__file__) ]
xml_file_path = [os.path.dirname(__file__)]

# basic types

Expand All @@ -26,4 +28,4 @@ class Example:
def addInteger(self, x):
self.numIntegers += 1
self.integers.update_size()
self.integers[self.numIntegers-1] = x
self.integers[self.numIntegers - 1] = x
1 change: 1 addition & 0 deletions examples/simple/testread.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from simple import SimpleFormat

x = SimpleFormat.Data()
f = open('somefile.simple', 'rb')
x.read(f)
Expand Down
1 change: 1 addition & 0 deletions examples/simple/testwrite.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from simple import SimpleFormat

x = SimpleFormat.Data()
x.example.num_integers = 5
x.example.integers.update_size()
Expand Down
3 changes: 2 additions & 1 deletion pyffi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

.. toctree::
:maxdepth: 3
:titlesonly:

formats/index
spells/index
object_models
object_models/index

"""

Expand Down
13 changes: 7 additions & 6 deletions pyffi/formats/bsa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,14 @@


import logging
import struct
import os
import re
import struct

import pyffi.object_models.xml
import pyffi.object_models.common
from pyffi.object_models.xml.basic import BasicBase
import pyffi.object_models
from pyffi.utils.graph import EdgeFilter
import pyffi.object_models.common
import pyffi.object_models.xml
from pyffi.object_models.basic import BasicBase


class BsaFormat(pyffi.object_models.xml.FileFormat):
Expand Down Expand Up @@ -152,7 +151,7 @@ def get_size(self, data=None):

def read(self, stream, data=None):
length, = struct.unpack('<B', stream.read(1))
self._value = stream.read(length)[:-1] # strip trailing null byte
self._value = stream.read(length)[:-1] # strip trailing null byte

def write(self, stream, data=None):
stream.write(struct.pack('<B', len(self._value)))
Expand Down Expand Up @@ -328,6 +327,8 @@ def write(self, stream):
# write the file
raise NotImplementedError


if __name__ == '__main__':
import doctest

doctest.testmod()
Loading