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