File: AppContext.py

package info (click to toggle)
sulfur 0.1.3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 372 kB
  • ctags: 313
  • sloc: python: 1,240; sh: 172; makefile: 92
file content (549 lines) | stat: -rw-r--r-- 17,346 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#
#  $Id: AppContext.py,v 1.5 1999/12/20 10:15:09 rob Exp $
#
#  Copyright 1999 Rob Tillotson <robt@debian.org>
#  All Rights Reserved
#
#  Permission to use, copy, modify, and distribute this software and
#  its documentation for any purpose and without fee or royalty is
#  hereby granted, provided that the above copyright notice appear in
#  all copies and that both the copyright notice and this permission
#  notice appear in supporting documentation or portions thereof,
#  including modifications, that you you make.
#
#  THE AUTHOR ROB TILLOTSON DISCLAIMS ALL WARRANTIES WITH REGARD TO
#  THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
#  AND FITNESS.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
#  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
#  RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
#  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
#  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE!
#
"""
"""

__version__ = '$Id: AppContext.py,v 1.5 1999/12/20 10:15:09 rob Exp $'

__copyright__ = 'Copyright 1999 Rob Tillotson <robt@debian.org>'


import sys, os, string, operator
from types import ClassType

from Application import *
import misc
import Registry

try:
    import cPickle
    pickle = cPickle
except:
    import pickle

# error levels.
ERR_WARNING = 'warning'
ERR_NORMAL  = 'error'


# Application context
# note that this IS NOT a plug-in.

# The AppContext object is a more-or-less abstract superclass for the
# object that will eventually be used to instantiate and run an application;
# it calls back to various stub methods to find, load, and preconfigure
# the application object.  (The AppContext object can be used directly if
# the application object is located by some other means, such as by being
# instantiated by another application's get_plugin method.)
#  TODO: make an AppContext subclass expressly designed to run one app
#        inside another
#
# Actual behavior is provided by some mixin classes which can be combined
# to make a complete AppContext object.  Only a couple of these are implemented
# right now: PathAppLoader and CLIProcessor.  The point of this is to provide
# a separation of jobs: the 'processor' handles the user interface end of things,
# the 'loader' finds and instantiates the application, and the context class
# handles the rest.  The processor class is probably most amenable to replacement,
# perhaps by something with a GUI.  The loader might be harder to replace,
# given how dependent Sulfur is on the regular Python module system, but it
# could conceivably be replaced with something that (for example) prods a CORBA
# server into returning an Application object.
#


# mixins:
#
#  - loading and instantiating the app [this may call back to plugin manager]
#  - plugin manager (including registration) [may call back to state manager]
#  - state manager (manages persistent app state and options)
#  - startup manager (finds out what app to run and gets initial options from the user)
#  - user interface manager (handles log() and error() )


class AppContext:
    app_type = 'Sulfur'
    
    def __init__(self, app=None):
	# The original design for this class used compile-time inheritance to
	# paste the various parts of the context together.  However, this makes
	# it difficult to assemble a variety of different contexts at runtime,
	# or to change the context behind the application's back.  Thus, this
	# code now uses a form of acquisition -- each of the following objects
	# is one part of the interface, and stub functions will be wrapped
	# around each one to provide the visible interface.
	#
	# Each one of these will have a 'start' and 'stop' method as well as
	# whatever other interfaces are necessary.
	# 
	self.loader = None
	self.startup_manager = None
	self.state_manager = None
	self.plugin_manager = None
	self.ui = None
	#
	self.app = app
	self.preloaded_plugins = []
	
    # change_* are only for use while the app is running.
    def change_loader(self, obj):
	if self.loader is not None: self.loader.stop()
	self.loader = obj
	self.loader.start(self)

    def change_startup(self, obj):
	if self.startup_manager is not None: self.startup_manager.stop()
	self.startup_manager = obj
	self.startup_manager.start(self)

    def change_state(self, obj):
	if self.state_manager is not None: self.state_manager.stop()
	self.state_manager = obj
	self.state_manager.start(self)

    def change_plugin_manager(self, obj):
	if self.plugin_manager is not None: self.plugin_manager.stop()
	self.plugin_manager = obj
	self.plugin_manager.start(self)

    def change_ui(self, obj):
	if self.ui is not None: self.ui.stop()
	self.ui = obj
	self.ui.start(self)

    # external API to applications.
    # we will use a form of acquisition here, searching all of the internal
    # pieces of the context... ui and plugin_manager are first, since they
    # are the ones that provide external interfaces at present.
    def __getattr__(self, k):
	if hasattr(self.ui, k): return getattr(self.ui, k)
	if hasattr(self.plugin_manager, k): return getattr(self.plugin_manager, k)
	if hasattr(self.state_manager, k): return getattr(self.state_manager, k)
	if hasattr(self.startup_manager, k): return getattr(self.startup_manager, k)
	if hasattr(self.loader, k): return getattr(self.loader, k)
	raise AttributeError, k
    
    def fatal_error(self, s):
	raise RuntimeError, s
    
    def unload(self):
	# unlink app context here
	self.app = None
	self.preloaded_plugins = []

    def __call__(self, *a, **kw):
	# order of initialization
	#  - initialize UI
	#  - load state
	#  - create and load registry
	#  - initialize plugin manager
	#  - find and instantiate application class [uses Loader and Startup]
	#  - call app.prerun()
	#  - call get_initial_args()
	#  - preload plugins if needed
	#  - call app.run()
	#  - call app.postrun()
	#  - save state
	#
	self.ui.start(self)
	self.state_manager.start(self)
	self.plugin_manager.start(self)
	self.loader.start(self)
	self.startup_manager.start(self)
	
	if self.app is None:
	    self.app = self.loader.get_app_object()
	    if self.app is None:
		self.fatal_error("could not find application")

	# even though this might have been done already by the plugin
	# manager, we do it again just in case.
	self.app.configure(self.registry)
	self.app.context = self
	self.app.registry = self.registry
	#
	self.app.prerun()
	# get_initial_args can change the app option values
	argv = self.startup_manager.get_initial_args()
	apply(self.app.run, (argv,)+a, kw)
	self.app.postrun()
	# save state
	self.startup_manager.stop()
	self.loader.stop()
	self.plugin_manager.stop()
	self.state_manager.stop()
	self.ui.stop()

class ContextComponent:
    def __init__(self):
	self.context = None
    def start(self, context):
	self.context = context
    def stop(self):
	self.context = None
	
# LOADERS
#
class Loader(ContextComponent):
    def get_app_object(self): return None
    
class PluginLoader(Loader):
    def __init__(self, collection='Application'):
	self.collection = collection
	
    def get_app_path(self):
	path = [ self.context.app_type ]
	ev = '%sPLUGINS' % string.upper(self.context.app_type)
	if os.environ.has_key(ev):
	    path = string.split(os.environ[ev], ':') + path
	return path

    def get_app_object(self):
	name = self.context.startup_manager.get_app_name()
	path = self.get_app_path()
	if type(name) == type(''):
	    return self.context.plugin_manager.get_plugin(self.collection, name)
	else:
	    for n in name:
		m = self.context.plugin_manager.get_plugin(self.collection, name)
		if m is not None: return m
	    return None
	
    def __load(self, name, path):
	m = None
	for pe in path:
	    if path: pe = pe + '.' + self.collection
	    else: pe = self.collection
	    try:
		m = misc.import_module(pe+'.'+name, globals())
		break
	    except:
		pass
	if m is None: return None
	for o in m.__dict__.values():
	    if type(o) == ClassType and hasattr(o, 'is_plugin'):
		return o
	return None
	

# STARTUP MANAGERS
#
class Startup(ContextComponent):
    def fatal_error(self, s): raise RuntimeError, s
    def get_app_name(self): return sys.argv[0]
    def get_initial_args(self): return []
    def help(self): pass
    
class CLIStartup(Startup):
    def fatal_error(self, s):
	print "Error initializing application context:", s
	sys.exit(2)

    def get_app_name(self):
	a = os.path.basename(sys.argv[0])
	n = None
	# if argv[0] does not end in ".py{c,o}", assume that this is being
	# invoked from a symlink; the name of this symlink can be
	# 'bar-foo' (where bar is the app_type) or just 'foo'.
	# otherwise, assume that the first argument is the application to run.
	# obviously, doing something useful with this on systems that don't
	# have Unix symlink and argv semantics may require overriding of
	# this code.
	if a[-3:] != '.py' and a[-4:] not in ['.pyc', '.pyo'] and \
	   a[-4:] != '-run': # FIXME this is a total hack
	    if a[:len(self.context.app_type)+1] == string.lower(self.context.app_type)+'-':
		n = a[len(self.context.app_type)+1:]
	    else:
		n = a
	else:
	    if len(sys.argv) > 1:
		n = sys.argv[1]
		sys.argv = sys.argv[1:]
	if n is None:
	    self.fatal_error("Cannot determine what application to run.")
	return n
	
    def get_initial_args(self):
	opts = {}
	for k, v in self.context.app.option_refs.items(): opts[k] = (self.context.app, v)
	for p in self.context.preloaded_plugins:
	    for k, v in p.option_refs.items(): opts[k] = (p, v)

	try:
	    argv = parse_cmd_options(sys.argv[1:], opts)
	except OptionParsingError, err:
	    print "error:", err
	    print
	    self.help()
	    sys.exit(1)

	# fixme
	if self.context.app.auto_cmd_help and self.context.app.has_option('help') \
	   and self.context.app.get_option('help'):
	    self.help()
	    sys.exit(0)

	return argv

    def help(self):
	hl = []
	l = []
	d = {}
	d.update(self.context.app.option_refs)
	#for o in extra_options: d[o.name] = o
	hl.append( (self.context.app, process_help(d)) )
	for p in self.context.preloaded_plugins: hl.append( (p, process_help(p.option_refs)) )

	# who says Python can't be written obscurely?
	maxlen = max(reduce(operator.add,
			    map(lambda x: map(lambda y: len(y[0]), x),
				map(lambda x: x[1], hl)), []))

	if maxlen > 40: maxlen = 40

	for p, h in hl:
	    if p is not self.context.app: l.append('%s: ' % (p.type_name and p.type_name or p.type))
	    l.append('%s %s by %s\n' % (p.name, p.version, p.author))
	    l.append(p.description)
	    l.append('\n')
	    if h:
		l.append('\n')
		l.append(help_string(h, maxlen))
		l.append('\n')

	if self.context.app.extra_help: l.append(self.app.extra_help)
	
	print string.join(l, '')

#
# STATE MANAGERS
#
# self.state is a dictionary-like object with application-defined contents.
# (other parts of the context may stuff things here too).
#
# self.state_load() is called early in the application running process,
# and it should load the state from persistent storage.
#
# self.state_save() is called after the application terminates, and it
# should save the state to where it can be loaded later.
#
# The state manager is also responsible for creating, loading, and saving the
# registry; eventually these will be folded together, maybe.
class State(ContextComponent):
    def start(self, context):
	ContextComponent.start(self, context)
	context.state = {}
	context.registry = Registry.Registry()
    
class PickleState(State):
    """A simple State Manager that stores the state in a pickle."""
    def __init__(self, path="~/.%s_state"):
	self.path = path
	
    def start(self, context):
	State.start(self, context)
	if string.find(self.path, '%s') >= 0:
	    self.state_file_name = os.path.expanduser(self.path % string.lower(self.context.app_type))
	else:
	    self.state_file_name = os.path.expanduser(self.path)
	try:
	    context.state = pickle.load(open(self.state_file_name))
	except:
	    context.state = {}

    def stop(self):
	try:
	    pickle.dump(self.context.state, open(self.state_file_name, 'w'))
	except:
	    pass
	State.stop(self)
	

# PLUGIN MANAGERS
#
# The plugin manager also handles products, which are collections of
# plugin collections.
#
class PluginManager(ContextComponent):
    def start(self, context):
	ContextComponent.start(self, context)
	self.plugins = {}
    def get_plugin(self, collection, name, *a, **kw): return None
    def list_plugins(self, collection): return []
    def list_plugin_info(self, collection): return []
    
class OriginalPluginManager(PluginManager):
    def start(self, context):
	PluginManager.start(self, context)
	path = [ context.app_type ]
	ev = '%sPLUGINS' % string.upper(context.app_type)
	if os.environ.has_key(ev):
	    path = string.split(os.environ[ev], ':') + path
	self.plugin_module_path = path
	self.plugins = {}
	if context.state.has_key('products'):
	    prd = {}
	    for pkg in self.context.state['products'].values():
		if pkg and pkg not in self.plugin_module_path:
		    self.plugin_module_path.append(pkg)
	
    def get_plugin(self, collection, name, *a, **kw):
	pname = collection + '.' + name
	
	if self.plugins.has_key(pname): return self.plugins[pname]

	for pe in self.plugin_module_path:
	    if pe: pe = pe + '.' + collection
	    else: pe = collection

	    try:
		m = misc.import_module(pe+'.'+name, globals())
		for o in m.__dict__.values():
		    if type(o) == ClassType and hasattr(o, 'is_plugin'):
			p = apply(o, a, kw)
			p.set_context(self.context.app)
			p.configure(self.context.registry)
			self.plugins[pname] = p
			return p
	    except:
		pass
	raise ImportError, 'Plugin: %s.%s' % (collection, name)

    def list_plugins(self, collection):
	"""List plugins in a particular collection.
	"""
	return misc.list_plugins(self.plugin_module_path, collection)

    def list_plugin_info(self, collection):
	"""List information about plugins in a particular collection.
	"""
	return misc.list_plugin_info(self.plugin_module_path, collection)

    # products
    # the API is not really specific to the concept of package-as-product,
    # but there seems no way to completely divorce the code from it.  This
    # probably makes sense, because if you radically change the way the
    # plugin manager works, you probably also have to radically change your
    # idea of what a product is...
    def product_install(self, package):
	if not self.context.state.has_key('products'):
	    self.context.state['products'] = {}

	try:
	    m = misc.import_module(package)
	except:
	    m = None

	if not m:
	    raise ImportError, "Could not import package '%s'" % package

	if not hasattr(m, '%s_PRODUCT' % string.upper(self.context.app_type)):
	    raise RuntimeError, "Package '%s' does not appear to contain a Sulfur product." % package

	name = getattr(m, '%s_PRODUCT' % string.upper(self.context.app_type))['name']
	
	if package not in self.plugin_module_path:
	    self.plugin_module_path.append(package)

	self.context.state['products'][name] = package

	return name
    
    def product_list(self):
	if not self.context.state.has_key('products'):
	    self.context.state['products'] = {}

	l = []
	for name, pkg in self.context.state['products'].items():
	    try:
		m = misc.import_module(pkg)
	    except:
		continue
	    if not hasattr(m, '%s_PRODUCT' % string.upper(self.context.app_type)):
		continue
	    p = getattr(m, '%s_PRODUCT' % string.upper(self.context.app_type))
	    q = {'package': pkg,
		 'name': p.get('name'),
		 'author': p.get('author'),
		 'version': p.get('version'),
		 'description': p.get('description')
		 }
	    l.append(q)
	l.sort(lambda x,y: cmp(x.get('name'),y.get('name')))
	return l

    def product_remove(self, name):
	if not self.context.state.has_key('products'):
	    self.context.state['products'] = {}

	if self.context.state['products'].has_key(name):
	    p = self.context.state['products'][name]
	    if p and p in self.plugin_module_path:
		self.plugin_module_path.remove(p)
	    del self.context.state['products'][name]

    def product_installed(self, name):
	if self.context.state.has_key('products'):
	    return self.context.state['products'].has_key(name)

    def product_info(self, name):
	for p in self.product_list():
	    if p['name'] == name: return p
	return {}

StandardPluginManager = OriginalPluginManager


# USER INTERFACES
class UI(ContextComponent):
    def log(self, s, name=None): pass
    def error(self, s, level='warning', name=None): pass

# yes, this looks redundant, but it is meant to allow for translatability.
_error_levels = {
    ERR_WARNING: 'warning',
    ERR_NORMAL : 'error',
    }

class BasicUI(UI):
    def start(self, context):
	UI.start(self, context)
	self.logfile = sys.stdout
	
    def log(self, s, name=None):
	if name is None: self.logfile.write('%s\n' % s)
	else: self.logfile.write('%s: %s\n' % (name, s))

    def error(self, s, level=ERR_NORMAL, name=None):
	t = _error_levels.get(level, 'error')
	if name is None: self.logfile.write('%s: %s\n' % (s, level))
	else: self.logfile.write('%s: %s: %s\n' % (name, s, level))

class CLIAppContext(AppContext):
    def __init__(self, *a, **kw):
	apply(AppContext.__init__, (self,)+a, kw)
	self.loader = PluginLoader()
	self.startup_manager = CLIStartup()
	self.state_manager = PickleState()
	self.plugin_manager = OriginalPluginManager()
	self.ui = BasicUI()