Atlas 0.7.0
Networking protocol for the Worldforge system.
file.py
1#read/write atlas files
2
3#Copyright (C) 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
21import atlas
22from atlas import codecs
23from atlas.transport.negotiation import NegotiationServer, NegotiationClient
24from atlas.transport.bridge import Bridge
25
26buf_size = 16384
27
28class File:
29 """client for reading/writing files: currently only read support"""
30 def __init__(self, name, mode="r", codec_id=None):
31 self.name = name
32 self.fp = open(name, mode)
33 self.mode = mode
34 self.objects = atlas.Messages()
35 if mode=="r":
36 self.bridge = Bridge(NegotiationClient(), functions=self)
37 else:
38 if codec_id:
39 self.codec_id = codec_id
40 else:
41 self.codec_id = codecs.get_default_codec().id
42 self.bridge = Bridge(NegotiationServer(codecs=(self.codec_id,)), functions=self)
44
45 def send_string(self, str):
46 if self.mode=="w":
47 self.fp.write(str)
48 if self.other_negotiation.state:
49 self.other_negotiation(str)
50 self.bridge.process_string(self.other_negotiation.get_send_str())
51
52 def operation_received(self, op):
53 self.objects.append(op)
54
55 def read_object(self):
56 while 1:
57 if self.objects:
58 return self.objects.pop(0)
59 data = self.fp.read(buf_size)
60 if not data:
61 return
62 self.bridge.process_string(data)
63
64 def write_object(self, obj):
65 self.bridge.process_operation(obj)
66
67 def check_negotiation_ok(self):
68 if not self.bridge.codec:
69 raise IOError("no codec negotiated in file %s" % self.name)
70
71 def read_all_as_list(self):
72 objects = atlas.Messages()
73 while 1:
74 obj = self.read_object()
75 if not obj: break
76 objects.append(obj)
78 return objects
79
80 def read_all_as_dict(self):
81 objects = {}
82 while 1:
83 obj = self.read_object()
84 if not obj: break
85 objects[obj.id] = obj
87 return objects
88
89 def read_and_analyse_all(self):
90 objects = {}
91 while 1:
92 obj = self.read_object()
93 if not obj: break
94 objects[obj.id] = obj
97 return objects
98
99 def close(self):
100 self.bridge.close()
101 self.fp.close()
102
103def read_file(name):
104 f = File(name)
105 return f.read_all_as_list()
106
107def read_file_as_dict(name):
108 f = File(name)
109 return f.read_all_as_dict()
110
111def read_and_analyse(name):
112 f = File(name)
113 return f.read_and_analyse_all()
114
115def write_file(objects, name, encoding=None):
116 f = File(name, "w", encoding)
117 for obj in objects:
118 f.write_object(obj)
119 f.close()
120
121def translate(input, output, output_encoding=None):
122 objects = read_file(input)
123 write_file(objects, output, output_encoding)
def find_parents_children_objects(objects)
Definition: __init__.py:274
def check_negotiation_ok(self)
Definition: file.py:67
def read_object(self)
Definition: file.py:55