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) 2006 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-6, 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
49 - def __str__(self):
50 s = [] 51 c = [] 52 53 if self.parent == "root": 54 parent = "None" 55 else: 56 parent = "devices.devids['''%s''']" % self.parent 57 58 for cap in sorted(self.capabilities): 59 value = self.capabilities[cap] 60 try: 61 c.append("'''%s''':%d" % (cap, int(value))) 62 except ValueError: 63 if value.lower() == "true": 64 c.append("'''%s''':True" % cap) 65 elif value.lower() == "false": 66 c.append("'''%s''':False" % cap) 67 else: 68 c.append("'''%s''':'''%s'''" % (cap, value)) 69 70 if c: 71 s.append(DeviceSerializer.new_caps % (self.devid, parent, 72 self.devid, self.devua, 73 self.actual_device_root, 74 ','.join(c))) 75 else: 76 s.append(DeviceSerializer.no_caps % (self.devid, parent, 77 self.devid, self.devua, 78 self.actual_device_root)) 79 80 return ''.join(s)
81 82
83 -class WurflPythonWriter(WurflProcessor):
84
85 - def __init__(self, wurflxml, device_handler=None, 86 options={"outfile":"wurfl.py"}):
87 WurflProcessor.__init__(self, wurflxml, device_handler=device_handler, 88 options=options) 89 self.groups = {}
90
91 - def handle_device(self, devobj):
92 self.outfile.write(str(devobj))
93
94 - def process_options(self):
95 if self.logfile != sys.stderr: 96 self.logfile = file(self.logfile, "wb") 97 if self.outfile is not None: 98 self.outfile = file(self.outfile, "wb") 99 else: 100 self.outfile = file("wurfl.py", "wb")
101
102 - def process_capability(self, devobj, group, capability):
103 attrs = capability.attrib 104 devobj.capabilities[attrs["name"]] = attrs["value"] 105 if devobj.devid == 'generic': 106 try: 107 self.groups[group.get('id')].append(attrs["name"]) 108 except KeyError: 109 self.groups[group.get('id')] = [] 110 self.groups[group.get('id')].append(attrs["name"])
111
112 - def start_process(self):
113 version = self.tree.findtext("*/ver").strip() 114 self.outfile.write('"""\n%s\n\n' % self.outfile.name) 115 self.outfile.write("Generated on: %s\n" % ctime()) 116 self.outfile.write("Generated by: wurfl2python.py v%s\n" % __version__) 117 self.outfile.write('"""\n\n') 118 self.outfile.write("__all__ = ['devices', 'wurfl_version']\n") 119 self.outfile.write("wurfl_version = '''%s'''\n\n" % version) 120 self.outfile.write("from pywurfl import *\n\n") 121 self.outfile.write("devices = Devices()\n")
122
123 - def end_process(self):
124 for group, names in self.groups.iteritems(): 125 self.outfile.write("devices.devids['''generic'''].groups") 126 self.outfile.write("['''%s'''] = ['''%s''']\n" % (group, 127 "''','''".join(names))) 128 self.outfile.write("\n") 129 self.outfile.write("for device in devices.devids.itervalues():\n") 130 self.outfile.write(" devices.devuas[device.devua] = device\n") 131 self.outfile.write("del device\n") 132 self.outfile.write("\n") 133 self.outfile.write("devices._normalize_types()\n")
134 135 136 if __name__ == "__main__": 137 op.add_option("-o", "--output", dest="outfile", default=None, 138 help="The name of the module to produce.") 139 main(WurflPythonWriter, DeviceSerializer, op) 140