configurationengine/source/cone/public/persistence.py
changeset 0 2e8eeb919028
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #
       
    16 
       
    17 import os
       
    18 from cone.public import api, exceptions
       
    19 
       
    20 
       
    21 class PersistenceFactory(api.FactoryBase):
       
    22     @classmethod
       
    23     def get_reader_for_elem(cls,elemname):
       
    24         for reader in ConeReader.__subclasses__():
       
    25             if reader.supported_elem(elemname):
       
    26                 return reader()
       
    27         raise exceptions.ConePersistenceError("No reader for given elemname %s found!" % elemname)
       
    28 
       
    29     @classmethod
       
    30     def get_writer_for_class(cls,classname):
       
    31         for writer in ConeWriter.__subclasses__():
       
    32             if writer.supported_class(classname):
       
    33                 return writer ()
       
    34         raise exceptions.ConePersistenceError("No writer for given class found!")
       
    35 
       
    36 
       
    37 class ConeHandler(object):
       
    38     ext        = ""
       
    39     class_type = ""
       
    40 
       
    41 
       
    42 class ConeReader(ConeHandler):
       
    43     '''
       
    44     Read data from the string and return a ConeObject
       
    45     '''
       
    46 
       
    47     @classmethod
       
    48     def supported_elem(cls, elemname, parent=None):
       
    49         """
       
    50         Class method to determine if this ConeReader supports reading 
       
    51         of the given element name
       
    52         """
       
    53         return False
       
    54 
       
    55     def loads(self,data):
       
    56         raise exceptions.NotSupportedException()
       
    57 
       
    58     def load(self,res):
       
    59         raise exceptions.NotSupportedException()
       
    60   
       
    61   
       
    62 class ConeWriter(object):
       
    63     '''
       
    64     Write a ConeObject to a string
       
    65     '''
       
    66     @classmethod
       
    67     def supported_class(cls, classname):
       
    68         """
       
    69         Class method to determine if this ConeWriter supports writing
       
    70         of the given class name
       
    71         """
       
    72         return False
       
    73 
       
    74     def dumps(self,ConeObject):
       
    75         raise exceptions.NotSupportedException()
       
    76 
       
    77     def dump(self,ConeObject,res,indent=True):
       
    78         raise exceptions.NotSupportedException()
       
    79 
       
    80 def indent(elem, level=0):
       
    81     i = os.linesep + level*"  "
       
    82     if len(elem):
       
    83         try:
       
    84             if not elem.text or not elem.text.strip():
       
    85                 elem.text = i + "  "
       
    86             for e in elem:
       
    87                 indent(e, level+1)
       
    88                 if not e.tail or not e.tail.strip():
       
    89                     e.tail = i + "  "
       
    90                 if not e.tail or not e.tail.strip():
       
    91                     e.tail = i
       
    92             else:
       
    93                 if level and (not elem.tail or not elem.tail.strip()):
       
    94                     elem.tail = i
       
    95         except AttributeError,e:
       
    96             # explanation for this kind of try-except required
       
    97             pass