Package pywurfl :: Module serialize
[hide private]
[frames] | no frames]

Source Code for Module pywurfl.serialize

  1  # pywurfl - Wireless Universal Resource File Tools in Python 
  2  # Copyright (C) 2006-2008 Armand Lynch 
  3  # 
  4  # This library is free software; you can redistribute it and/or modify it 
  5  # under the terms of the GNU Lesser General Public License as published by the 
  6  # Free Software Foundation; either version 2.1 of the License, or (at your 
  7  # option) any later version. 
  8  # 
  9  # This library is distributed in the hope that it will be useful, but WITHOUT 
 10  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 11  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
 12  # details. 
 13  # 
 14  # You should have received a copy of the GNU Lesser General Public License 
 15  # along with this library; if not, write to the Free Software Foundation, Inc., 
 16  # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 17  # 
 18  # Armand Lynch <lyncha@users.sourceforge.net> 
 19   
 20  __doc__ = \ 
 21  """ 
 22  pywurfl WURFL class hierarchy serializer 
 23  """ 
 24   
 25  from time import ctime 
 26   
 27  try: 
 28      from cElementTree import ElementTree, Element, SubElement 
 29  except ImportError: 
 30      from elementtree.ElementTree import ElementTree, Element, SubElement 
 31   
 32  from pywurfl import __version__ as pw_version 
 33  from pywurfl.exceptions import WURFLException 
 34   
 35   
 36  __author__ = "Armand Lynch <lyncha@users.sourceforge.net>" 
 37  __copyright__ = "Copyright 2006-2008, Armand Lynch" 
 38  __license__ = "LGPL" 
 39  __url__ = "http://celljam.net/" 
 40  __all__ = ['Serialize'] 
 41   
 42   
43 -class Serialize(object):
44 - def __init__(self, devices):
45 self.root_device = devices.select_id('generic', instance=False) 46 self.common_caps = ['actual_device_root', 'children', 'devid', 'devua', 47 'groups', 'fall_back'] 48 self.cap_groups = {}
49
50 - def _find_group(self, cap):
51 group = self.cap_groups.get(cap) 52 if group is None: 53 for group in self.root_device.groups: 54 if cap in self.root_device.groups[group]: 55 self.cap_groups[cap] = group 56 return group 57 else: 58 msg = "'%s' capability does not belong to a generic group" 59 raise WURFLException(msg % cap) 60 return group
61
62 - def _get_value(self, x):
63 if isinstance(x, bool): 64 if x: 65 return "true" 66 else: 67 return "false" 68 if isinstance(x, int) or isinstance(x, float): 69 return str(x) 70 return x
71
72 - def _walk(self, device, devices):
73 new_dev = SubElement(devices, 'device') 74 new_dev.set('fall_back', device.fall_back) 75 new_dev.set('id', device.devid) 76 new_dev.set('user_agent', device.devua) 77 if device.actual_device_root: 78 new_dev.set('actual_device_root', 'true') 79 80 groups = {} 81 for cap in (c for c in device.__dict__ if c not in self.common_caps and 82 not c.startswith('_')): 83 value = self._get_value(getattr(device, cap)) 84 try: 85 groups[self._find_group(cap)].append((cap, value)) 86 except (AttributeError, KeyError): 87 groups[self._find_group(cap)] = [(cap, value)] 88 89 90 for group in groups: 91 new_group = SubElement(new_dev, 'group') 92 new_group.set('id', group) 93 for cap, value in groups[group]: 94 new_cap = SubElement(new_group, 'capability') 95 new_cap.set('name', cap) 96 new_cap.set('value', value) 97 98 for child in device.children: 99 self._walk(child, devices)
100
101 - def to_xml(self, filename):
102 """ 103 Serialize pywurfl's WURFL class hierarchy to xml 104 105 @param filename: The filename to save the hierarchy to 106 @type filename: string 107 """ 108 109 root = Element('wurfl') 110 version = SubElement(root, 'version') 111 ver = SubElement(version, 'ver') 112 ver.text = "Generated by pywurfl v%s" % pw_version 113 last_updated = SubElement(version, 'last_updated') 114 last_updated.text = ctime() 115 116 devs = SubElement(root, 'devices') 117 118 self._walk(self.root_device, devs) 119 120 tree = ElementTree(root) 121 tree.write(filename)
122 123 124 if __name__ == '__main__': 125 from wurfl import devices 126 Serialize(devices).to_xml('my.xml') 127