PEP 8 conformance for data handling library
[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 self.universe.files[
95                include_file
96                ].is_writeable():
97                 DataFile(include_file, self.universe)
98
99     def save(self):
100         u"""Write the data, if necessary."""
101         import codecs
102         import os
103         import os.path
104         import re
105         import stat
106
107         # when modified, writeable and has content or the file exists
108         if self.modified and self.is_writeable() and (
109            self.data.sections() or os.path.exists(self.filename)
110            ):
111
112             # make parent directories if necessary
113             if not os.path.exists(os.path.dirname(self.filename)):
114                 os.makedirs(os.path.dirname(self.filename))
115
116             # backup the file
117             if self.data.has_option(u"__control__", u"backup_count"):
118                 max_count = self.data.has_option(
119                     u"__control__", u"backup_count")
120             else:
121                 max_count = self.universe.categories[
122                     u"internal"
123                 ][
124                     u"limits"
125                 ].getint(u"default_backup_count")
126             if os.path.exists(self.filename) and max_count:
127                 backups = []
128                 for candidate in os.listdir(os.path.dirname(self.filename)):
129                     if re.match(
130                        os.path.basename(self.filename) +
131                        u"""\.\d+$""", candidate
132                        ):
133                         backups.append(int(candidate.split(u".")[-1]))
134                 backups.sort()
135                 backups.reverse()
136                 for old_backup in backups:
137                     if old_backup >= max_count - 1:
138                         os.remove(self.filename + u"." + unicode(old_backup))
139                     elif not os.path.exists(
140                         self.filename + u"." + unicode(old_backup + 1)
141                     ):
142                         os.rename(
143                             self.filename + u"." + unicode(old_backup),
144                             self.filename + u"." + unicode(old_backup + 1)
145                         )
146                 if not os.path.exists(self.filename + u".0"):
147                     os.rename(self.filename, self.filename + u".0")
148
149             # our data file
150             file_descriptor = codecs.open(self.filename, u"w", u"utf-8")
151
152             # if it's marked private, chmod it appropriately
153             if self.filename in self.universe.private_files and oct(
154                stat.S_IMODE(os.stat(self.filename)[stat.ST_MODE])
155                ) != 0600:
156                 os.chmod(self.filename, 0600)
157
158             # write it back sorted, instead of using ConfigParser
159             sections = self.data.sections()
160             sections.sort()
161             for section in sections:
162                 file_descriptor.write(u"[" + section + u"]\n")
163                 options = self.data.options(section)
164                 options.sort()
165                 for option in options:
166                     file_descriptor.write(
167                         option + u" = " +
168                         self.data.get(section, option) + u"\n"
169                     )
170                 file_descriptor.write(u"\n")
171
172             # flush and close the file
173             file_descriptor.flush()
174             file_descriptor.close()
175
176             # unset the modified flag
177             self.modified = False
178
179     def is_writeable(self):
180         u"""Returns True if the __control__ read_only is False."""
181         return not self.data.has_option(
182             u"__control__", u"read_only"
183         ) or not self.data.getboolean(
184             u"__control__", u"read_only"
185         )
186
187
188 def find_file(
189     file_name=None,
190     root_path=None,
191     search_path=None,
192     default_dir=None,
193     relative=None,
194     universe=None
195 ):
196     u"""Return an absolute file path based on configuration."""
197     import os
198     import os.path
199     import sys
200
201     # make sure to get rid of any surrounding quotes first thing
202     if file_name:
203         file_name = file_name.strip(u"\"'")
204
205     # this is all unnecessary if it's already absolute
206     if file_name and os.path.isabs(file_name):
207         return os.path.realpath(file_name)
208
209     # when no file name is specified, look for <argv[0]>.conf
210     elif not file_name:
211         file_name = os.path.basename(sys.argv[0]) + u".conf"
212
213     # if a universe was provided, try to get some defaults from there
214     if universe:
215
216         if hasattr(
217            universe,
218            u"contents"
219            ) and u"internal:storage" in universe.contents:
220             storage = universe.categories[u"internal"][u"storage"]
221             if not root_path:
222                 root_path = storage.get(u"root_path").strip("\"'")
223             if not search_path:
224                 search_path = storage.getlist(u"search_path")
225             if not default_dir:
226                 default_dir = storage.get(u"default_dir").strip("\"'")
227
228         # if there's only one file loaded, try to work around a chicken<egg
229         elif hasattr(universe, u"files") and len(
230             universe.files
231         ) == 1 and not universe.files[universe.files.keys()[0]].is_writeable():
232             data_file = universe.files[universe.files.keys()[0]].data
233
234             # try for a fallback default directory
235             if not default_dir and data_file.has_option(
236                u"internal:storage",
237                u"default_dir"
238                ):
239                 default_dir = data_file.get(
240                     u"internal:storage",
241                     u"default_dir"
242                 ).strip(u"\"'")
243
244             # try for a fallback root path
245             if not root_path and data_file.has_option(
246                u"internal:storage",
247                u"root_path"
248                ):
249                 root_path = data_file.get(
250                     u"internal:storage",
251                     u"root_path"
252                 ).strip(u"\"'")
253
254             # try for a fallback search path
255             if not search_path and data_file.has_option(
256                u"internal:storage",
257                u"search_path"
258                ):
259                 search_path = makelist(
260                     data_file.get(u"internal:storage",
261                                   u"search_path").strip(u"\"'")
262                 )
263
264         # another fallback root path, this time from the universe startdir
265         if not root_path and hasattr(universe, "startdir"):
266             root_path = universe.startdir
267
268     # when no root path is specified, assume the current working directory
269     if not root_path:
270         root_path = os.getcwd()
271
272     # otherwise, make sure it's absolute
273     elif not os.path.isabs(root_path):
274         root_path = os.path.realpath(root_path)
275
276     # if there's no search path, just use the root path and etc
277     if not search_path:
278         search_path = [root_path, u"etc"]
279
280     # work on a copy of the search path, to avoid modifying the caller's
281     else:
282         search_path = search_path[:]
283
284     # if there's no default path, use the last element of the search path
285     if not default_dir:
286         default_dir = search_path[-1]
287
288     # if an existing file or directory reference was supplied, prepend it
289     if relative:
290         relative = relative.strip(u"\"'")
291         if os.path.isdir(relative):
292             search_path = [relative] + search_path
293         else:
294             search_path = [os.path.dirname(relative)] + search_path
295
296     # make the search path entries absolute and throw away any dupes
297     clean_search_path = []
298     for each_path in search_path:
299         each_path = each_path.strip(u"\"'")
300         if not os.path.isabs(each_path):
301             each_path = os.path.realpath(os.path.join(root_path, each_path))
302         if each_path not in clean_search_path:
303             clean_search_path.append(each_path)
304
305     # start hunting for the file now
306     for each_path in clean_search_path:
307
308         # if the file exists and is readable, we're done
309         if os.path.isfile(os.path.join(each_path, file_name)):
310             file_name = os.path.realpath(os.path.join(each_path, file_name))
311             break
312
313     # it didn't exist after all, so use the default path instead
314     if not os.path.isabs(file_name):
315         file_name = os.path.join(default_dir, file_name)
316     if not os.path.isabs(file_name):
317         file_name = os.path.join(root_path, file_name)
318
319     # and normalize it last thing before returning
320     file_name = os.path.realpath(file_name)
321
322     # normalize the resulting file path and hand it back
323     return file_name
324
325
326 def makelist(value):
327     u"""Turn string into list type."""
328     if value[0] + value[-1] == u"[]":
329         return eval(value)
330     elif value[0] + value[-1] == u"\"\"":
331         return [value[1:-1]]
332     else:
333         return [value]
334
335
336 def makedict(value):
337     u"""Turn string into dict type."""
338     if value[0] + value[-1] == u"{}":
339         return eval(value)
340     elif value.find(u":") > 0:
341         return eval(u"{" + value + u"}")
342     else:
343         return {value: None}