def test_json_to_json(self): with TemporaryDirectory() as temp_dir: target = "{}/out.json".format(temp_dir) params = { "source": {"filename": "tests/configs/things.json"}, "destination": {"output_file": target} } apply_chain(params, temp_dir) with open(target, 'r') as f: data = json.load(f) assert len(data["tables"]["countries"]["columns"]) == 2 assert data["tables"]["countries"]["rows"][1]["code"] == ""
def test_fill(self): with TemporaryDirectory() as temp_dir: target = "{}/out.json".format(temp_dir) params = { "source": {"filename": "tests/configs/fill.json"}, "flags": { "geocoder": "dummy", "address": {"countries": ["country"]} }, "destination": {"output_file": target} } apply_chain(params, temp_dir) with open(target, 'r') as f: data = json.load(f) assert data["tables"]["countries"]["rows"][0]["zip"] == "PO-STAL"
def run(argv): parser = argparse.ArgumentParser(description='Run a website from a spreadsheet. ' 'Take a spreadsheet (from google sheets or locally), and ' 'convert it to a .json file that a static website ' 'generator like jekyll can use. Optionally strip private ' 'information and add derived geographic fields like ' 'latitude and longitude.') parser.add_argument('--config', nargs='*', required=False, default=['_sheetsite.yml', '_sheetsite.json'], help='name of configuration file.') parser.add_argument('--cache-dir', nargs=1, required=False, default=['_cache'], help='name of default cache directory.') args = parser.parse_args(argv) config_file = None for config_candidate in args.config: if os.path.exists(config_candidate): config_file = config_candidate break if not config_file: print("Could not find config file", args.config) exit(1) params = load_config(config_file) files = apply_chain(params, args.cache_dir[0]) diff = compute_diff(files, 'ansi') print(diff)
def test_multiple_to_multiple_add(self): with TemporaryDirectory() as temp_dir: target = "{}/out.json".format(temp_dir) params = { "source": {"filename": "tests/configs/things.json"}, "flags": { "geocoder": "dummy", "address": {"countries": ["code", "country", "Earth"]}, "add": {"countries": ["city", "address"]} }, "destination": {"output_file": target} } apply_chain(params, temp_dir) with open(target, 'r') as f: data = json.load(f) assert data["tables"]["countries"]["rows"][0]["city"] == "Cityville" assert data["tables"]["countries"]["rows"][0]["address"] == "uk United Kingdom Earth"
def test_rename(self): with TemporaryDirectory() as temp_dir: target = "{}/out.json".format(temp_dir) params = { "source": {"filename": "tests/configs/multirow.json"}, "flags": { "geocoder": "dummy", "rename": {"places": {"web": "website"}}, "address": {"places": ["street", "city", "state", "country"]}, "add": {"places": ["lat", "lon", "address"]} }, "destination": {"output_file": target} } apply_chain(params, temp_dir) with open(target, 'r') as f: data = json.load(f) places = data["tables"]["places"]["rows"] self.assertIn('website', places[0]) self.assertNotIn('web', places[0])
def test_multirow(self): with TemporaryDirectory() as temp_dir: target = "{}/out.json".format(temp_dir) params = { "source": {"filename": "tests/configs/multirow.json"}, "flags": { "geocoder": "dummy", "group": "web", "address": {"places": ["street", "city", "state", "country"]}, "add": {"places": ["lat", "lon", "address"]} }, "destination": {"output_file": target} } apply_chain(params, temp_dir) with open(target, 'r') as f: data = json.load(f) places = data["tables"]["places"]["rows"] self.assertEqual(places[0]["address"], "Test1 Test2") self.assertEqual(places[1]["address"], "Test1") self.assertEqual(places[2]["address"], "305 Memorial Dr Cambridge Massachusetts United States") self.assertEqual(places[3]["address"], "306 Memorial Dr Cambridge Massachusetts United States")
def update_site(params, path, site, name): source = site['source'] destination = site['destination'] site_params = { 'name': params.get('title', 'unknown'), 'sheet_link': source.get('link', None), 'site_link': destination.get('link', None) } files = apply_chain(site, path) diff_html = compute_diff(files) notify_all.delay(name=name, site_params=site_params, diff_html=diff_html) return True
def update_site(params, path, site, name): source = site['source'] destination = site['destination'] site_params = { 'name': params.get('title', None), 'who': params.get('who', None), 'sheet_link': source.get('link', None), 'site_link': destination.get('link', None), 'no_notify': params['no_notify'] } files = apply_chain(site, path) diff_html, diff_text = compute_diff(files, format='both') from sheetsite.tasks.notify import notify_all notify_all.delay(name=name, site_params=site_params, diff_html=diff_html, diff_text=diff_text) return True