Back to index

plone3  3.1.7
uuns.py
Go to the documentation of this file.
00001 ##################################################################
00002 # Marshall: A framework for pluggable marshalling policies
00003 # Copyright (C) 2004 EnfoldSystems, LLC
00004 # Copyright (C) 2004 ObjectRealms, LLC
00005 #
00006 # This program is free software; you can redistribute it and/or modify
00007 # it under the terms of the GNU General Public License as published by
00008 # the Free Software Foundation; either version 2 of the License, or
00009 # (at your option) any later version.
00010 #
00011 # This program is distributed in the hope that it will be useful,
00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 # GNU General Public License for more details.
00015 #
00016 # You should have received a copy of the GNU General Public License
00017 # along with this program; if not, write to the Free Software
00018 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ##################################################################
00020 
00021 """
00022 $Id: $
00023 """
00024 
00025 # Georg Gogo. BERNHARD: UUencoded pluggable namespace for all at fields
00026 
00027 #from DateTime import DateTime
00028 from Products.Marshall.public import XmlNamespace, SchemaAttribute
00029 from Products.Archetypes import public as atapi
00030 from cStringIO import StringIO
00031 
00032 import base64
00033 import uu
00034 
00035 DEBUG = False
00036 
00037 
00038 
00039 class UUAttribute(SchemaAttribute):
00040 
00041 #    def get(self, instance):
00042 #        values = atapi.BaseObject.__getitem__(instance, self.name)
00043 #        
00044 #        import pdb; pdb.set_trace() # @@@
00045 #        
00046 #        field = instance.Schema().getField(self.name)
00047 #        values = field.getRaw(instance)
00048 #        
00049 #        
00050 #        
00051 #        
00052 #        if not isinstance( values, ( list, tuple ) ):
00053 #            values = [values]
00054 #        return filter(None, values)
00055 
00056 
00057 #    def get(self, instance):
00058 #        values = atapi.BaseObject.__getitem__(instance, self.name)
00059 #        if not isinstance( values, ( list, tuple ) ):
00060 #            values = [values]
00061 #        return filter(None, values)
00062         
00063         
00064     def base64encode(self, string) :
00065         return base64.encodestring(string)
00066         
00067     def base64decode(self, string) :
00068         return base64.decodestring(string)
00069         
00070     def uuencode(self, string) :
00071         # uu encode a srting
00072         stringio = StringIO()
00073         uu.encode(StringIO(string), stringio)
00074         return stringio.getvalue()
00075         
00076     def uudecode(self, string) :
00077         # uu decode a srting
00078         stringio = StringIO()
00079         uu.decode(StringIO(string), stringio)
00080         return stringio.getvalue()
00081         
00082     def char_encode(self, string) :
00083         return self.uuencode(string)
00084 
00085     def char_decode(self, string) :
00086         return self.uudecode(string)
00087     
00088 
00089     def deserialize(self, instance, ns_data):
00090         values = ns_data.get( self.name )
00091         if not values:
00092             return
00093         
00094         field = instance.Schema().getField(self.name)
00095         baseunit = field.getBaseUnit(instance)
00096         isBinary = baseunit.isBinary() 
00097         if isBinary :
00098             values = self.char_decode( values )
00099         
00100         mutator = instance.Schema()[self.name].getMutator(instance)
00101         if not mutator:
00102             #raise AttributeError("No Mutator for %s"%self.name)
00103             return
00104         
00105         mutator(values)
00106 
00107 
00108     def serialize(self, dom, parent_node, instance):
00109         
00110         if DEBUG :
00111             print "uuns/UUattribute", instance, self.name
00112         
00113         field = instance.Schema().getField(self.name)
00114         baseunit = field.getBaseUnit(instance)
00115         value = baseunit.getRaw(encoding=baseunit.original_encoding)
00116 
00117         isBinary = baseunit.isBinary() 
00118         if isBinary :
00119             value = self.char_encode(value)
00120         
00121         elname = "%s:%s"%(self.namespace.prefix, self.name)
00122         node = dom.createElementNS( self.namespace.xmlns,
00123                                     elname )
00124         value_node = dom.createTextNode( value )
00125         node.appendChild( value_node )
00126         node.normalize()
00127         
00128         if  isBinary :
00129             id_attr = dom.createAttributeNS( self.namespace.xmlns, "binary")
00130             id_attr.value = "uuencoded"
00131             node.setAttributeNode( id_attr )
00132         
00133         parent_node.appendChild( node )
00134         
00135                                   
00136 class UUNS(XmlNamespace):
00137     
00138     xmlns = 'uuns:ns:data'
00139     prefix = 'at_data'
00140     attributes = []
00141     
00142     #uses_at_fields = True
00143     
00144     
00145     def getAttributeByName(self, schema_name, context=None):
00146         if context is not None and schema_name not in self.at_fields:
00147             assert context.instance.Schema().has_key( schema_name )
00148         
00149         #if schema_name in self.at_fields:
00150         #    return self.at_fields[ schema_name ]
00151             
00152         attribute = UUAttribute( schema_name )
00153         attribute.setNamespace( self )
00154         
00155         return attribute
00156         
00157         
00158     def getAttributes(self, instance):
00159         
00160         field_keys = instance.Schema().keys()
00161         if DEBUG :
00162             print "UUNS/getAttributes", field_keys
00163         #import pdb; pdb.set_trace() # @@@
00164         #return fields
00165         
00166         # remove fields delegated to other namespaces
00167         #fields = []
00168         #for ns in getRegisteredNamespaces():
00169         #    if ns.uses_at_fields:
00170         #        fields.extend( ns.getATFields() )
00171         #mset = Set(fields)
00172         #assert len(mset) == len(fields), "Multiple NS multiplexing field"
00173         #
00174         #field_keys = Set(instance.Schema().keys())-mset
00175         #
00176         ## remove primary field if still present
00177         #p = instance.getPrimaryField()
00178         #pk = p and p.getName() or None
00179         #if pk:
00180         #    field_keys.remove( pk )
00181         #
00182 
00183 
00184 #        import pdb; pdb.set_trace() # @@@
00185         
00186         for fk in field_keys:
00187             field = instance.Schema().getField(fk)
00188             
00189             if hasattr(field, 'getBaseUnit') :
00190                 baseunit = field.getBaseUnit(instance)
00191             else :
00192                 #print ">>>>>>>> field==", field, "no getBaseUnit!"
00193                 continue
00194                 
00195             isBinary = baseunit.isBinary()
00196             
00197             if isBinary or instance.getPrimaryField() == field:
00198                 yield self.getAttributeByName( fk )
00199             
00200             if DEBUG :        
00201                 print fk, isBinary
00202             
00203         #
00204         ## yield additional intrinsic at framework attrs
00205         #for attribute in self.at_fields.values():
00206         #    yield attribute
00207 
00208 
00209     def serialize(self, dom, parent_node, instance):
00210 
00211         for attribute in self.getAttributes( instance ):
00212             attribute.serialize( dom, parent_node, instance )
00213 
00214     def deserialize(self, instance, ns_data):
00215         if not ns_data:
00216             return
00217 
00218         for attribute in self.getAttributes( instance ):
00219             attribute.deserialize( instance, ns_data )