def _save_json(self, result, repo, file_name=None): """for a result that is a dictionary or list, save as json Parameters ========== result: the result object to save, should be dict or list """ file_name = self.params.get("file_name", file_name) or "result.json" destination = os.path.join(repo, self.name, file_name) write_json(result, destination) return destination
def test_write_read_files(self): """test_write_read_files will test the functions write_file and read_file """ print("Testing utils.write_file") from watchme.utils import write_file tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) write_file(tmpfile, "mocos!") self.assertTrue(os.path.exists(tmpfile)) print("Testing utils.read_file...") from watchme.utils import read_file content = read_file(tmpfile)[0] self.assertEqual("mocos!", content) from watchme.utils import write_json print("Testing utils.write_json.") print("...Case 1: Providing bad json") bad_json = {"IWuvWaffles?'}": [{True}, "2", 3]} tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) with self.assertRaises(TypeError): write_json(bad_json, tmpfile) print("...Case 2: Providing good json") good_json = {"IWuvWaffles!": [True, "2", 3]} tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) write_json(good_json, tmpfile) with open(tmpfile, "r") as filey: content = json.loads(filey.read()) self.assertTrue(isinstance(content, dict)) self.assertTrue("IWuvWaffles!" in content) print("Testing utils.print_json") from watchme.utils import print_json result = print_json({1: 1}) self.assertEqual('{\n "1": 1\n}', result)
def main(args, extra): """export temporal data for a watcher """ # Required - will print help if not provided name = args.watcher[0] task = args.task[0] filename = args.filename[0] if not task.startswith("task") and not task.startswith("decorator"): example = "watchme export watcher task-reddit result.txt" bot.exit('Task name must start with "task" or "decorator": %s' % example) # Use the output file, or a temporary file out = args.out # Get the watcher to interact with, must already exist watcher = get_watcher(name, base=args.base, create=False) if out is not None: if os.path.exists(out) and args.force is False: bot.exit("%s exists! Use --force to overwrite." % out) # Export the data to file result = watcher.export_dict(task=task, filename=filename, name=name, export_json=args.json, base=args.base) if result is not None: if out is None: print(json.dumps(result, indent=4)) else: write_json(result, out) bot.info("Result written to %s" % out)
def main(args, extra): '''activate one or more watchers ''' # Required - will print help if not provided name = args.watcher[0] task = args.task[0] filename = args.filename[0] if not task.startswith('task'): example = 'watchme add watcher task-reddit url@https://www.reddit.com' bot.exit('Task name must start with "task", e.g., %s' % example) # Use the output file, or a temporary file out = args.out # Get the watcher to interact with, must already exist watcher = get_watcher(name, base=args.base, create=False) if out is not None: if os.path.exists(out) and args.force is False: bot.exit('%s exists! Use --force to overwrite.' % out) # Export the data to file result = watcher.export_dict(task=task, filename=filename, name=name, export_json=args.json, base=args.base) if result != None: if out == None: print(json.dumps(result, indent=4)) else: write_json(result, out) bot.info('Result written to %s' % out)