def declare(self, data, loop=None): super().declare(data, loop) events = (i for i in self._services.values() if isinstance(i, Persistent.RSON)) for each in events: path = Persistent.make_path( Persistent.recent_slot(each.path)._replace(file=each.path.file) ) fP = os.path.join(*path) with Expert.declaration(fP) as output: for i in data.get(each.attr, []): try: Assembly.dump(i, output, indent=0) except Exception as e: self._log.error(". ".join(getattr(e, "args", e) or e)) finally: output.write("\n") pickles = (i for i in self._services.values() if isinstance(i, Persistent.Pickled) and data.get(i.name, False)) for p in pickles: fP = os.path.join(*p.path) with open(fP, "wb") as fObj: pickle.dump(data[p.name], fObj, 4)
def test_content_goes_to_file_object(self): fP = os.path.join( DeclarationTests.drcty, DeclarationTests.node) fObj = StringIO() self.assertFalse(os.path.isfile(fP)) with Expert.declaration(fObj) as output: json.dump("Test string", output) self.assertFalse(os.path.isfile(fP)) self.assertEqual('"Test string"', fObj.getvalue())
def test_content_goes_to_named_file(self): fP = os.path.join( DeclarationTests.drcty, DeclarationTests.node) self.assertFalse(os.path.isfile(fP)) with Expert.declaration(fP) as output: json.dump("Test string", output) self.assertTrue(os.path.isfile(fP)) with open(fP, 'r') as check: self.assertEqual('"Test string"', check.read())