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 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   
 26  from time import ctime 
 27   
 28  try: 
 29      from cElementTree import ElementTree, Element, SubElement 
 30  except ImportError: 
 31      from elementtree.ElementTree import ElementTree, Element, SubElement 
 32   
 33  from pywurfl import __version__ as pw_version 
 34  from pywurfl.exceptions import BaseException 
 35   
 36   
 37  __author__ = "Armand Lynch <lyncha@users.sourceforge.net>" 
 38  __copyright__ = "Copyright 2006, Armand Lynch" 
 39  __license__ = "LGPL" 
 40  __url__ = "http://celljam.net/" 
 41  __all__ = ['Serialize'] 
 42   
 43   
44 -class Serialize(object):
45 - def __init__(self, devices):
46 self.root_device = devices.select_id('generic', instance=False) 47 self.common_caps = ['actual_device_root', 'children', 'devid', 'devua', 48 'groups', 'fall_back'] 49 self.cap_groups = {}
50
51 - def _find_group(self, cap):
52 group = self.cap_groups.get(cap) 53 if group is None: 54 for group in self.root_device.groups: 55 if cap in self.root_device.groups[group]: 56 self.cap_groups[cap] = group 57 return group 58 else: 59 msg = "'%s' capability does not belong to a generic group" 60 raise BaseException(msg % cap) 61 return group
62
63 - def _get_value(self, x):
64 if isinstance(x, bool): 65 if x: 66 return "true" 67 else: 68 return "false" 69 if isinstance(x, int) or isinstance(x, float): 70 return str(x) 71 return x
72
73 - def _walk(self, device, devices):
74 new_dev = SubElement(devices, 'device') 75 new_dev.set('fall_back', device.fall_back) 76 new_dev.set('id', device.devid) 77 new_dev.set('user_agent', device.devua) 78 if device.actual_device_root: 79 new_dev.set('actual_device_root', 'true') 80 81 groups = {} 82 for cap in (c for c in device.__dict__ if c not in self.common_caps and 83 not c.startswith('_')): 84 value = self._get_value(getattr(device, cap)) 85 try: 86 groups[self._find_group(cap)].append((cap, value)) 87 except (AttributeError, KeyError): 88 groups[self._find_group(cap)] = [(cap, value)] 89 90 91 for group in groups: 92 new_group = SubElement(new_dev, 'group') 93 new_group.set('id', group) 94 for cap, value in groups[group]: 95 new_cap = SubElement(new_group, 'capability') 96 new_cap.set('name', cap) 97 new_cap.set('value', value) 98 99 for child in device.children: 100 self._walk(child, devices)
101
102 - def to_xml(self, filename):
103 """ 104 Serialize pywurfl's WURFL class hierarchy to xml 105 106 @param filename: The filename to save the hierarchy to 107 @type filename: string 108 """ 109 110 root = Element('wurfl') 111 version = SubElement(root, 'version') 112 ver = SubElement(version, 'ver') 113 ver.text = "Generated by pywurfl v%s" % pw_version 114 last_updated = SubElement(version, 'last_updated') 115 last_updated.text = ctime() 116 117 devs = SubElement(root, 'devices') 118 119 self._walk(self.root_device, devs) 120 121 tree = ElementTree(root) 122 tree.write(filename)
123 124 125 if __name__ == '__main__': 126 from wurfl import devices 127 Serialize(devices).to_xml('my.xml') 128