示例#1
0
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
示例#2
0
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
示例#3
0
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
示例#4
0
 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')
示例#5
0
 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')
示例#6
0
 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')
示例#7
0
 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')
示例#8
0
 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)
示例#9
0
 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')
示例#10
0
 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)
示例#11
0
 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')
示例#12
0
 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')
示例#13
0
 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')
示例#14
0
 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')
示例#15
0
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
示例#16
0
 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')
示例#17
0
 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')
示例#18
0
 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")
示例#19
0
 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')
示例#20
0
 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')
示例#21
0
文件: main.py 项目: ckan/ckanapi
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
示例#22
0
    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())
示例#23
0
    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())
示例#24
0
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
示例#25
0
    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(),
        )