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