Atlas 0.7.0
Networking protocol for the Worldforge system.
codecs/__init__.py
1#codec factory, list of codecs, etc..
2
3#Copyright (C) 2000,2001 by Aloril
4#Copyright (C) 2002 by AIR-IX SUUNNITTELU/Ahiplan Oy
5
6#This library is free software; you can redistribute it and/or
7#modify it under the terms of the GNU Lesser General Public
8#License as published by the Free Software Foundation; either
9#version 2.1 of the License, or (at your option) any later version.
10
11#This library is distributed in the hope that it will be useful,
12#but WITHOUT ANY WARRANTY; without even the implied warranty of
13#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14#Lesser General Public License for more details.
15
16#You should have received a copy of the GNU Lesser General Public
17#License along with this library; if not, write to the Free Software
18#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20
21#ids: list of codec ids
22#factory_dict: dictionary of codec factories
23#factory_list: list of codec factories
24#example usage:
25#to get codec:
26#codec = codec_dict["XML"]()
27#to encode object:
28#output_string = codec.encode(obj)
29#to decode string (returns list of decodec objects)
30#obj_list = codec.decode(some_string)
31
32from . import xml
33#from . import xml2
34#import packed
35# from . import binary1
36# from . import binary2
37#import bach
38
39class Codec:
40 def __init__(self, id, decoder, encoder):
41 self.id = id
42 self.decoder = decoder
43 self.encoder = encoder
44 def encode(self, obj):
45 return self.encoder(obj)
46 def decode(self, str):
47 return self.decoder(str)
48 def close(self):
49 return self.encoder.close()
50 def set_stream_mode(self, mode=1):
51 self.decoder.set_stream_mode(mode)
52 self.encoder.set_stream_mode(mode)
53
55 def __init__(self, id, decoder_factory, encoder_factory):
56 self.id = id
57 self.decoder_factory = decoder_factory
58 self.encoder_factory = encoder_factory
59 def __call__(self):
60 return Codec(*(self.id, self.decoder_factory(),
61 self.encoder_factory()))
62
63factory_list = [
64 # CodecFactory("Bach_beta2",
65 # bach.get_decoder,
66 # bach.get_encoder),
67 CodecFactory("XML",
68 xml.get_encoder,
69 xml.get_encoder),
70 # CodecFactory("XML2_test",
71 # xml2.get_parser,
72 # lambda :xml2.gen_xml2),
73 # CodecFactory("Packed",
74 # packed.get_parser,
75 # packed.get_encoder),
76 # CodecFactory("Binary1_beta",
77 # binary1.get_parser,
78 # lambda :binary1.gen_binary1),
79 # CodecFactory("Binary2_test",
80 # binary2.get_parser,
81 # lambda :binary2.gen_binary2)
82 ]
83
84factory_dict = {}
85ids = []
86for factory in factory_list:
87 factory_dict[factory.id] = factory
88 ids.append(factory.id)
89 def _get_codec(factory=factory):
90 return factory()
91 globals()["get_" + factory.id] = _get_codec
92
93del _get_codec
94
95def get_codec(name):
96 return factory_dict[name]()
97
98def get_default_codec():
99 return factory_list[0]()