home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / syck / loaders.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  13.8 KB  |  485 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import generators
  5.  
  6. try:
  7.     import datetime
  8. except ImportError:
  9.     pass
  10.  
  11.  
  12. try:
  13.     Set = set
  14. except:
  15.     
  16.     try:
  17.         from sets import Set
  18.     except ImportError:
  19.         
  20.         def Set(items):
  21.             set = { }
  22.             for items in items:
  23.                 set[items] = None
  24.             
  25.             return set
  26.  
  27.  
  28.  
  29. import _syck
  30. import sys
  31. import re
  32. import warnings
  33. __all__ = [
  34.     'GenericLoader',
  35.     'Loader',
  36.     'parse',
  37.     'load',
  38.     'parse_documents',
  39.     'load_documents',
  40.     'NotUnicodeInputWarning']
  41.  
  42. class NotUnicodeInputWarning(UserWarning):
  43.     pass
  44.  
  45.  
  46. class GenericLoader(_syck.Parser):
  47.     
  48.     def load(self):
  49.         node = self.parse()
  50.         if self.eof:
  51.             return None
  52.         return self._convert(node, { })
  53.  
  54.     
  55.     def _convert(self, node, node_to_object):
  56.         if node in node_to_object:
  57.             return node_to_object[node]
  58.         value = None
  59.         if node.kind == 'scalar':
  60.             value = node.value
  61.         elif node.kind == 'seq':
  62.             value = []
  63.             for item_node in node.value:
  64.                 value.append(self._convert(item_node, node_to_object))
  65.             
  66.         elif node.kind == 'map':
  67.             value = { }
  68.             for key_node in node.value:
  69.                 key_object = self._convert(key_node, node_to_object)
  70.                 value_object = self._convert(node.value[key_node], node_to_object)
  71.                 
  72.                 try:
  73.                     if key_object in value:
  74.                         value = None
  75.                         break
  76.                     
  77.                     value[key_object] = value_object
  78.                 continue
  79.                 except TypeError:
  80.                     value = None
  81.                     break
  82.                     continue
  83.                 
  84.  
  85.             
  86.             if value is None:
  87.                 value = []
  88.                 for key_node in node.value:
  89.                     key_object = self._convert(key_node, node_to_object)
  90.                     value_object = self._convert(node.value[key_node], node_to_object)
  91.                 
  92.                 value.append((key_object, value_object))
  93.             
  94.         
  95.         node.value = value
  96.         object = self.construct(node)
  97.         node_to_object[node] = object
  98.         return object
  99.  
  100.     
  101.     def construct(self, node):
  102.         return node.value
  103.  
  104.  
  105.  
  106. class Merge:
  107.     pass
  108.  
  109.  
  110. class Default:
  111.     pass
  112.  
  113.  
  114. class Loader(GenericLoader):
  115.     inf_value = float('inf')
  116.     nan_value = inf_value / inf_value
  117.     timestamp_expr = re.compile('(?P<year>\\d\\d\\d\\d)-(?P<month>\\d\\d)-(?P<day>\\d\\d)(?:(?:[Tt]|[ \\t]+)(?P<hour>\\d\\d):(?P<minute>\\d\\d):(?P<second>\\d\\d)(?:\\.(?P<micro>\\d+)?)?[ \\t]*(?:Z|(?P<zhour>[+-]\\d\\d)(?::(?P<zminute>\\d\\d))?)?)?')
  118.     merge_key = Merge()
  119.     default_key = Default()
  120.     non_ascii = []
  121.     for i in range(256):
  122.         ch = chr(i)
  123.         if ch.isalnum():
  124.             non_ascii.append(ch)
  125.             continue
  126.         non_ascii.append('_')
  127.     
  128.     non_ascii = ''.join(non_ascii)
  129.     python_bools = {
  130.         'True': True,
  131.         'False': False }
  132.     
  133.     class python_class:
  134.         pass
  135.  
  136.     
  137.     def find_constructor(self, node):
  138.         parts = []
  139.         if node.tag:
  140.             parts = node.tag.split(':')
  141.         
  142.         if parts:
  143.             if parts[0] == 'tag':
  144.                 parts.pop(0)
  145.                 if parts:
  146.                     if parts[0] == 'yaml.org,2002':
  147.                         parts.pop(0)
  148.                     elif parts[0] == 'python.yaml.org,2002':
  149.                         parts[0] = 'python'
  150.                     
  151.                 
  152.             elif parts[0] == 'x-private':
  153.                 parts[0] = 'private'
  154.             
  155.         
  156.         parts = [ part.translate(self.non_ascii) for part in parts ]
  157.         while parts:
  158.             method = 'construct_' + '_'.join(parts)
  159.             if hasattr(self, method):
  160.                 return getattr(self, method)
  161.             parts.pop()
  162.             continue
  163.             hasattr(self, method)
  164.  
  165.     
  166.     def construct(self, node):
  167.         if node.kind == 'map' and self.merge_key in node.value:
  168.             self.merge_maps(node)
  169.         
  170.         constructor = self.find_constructor(node)
  171.         if constructor:
  172.             return constructor(node)
  173.         return node.value
  174.  
  175.     
  176.     def construct_null(self, node):
  177.         pass
  178.  
  179.     
  180.     def construct_bool_yes(self, node):
  181.         return True
  182.  
  183.     
  184.     def construct_bool_no(self, node):
  185.         return False
  186.  
  187.     
  188.     def construct_str(self, node):
  189.         
  190.         try:
  191.             value = unicode(node.value, 'utf-8')
  192.         except UnicodeDecodeError:
  193.             warnings.warn('scalar value is not utf-8', NotUnicodeInputWarning)
  194.             return node.value
  195.  
  196.         
  197.         try:
  198.             return value.encode('ascii')
  199.         except UnicodeEncodeError:
  200.             return value
  201.  
  202.  
  203.     
  204.     def construct_numeric_base60(self, num_type, node):
  205.         digits = [ num_type(part) for part in node.value.split(':') ]
  206.         digits.reverse()
  207.         base = 1
  208.         value = num_type(0)
  209.         for digit in digits:
  210.             value += digit * base
  211.             base *= 60
  212.         
  213.         return value
  214.  
  215.     
  216.     def construct_int(self, node):
  217.         return int(node.value)
  218.  
  219.     
  220.     def construct_int_hex(self, node):
  221.         return int(node.value, 16)
  222.  
  223.     
  224.     def construct_int_oct(self, node):
  225.         return int(node.value, 8)
  226.  
  227.     
  228.     def construct_int_base60(self, node):
  229.         return self.construct_numeric_base60(int, node)
  230.  
  231.     
  232.     def construct_float(self, node):
  233.         return float(node.value)
  234.  
  235.     construct_float_fix = construct_float
  236.     construct_float_exp = construct_float
  237.     
  238.     def construct_float_base60(self, node):
  239.         return self.construct_numeric_base60(float, node)
  240.  
  241.     
  242.     def construct_float_inf(self, node):
  243.         return self.inf_value
  244.  
  245.     
  246.     def construct_float_neginf(self, node):
  247.         return -(self.inf_value)
  248.  
  249.     
  250.     def construct_float_nan(self, node):
  251.         return self.nan_value
  252.  
  253.     
  254.     def construct_binary(self, node):
  255.         return node.value.decode('base64')
  256.  
  257.     
  258.     def construct_timestamp(self, node):
  259.         match = self.timestamp_expr.match(node.value)
  260.         values = match.groupdict()
  261.         for key in values:
  262.             if values[key]:
  263.                 values[key] = int(values[key])
  264.                 continue
  265.             values[key] = 0
  266.         
  267.         micro = values['micro']
  268.         if micro:
  269.             while 10 * micro < 1000000:
  270.                 micro *= 10
  271.         
  272.         stamp = datetime.datetime(values['year'], values['month'], values['day'], values['hour'], values['minute'], values['second'], micro)
  273.         diff = datetime.timedelta(hours = values['zhour'], minutes = values['zminute'])
  274.         return stamp - diff
  275.  
  276.     construct_timestamp_ymd = construct_timestamp
  277.     construct_timestamp_iso8601 = construct_timestamp
  278.     construct_timestamp_spaced = construct_timestamp
  279.     
  280.     def construct_merge(self, node):
  281.         return self.merge_key
  282.  
  283.     
  284.     def construct_default(self, node):
  285.         return self.default_key
  286.  
  287.     
  288.     def merge_maps(self, node):
  289.         maps = node.value[self.merge_key]
  290.         del node.value[self.merge_key]
  291.         if not isinstance(maps, list):
  292.             maps = [
  293.                 maps]
  294.         
  295.         maps.reverse()
  296.         maps.append(node.value.copy())
  297.         for item in maps:
  298.             node.value.update(item)
  299.         
  300.  
  301.     
  302.     def construct_omap(self, node):
  303.         omap = []
  304.         for mapping in node.value:
  305.             for key in mapping:
  306.                 omap.append((key, mapping[key]))
  307.             
  308.         
  309.         return omap
  310.  
  311.     
  312.     def construct_pairs(self, node):
  313.         pairs = []
  314.         for mapping in node.value:
  315.             for key in mapping:
  316.                 pairs.append((key, mapping[key]))
  317.             
  318.         
  319.         return pairs
  320.  
  321.     
  322.     def construct_set(self, node):
  323.         return Set(node.value)
  324.  
  325.     
  326.     def construct_python_none(self, node):
  327.         pass
  328.  
  329.     
  330.     def construct_python_bool(self, node):
  331.         return self.python_bools[node.value]
  332.  
  333.     
  334.     def construct_python_int(self, node):
  335.         return int(node.value)
  336.  
  337.     
  338.     def construct_python_long(self, node):
  339.         return long(node.value)
  340.  
  341.     
  342.     def construct_python_float(self, node):
  343.         return float(node.value)
  344.  
  345.     
  346.     def construct_python_complex(self, node):
  347.         return complex(node.value)
  348.  
  349.     
  350.     def construct_python_str(self, node):
  351.         return str(node.value)
  352.  
  353.     
  354.     def construct_python_unicode(self, node):
  355.         return unicode(node.value, 'utf-8')
  356.  
  357.     
  358.     def construct_python_list(self, node):
  359.         return node.value
  360.  
  361.     
  362.     def construct_python_tuple(self, node):
  363.         return tuple(node.value)
  364.  
  365.     
  366.     def construct_python_dict(self, node):
  367.         return node.value
  368.  
  369.     
  370.     def find_python_object(self, node):
  371.         full_name = node.tag.split(':')[3]
  372.         parts = full_name.split('.')
  373.         object_name = parts.pop()
  374.         module_name = '.'.join(parts)
  375.         if not module_name:
  376.             module_name = '__builtin__'
  377.         else:
  378.             __import__(module_name)
  379.         return getattr(sys.modules[module_name], object_name)
  380.  
  381.     
  382.     def find_python_state(self, node):
  383.         if node.kind == 'seq':
  384.             args = node.value
  385.             kwds = { }
  386.             state = { }
  387.         else:
  388.             args = node.value.get('args', [])
  389.             kwds = node.value.get('kwds', { })
  390.             state = node.value.get('state', { })
  391.         return (args, kwds, state)
  392.  
  393.     
  394.     def set_python_state(self, object, state):
  395.         if hasattr(object, '__setstate__'):
  396.             object.__setstate__(state)
  397.         else:
  398.             slotstate = { }
  399.             if isinstance(state, tuple) and len(state) == 2:
  400.                 (state, slotstate) = state
  401.             
  402.             if hasattr(object, '__dict__'):
  403.                 object.__dict__.update(state)
  404.             elif state:
  405.                 slotstate.update(state)
  406.             
  407.             for key, value in slotstate.items():
  408.                 setattr(object, key, value)
  409.             
  410.  
  411.     
  412.     def construct_python_name(self, node):
  413.         return self.find_python_object(node)
  414.  
  415.     
  416.     def construct_python_module(self, node):
  417.         module_name = node.tag.split(':')[3]
  418.         __import__(module_name)
  419.         return sys.modules[module_name]
  420.  
  421.     
  422.     def construct_python_object(self, node):
  423.         cls = self.find_python_object(node)
  424.         if type(cls) is type(self.python_class):
  425.             if hasattr(cls, '__getnewargs__'):
  426.                 object = cls()
  427.             else:
  428.                 object = self.python_class()
  429.                 object.__class__ = cls
  430.         else:
  431.             object = cls.__new__(cls)
  432.         self.set_python_state(object, node.value)
  433.         return object
  434.  
  435.     
  436.     def construct_python_new(self, node):
  437.         cls = self.find_python_object(node)
  438.         (args, kwds, state) = self.find_python_state(node)
  439.         if type(cls) is type(self.python_class):
  440.             object = cls(*args, **kwds)
  441.         else:
  442.             object = cls.__new__(cls, *args, **kwds)
  443.         self.set_python_state(object, state)
  444.         return object
  445.  
  446.     
  447.     def construct_python_apply(self, node):
  448.         constructor = self.find_python_object(node)
  449.         (args, kwds, state) = self.find_python_state(node)
  450.         object = constructor(*args, **kwds)
  451.         self.set_python_state(object, state)
  452.         return object
  453.  
  454.  
  455.  
  456. def parse(source, Loader = Loader, **parameters):
  457.     loader = Loader(source, **parameters)
  458.     return loader.parse()
  459.  
  460.  
  461. def load(source, Loader = Loader, **parameters):
  462.     loader = Loader(source, **parameters)
  463.     return loader.load()
  464.  
  465.  
  466. def parse_documents(source, Loader = Loader, **parameters):
  467.     loader = Loader(source, **parameters)
  468.     while True:
  469.         node = loader.parse()
  470.         if loader.eof:
  471.             break
  472.         
  473.         yield node
  474.  
  475.  
  476. def load_documents(source, Loader = Loader, **parameters):
  477.     loader = Loader(source, **parameters)
  478.     while True:
  479.         object = loader.load()
  480.         if loader.eof:
  481.             break
  482.         
  483.         yield object
  484.  
  485.