def test_from_invalid_json(self): # Invalid JSON with capture_output(): jd = JSONObjectDict.from_json(JSONObject, '{"foo":{}') self.failIf(jd) # Valid but unexpected Command field self.failIf(JSONObjectDict.from_json(JSONObject, '{"bar":{"name":"bar", "invalid":"foo"}'))
def setUp(self): module = self.modules["CustomCommands"] globals().update(vars(module)) self.plugin = self.plugins["CustomCommands"].cls config.init() self.cmd_list = CustomCommands.DEFAULT_COMS self.commands = JSONObjectDict.from_list(self.cmd_list)
def test_from_list(self): baz_man = JSONObject("baz man!") lst = [JSONObject("foo"), JSONObject("bar"), baz_man] data = JSONObjectDict.from_list(lst) self.failUnlessEqual(len(data), len(lst)) self.failUnless("baz man!" in data) self.failUnlessEqual(baz_man, data["baz man!"])
def test_save_all(self): data = JSONObjectDict.from_json(self.WibbleData, self.WIBBLE_JSON_STR) fd, filename = mkstemp(suffix=".json") os.close(fd) try: ret = data.save(filename) with open(filename, "rb") as f: jstr = f.read() # Check we also return the string as documented... self.failUnlessEqual(jstr, ret) finally: os.unlink(filename) # Check we have the right number of items self.failUnlessEqual(len(json.loads(jstr)), len(data)) # Check them one by one (for convenience of debugging) parsed = JSONObjectDict.from_json(self.WibbleData, jstr) for o in data.values(): self.failUnlessEqual(parsed[o.name], o)
def _get_saved_commands(cls): filename = cls.COMS_FILE print_d("Loading saved commands from '%s'..." % filename) coms = None try: with open(filename, "r", encoding="utf-8") as f: coms = JSONObjectDict.from_json(Command, f.read()) except (IOError, ValueError) as e: print_w("Couldn't parse saved commands (%s)" % e) # Failing all else... if not coms: print_d("No commands found in %s. Using defaults." % filename) coms = {c.name: c for c in cls.DEFAULT_COMS} print_d("Loaded commands: %s" % coms.keys()) return coms
def _get_saved_searches(cls): filename = cls.COMS_FILE print_d("Loading saved commands from '%s'..." % filename) coms = None try: with open(filename) as f: coms = JSONObjectDict.from_json(Command, f.read()) except IOError: pass except ValueError as e: print_w("Couldn't parse saved commands (%s)" % e) # Failing all else... if not coms: print_d("No commands found in %s. Using defaults." % filename) coms = dict([(c.name, c) for c in cls.DEFAULT_COMS]) print_d("Loaded commands: %s" % coms.keys()) return coms
def test_subclass_from_json(self): coms = JSONObjectDict.from_json(self.WibbleData, self.WIBBLE_JSON_STR) self.failUnlessEqual(len(coms), 2) self.failUnlessEqual(coms['foo'].__class__, self.WibbleData)
def __finish(self, widget): # TODO: Warn about impending deletion of nameless items, or something all = JSONObjectDict.from_list( [row[0] for row in self.model if row[0].name]) all.save(filename=self.filename)
def setUp(self): globals().update(vars(self.modules["CustomCommands"])) config.init() self.cmd_list = CustomCommands.DEFAULT_COMS self.commands = JSONObjectDict.from_list(self.cmd_list)