Additional style cleanup
[mudpy.git] / lib / mudpy / data.py
1 # -*- coding: utf-8 -*-
2 u"""Data interface functions for the mudpy engine."""
3
4 # Copyright (c) 2004-2011 Jeremy Stanley <fungi@yuggoth.org>. Permission
5 # to use, copy, modify, and distribute this software is granted under
6 # terms provided in the LICENSE file distributed with this software.
7
8
9 class DataFile:
10
11     u"""A file containing universe elements."""
12
13     def __init__(self, filename, universe):
14         self.filename = filename
15         self.universe = universe
16         self.load()
17
18     def load(self):
19         u"""Read a file and create elements accordingly."""
20         import ConfigParser
21         import misc
22         import os
23         import os.path
24         self.data = ConfigParser.RawConfigParser()
25         self.modified = False
26         if os.access(self.filename, os.R_OK):
27             self.data.read(self.filename)
28         if not hasattr(self.universe, u"files"):
29             self.universe.files = {}
30         self.universe.files[self.filename] = self
31         includes = []
32         if self.data.has_option(u"__control__", u"include_files"):
33             for included in makelist(
34                 self.data.get(u"__control__", u"include_files")
35             ):
36                 included = find_file(
37                     included,
38                     relative=self.filename,
39                     universe=self.universe
40                 )
41                 if included not in includes:
42                     includes.append(included)
43         if self.data.has_option(u"__control__", u"include_dirs"):
44             for included in [
45                 os.path.join(x, u"__init__.mpy") for x in makelist(
46                     self.data.get(u"__control__", u"include_dirs")
47                 )
48             ]:
49                 included = find_file(
50                     included,
51                     relative=self.filename,
52                     universe=self.universe
53                 )
54                 if included not in includes:
55                     includes.append(included)
56         if self.data.has_option(u"__control__", u"default_files"):
57             origins = makedict(
58                 self.data.get(u"__control__", u"default_files")
59             )
60             for key in origins.keys():
61                 origins[key] = find_file(
62                     origins[key],
63                     relative=self.filename,
64                     universe=self.universe
65                 )
66                 if origins[key] not in includes:
67                     includes.append(origins[key])
68                 self.universe.default_origins[key] = origins[key]
69                 if key not in self.universe.categories:
70                     self.universe.categories[key] = {}
71         if self.data.has_option(u"__control__", u"private_files"):
72             for item in makelist(
73                 self.data.get(u"__control__", u"private_files")
74             ):
75                 item = find_file(
76                     item,
77                     relative=self.filename,
78                     universe=self.universe
79                 )
80                 if item not in includes:
81                     includes.append(item)
82                 if item not in self.universe.private_files:
83                     self.universe.private_files.append(item)
84         for section in self.data.sections():
85             if section != u"__control__":
86                 misc.Element(section, self.universe, self.filename)
87         for include_file in includes:
88             if not os.path.isabs(include_file):
89                 include_file = find_file(
90                     include_file,
91                     relative=self.filename,
92                     universe=self.universe
93                 )
94             if (include_file not in self.universe.files or not
95                     self.universe.files[include_file].is_writeable()):
96                 DataFile(include_file, self.universe)
97
98     def save(self):
99         u"""Write the data, if necessary."""
100         import codecs
101         import os
102         import os.path
103         import re
104         import stat
105
106         # when modified, writeable and has content or the file exists
107         if self.modified and self.is_writeable() and (
108            self.data.sections() or os.path.exists(self.filename)
109            ):
110
111             # make parent directories if necessary
112             if not os.path.exists(os.path.dirname(self.filename)):
113                 os.makedirs(os.path.dirname(self.filename))
114
115             # backup the file
116             if self.data.has_option(u"__control__", u"backup_count"):
117                 max_count = self.data.has_option(
118                     u"__control__", u"backup_count")
119             else:
120                 max_count = self.universe.categories[
121                     u"internal"
122                 ][
123                     u"limits"
124                 ].getint(u"default_backup_count")
125             if os.path.exists(self.filename) and max_count:
126                 backups = []
127                 for candidate in os.listdir(os.path.dirname(self.filename)):
128                     if re.match(
129                        os.path.basename(self.filename) +
130                        u"""\.\d+$""", candidate
131                        ):
132                         backups.append(int(candidate.split(u".")[-1]))
133                 backups.sort()
134                 backups.reverse()
135                 for old_backup in backups:
136                     if old_backup >= max_count - 1:
137                         os.remove(self.filename + u"." + unicode(old_backup))
138                     elif not os.path.exists(
139                         self.filename + u"." + unicode(old_backup + 1)
140                     ):
141                         os.rename(
142                             self.filename + u"." + unicode(old_backup),
143                             self.filename + u"." + unicode(old_backup + 1)
144                         )
145                 if not os.path.exists(self.filename + u".0"):
146                     os.rename(self.filename, self.filename + u".0")
147
148             # our data file
149             file_descriptor = codecs.open(self.filename, u"w", u"utf-8")
150
151             # if it's marked private, chmod it appropriately
152             if self.filename in self.universe.private_files and oct(
153                stat.S_IMODE(os.stat(self.filename)[stat.ST_MODE])
154                ) != 0600:
155                 os.chmod(self.filename, 0600)
156
157             # write it back sorted, instead of using ConfigParser
158             sections = self.data.sections()
159             sections.sort()
160             for section in sections:
161                 file_descriptor.write(u"[" + section + u"]\n")
162                 options = self.data.options(section)
163                 options.sort()
164                 for option in options:
165                     file_descriptor.write(
166                         option + u" = " +
167                         self.data.get(section, option) + u"\n"
168                     )
169                 file_descriptor.write(u"\n")
170
171             # flush and close the file
172             file_descriptor.flush()
173             file_descriptor.close()
174
175             # unset the modified flag
176             self.modified = False
177
178     def is_writeable(self):
179         u"""Returns True if the __control__ read_only is False."""
180         return not self.data.has_option(
181             u"__control__", u"read_only"
182         ) or not self.data.getboolean(
183             u"__control__", u"read_only"
184         )
185
186
187 def find_file(
188     file_name=None,
189     root_path=None,
190     search_path=None,
191     default_dir=None,
192     relative=None,
193     universe=None
194 ):
195     u"""Return an absolute file path based on configuration."""
196     import os
197     import os.path
198     import sys
199
200     # make sure to get rid of any surrounding quotes first thing
201     if file_name:
202         file_name = file_name.strip(u"\"'")
203
204     # this is all unnecessary if it's already absolute
205     if file_name and os.path.isabs(file_name):
206         return os.path.realpath(file_name)
207
208     # when no file name is specified, look for <argv[0]>.conf
209     elif not file_name:
210         file_name = os.path.basename(sys.argv[0]) + u".conf"
211
212     # if a universe was provided, try to get some defaults from there
213     if universe:
214
215         if hasattr(
216            universe,
217            u"contents"
218            ) and u"internal:storage" in universe.contents:
219             storage = universe.categories[u"internal"][u"storage"]
220             if not root_path:
221                 root_path = storage.get(u"root_path").strip("\"'")
222             if not search_path:
223                 search_path = storage.getlist(u"search_path")
224             if not default_dir:
225                 default_dir = storage.get(u"default_dir").strip("\"'")
226
227         # if there's only one file loaded, try to work around a chicken<egg
228         elif hasattr(universe, u"files") and len(
229             universe.files
230         ) == 1 and not universe.files[universe.files.keys()[0]].is_writeable():
231             data_file = universe.files[universe.files.keys()[0]].data
232
233             # try for a fallback default directory
234             if not default_dir and data_file.has_option(
235                u"internal:storage",
236                u"default_dir"
237                ):
238                 default_dir = data_file.get(
239                     u"internal:storage",
240                     u"default_dir"
241                 ).strip(u"\"'")
242
243             # try for a fallback root path
244             if not root_path and data_file.has_option(
245                u"internal:storage",
246                u"root_path"
247                ):
248                 root_path = data_file.get(
249                     u"internal:storage",
250                     u"root_path"
251                 ).strip(u"\"'")
252
253             # try for a fallback search path
254             if not search_path and data_file.has_option(
255                u"internal:storage",
256                u"search_path"
257                ):
258                 search_path = makelist(
259                     data_file.get(u"internal:storage",
260                                   u"search_path").strip(u"\"'")
261                 )
262
263         # another fallback root path, this time from the universe startdir
264         if not root_path and hasattr(universe, "startdir"):
265             root_path = universe.startdir
266
267     # when no root path is specified, assume the current working directory
268     if not root_path:
269         root_path = os.getcwd()
270
271     # otherwise, make sure it's absolute
272     elif not os.path.isabs(root_path):
273         root_path = os.path.realpath(root_path)
274
275     # if there's no search path, just use the root path and etc
276     if not search_path:
277         search_path = [root_path, u"etc"]
278
279     # work on a copy of the search path, to avoid modifying the caller's
280     else:
281         search_path = search_path[:]
282
283     # if there's no default path, use the last element of the search path
284     if not default_dir:
285         default_dir = search_path[-1]
286
287     # if an existing file or directory reference was supplied, prepend it
288     if relative:
289         relative = relative.strip(u"\"'")
290         if os.path.isdir(relative):
291             search_path = [relative] + search_path
292         else:
293             search_path = [os.path.dirname(relative)] + search_path
294
295     # make the search path entries absolute and throw away any dupes
296     clean_search_path = []
297     for each_path in search_path:
298         each_path = each_path.strip(u"\"'")
299         if not os.path.isabs(each_path):
300             each_path = os.path.realpath(os.path.join(root_path, each_path))
301         if each_path not in clean_search_path:
302             clean_search_path.append(each_path)
303
304     # start hunting for the file now
305     for each_path in clean_search_path:
306
307         # if the file exists and is readable, we're done
308         if os.path.isfile(os.path.join(each_path, file_name)):
309             file_name = os.path.realpath(os.path.join(each_path, file_name))
310             break
311
312     # it didn't exist after all, so use the default path instead
313     if not os.path.isabs(file_name):
314         file_name = os.path.join(default_dir, file_name)
315     if not os.path.isabs(file_name):
316         file_name = os.path.join(root_path, file_name)
317
318     # and normalize it last thing before returning
319     file_name = os.path.realpath(file_name)
320
321     # normalize the resulting file path and hand it back
322     return file_name
323
324
325 def makelist(value):
326     u"""Turn string into list type."""
327     if value[0] + value[-1] == u"[]":
328         return eval(value)
329     elif value[0] + value[-1] == u"\"\"":
330         return [value[1:-1]]
331     else:
332         return [value]
333
334
335 def makedict(value):
336     u"""Turn string into dict type."""
337     if value[0] + value[-1] == u"{}":
338         return eval(value)
339     elif value.find(u":") > 0:
340         return eval(u"{" + value + u"}")
341     else:
342         return {value: None}