Imported from archive.
[mudpy.git] / lib / muff / muffvars.py
1 """Global variable objects for the MUFF Engine"""
2
3 # Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>, all rights reserved.
4 # Licensed per terms in the LICENSE file distributed with this software.
5
6 # persistent variables are stored in ini-style files supported by ConfigParser
7 import ConfigParser
8
9 # need to be able to create the variable save file's directory
10 import os
11
12 # hack to load all modules in the muff package
13 import muff
14 for module in muff.__all__:
15         exec("import " + module)
16
17 # does the files:variable setting exist yet?
18 try:
19         if muffconf.get("files", "variable"): pass
20
21 # if not, reload the muffconf module
22 except AttributeError:
23         reload(muffconf)
24
25 # now we can safely load persistent variables from file
26 variable_file = muffconf.get("files", "variable")
27 variable_data = ConfigParser.SafeConfigParser()
28 variable_data.read(variable_file)
29
30 # if there is no userlist, create an empty one
31 try:
32         if userlist: pass
33 except NameError:
34         userlist = []
35
36 # if there is no listening socket, create an empty one
37 try:
38         if newsocket: pass
39 except NameError:
40         newsocket = None
41
42 # flag to raise when the world should be shut down
43 terminate_world = False
44
45 # flag to raise when all code modules, config and data should be reloaded
46 reload_modules = False
47
48 # a dict of replacement macros
49 macros = {
50         "$(eol)": "\r\n",
51         "$(bld)": chr(27) + "[1m",
52         "$(nrm)": chr(27) + "[0m",
53         "$(blk)": chr(27) + "[30m",
54         "$(grn)": chr(27) + "[32m",
55         "$(red)": chr(27) + "[31m"
56         }
57
58 def save():
59         """Function to save persistent variables to a file."""
60
61         # try to open the variable file
62         try:
63                 file_descriptor = file(variable_file, "w")
64
65         # failing that, make the directory in which it resides first
66         except IOError:
67                 os.makedirs(os.sep.join(variable_file.split(os.sep)[:-1]))
68                 file_descriptor = file(variable_file, "w")
69
70         # now write the data and close out the file
71         variable_data.write(file_descriptor)
72         file_descriptor.flush()
73         file_descriptor.close()
74