Ignore:
Timestamp:
08/25/05 01:30:06 (9 years ago)
Author:
xi
Message:

Adding some docstrings.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/syck/dumpers.py

    r24 r25  
     1""" 
     2syck.dumpers is a high-level wrapper for the Syck YAML emitter. 
     3Do not use it directly, use the module 'syck' instead. 
     4""" 
    15 
    26import _syck 
     
    1014    'emit', 'dump', 'emit_documents', 'dump_documents'] 
    1115 
    12 INF = 1e300000 
    13 NEGINF = -INF 
    14 NAN = INF/INF 
    15  
    16  
    1716class GenericDumper(_syck.Emitter): 
     17    """ 
     18    GenericDumper dumps native Python objects into YAML documents. 
     19    """ 
    1820 
    1921    def dump(self, object): 
     22        """Dumps the given Python object as a YAML document.""" 
    2023        self.emit(self._convert(object, {})) 
    2124 
     
    3033                node.value[index] = self._convert(item, object_to_node) 
    3134        elif node.kind == 'map': 
    32             for key in node.value.keys(): 
    33                 value = node.value[key] 
    34                 del node.value[key] 
    35                 node.value[self._convert(key, object_to_node)] =    \ 
    36                         self._convert(value, object_to_node) 
     35            if isinstance(node.value, dict): 
     36                for key in node.value.keys(): 
     37                    value = node.value[key] 
     38                    del node.value[key] 
     39                    node.value[self._convert(key, object_to_node)] =    \ 
     40                            self._convert(value, object_to_node) 
     41            elif isinstance(node.value, list): 
     42                for index in range(len(node.value)): 
     43                    key, value = node.value[index] 
     44                    node.value[index] = (self._convert(key, object_to_node), 
     45                            self._convert(value, object_to_node)) 
    3746#        # Workaround against a Syck bug: 
    3847#        if node.kind == 'scalar' and node.style not in ['1quote', '2quote'] \ 
     
    4251 
    4352    def represent(self, object): 
     53        """Represents the given Python object as a 'Node'.""" 
    4454        if isinstance(object, dict): 
    4555            return _syck.Map(object.copy(), tag="tag:yaml.org,2002:map") 
     
    5060 
    5161    def allow_aliases(self, object): 
     62        """Checks whether the given object can be aliased.""" 
    5263        return True 
    5364 
    5465class Dumper(GenericDumper): 
    55  
    56     def __init__(self, *args, **kwds): 
    57         super(Dumper, self).__init__(*args, **kwds) 
     66    """ 
     67    Dumper dumps native Python objects into YAML documents. 
     68    """ 
     69 
     70    INF = 1e300000 
     71    inf_value = repr(INF) 
     72    neginf_value = repr(-INF) 
     73    nan_value = repr(INF/INF) 
    5874 
    5975    def find_representer(self, object): 
     76        """ 
     77        For the given object, find a method that can represent it as a 'Node' 
     78        object. 
     79 
     80        If the type of the object has the form 'package.module.type', 
     81        find_representer() returns the method 'represent_package_module_type'. 
     82        If this method does not exist, it checks the base types. 
     83        """ 
    6084        for object_type in type(object).__mro__: 
    6185            if object_type.__module__ == '__builtin__': 
     
    6892 
    6993    def represent(self, object): 
     94        """Represents the given Python object as a 'Node'.""" 
    7095        representer = self.find_representer(object) 
    7196        if representer: 
     
    97122    def represent_float(self, object): 
    98123        value = repr(object) 
    99         if value == repr(INF): 
     124        if value == self.inf_value: 
    100125            value = '.inf' 
    101         elif value == repr(NEGINF): 
     126        elif value == self.neginf_value: 
    102127            value = '-.inf' 
    103         elif value == repr(NAN): 
     128        elif value == self.nan_value: 
    104129            value = '.nan' 
    105130        return _syck.Scalar(value, tag="tag:yaml.org,2002:float") 
     
    107132    def represent_sets_Set(self, object): 
    108133        return _syck.Seq(list(object), tag="tag:yaml.org,2002:set") 
     134    represent_set = represent_sets_Set 
    109135 
    110136    def represent_datetime_datetime(self, object): 
     
    121147 
    122148    def represent_type(self, object): 
    123         # TODO: Python 2.2 does not provide the module name of a function 
    124149        name = '%s.%s' % (object.__module__, object.__name__) 
    125150        return _syck.Scalar('', tag="tag:python.yaml.org,2002:name:"+name) 
    126151    represent_classobj = represent_type 
    127152    represent_class = represent_type 
     153    # TODO: Python 2.2 does not provide the module name of a function 
    128154    represent_function = represent_type 
    129155    represent_builtin_function_or_method = represent_type 
     
    200226                    tag="tag:python.yaml.org,2002:apply:"+class_name) 
    201227 
     228    def represent__syck_Node(self, object): 
     229        object_type = type(object) 
     230        type_name = '%s.%s' % (object_type.__module__, object_type.__name__) 
     231        state = [] 
     232        if hasattr(object_type, '__slotnames__'): 
     233            for name in object_type.__slotnames__: 
     234                value = getattr(object, name) 
     235                if value: 
     236                    state.append((name, value)) 
     237        return _syck.Map(state, 
     238                tag="tag:python.yaml.org,2002:object:"+type_name) 
     239 
    202240    def allow_aliases(self, object): 
     241        """Checks whether the given object can be aliased.""" 
    203242        if object is None or type(object) in [int, bool, float]: 
    204243            return False 
     
    210249 
    211250def emit(node, output=None, Dumper=Dumper, **parameters): 
     251    """ 
     252    Emits the given node to the output. 
     253 
     254    If output is None, returns the produced YAML document. 
     255    """ 
    212256    if output is None: 
    213257        dumper = Dumper(StringIO.StringIO(), **parameters) 
     
    219263 
    220264def dump(object, output=None, Dumper=Dumper, **parameters): 
     265    """ 
     266    Dumps the given object to the output. 
     267 
     268    If output is None, returns the produced YAML document. 
     269    """ 
    221270    if output is None: 
    222271        dumper = Dumper(StringIO.StringIO(), **parameters) 
     
    228277 
    229278def emit_documents(nodes, output=None, Dumper=Dumper, **parameters): 
     279    """ 
     280    Emits the list of nodes to the output. 
     281     
     282    If output is None, returns the produced YAML document. 
     283    """ 
    230284    if output is None: 
    231285        dumper = Dumper(StringIO.StringIO(), **parameters) 
     
    238292 
    239293def dump_documents(objects, output=None, Dumper=Dumper, **parameters): 
     294    """ 
     295    Dumps the list of objects to the output. 
     296     
     297    If output is None, returns the produced YAML document. 
     298    """ 
    240299    if output is None: 
    241300        dumper = Dumper(StringIO.StringIO(), **parameters) 
Note: See TracChangeset for help on using the changeset viewer.