Skip to content

Commit 26a5ee3

Browse files
committed
Small improvements on instance.py by Cheer Xiao.
- _load_python split into three functions: _compile, _exec and _execfile, mimicing corresponding builtin functions; also the os.path fiddling is isolated to _exec, the motivation being that lib/ should be available for interfaces.py besides extensions and detectors. - all variables named `vars` are renamed to `env` as "vars" shadows the builtin with the same name.
1 parent 7b0ad06 commit 26a5ee3

File tree

1 file changed

+34
-39
lines changed

1 file changed

+34
-39
lines changed

roundup/instance.py

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@
3535
from roundup.cgi import client, templating
3636
from roundup.cgi import actions as cgi_actions
3737

38-
class Vars:
39-
def __init__(self, vars):
40-
self.__dict__.update(vars)
41-
4238
class Tracker:
4339
def __init__(self, tracker_home, optimize=0):
4440
"""New-style tracker instance constructor
@@ -63,27 +59,21 @@ def __init__(self, tracker_home, optimize=0):
6359
self.load_interfaces()
6460
self.templates = templating.get_templates(self.config["TEMPLATES"], self.config["TEMPLATE_ENGINE"])
6561
self.backend = backends.get_backend(self.get_backend_name())
62+
63+
libdir = os.path.join(self.tracker_home, 'lib')
64+
self.libdir = os.path.isdir(libdir) and libdir or ''
65+
6666
if self.optimize:
67-
libdir = os.path.join(self.tracker_home, 'lib')
68-
if os.path.isdir(libdir):
69-
sys.path.insert(1, libdir)
7067
self.templates.precompileTemplates()
7168
# initialize tracker extensions
7269
for extension in self.get_extensions('extensions'):
7370
extension(self)
7471
# load database schema
75-
schemafilename = os.path.join(self.tracker_home, 'schema.py')
76-
# Note: can't use built-in open()
77-
# because of the global function with the same name
78-
schemafile = file(schemafilename, 'rt')
79-
self.schema = compile(schemafile.read(), schemafilename, 'exec')
80-
schemafile.close()
72+
self.schema = self._compile('schema.py')
8173
# load database detectors
8274
self.detectors = self.get_extensions('detectors')
8375
# db_open is set to True after first open()
8476
self.db_open = 0
85-
if libdir in sys.path:
86-
sys.path.remove(libdir)
8777

8878
def get_backend_name(self):
8979
f = file(os.path.join(self.config.DATABASE, 'backend_name'))
@@ -97,7 +87,7 @@ def open(self, name=None):
9787
# because the schema has security settings that must be
9888
# applied to each database instance
9989
backend = self.backend
100-
vars = {
90+
env = {
10191
'Class': backend.Class,
10292
'FileClass': backend.FileClass,
10393
'IssueClass': backend.IssueClass,
@@ -112,28 +102,23 @@ def open(self, name=None):
112102
'db': backend.Database(self.config, name)
113103
}
114104

115-
libdir = os.path.join(self.tracker_home, 'lib')
116-
if os.path.isdir(libdir):
117-
sys.path.insert(1, libdir)
118105
if self.optimize:
119106
# execute preloaded schema object
120-
exec(self.schema, vars)
107+
exec(self.schema, env)
121108
if callable (self.schema_hook):
122-
self.schema_hook(**vars)
109+
self.schema_hook(**env)
123110
# use preloaded detectors
124111
detectors = self.detectors
125112
else:
126113
# execute the schema file
127-
self._load_python('schema.py', vars)
114+
self._execfile('schema.py', env)
128115
if callable (self.schema_hook):
129-
self.schema_hook(**vars)
116+
self.schema_hook(**env)
130117
# reload extensions and detectors
131118
for extension in self.get_extensions('extensions'):
132119
extension(self)
133120
detectors = self.get_extensions('detectors')
134-
if libdir in sys.path:
135-
sys.path.remove(libdir)
136-
db = vars['db']
121+
db = env['db']
137122
# apply the detectors
138123
for detector in detectors:
139124
detector(db)
@@ -168,12 +153,12 @@ def open(self, name=None):
168153

169154
def load_interfaces(self):
170155
"""load interfaces.py (if any), initialize Client and MailGW attrs"""
171-
vars = {}
156+
env = {}
172157
if os.path.isfile(os.path.join(self.tracker_home, 'interfaces.py')):
173-
self._load_python('interfaces.py', vars)
174-
self.Client = vars.get('Client', client.Client)
175-
self.MailGW = vars.get('MailGW', mailgw.MailGW)
176-
self.TemplatingUtils = vars.get('TemplatingUtils', templating.TemplatingUtils)
158+
self._execfile('interfaces.py', env)
159+
self.Client = env.get('Client', client.Client)
160+
self.MailGW = env.get('MailGW', mailgw.MailGW)
161+
self.TemplatingUtils = env.get('TemplatingUtils', templating.TemplatingUtils)
177162

178163
def get_extensions(self, dirname):
179164
"""Load python extensions
@@ -193,15 +178,15 @@ def get_extensions(self, dirname):
193178
for name in os.listdir(dirpath):
194179
if not name.endswith('.py'):
195180
continue
196-
vars = {}
197-
self._load_python(os.path.join(dirname, name), vars)
198-
extensions.append(vars['init'])
181+
env = {}
182+
self._execfile(os.path.join(dirname, name), env)
183+
extensions.append(env['init'])
199184
sys.path.remove(dirpath)
200185
return extensions
201186

202187
def init(self, adminpw):
203188
db = self.open('admin')
204-
self._load_python('initial_data.py', {'db': db, 'adminpw': adminpw,
189+
self._execfile('initial_data.py', {'db': db, 'adminpw': adminpw,
205190
'admin_email': self.config['ADMIN_EMAIL']})
206191
db.commit()
207192
db.close()
@@ -212,10 +197,20 @@ def exists(self):
212197
def nuke(self):
213198
self.backend.db_nuke(self.config)
214199

215-
def _load_python(self, file, vars):
216-
file = os.path.join(self.tracker_home, file)
217-
execfile(file, vars)
218-
return vars
200+
def _compile(self, fname):
201+
fname = os.path.join(self.tracker_home, fname)
202+
return compile(file(fname).read(), fname, 'exec')
203+
204+
def _exec(self, obj, env):
205+
if self.libdir:
206+
sys.path.insert(1, self.libdir)
207+
exec(obj, env)
208+
if self.libdir:
209+
sys.path.remove(self.libdir)
210+
return env
211+
212+
def _execfile(self, fname, env):
213+
self._exec(self._compile(fname), env)
219214

220215
def registerAction(self, name, action):
221216

0 commit comments

Comments
 (0)