def main(running_with_paster=False): """ ckanapi command line entry point """ arguments = parse_arguments() if not running_with_paster and not arguments['--remote']: return _switch_to_paster(arguments) if arguments['--remote']: ckan = RemoteCKAN(arguments['--remote'], apikey=arguments['--apikey'], user_agent="ckanapi-cli/{version} (+{url})".format( version=__version__, url='https://github.com/open-data/ckanapi'), get_only=arguments['--get-request'], ) else: ckan = LocalCKAN(username=arguments['--ckan-user']) if arguments['action']: for r in action(ckan, arguments): sys.stdout.write(r) return things = ['datasets', 'groups', 'organizations'] thing = [x for x in things if arguments[x]] if arguments['load']: assert len(thing) == 1, thing return load_things(ckan, thing[0], arguments) if arguments['dump']: assert len(thing) == 1, thing return dump_things(ckan, thing[0], arguments) assert 0, arguments # we shouldn't be here
def main(running_with_paster=False): """ ckanapi command line entry point """ arguments = parse_arguments() if not running_with_paster and not arguments['--remote']: return _switch_to_paster(arguments) if arguments['--remote']: ckan = RemoteCKAN( arguments['--remote'], apikey=arguments['--apikey'], user_agent="ckanapi-cli/{version} (+{url})".format( version=__version__, url='https://github.com/open-data/ckanapi'), get_only=arguments['--get-request'], ) else: ckan = LocalCKAN(username=arguments['--ckan-user']) if arguments['action']: try: for r in action(ckan, arguments): sys.stdout.write(r) return except CLIError, e: sys.stderr.write(e.args[0] + '\n') return 1
def main(running_with_paster=False): """ ckanapi command line entry point """ arguments = parse_arguments() if not running_with_paster and not arguments["--remote"]: return _switch_to_paster(arguments) if arguments["--remote"]: ckan = RemoteCKAN( arguments["--remote"], apikey=arguments["--apikey"], user_agent="ckanapi-cli/{version} (+{url})".format( version=__version__, url="https://github.com/open-data/ckanapi" ), get_only=arguments["--get-request"], ) else: ckan = LocalCKAN(username=arguments["--ckan-user"]) if arguments["action"]: try: for r in action(ckan, arguments): sys.stdout.write(r) return except CLIError, e: sys.stderr.write(e.args[0] + "\n") return 1
def test_key_json(self): ckan = MockCKAN('shake_it', {'who': ['just', 'me']}, "yeah") rval = action(ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who:["just", "me"]'], '--output-json': False, '--output-jsonl': False, '--input-json': False, '--input': None, }) self.assertEqual(b''.join(rval), b'"yeah"\n')
def test_jsonl(self): ckan = MockCKAN('shake_it', {'who': 'me'}, [99,98,97]) rval = action(ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who=me'], '--output-json': False, '--output-jsonl': True, '--input-json': False, '--input': None, }) self.assertEqual(b''.join(rval), b'99\n98\n97\n')
def test_compact_fallback(self): ckan = MockCKAN('shake_it', {'who': 'me'}, {"oh": ["right", "on"]}) rval = action(ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who=me'], '--output-json': False, '--output-jsonl': True, '--input-json': False, '--input': None, }) self.assertEqual(b''.join(rval), b'{"oh":["right","on"]}\n')
def test_key_json_or_string(self): ckan = MockCKAN('shake_it', {'who': 'me:you'}, "yeah") rval = action(ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who=me:you'], '--output-json': False, '--output-jsonl': False, '--input-json': False, '--input': None, }) self.assertEqual(b''.join(rval), b'"yeah"\n')
def test_bad_key_json(self): ckan = MockCKAN('shake_it', {'who': 'me'}, "yeah") rval = action(ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who:me'], '--output-json': False, '--output-jsonl': False, '--input-json': False, '--input': None, }) self.assertRaises(CLIError, list, rval)
def test_key_json(self): ckan = MockCKAN('shake_it', {'who': ['just', 'me']}, "yeah") rval = action( ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who:["just", "me"]'], '--output-json': False, '--output-jsonl': False, '--input-json': False, '--input': None, }) self.assertEqual(b''.join(rval), b'"yeah"\n')
def test_bad_key_json(self): ckan = MockCKAN('shake_it', {'who': 'me'}, "yeah") rval = action( ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who:me'], '--output-json': False, '--output-jsonl': False, '--input-json': False, '--input': None, }) self.assertRaises(CLIError, list, rval)
def test_key_json_or_string(self): ckan = MockCKAN('shake_it', {'who': 'me:you'}, "yeah") rval = action( ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who=me:you'], '--output-json': False, '--output-jsonl': False, '--input-json': False, '--input': None, }) self.assertEqual(b''.join(rval), b'"yeah"\n')
def test_compact_fallback(self): ckan = MockCKAN('shake_it', {'who': 'me'}, {"oh": ["right", "on"]}) rval = action( ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who=me'], '--output-json': False, '--output-jsonl': True, '--input-json': False, '--input': None, }) self.assertEqual(b''.join(rval), b'{"oh":["right","on"]}\n')
def test_jsonl(self): ckan = MockCKAN('shake_it', {'who': 'me'}, [99, 98, 97]) rval = action( ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who=me'], '--output-json': False, '--output-jsonl': True, '--input-json': False, '--input': None, }) self.assertEqual(b''.join(rval), b'99\n98\n97\n')
def test_compact(self): ckan = MockCKAN('shake_it', {'who': 'me'}, ["right", "on"]) rval = action( ckan, { 'ACTION_NAME': 'shake_it', 'KEY=VALUE': ['who=me'], '--output-json': True, '--output-jsonl': False, '--input-json': False, '--input': None, }) self.assertEqual(b''.join(rval), b'["right","on"]\n')
def main(running_with_paster=False): """ ckanapi command line entry point """ arguments = parse_arguments() if not running_with_paster and not arguments['--remote']: return _switch_to_paster(arguments) if arguments['--remote']: ckan = RemoteCKAN( arguments['--remote'], apikey=arguments['--apikey'], user_agent="ckanapi-cli/{version} (+{url})".format( version=__version__, url='https://github.com/open-data/ckanapi'), get_only=arguments['--get-request'], ) else: ckan = LocalCKAN(username=arguments['--ckan-user']) stdout = getattr(sys.stdout, 'buffer', sys.stdout) if arguments['action']: try: for r in action(ckan, arguments): stdout.write(r) return except CLIError as e: sys.stderr.write(e.args[0] + '\n') return 1 things = ['datasets', 'groups', 'organizations', 'users', 'related'] thing = [x for x in things if arguments[x]] if (arguments['load'] or arguments['dump'] or arguments['delete'] ) and arguments['--processes'] != '1' and os.name == 'nt': sys.stderr.write( "multiple worker processes are not supported on windows\n") arguments['--processes'] = '1' if arguments['load']: return load_things(ckan, thing[0], arguments) if arguments['dump']: return dump_things(ckan, thing[0], arguments) if arguments['delete']: return delete_things(ckan, thing[0], arguments) if arguments['search']: return search_datasets(ckan, arguments) assert 0, arguments # we shouldn't be here
def test_stdin_json(self): ckan = MockCKAN('shake_it', {'who': ['just', 'me']}, "yeah") rval = action(ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who=me'], '--output-json': False, '--output-jsonl': False, '--input-json': True, '--input': None, }, stdin=BytesIO(b'{"who":["just","me"]}'), ) self.assertEqual(b''.join(rval), b'"yeah"\n')
def test_compact_fallback(self): ckan = MockCKAN("shake_it", {"who": "me"}, {"oh": ["right", "on"]}) rval = action( ckan, { "ACTION_NAME": "shake_it", "KEY=VALUE": ["who=me"], "--output-json": False, "--output-jsonl": True, "--input-json": False, "--input": None, }, ) self.assertEqual(b"".join(rval), b'{"oh":["right","on"]}\n')
def test_jsonl(self): ckan = MockCKAN("shake_it", {"who": "me"}, [99, 98, 97]) rval = action( ckan, { "ACTION_NAME": "shake_it", "KEY=VALUE": ["who=me"], "--output-json": False, "--output-jsonl": True, "--input-json": False, "--input": None, }, ) self.assertEqual(b"".join(rval), b"99\n98\n97\n")
def test_stdin_json(self): ckan = MockCKAN('shake_it', {'who': ['just', 'me']}, "yeah") rval = action( ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who=me'], '--output-json': False, '--output-jsonl': False, '--input-json': True, '--input': None, }, stdin=BytesIO(b'{"who":["just","me"]}'), ) self.assertEqual(b''.join(rval), b'"yeah"\n')
def test_stdin_json(self): ckan = MockCKAN("shake_it", {"who": ["just", "me"]}, "yeah") rval = action( ckan, { "ACTION_NAME": "shake_it", "KEY=VALUE": ["who=me"], "--output-json": False, "--output-jsonl": False, "--input-json": True, "--input": None, }, stdin=BytesIO(b'{"who":["just","me"]}'), ) self.assertEqual(b"".join(rval), b'"yeah"\n')
def main(running_with_paster=False): """ ckanapi command line entry point """ arguments = parse_arguments() if not running_with_paster and not arguments['--remote']: return _switch_to_paster(arguments) if arguments['--remote']: ckan = RemoteCKAN(arguments['--remote'], apikey=arguments['--apikey'], user_agent="ckanapi-cli/{version} (+{url})".format( version=__version__, url='https://github.com/open-data/ckanapi'), get_only=arguments['--get-request'], ) else: ckan = LocalCKAN(username=arguments['--ckan-user']) stdout = getattr(sys.stdout, 'buffer', sys.stdout) if arguments['action']: try: for r in action(ckan, arguments): stdout.write(r) return except CLIError as e: sys.stderr.write(e.args[0] + '\n') return 1 things = ['datasets', 'groups', 'organizations', 'users', 'related'] thing = [x for x in things if arguments[x]] if (arguments['load'] or arguments['dump'] or arguments['delete'] ) and arguments['--processes'] != '1' and os.name == 'nt': sys.stderr.write( "multiple worker processes are not supported on windows\n") arguments['--processes'] = '1' if arguments['load']: return load_things(ckan, thing[0], arguments) if arguments['dump']: return dump_things(ckan, thing[0], arguments) if arguments['delete']: return delete_things(ckan, thing[0], arguments) assert 0, arguments # we shouldn't be here
def test_pretty(self): ckan = MockCKAN('shake_it', {'who': 'me'}, {"oh": ["right", "on"]}) rval = action(ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who=me'], '--output-json': False, '--output-jsonl': False, '--input-json': False, '--input': None, }) self.assertEqual(b''.join(rval), b""" { "oh": [ "right", "on" ] } """.lstrip())
def test_pretty(self): ckan = MockCKAN('shake_it', {'who': 'me'}, {"oh": ["right", "on"]}) rval = action( ckan, { 'ACTION_NAME': 'shake_it', 'KEY=STRING': ['who=me'], '--output-json': False, '--output-jsonl': False, '--input-json': False, '--input': None, }) self.assertEqual( b''.join(rval), b""" { "oh": [ "right", "on" ] } """.lstrip())
def main(running_with_paster=False): """ ckanapi command line entry point """ arguments = parse_arguments() if not running_with_paster and not arguments["--remote"]: return _switch_to_paster(arguments) if arguments["--remote"]: ckan = RemoteCKAN( arguments["--remote"], apikey=arguments["--apikey"], user_agent="ckanapi-cli/{version} (+{url})".format( version=__version__, url="https://github.com/open-data/ckanapi" ), get_only=arguments["--get-request"], ) else: ckan = LocalCKAN(username=arguments["--ckan-user"]) if arguments["action"]: for r in action(ckan, arguments): sys.stdout.write(r) return things = ["datasets", "groups", "organizations"] thing = [x for x in things if arguments[x]] if (arguments["load"] or arguments["dump"]) and arguments["--processes"] != "1" and os.name == "nt": sys.stderr.write("multiple worker processes are not supported on windows\n") arguments["--processes"] = "1" if arguments["load"]: assert len(thing) == 1, thing return load_things(ckan, thing[0], arguments) if arguments["dump"]: assert len(thing) == 1, thing return dump_things(ckan, thing[0], arguments) assert 0, arguments # we shouldn't be here
def test_pretty(self): ckan = MockCKAN("shake_it", {"who": "me"}, {"oh": ["right", "on"]}) rval = action( ckan, { "ACTION_NAME": "shake_it", "KEY=VALUE": ["who=me"], "--output-json": False, "--output-jsonl": False, "--input-json": False, "--input": None, }, ) self.assertEqual( b"".join(rval), b""" { "oh": [ "right", "on" ] } """.lstrip(), )