Atlas  0.7.0
Networking protocol for the Worldforge system.
gen_bach.py
1 #generate bach from objects
2 
3 #Copyright 2000, 2001 by Aloril
4 #Copyright 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 import string
22 from atlas.typemap import *
23 
24 #see bach.py for encoder
25 
26 def gen_bach(obj):
27  return encode(obj)+"\n"
28 
29 def encode(value, indent=""):
30  return type_name2bach_code[get_atlas_type(value)](value, indent)
31 
32 int_characters = "+-" + string.digits
33 float_characters = ".eE" + int_characters
34 plain_name_characters = string.digits + string.ascii_uppercase + string.ascii_lowercase + "_"
35 string_quoted = '"\\'
36 def encode_name(value):
37  res = []
38  plain_flag = 1
39  if isinstance(value, str):
40  value = str(value)
41  for ch in value:
42  if ch in string_quoted:
43  res.append("\\" + ch)
44  else:
45  res.append(ch)
46  if not ch in plain_name_characters:
47  plain_flag = 0
48  if plain_flag:
49  return "".join(res)
50  else:
51  return '"%s"' % "".join(res)
52 
53 def encode_string(value, indent):
54  res = []
55  for ch in value:
56  if ch in '"\\':
57  res.append("\\" + ch)
58  else:
59  res.append(ch)
60  return '"%s"' % "".join(res)
61 
62 
70 
71 def encode_map(obj, indent=""):
72  if not obj: return "{}"
73  str_list = []
74  indent = indent + "\t"
75  for name, value in list(obj.items()):
76  if value is not None:
77  str_type = get_atlas_type(value)
78  add_nl = 0
79  str_name = encode_name(name)
80  str_value = encode(value, indent)
81  if str_type=="map":
82  if value: add_nl = 1
83  elif str_type=="list":
84  if str_value.find("\t")>=0: add_nl = 1
85  #if add_nl: str_value = "\n<a>%s<b>\n<c>%s<d>" % (str_value, indent)
86  str_list.append("%s%s: %s" % (indent, str_name, str_value))
87  return "{\n%s\n%s}" % (",\n".join(str_list), indent[:-1])
88 
89 def encode_list(lst, indent=""):
90  str_list = []
91  indent = indent + "\t"
92  complex_in_list = 0
93  for item in lst:
94  str_type = get_atlas_type(item)
95  str_value = encode(item, indent)
96  str_list.append(str_value)
97  if str_type in ["map", "list"]:
98  complex_in_list = 1
99  if complex_in_list:
100  str_list = list(map(lambda s,i=indent:"\n"+i+s, str_list))
101  res = ",".join(str_list) + "\n" + indent[:-1]
102  else:
103  res = ", ".join(str_list)
104  return "[%s]" % res
105 
106 def encode_int(value, indent=""):
107  return str(value)
108 
109 def encode_float(value, indent=""):
110  return str(value)
111 
112 type_name2bach_code = {"map": encode_map,
113  "list": encode_list,
114  "int": encode_int,
115  "float": encode_float,
116  "string": encode_string}
atlas.typemap
Definition: typemap.py:1