def testNames(self): o = [1, 2, 3] p = sob.Persistent(o, "object") for style in "source pickle".split(): p.setStyle(style) p.save() o1 = sob.load("object.ta" + style[0], style) self.assertEqual(o, o1) for tag in "lala lolo".split(): p.save(tag) o1 = sob.load("object-" + tag + ".ta" + style[0], style) self.assertEqual(o, o1)
def testNames(self): o = [1,2,3] p = sob.Persistent(o, 'object') for style in 'source pickle'.split(): p.setStyle(style) p.save() o1 = sob.load('object.ta'+style[0], style) self.failUnlessEqual(o, o1) for tag in 'lala lolo'.split(): p.save(tag) o1 = sob.load('object-'+tag+'.ta'+style[0], style) self.failUnlessEqual(o, o1)
def testNames(self): o = [1, 2, 3] p = sob.Persistent(o, 'object') for style in 'source pickle'.split(): p.setStyle(style) p.save() o1 = sob.load('object.ta' + style[0], style) self.assertEqual(o, o1) for tag in 'lala lolo'.split(): p.save(tag) o1 = sob.load('object-' + tag + '.ta' + style[0], style) self.assertEqual(o, o1)
def testNames(self): o = [1,2,3] p = sob.Persistent(o, 'object') for style in 'xml source pickle'.split(): if style == 'xml' and not gotMicrodom: continue p.setStyle(style) p.save() o1 = sob.load('object.ta'+style[0], style) self.failUnlessEqual(o, o1) for tag in 'lala lolo'.split(): p.save(tag) o1 = sob.load('object-'+tag+'.ta'+style[0], style) self.failUnlessEqual(o, o1)
def test_saveEncryptedDeprecation(self): """ Persisting data with encryption is deprecated. """ tempDir = FilePath(self.mktemp()) tempDir.makedirs() persistedPath = tempDir.child('epersisttest.python') data = b'once I was the king of spain' persistance = sob.Persistent(data, 'test-data') persistance.save(filename=persistedPath.path, passphrase=b'some-pass') # Check deprecation message. warnings = self.flushWarnings([persistance._saveTemp]) self.assertEqual(1, len(warnings)) self.assertIs(DeprecationWarning, warnings[0]['category']) self.assertEqual( 'Saving encrypted persisted data is deprecated since ' 'Twisted 15.5.0', warnings[0]['message']) # Check that data is still valid, even if we are deprecating this # functionality. loadedData = sob.load( persistedPath.path, persistance.style, b'some-pass') self.assertEqual(data, loadedData) self.flushWarnings([sob.load])
def test_saveEncryptedDeprecation(self): """ Persisting data with encryption is deprecated. """ tempDir = FilePath(self.mktemp()) tempDir.makedirs() persistedPath = tempDir.child('epersisttest.python') data = b'once I was the king of spain' persistance = sob.Persistent(data, 'test-data') persistance.save(filename=persistedPath.path, passphrase=b'some-pass') # Check deprecation message. warnings = self.flushWarnings([persistance._saveTemp]) self.assertEqual(1, len(warnings)) self.assertIs(DeprecationWarning, warnings[0]['category']) self.assertEqual( 'Saving encrypted persisted data is deprecated since ' 'Twisted 15.5.0', warnings[0]['message']) # Check that data is still valid, even if we are deprecating this # functionality. loadedData = sob.load(persistedPath.path, persistance.style, b'some-pass') self.assertEqual(data, loadedData) self.flushWarnings([sob.load])
def testStyles(self): for o in objects: p = sob.Persistent(o, "") for style in "source pickle".split(): p.setStyle(style) p.save(filename="persisttest." + style) o1 = sob.load("persisttest." + style, style) self.assertEqual(o, o1)
def testStyles(self): for o in objects: p = sob.Persistent(o, '') for style in 'source pickle'.split(): p.setStyle(style) p.save(filename='persisttest.' + style) o1 = sob.load('persisttest.' + style, style) self.assertEqual(o, o1)
def testStyles(self): for o in objects: p = sob.Persistent(o, '') for style in 'source pickle'.split(): p.setStyle(style) p.save(filename='persisttest.'+style) o1 = sob.load('persisttest.'+style, style) self.failUnlessEqual(o, o1)
def restore(self): if not self.filename: raise exceptions.StateRestoreError, \ "It appears that state was never saved; cannot restore" if os.path.exists(self.filename): s = sob.load(self.filename, 'pickle') for key, val in s.__dict__.items(): setattr(self, key, val)
def testEncryptedStyles(self): for o in objects: phrase='once I was the king of spain' p = sob.Persistent(o, '') for style in 'source pickle'.split(): p.setStyle(style) p.save(filename='epersisttest.'+style, passphrase=phrase) o1 = sob.load('epersisttest.'+style, style, phrase) self.failUnlessEqual(o, o1)
def testEverythingEphemeralGetattr(self): """ Verify that _EverythingEphermal.__getattr__ works. """ self.fakeMain.testMainModGetattr = 1 dirname = self.mktemp() os.mkdir(dirname) filename = os.path.join(dirname, 'persisttest.ee_getattr') f = file(filename, 'w') f.write('import __main__\n') f.write('if __main__.testMainModGetattr != 1: raise AssertionError\n') f.write('app = None\n') f.close() sob.load(filename, 'source')
def testEverythingEphemeralSetattr(self): """ Verify that _EverythingEphemeral.__setattr__ won't affect __main__. """ self.fakeMain.testMainModSetattr = 1 dirname = self.mktemp() os.mkdir(dirname) filename = os.path.join(dirname, 'persisttest.ee_setattr') with open(filename, 'w') as f: f.write('import __main__\n') f.write('__main__.testMainModSetattr = 2\n') f.write('app = None\n') sob.load(filename, 'source') self.assertEqual(self.fakeMain.testMainModSetattr, 1)
def testEncryptedStyles(self): for o in objects: phrase = b'once I was the king of spain' p = sob.Persistent(o, '') for style in 'source pickle'.split(): p.setStyle(style) p.save(filename='epersisttest.' + style, passphrase=phrase) o1 = sob.load('epersisttest.' + style, style, phrase) self.assertEqual(o, o1)
def testEncryptedStyles(self): for o in objects: phrase = b"once I was the king of spain" p = sob.Persistent(o, "") for style in "source pickle".split(): p.setStyle(style) p.save(filename="epersisttest." + style, passphrase=phrase) o1 = sob.load("epersisttest." + style, style, phrase) self.assertEqual(o, o1)
def testStylesBeingSet(self): o = Dummy() o.foo = 5 o.setComponent(sob.IPersistable, sob.Persistent(o, "lala")) for style in "source pickle".split(): sob.IPersistable(o).setStyle(style) sob.IPersistable(o).save(filename="lala." + style) o1 = sob.load("lala." + style, style) self.assertEqual(o.foo, o1.foo) self.assertEqual(sob.IPersistable(o1).style, style)
def testStylesBeingSet(self): o = Dummy() o.foo = 5 o.setComponent(sob.IPersistable, sob.Persistent(o, 'lala')) for style in 'source pickle'.split(): sob.IPersistable(o).setStyle(style) sob.IPersistable(o).save(filename='lala.'+style) o1 = sob.load('lala.'+style, style) self.failUnlessEqual(o.foo, o1.foo) self.failUnlessEqual(sob.IPersistable(o1).style, style)
def testStyles(self): for o in objects: p = sob.Persistent(o, '') for style in 'xml source pickle'.split(): if style == 'xml' and not gotMicrodom: continue p.setStyle(style) p.save(filename='persisttest.'+style) o1 = sob.load('persisttest.'+style, style) self.failUnlessEqual(o, o1)
def testStylesBeingSet(self): o = Dummy() o.foo = 5 o.setComponent(sob.IPersistable, sob.Persistent(o, 'lala')) for style in 'source pickle'.split(): sob.IPersistable(o).setStyle(style) sob.IPersistable(o).save(filename='lala.' + style) o1 = sob.load('lala.' + style, style) self.assertEqual(o.foo, o1.foo) self.assertEqual(sob.IPersistable(o1).style, style)
def testEncryptedStyles(self): for o in objects: phrase = 'once I was the king of spain' p = sob.Persistent(o, '') for style in 'xml source pickle'.split(): if style == 'xml' and not gotMicrodom: continue p.setStyle(style) p.save(filename='epersisttest.' + style, passphrase=phrase) o1 = sob.load('epersisttest.' + style, style, phrase) self.failUnlessEqual(o, o1)
def testStylesBeingSet(self): o = Dummy() o.foo = 5 o.setComponent(sob.IPersistable, sob.Persistent(o, 'lala')) for style in 'xml source pickle'.split(): if style == 'xml' and not gotMicrodom: continue sob.IPersistable(o).setStyle(style) sob.IPersistable(o).save(filename='lala.'+style) o1 = sob.load('lala.'+style, style) self.failUnlessEqual(o.foo, o1.foo) self.failUnlessEqual(sob.IPersistable(o1).style, style)
def test_encryptedStyles(self): """ Data can be persisted with encryption for all the supported styles. """ for o in objects: phrase = b'once I was the king of spain' p = sob.Persistent(o, '') for style in 'source pickle'.split(): p.setStyle(style) p.save(filename='epersisttest.' + style, passphrase=phrase) o1 = sob.load('epersisttest.' + style, style, phrase) self.assertEqual(o, o1) self.flushWarnings([p._saveTemp, sob.load])
def test_encryptedStyles(self): """ Data can be persisted with encryption for all the supported styles. """ for o in objects: phrase = b'once I was the king of spain' p = sob.Persistent(o, '') for style in 'source pickle'.split(): p.setStyle(style) p.save(filename='epersisttest.'+style, passphrase=phrase) o1 = sob.load('epersisttest.'+style, style, phrase) self.assertEqual(o, o1) self.flushWarnings([p._saveTemp, sob.load])
def testEncryptedStyles(self): try: import Crypto except ImportError: raise unittest.SkipTest() for o in objects: phrase='once I was the king of spain' p = sob.Persistent(o, '') for style in 'xml source pickle'.split(): if style == 'xml' and not gotMicrodom: continue p.setStyle(style) p.save(filename='epersisttest.'+style, passphrase=phrase) o1 = sob.load('epersisttest.'+style, style, phrase) self.failUnlessEqual(o, o1)
def loadApplication(filename, kind, passphrase=None): """Load Application from a given file. The serialization format it was saved in should be given as C{kind}, and is one of 'pickle', 'source', 'xml' or 'python'. If C{passphrase} is given, the application was encrypted with the given passphrase. @type filename: C{str} @type kind: C{str} @type passphrase: C{str} """ if kind == 'python': application = sob.loadValueFromFile(filename, 'application', passphrase) else: application = sob.load(filename, kind, passphrase) return application
def loadApplication(filename, kind, passphrase=None): """ Load Application from a given file. The serialization format it was saved in should be given as C{kind}, and is one of C{pickle}, C{source}, C{xml} or C{python}. If C{passphrase} is given, the application was encrypted with the given passphrase. @type filename: C{str} @type kind: C{str} @type passphrase: C{str} """ if kind == "python": application = sob.loadValueFromFile(filename, "application") else: application = sob.load(filename, kind) return application
def loadApplication(filename, kind, passphrase=None): """Load Application from file @type filename: C{str} @type kind: C{str} @type passphrase: C{str} Load application from a given file. The serialization format it was saved in should be given as C{kind}, and is one of 'pickle', 'source', 'xml' or 'python'. If C{passphrase} is given, the application was encrypted with the given passphrase. """ if kind == 'python': application = sob.loadValueFromFile(filename, 'application', passphrase) else: application = sob.load(filename, kind, passphrase) if IService(application, None) is None: from twisted.application import compat application = compat.convert(application) return application
def testEverythingEphemeralGetattr(self): """ L{_EverythingEphermal.__getattr__} will proxy the __main__ module as an L{Ephemeral} object, and during load will be transparent, but after load will return L{Ephemeral} objects from any accessed attributes. """ self.fakeMain.testMainModGetattr = 1 dirname = self.mktemp() os.mkdir(dirname) filename = os.path.join(dirname, "persisttest.ee_getattr") global mainWhileLoading mainWhileLoading = None with open(filename, "w") as f: f.write( dedent( """ app = [] import __main__ app.append(__main__.testMainModGetattr == 1) try: __main__.somethingElse except AttributeError: app.append(True) else: app.append(False) from twisted.test import test_sob test_sob.mainWhileLoading = __main__ """ ) ) loaded = sob.load(filename, "source") self.assertIsInstance(loaded, list) self.assertTrue(loaded[0], "Expected attribute not set.") self.assertTrue(loaded[1], "Unexpected attribute set.") self.assertIsInstance(mainWhileLoading, Ephemeral) self.assertIsInstance(mainWhileLoading.somethingElse, Ephemeral) del mainWhileLoading
def test_loadEncryptedDeprecation(self): """ Loading encrypted persisted data is deprecated. """ tempDir = FilePath(self.mktemp()) tempDir.makedirs() persistedPath = tempDir.child('epersisttest.python') data = b'once I was the king of spain' persistance = sob.Persistent(data, 'test-data') persistance.save(filename=persistedPath.path, passphrase=b'some-pass') # Clean all previous warnings as save will also raise a warning. self.flushWarnings([persistance._saveTemp]) loadedData = sob.load(persistedPath.path, persistance.style, b'some-pass') self.assertEqual(data, loadedData) warnings = self.flushWarnings([sob.load]) self.assertEqual(1, len(warnings)) self.assertIs(DeprecationWarning, warnings[0]['category']) self.assertEqual( 'Loading encrypted persisted data is deprecated since ' 'Twisted 15.5.0', warnings[0]['message'])
def loadApplication(filename, kind, passphrase=None): """ Load Application from a given file. The serialization format it was saved in should be given as C{kind}, and is one of C{pickle}, C{source}, C{xml} or C{python}. If C{passphrase} is given, the application was encrypted with the given passphrase. @type filename: C{str} @type kind: C{str} @type passphrase: C{str} """ # FIXME: https://twistedmatrix.com/trac/ticket/7827 # twisted.persisted is not yet ported to Python 3, so import it here. from twisted.persisted import sob if kind == 'python': application = sob.loadValueFromFile(filename, 'application', passphrase) else: application = sob.load(filename, kind, passphrase) return application
def test_loadEncryptedDeprecation(self): """ Loading encrypted persisted data is deprecated. """ tempDir = FilePath(self.mktemp()) tempDir.makedirs() persistedPath = tempDir.child('epersisttest.python') data = b'once I was the king of spain' persistance = sob.Persistent(data, 'test-data') persistance.save(filename=persistedPath.path, passphrase=b'some-pass') # Clean all previous warnings as save will also raise a warning. self.flushWarnings([persistance._saveTemp]) loadedData = sob.load( persistedPath.path, persistance.style, b'some-pass') self.assertEqual(data, loadedData) warnings = self.flushWarnings([sob.load]) self.assertEqual(1, len(warnings)) self.assertIs(DeprecationWarning, warnings[0]['category']) self.assertEqual( 'Loading encrypted persisted data is deprecated since ' 'Twisted 15.5.0', warnings[0]['message'])