Changeset 2716 in products


Ignore:
Timestamp:
Aug 3, 2010 3:39:15 PM (14 years ago)
Author:
mylan
Message:

Added dict type field serialization and deserialization

File:
1 edited

Legend:

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

    r1455 r2716  
    1818 
    1919 
     20 
     21####################################### 
     22#         Setup logging system        # 
     23####################################### 
     24import logging 
     25DEFAULT_LOG = "/tmp/quintagroup.transmogrifier.log" 
     26FORMAT = "[%(asctime)s]: %(message)s" 
     27#FORMAT = "[%H:%M:%S]: %(message)s" 
     28def createHandler(hndlr_cls, level, *args, **kwargs): 
     29    hndlr = hndlr_cls(*args, **kwargs) 
     30    hndlr.setLevel(level) 
     31    hndlr.setFormatter(logging.Formatter(FORMAT, datefmt="%H:%M:%S")) 
     32    return hndlr 
     33 
     34# Very IMPORTANT create logger as logging.Logger NOT logging.getLogger !!! 
     35logger = logging.Logger("Quintagroup Transmogrifier", logging.NOTSET) 
     36logger.addHandler(createHandler( 
     37        logging.FileHandler, logging.DEBUG, DEFAULT_LOG)) 
     38 
     39####################################### 
     40 
     41_marker = () 
     42 
     43 
    2044class ATAttribute(base.ATAttribute): 
    2145 
     
    2549        if not values: 
    2650            return 
     51 
     52        # if self.name in ['payablesMap', 'fieldMap']: 
     53        #     import ipdb;ipdb.set_trace() 
    2754 
    2855        is_ref = self.isReference(instance) 
     
    6188                node.appendChild(value_node) 
    6289            else: 
     90                items = getattr(value, 'items', _marker) 
     91                if items is not _marker and callable(items): 
     92                    type_attr = dom.createAttribute("type") 
     93                    type_attr.value = "dict" 
     94                    node.setAttributeNode( type_attr ) 
    6395                value_node = dom.createTextNode(value) 
    6496                node.appendChild(value_node) 
     
    81113        if te is not None: 
    82114            value = value.decode(te) 
     115        # process dictionary type 
     116        vtype = context.node.attrib.get('type', None) 
     117        if vtype and vtype=='dict': 
     118            try: 
     119                s = value.strip("{}\t\n") 
     120                # value = dict([map(lambda x:x.strip(" '\""), item.split("': '")) \ 
     121                #              for item in s.split("', '")]) 
     122                value = dict([map(lambda x:x.strip(" '\""), item.split(": ")) \ 
     123                             for item in s.split(", ")]) 
     124            except: 
     125                #import ipdb;ipdb.set_trace() 
     126                logger.error("Error on processing '%s' dict type field,\n"\ 
     127                    " for object: '%s'\n  " \ 
     128                    " for the following data: '%s'" % (str(context.instance.id), 
     129                     context.instance.absolute_url(), str(value))) 
     130 
    83131 
    84132        data = context.getDataFor(self.namespace.xmlns) 
Note: See TracChangeset for help on using the changeset viewer.