1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 #
18 # $Id: instance.py,v 1.12 2004-02-11 23:55:08 richard Exp $
20 '''Tracker handling (open tracker).
22 Backwards compatibility for the old-style "imported" trackers.
23 '''
24 __docformat__ = 'restructuredtext'
26 import os
28 class Vars:
29 ''' I'm just a container '''
31 class Tracker:
32 def __init__(self, tracker_home):
33 self.tracker_home = tracker_home
34 self.select_db = self._load_python('select_db.py')
35 self.config = self._load_config('config.py')
36 raise NotImplemented, 'this is *so* not finished'
37 self.init = XXX
38 self.Client = XXX
39 self.MailGW = XXX
41 def open(self):
42 return self._load_config('schema.py').db
43 self._load_config('security.py', db=db)
46 def _load_python(self, file):
47 file = os.path.join(tracker_home, file)
48 vars = Vars()
49 execfile(file, vars.__dict__)
50 return vars
53 class TrackerError(Exception):
54 pass
57 class OldStyleTrackers:
58 def __init__(self):
59 self.number = 0
60 self.trackers = {}
62 def open(self, tracker_home):
63 ''' Open the tracker.
65 Raise ValueError if the tracker home doesn't exist.
66 '''
67 import imp
68 # sanity check existence of tracker home
69 if not os.path.exists(tracker_home):
70 raise ValueError, 'no such directory: "%s"'%tracker_home
72 # sanity check tracker home contents
73 for reqd in 'config dbinit select_db interfaces'.split():
74 if not os.path.exists(os.path.join(tracker_home, '%s.py'%reqd)):
75 raise TrackerError, 'File "%s.py" missing from tracker '\
76 'home "%s"'%(reqd, tracker_home)
78 if self.trackers.has_key(tracker_home):
79 return imp.load_package(self.trackers[tracker_home],
80 tracker_home)
81 self.number = self.number + 1
82 modname = '_roundup_tracker_%s'%self.number
83 self.trackers[tracker_home] = modname
85 # load the tracker
86 tracker = imp.load_package(modname, tracker_home)
88 # ensure the tracker has all the required bits
89 for required in 'config open init Client MailGW'.split():
90 if not hasattr(tracker, required):
91 raise TrackerError, \
92 'Required tracker attribute "%s" missing'%required
94 return tracker
96 OldStyleTrackers = OldStyleTrackers()
97 def open(tracker_home):
98 if os.path.exists(os.path.join(tracker_home, 'dbinit.py')):
99 # user should upgrade...
100 return OldStyleTrackers.open(tracker_home)
102 return Tracker(tracker_home)
104 # vim: set filetype=python ts=4 sw=4 et si