pywurfl.serialize

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