~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/persisted/sob.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
    import cStringIO as StringIO
20
20
except ImportError:
21
21
    import StringIO
22
 
from twisted.python import components, log, runtime
 
22
from twisted.python import log, runtime
23
23
from twisted.persisted import styles
24
 
from zope.interface import implements
 
24
from zope.interface import implements, Interface
25
25
 
26
26
# Note:
27
27
# These encrypt/decrypt functions only work for data formats
39
39
    return AES.new(md5.new(passphrase).digest()[:16]).decrypt(data)
40
40
 
41
41
 
42
 
class IPersistable(components.Interface):
 
42
class IPersistable(Interface):
43
43
 
44
44
    """An object which can be saved in several formats to a file"""
45
45
 
46
 
    def setStyle(self, style):
 
46
    def setStyle(style):
47
47
        """Set desired format.
48
48
 
49
49
        @type style: string (one of 'pickle', 'source' or 'xml')
50
50
        """
51
51
 
52
 
    def save(self, tag=None, filename=None, passphrase=None):
 
52
    def save(tag=None, filename=None, passphrase=None):
53
53
        """Save object to file.
54
54
 
55
55
        @type tag: string
135
135
 
136
136
    initRun = 0
137
137
 
 
138
    def __init__(self, mainMod):
 
139
        """
 
140
        @param mainMod: The '__main__' module that this class will proxy.
 
141
        """
 
142
        self.mainMod = mainMod
 
143
 
138
144
    def __getattr__(self, key):
139
145
        try:
140
 
            return getattr(mainMod, key)
 
146
            return getattr(self.mainMod, key)
141
147
        except AttributeError:
142
148
            if self.initRun:
143
149
                raise
157
163
    """
158
164
    mode = 'r'
159
165
    if style=='source':
160
 
        from twisted.persisted.aot import unjellyFromSource as load
 
166
        from twisted.persisted.aot import unjellyFromSource as _load
161
167
    elif style=='xml':
162
 
        from twisted.persisted.marmalade import unjellyFromXML as load
 
168
        from twisted.persisted.marmalade import unjellyFromXML as _load
163
169
    else:
164
 
        load, mode = pickle.load, 'rb'
 
170
        _load, mode = pickle.load, 'rb'
165
171
    if passphrase:
166
172
        fp = StringIO.StringIO(_decrypt(passphrase,
167
173
                                        open(filename, 'rb').read()))
168
174
    else:
169
175
        fp = open(filename, mode)
170
 
    mainMod = sys.modules['__main__']
171
 
    ee = _EverythingEphemeral()
 
176
    ee = _EverythingEphemeral(sys.modules['__main__'])
172
177
    sys.modules['__main__'] = ee
173
178
    ee.initRun = 1
174
 
    value = load(fp)
175
 
    sys.modules['__main__'] = mainMod
 
179
    try:
 
180
        value = _load(fp)
 
181
    finally:
 
182
        # restore __main__ if an exception is raised.
 
183
        sys.modules['__main__'] = ee.mainMod
 
184
 
176
185
    styles.doUpgrade()
177
186
    ee.initRun = 0
178
187
    persistable = IPersistable(value, None)