basic.py :  » Windows » Python-File-Format-Interface » PyFFI-2.1.4 » pyffi » object_models » xml » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Windows » Python File Format Interface 
Python File Format Interface » PyFFI 2.1.4 » pyffi » object_models » xml » basic.py
"""Implements base class for basic types."""

# --------------------------------------------------------------------------
# ***** BEGIN LICENSE BLOCK *****
#
# Copyright (c) 2007-2009, Python File Format Interface
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#
#    * Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials provided
#      with the distribution.
#
#    * Neither the name of the Python File Format Interface
#      project nor the names of its contributors may be used to endorse
#      or promote products derived from this software without specific
#      prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# ***** END LICENSE BLOCK *****
# --------------------------------------------------------------------------

from pyffi.utils.graph import DetailNode

class BasicBase(DetailNode):
    """Base class from which all basic types are derived.

    The BasicBase class implements the interface for basic types.
    All basic types are derived from this class.
    They must override read, write, get_value, and set_value.

    >>> import struct
    >>> class UInt(BasicBase):
    ...     def __init__(self, template = None, argument = 0):
    ...         self.__value = 0
    ...     def read(self, version = None, user_version = None, f = None,
    ...              link_stack = [], argument = None):
    ...         self.__value, = struct.unpack('<I', f.read(4))
    ...     def write(self, version = None, user_version = None, f = None,
    ...               block_index_dct = {}, argument = None):
    ...         f.write(struct.pack('<I', self.__value))
    ...     def get_value(self):
    ...         return self.__value
    ...     def set_value(self, value):
    ...         self.__value = int(value)
    >>> x = UInt()
    >>> x.set_value('123')
    >>> x.get_value()
    123
    >>> class Test(BasicBase): # bad: read, write, get_value, and set_value are
    ...                        # not implemented
    ...     pass
    >>> x = Test() # doctest: +ELLIPSIS
    >>> x.set_value('123') # doctest: +ELLIPSIS
    Traceback (most recent call last):
        ...
    NotImplementedError
    """

    _is_template = False # is it a template type?
    _has_links = False # does the type contain a Ref or a Ptr?
    _has_refs = False # does the type contain a Ref?
    _has_strings = False # does the type contain a string?

    def __init__(self, template = None, argument = None, parent = None):
        """Initializes the instance.

        :param template: type used as template
        :param argument: argument used to initialize the instance
            (see the Struct class).
        :param parent: The parent of this instance, that is, the instance this
            instance is an attribute of."""
        # parent disabled for performance
        #self._parent = weakref.ref(parent) if parent else None
        pass

    # string representation
    def __str__(self):
        """Return string representation."""
        return str(self.get_value())

    def read(self, stream, **kwargs):
        """Read object from file."""
        raise NotImplementedError

    def write(self, stream, **kwargs):
        """Write object to file."""
        raise NotImplementedError

    def fix_links(self, **kwargs):
        """Fix links. Called when all objects have been read, and converts
        block indices into blocks."""
        pass

    def get_links(self, **kwargs):
        """Return all links referred to in this object."""
        return []

    def get_strings(self, **kwargs):
        """Return all strings used by this object."""
        return []

    def get_refs(self, **kwargs):
        """Return all references (excluding weak pointers) used by this
        object."""
        return []

    def get_value(self):
        """Return object value."""
        raise NotImplementedError

    def set_value(self, value):
        """Set object value."""
        raise NotImplementedError

    def get_size(self, **kwargs):
        """Returns size of the object in bytes."""
        raise NotImplementedError

    def get_hash(self, **kwargs):
        """Returns a hash value (an immutable object) that can be used to
        identify the object uniquely."""
        raise NotImplementedError

    def replace_global_node(self, oldbranch, newbranch, **kwargs):
        """Replace a given branch."""
        pass

    #
    # user interface functions come next
    # these functions are named after similar ones in the TreeItem example
    # at http://doc.trolltech.com/4.3/itemviews-simpletreemodel.html
    #

    # DetailNode

    def get_detail_display(self):
        """Return an object that can be used to display the instance."""
        return str(self.get_value())

    # editor functions: default implementation assumes that the value is
    # also suitable for an editor; override if not

    def get_editor_value(self):
        """Return value suitable for editor."""
        return self.get_value()

    def set_editor_value(self, editorvalue):
        """Set value from editor value."""
        return self.set_value(editorvalue)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.