Ignore:
Timestamp:
Aug 4, 2010 11:49:56 AM (14 years ago)
Author:
mylan
Message:

Added deserialization for dictionary type fields

File:
1 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.transmogrifier/branches/dictionary/quintagroup/transmogrifier/namespaces/atns.py

    r2719 r2720  
    1717from quintagroup.transmogrifier.namespaces.util import has_ctrlchars 
    1818 
    19 ####################################### 
    20 #         Setup logging system        # 
    21 ####################################### 
    22 import logging 
    23 DEFAULT_LOG = "/tmp/quintagroup.transmogrifier.log" 
    24 FORMAT = "[%(asctime)s]: %(message)s" 
    25 #FORMAT = "[%H:%M:%S]: %(message)s" 
    26 def createHandler(hndlr_cls, level, *args, **kwargs): 
    27     hndlr = hndlr_cls(*args, **kwargs) 
    28     hndlr.setLevel(level) 
    29     hndlr.setFormatter(logging.Formatter(FORMAT, datefmt="%H:%M:%S")) 
    30     return hndlr 
    31  
    32 # Very IMPORTANT create logger as logging.Logger NOT logging.getLogger !!! 
    33 logger = logging.Logger("Quintagroup Transmogrifier", logging.NOTSET) 
    34 logger.addHandler(createHandler( 
    35         logging.FileHandler, logging.DEBUG, DEFAULT_LOG)) 
    36  
    37 ####################################### 
    38  
    3919_marker = () 
    40  
    4120 
    4221class ATAttribute(base.ATAttribute): 
     
    5534 
    5635        def addNode(node, value, nspref): 
    57             # Create text node and add *value* to the *node*. 
    58             # Use *nspref* if needed. 
    5936            if is_ref: 
    6037                if config.HANDLE_REFS: 
     
    8158            return 
    8259 
    83         # if self.name in ['payablesMap', 'fieldMap']: 
    84         #     import ipdb;ipdb.set_trace() 
    85  
    8660        is_ref = self.isReference(instance) 
    8761         
     
    9569            items = getattr(value, 'items', _marker) 
    9670            if items is not _marker and callable(items): 
     71                # Map field 
    9772                # set type attribute for the field 
    9873                type_attr = dom.createAttribute("type") 
     
    11287                    node.appendChild(item_node) 
    11388            else: 
     89                # Common field 
    11490                value = getPreparedValue(value) 
    11591                addNode(node, value, self.namespace.xmlns) 
     
    12197 
    12298    def processXmlValue(self, context, value): 
    123         if value is None: 
    124             return 
     99        def getValue(node, value): 
     100            if value is None: 
     101                return 
     102            value = value.strip() 
     103            if not value: 
     104                return 
     105            # decode node value if needed 
     106            te = node.get('transfer_encoding', None) 
     107            if te is not None: 
     108                value = value.decode(te) 
     109            return value 
     110             
     111        # Get value type 
     112        vtype = context.node.attrib.get('type', None) 
     113        if vtype=='dict': 
     114            # process dictionary type 
     115            d = {} 
     116            for item in context.node: 
     117                k = item.get("key", None) 
     118                v = getValue(item, item.text) 
     119                if k and v: 
     120                    d[k] = v 
     121            value = d 
     122        else: 
     123            # Common field 
     124            value = getValue(context.node, value) 
    125125 
    126         value = value.strip() 
    127126        if not value: 
    128127            return 
    129  
    130         # decode node value if needed 
    131         te = context.node.get('transfer_encoding', None) 
    132         if te is not None: 
    133             value = value.decode(te) 
    134         # process dictionary type 
    135         vtype = context.node.attrib.get('type', None) 
    136         if vtype and vtype=='dict': 
    137             try: 
    138                 s = value.strip("{}\t\n") 
    139                 # value = dict([map(lambda x:x.strip(" '\""), item.split("': '")) \ 
    140                 #              for item in s.split("', '")]) 
    141                 value = dict([map(lambda x:x.strip(" '\""), item.split(": ")) \ 
    142                              for item in s.split(", ")]) 
    143             except: 
    144                 #import ipdb;ipdb.set_trace() 
    145                 logger.error("Error on processing '%s' dict type field,\n"\ 
    146                     " for object: '%s'\n  " \ 
    147                     " for the following data: '%s'" % (str(context.instance.id), 
    148                      context.instance.absolute_url(), str(value))) 
    149  
    150128 
    151129        data = context.getDataFor(self.namespace.xmlns) 
Note: See TracChangeset for help on using the changeset viewer.