Module wurfl2python
[hide private]
[frames] | no frames]

Source Code for Module wurfl2python

  1  #!/usr/bin/env python 
  2  # WURFL 2 Python - Wireless Universal Resource File to Python conversion utility 
  3  # Copyright (C) 2004-2008 Armand Lynch 
  4  # 
  5  # This library is free software; you can redistribute it and/or modify it 
  6  # under the terms of the GNU Lesser General Public License as published by the 
  7  # Free Software Foundation; either version 2.1 of the License, or (at your 
  8  # option) any later version. 
  9  # 
 10  # This library is distributed in the hope that it will be useful, but WITHOUT 
 11  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 12  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
 13  # details. 
 14  # 
 15  # You should have received a copy of the GNU Lesser General Public License 
 16  # along with this library; if not, write to the Free Software Foundation, Inc., 
 17  # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 18  # 
 19  # Armand Lynch <lyncha@users.sourceforge.net> 
 20   
 21  __doc__ = \ 
 22  """ 
 23  WURFL <wurfl.sourceforge.net> conversion utility 
 24   
 25  usage: wurfl2python.py [options] WURFL_XML_FILE 
 26  """ 
 27   
 28  import sys 
 29  from time import ctime 
 30   
 31  from pywurfl.wurflprocessor import DeviceHandler, WurflProcessor, op, main 
 32   
 33   
 34  __version__ = "4.3.0a" 
 35  __license__ = "LGPL" 
 36  __copyright__ = "Copyright 2004-2008, Armand Lynch" 
 37  __author__ = "Armand Lynch <lyncha@users.sourceforge.net>" 
 38  __contributors__ = "Pau Aliagas <pau@newtral.org>" 
 39  __url__ = "http://celljam.net/" 
 40  __all__ = ['DeviceSerializer'] 
 41   
 42   
43 -class DeviceSerializer(DeviceHandler):
44 45 idstr = "devices.devids['''%s'''] = " 46 new_caps = idstr + "devclass(%s, '''%s''', '''%s''', %s, {%s})\n" 47 no_caps = idstr + "devclass(%s, '''%s''', '''%s''', %s, None)\n" 48 capability_types = {} 49
50 - def __str__(self):
51 s = [] 52 c = [] 53 54 if self.parent == "root": 55 parent = "None" 56 else: 57 parent = "devices.devids['''%s''']" % self.parent 58 59 for cap in sorted(self.capabilities): 60 value = self.capabilities[cap] 61 cap_type = self.capability_types.get(cap, None) 62 if cap_type == int: 63 c.append("'''%s''':%d" % (cap, int(value.strip()))) 64 elif cap_type == float: 65 c.append("'''%s''':%f" % (cap, float(value.strip()))) 66 elif cap_type == bool: 67 if value.lower() == "true": 68 c.append("'''%s''':True" % cap) 69 elif value.lower() == "false": 70 c.append("'''%s''':False" % cap) 71 else: 72 c.append("'''%s''':'''%s'''" % (cap, value)) 73 74 if c: 75 s.append(self.new_caps % (self.devid, parent, self.devid, 76 self.devua, self.actual_device_root, 77 ','.join(c))) 78 else: 79 s.append(self.no_caps % (self.devid, parent, self.devid, self.devua, 80 self.actual_device_root)) 81 82 return ''.join(s)
83 84
85 -class WurflPythonWriter(WurflProcessor):
86
87 - def __init__(self, wurflxml, device_handler=None, 88 options={"outfile":"wurfl.py"}):
89 WurflProcessor.__init__(self, wurflxml, device_handler=device_handler, 90 options=options) 91 self.groups = {}
92
93 - def handle_device(self, devobj):
94 self.outfile.write(str(devobj))
95
96 - def process_options(self):
97 if self.logfile != sys.stderr: 98 self.logfile = file(self.logfile, "wb") 99 if self.outfile is not None: 100 self.outfile = file(self.outfile, "wb") 101 else: 102 self.outfile = file("wurfl.py", "wb")
103
104 - def process_capability(self, devobj, group, capability):
105 attrs = capability.attrib 106 devobj.capabilities[attrs["name"]] = attrs["value"] 107 if devobj.devid == 'generic': 108 try: 109 self.groups[group.get('id')].append(attrs["name"]) 110 except KeyError: 111 self.groups[group.get('id')] = [] 112 self.groups[group.get('id')].append(attrs["name"])
113
114 - def start_process(self):
115 self.device_handler.capability_types = get_normalized_types(self.tree) 116 117 version = self.tree.findtext("*/ver").strip() 118 self.outfile.write('"""\n%s\n\n' % self.outfile.name) 119 self.outfile.write("Generated on: %s\n" % ctime()) 120 self.outfile.write("Generated by: wurfl2python.py v%s\n" % __version__) 121 self.outfile.write('"""\n\n') 122 self.outfile.write("__all__ = ['devices', 'wurfl_version']\n") 123 self.outfile.write("wurfl_version = '''%s'''\n\n" % version) 124 self.outfile.write("from pywurfl import *\n\n") 125 self.outfile.write("devices = Devices()\n")
126
127 - def end_process(self):
128 for group, names in self.groups.iteritems(): 129 self.outfile.write("devices.devids['''generic'''].groups") 130 self.outfile.write("['''%s'''] = ['''%s''']\n" % (group, 131 "''','''".join(names))) 132 self.outfile.write("\n") 133 self.outfile.write("for device in devices.devids.itervalues():\n") 134 self.outfile.write(" devices.devuas[device.devua] = device\n") 135 self.outfile.write("del device\n") 136 self.outfile.write("\n")
137 138
139 -def get_normalized_types(tree):
140 caps = {} 141 for capability in tree.findall("devices/device/group/capability"): 142 name = capability.attrib['name'] 143 value = capability.attrib['value'] 144 if name not in caps: 145 try: 146 int(value) 147 caps[name] = int 148 continue 149 except (TypeError, ValueError): 150 pass 151 try: 152 float(value) 153 caps[name] = float 154 continue 155 except (TypeError, ValueError): 156 pass 157 158 if value.strip().lower() in ("true", "false"): 159 caps[name] = bool 160 continue 161 else: 162 caps[name] = str 163 else: 164 if caps[name] == str: 165 continue 166 elif caps[name] == bool: 167 if value.strip().lower() in ("true", "false"): 168 continue 169 else: 170 caps[name] = str 171 elif caps[name] == float: 172 try: 173 float(value) 174 continue 175 except (TypeError, ValueError): 176 caps[name] = str 177 elif caps[name] == int: 178 try: 179 int(value) 180 continue 181 except (TypeError, ValueError): 182 caps[name] = str 183 return caps
184 185 186 if __name__ == "__main__": 187 op.add_option("-o", "--output", dest="outfile", default=None, 188 help="The name of the module to produce.") 189 main(WurflPythonWriter, DeviceSerializer, op) 190