Ignore:
Timestamp:
04/10/06 20:34:16 (8 years ago)
Author:
xi
Message:

Implement yaml.dump().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pyyaml/trunk/lib/yaml/__init__.py

    r132 r133  
    11 
    22from error import * 
     3 
    34from reader import * 
    45from scanner import * 
     
    78from resolver import * 
    89from constructor import * 
     10 
    911from emitter import * 
     12from serializer import * 
     13from representer import * 
     14 
     15from detector import * 
    1016 
    1117from tokens import * 
    1218from events import * 
    1319from nodes import * 
     20 
     21from yaml_object import * 
    1422 
    1523def parse(data, Reader=Reader, Scanner=Scanner, Parser=Parser): 
     
    1927    return parser 
    2028 
    21 def load(data, Reader=Reader, Scanner=Scanner, Parser=Parser, 
     29def load_all(data, Reader=Reader, Scanner=Scanner, Parser=Parser, 
    2230        Composer=Composer, Resolver=Resolver, Constructor=Constructor): 
    2331    reader = Reader(data) 
     
    2937    return constructor 
    3038 
    31 def load_document(*args, **kwds): 
    32     for document in load(*args, **kwds): 
     39def safe_load_all(data, Reader=Reader, Scanner=Scanner, Parser=Parser, 
     40        Composer=Composer, Resolver=Resolver, Constructor=SafeConstructor): 
     41    return load_all(data, Reader, Scanner, Parser, Composer, Resolver, 
     42            Constructor) 
     43 
     44def load(data, *args, **kwds): 
     45    for document in load_all(data, *args, **kwds): 
    3346        return document 
    3447 
     48def safe_load(data, *args, **kwds): 
     49    for document in safe_load_all(data, *args, **kwds): 
     50        return document 
     51 
     52def emit(events, writer=None, Emitter=Emitter): 
     53    if writer is None: 
     54        try: 
     55            from cStringIO import StringIO 
     56        except ImportError: 
     57            from StringIO import StringIO 
     58        writer = StringIO() 
     59        return_value = True 
     60    else: 
     61        return_value = False 
     62    emitter = Emitter(writer) 
     63    for event in events: 
     64        emitter.emit(event) 
     65    if return_value: 
     66        return writer.getvalue() 
     67 
     68def dump_all(natives, writer=None, Emitter=Emitter, 
     69        Serializer=Serializer, Representer=Representer, 
     70        encoding=None, line_break=None, canonical=None, 
     71        indent=None, width=None, allow_unicode=None): 
     72    if writer is None: 
     73        try: 
     74            from cStringIO import StringIO 
     75        except ImportError: 
     76            from StringIO import StringIO 
     77        writer = StringIO() 
     78        return_value = True 
     79    else: 
     80        return_value = False 
     81    emitter = Emitter(writer) 
     82    serializer = Serializer(emitter, encoding=encoding, line_break=line_break, 
     83            canonical=canonical, indent=indent, width=width, 
     84            allow_unicode=allow_unicode) 
     85    representer = Representer(serializer) 
     86    for native in natives: 
     87        representer.represent(native) 
     88    representer.close() 
     89    if return_value: 
     90        return writer.getvalue() 
     91 
     92def safe_dump_all(natives, writer=None, Emitter=Emitter, 
     93        Serializer=Serializer, Representer=SafeRepresenter, 
     94        encoding=None, line_break=None, canonical=None, 
     95        indent=None, width=None, allow_unicode=None): 
     96    return dump_all(natives, writer, Emitter, Serializer, Representer, 
     97            encoding, line_break, canonical, indent, width, allow_unicode) 
     98 
     99def dump(native, *args, **kwds): 
     100    return dump_all([native], *args, **kwds) 
     101 
     102def safe_dump(native, *args, **kwds): 
     103    return safe_dump_all([native], *args, **kwds) 
     104 
Note: See TracChangeset for help on using the changeset viewer.