Exemple #1
0
    def __init__(self, url=None, spec=None):
        self.url = url
        self.headers = {}
        self.querystring_params = {}
        self.body_params = {}
        self.body_json_params = {}
        self.options = {}
        self.should_exit = False

        # Create a tree for supporting API spec and ls command
        self.root = Node('root')
        if spec:
            paths = spec.get('paths')
            if paths:
                for path in paths:
                    path_tokens = list(filter(lambda s: s, path.split('/')))
                    self.root.add_path(*path_tokens)
                    endpoint = paths[path]
                    for method, info in endpoint.items():
                        params = info.get('parameters')
                        if params:
                            for param in params:
                                if param.get('in') != 'path':
                                    full_path = path_tokens + [param['name']]
                                    self.root.add_path(*full_path,
                                                       node_type='file')
Exemple #2
0
    def test_eq(self):
        a = Node('a', data={'type': 'file'})
        b = Node('b', data={'type': 'dir'})
        self.assertNotEqual(a, b)

        a = Node('a', data={'type': 'file'})
        b = Node('a', data={'type': 'file'})
        self.assertEqual(a, b)
Exemple #3
0
class Context(object):
    def __init__(self, url=None, spec=None):
        self.url = url
        self.headers = {}
        self.querystring_params = {}
        self.body_params = {}
        self.body_json_params = {}
        self.options = {}
        self.should_exit = False

        # Create a tree for supporting API spec and ls command
        self.root = Node('root')
        if spec:
            paths = spec.get('paths')
            if paths:
                for path in paths:
                    path_tokens = list(filter(lambda s: s, path.split('/')))
                    self.root.add_path(*path_tokens)
                    endpoint = paths[path]
                    for method, info in endpoint.items():
                        params = info.get('parameters')
                        if params:
                            for param in params:
                                if param.get('in') != 'path':
                                    full_path = path_tokens + [param['name']]
                                    self.root.add_path(*full_path,
                                                       node_type='file')

    def __eq__(self, other):
        return (self.url == other.url and self.headers == other.headers
                and self.options == other.options
                and self.querystring_params == other.querystring_params
                and self.body_params == other.body_params
                and self.body_json_params == other.body_json_params
                and self.should_exit == other.should_exit)

    def copy(self):
        context = Context(self.url)
        context.headers = self.headers.copy()
        context.querystring_params = self.querystring_params.copy()
        context.body_params = self.body_params.copy()
        context.body_json_params = self.body_json_params.copy()
        context.options = self.options.copy()
        context.should_exit = self.should_exit
        return context

    def update(self, context):
        if context.url:
            self.url = context.url

        self.headers.update(context.headers)
        self.querystring_params.update(context.querystring_params)
        self.body_params.update(context.body_params)
        self.body_json_params.update(context.body_json_params)
        self.options.update(context.options)
        self.should_exit = self.should_exit
Exemple #4
0
    def __init__(self, url=None, spec=None):
        self.url = url
        self.headers = {}
        self.querystring_params = {}
        self.body_params = {}
        self.body_json_params = {}
        self.options = {}
        self.should_exit = False

        # Create a tree for supporting API spec and ls command
        self.root = Node('root')
        if spec:
            if not self.url:
                schemes = spec.get('schemes')
                scheme = schemes[0] if schemes else 'https'
                self.url = (scheme + '://' +
                            spec.get('host', 'http://localhost:8000') +
                            spec.get('basePath', ''))

            base_path_tokens = list(
                filter(lambda s: s,
                       spec.get('basePath', '').split('/')))
            paths = spec.get('paths')
            if paths:
                for path in paths:
                    path_tokens = (base_path_tokens +
                                   list(filter(lambda s: s, path.split('/'))))
                    if path == '/':  # Path is a trailing slash
                        path_tokens.insert(len(base_path_tokens), '/')
                    elif path[-1] == '/':  # Path ends with a trailing slash
                        path_tokens[-1] = path_tokens[-1] + '/'
                    self.root.add_path(*path_tokens)
                    endpoint = paths[path]
                    for method, info in endpoint.items():
                        params = info.get('parameters')
                        if params:
                            for param in params:
                                if param.get("$ref"):
                                    for section in param.get("$ref").split(
                                            '/'):
                                        param = param.get(
                                            section
                                        ) if not section == "#" else spec

                                if param.get('in') != 'path':
                                    full_path = path_tokens + [param['name']]
                                    self.root.add_path(*full_path,
                                                       node_type='file')
        elif not self.url:
            self.url = 'http://localhost:8000'
Exemple #5
0
 def setUp(self):
     # Make a tree like this:
     #          root
     #     a             h
     #  b     d        i   n
     # c f   e g     k     o
     #             l m p
     self.root = Node('root')
     self.root.add_path('a', 'b', 'c')
     self.root.add_path('a', 'b', 'f')
     self.root.add_path('a', 'd', 'e')
     self.root.add_path('a', 'd', 'g')
     self.root.add_path('h', 'i', 'k', 'l')
     self.root.add_path('h', 'i', 'k', 'm')
     self.root.add_path('h', 'i', 'k', 'p')
     self.root.add_path('h', 'n', 'o')
Exemple #6
0
    def __init__(self, url=None, spec=None):
        self.url = url
        self.headers = {}
        self.querystring_params = {}
        self.body_params = {}
        self.body_json_params = {}
        self.options = {}
        self.should_exit = False

        # Create a tree for supporting API spec and ls command
        self.root = Node('root')
        if spec:
            if not self.url:
                schemes = spec.get('schemes')
                scheme = schemes[0] if schemes else 'https'
                self.url = (scheme + '://' +
                            spec.get('host', 'http://localhost:8000') +
                            spec.get('basePath', ''))

            base_path_tokens = list(filter(lambda s: s,
                                    spec.get('basePath', '').split('/')))
            paths = spec.get('paths')
            if paths:
                for path in paths:
                    path_tokens = (base_path_tokens +
                                   list(filter(lambda s: s, path.split('/'))))
                    if path == '/':  # Path is a trailing slash
                        path_tokens.insert(len(base_path_tokens), '/')
                    elif path[-1] == '/':  # Path ends with a trailing slash
                        path_tokens[-1] = path_tokens[-1] + '/'
                    self.root.add_path(*path_tokens)
                    endpoint = paths[path]
                    for method, info in endpoint.items():
                        params = info.get('parameters')
                        if params:
                            for param in params:
                                if param.get('in') != 'path':
                                    full_path = path_tokens + [param['name']]
                                    self.root.add_path(*full_path,
                                                       node_type='file')
        elif not self.url:
            self.url = 'http://localhost:8000'
Exemple #7
0
    def test_find_child_wildcard(self):
        root = Node('root')
        root.add_path('a')
        root.add_path('{b}')
        root.add_path('c')

        self.assertEqual(root.find_child('a').name, 'a')
        self.assertEqual(root.find_child('c').name, 'c')
        self.assertEqual(root.find_child('x').name, '{b}')
        self.assertFalse(root.find_child('x', wildcard=False))
Exemple #8
0
class TestNode(unittest.TestCase):
    def setUp(self):
        # Make a tree like this:
        #          root
        #     a             h
        #  b     d        i   n
        # c f   e g     k     o
        #             l m p
        self.root = Node('root')
        self.root.add_path('a', 'b', 'c')
        self.root.add_path('a', 'b', 'f')
        self.root.add_path('a', 'd', 'e')
        self.root.add_path('a', 'd', 'g')
        self.root.add_path('h', 'i', 'k', 'l')
        self.root.add_path('h', 'i', 'k', 'm')
        self.root.add_path('h', 'i', 'k', 'p')
        self.root.add_path('h', 'n', 'o')

    def test_illegal_name(self):
        self.assertRaises(ValueError, Node, '.')
        self.assertRaises(ValueError, Node, '..')

    def test_str(self):
        node = Node('my node')
        self.assertEqual(str(node), 'my node')

    def test_cmp_same_type(self):
        a = Node('a', data={'type': 'dir'})
        b = Node('b', data={'type': 'dir'})
        self.assertTrue(a < b)

    def test_cmp_different_type(self):
        a = Node('a', data={'type': 'file'})
        b = Node('b', data={'type': 'dir'})
        self.assertTrue(b < a)

    def test_eq(self):
        a = Node('a', data={'type': 'file'})
        b = Node('b', data={'type': 'dir'})
        self.assertNotEqual(a, b)

        a = Node('a', data={'type': 'file'})
        b = Node('a', data={'type': 'file'})
        self.assertEqual(a, b)

    def test_add_path_and_find_child(self):
        # Level 1 (root)
        self.assertEqual(set(c.name for c in self.root.children), set('ah'))

        # Level 2
        node_a = self.root.find_child('a')
        node_h = self.root.find_child('h')
        self.assertEqual(set(c.name for c in node_a.children), set('bd'))
        self.assertEqual(set(c.name for c in node_h.children), set('in'))

        # Level 3
        node_b = node_a.find_child('b')
        node_i = node_h.find_child('i')
        self.assertEqual(set(c.name for c in node_b.children), set('cf'))
        self.assertEqual(set(c.name for c in node_i.children), set('k'))

        # Level 4
        node_c = node_b.find_child('c')
        node_k = node_i.find_child('k')
        self.assertEqual(set(c.name for c in node_c.children), set())
        self.assertEqual(set(c.name for c in node_k.children), set('lmp'))

        # Return None if child can't be found
        self.assertFalse(node_c.find_child('x'))

    def test_find_child_wildcard(self):
        root = Node('root')
        root.add_path('a')
        root.add_path('{b}')
        root.add_path('c')

        self.assertEqual(root.find_child('a').name, 'a')
        self.assertEqual(root.find_child('c').name, 'c')
        self.assertEqual(root.find_child('x').name, '{b}')
        self.assertFalse(root.find_child('x', wildcard=False))

    def test_ls(self):
        self.assertEqual([n.name for n in self.root.ls('a')], list('bd'))
        self.assertEqual([n.name for n in self.root.ls('a', 'b')], list('cf'))
        self.assertEqual([n.name for n in self.root.ls('a', 'b', 'c')], [])
        self.assertEqual([n.name for n in self.root.ls('h', 'i', 'k')],
                         list('lmp'))

    def test_ls_root(self):
        self.assertEqual([n.name for n in self.root.ls()], list('ah'))

    def test_ls_non_existing(self):
        self.assertEqual([n.name for n in self.root.ls('x')], [])
        self.assertEqual([n.name for n in self.root.ls('a', 'b', 'x')], [])

    def test_ls_parent(self):
        self.assertEqual([n.name for n in self.root.ls('..')], list('ah'))
        self.assertEqual([n.name for n in self.root.ls('..', '..', '..')],
                         list('ah'))
        self.assertEqual([n.name for n in self.root.ls('..', '..', 'h')],
                         list('in'))
        self.assertEqual(
            [n.name for n in self.root.ls('..', '..', 'h', '..', 'a')],
            list('bd'))

    def test_ls_dot(self):
        self.assertEqual([n.name for n in self.root.ls('.')], list('ah'))
        self.assertEqual([n.name for n in self.root.ls('.', '.', '.')],
                         list('ah'))
        self.assertEqual([n.name for n in self.root.ls('.', 'a', 'b')],
                         list('cf'))
        self.assertEqual([n.name for n in self.root.ls('.', 'h', '.')],
                         list('in'))
        self.assertEqual(
            [n.name for n in self.root.ls('.', 'h', '.', '.', 'n')], ['o'])

    def test_ls_sort_by_types(self):
        self.root.add_path('q', 'r')
        self.root.add_path('q', 's', node_type='file')
        self.root.add_path('q', 't', node_type='file')
        self.root.add_path('q', 'u')
        self.root.add_path('q', 'v', node_type='file')

        self.assertEqual([n.name for n in self.root.ls('q')], list('rustv'))
Exemple #9
0
 def test_cmp_different_type(self):
     a = Node('a', data={'type': 'file'})
     b = Node('b', data={'type': 'dir'})
     self.assertTrue(b < a)
Exemple #10
0
 def test_cmp_same_type(self):
     a = Node('a', data={'type': 'dir'})
     b = Node('b', data={'type': 'dir'})
     self.assertTrue(a < b)
Exemple #11
0
 def test_str(self):
     node = Node('my node')
     self.assertEqual(str(node), 'my node')
Exemple #12
0
class Context(object):
    def __init__(self, url=None, spec=None):
        self.url = url
        self.headers = {}
        self.querystring_params = {}
        self.body_params = {}
        self.body_json_params = {}
        self.options = {}
        self.should_exit = False

        # Create a tree for supporting API spec and ls command
        self.root = Node('root')
        if spec:
            if not self.url:
                schemes = spec.get('schemes')
                scheme = schemes[0] if schemes else 'https'
                self.url = (scheme + '://' +
                            spec.get('host', 'http://localhost:8000') +
                            spec.get('basePath', ''))

            base_path_tokens = list(
                filter(lambda s: s,
                       spec.get('basePath', '').split('/')))
            paths = spec.get('paths')
            if paths:
                for path in paths:
                    path_tokens = (base_path_tokens +
                                   list(filter(lambda s: s, path.split('/'))))
                    if path == '/':  # Path is a trailing slash
                        path_tokens.insert(len(base_path_tokens), '/')
                    elif path[-1] == '/':  # Path ends with a trailing slash
                        path_tokens[-1] = path_tokens[-1] + '/'
                    self.root.add_path(*path_tokens)
                    endpoint = paths[path]
                    for method, info in endpoint.items():
                        params = info.get('parameters')
                        if params:
                            for param in params:
                                if param.get('in') != 'path':
                                    full_path = path_tokens + [param['name']]
                                    self.root.add_path(*full_path,
                                                       node_type='file')
        elif not self.url:
            self.url = 'http://localhost:8000'

    def __eq__(self, other):
        return (self.url == other.url and self.headers == other.headers
                and self.options == other.options
                and self.querystring_params == other.querystring_params
                and self.body_params == other.body_params
                and self.body_json_params == other.body_json_params
                and self.should_exit == other.should_exit)

    def copy(self):
        context = Context(self.url)
        context.headers = self.headers.copy()
        context.querystring_params = self.querystring_params.copy()
        context.body_params = self.body_params.copy()
        context.body_json_params = self.body_json_params.copy()
        context.options = self.options.copy()
        context.should_exit = self.should_exit
        return context

    def update(self, context):
        if context.url:
            self.url = context.url

        self.headers.update(context.headers)
        self.querystring_params.update(context.querystring_params)
        self.body_params.update(context.body_params)
        self.body_json_params.update(context.body_json_params)
        self.options.update(context.options)
        self.should_exit = self.should_exit
Exemple #13
0
    def __init__(self, url=None, spec=None):
        self.url = url
        self.headers = {}
        self.querystring_params = {}
        self.body_params = {}
        self.body_json_params = {}
        self.options = {}
        self.should_exit = False

        # Create a tree for supporting API spec and ls command
        self.root = Node('root')
        if spec:
            if not self.url:
                schemes = spec.get('schemes')
                scheme = schemes[0] if schemes else 'https'
                self.url = (scheme + '://' +
                            spec.get('host', 'http://localhost:8000') +
                            spec.get('basePath', ''))

            base_path_tokens = list(
                filter(lambda s: s,
                       spec.get('basePath', '').split('/')))
            paths = spec.get('paths')
            if paths:
                for path in paths:
                    path_tokens = (base_path_tokens +
                                   list(filter(lambda s: s, path.split('/'))))
                    if path == '/':  # Path is a trailing slash
                        path_tokens.insert(len(base_path_tokens), '/')
                    elif path[-1] == '/':  # Path ends with a trailing slash
                        path_tokens[-1] = path_tokens[-1] + '/'
                    self.root.add_path(*path_tokens)
                    endpoint = dict(paths[path])
                    # path parameters (apply to all paths if not overriden)
                    # exclude $ref as we have no system to handle that now
                    global_parameters = list(endpoint.pop('parameters', []))
                    # not used
                    endpoint.pop('servers', None)
                    endpoint.pop('$ref', None)
                    endpoint.pop('summary', None)
                    endpoint.pop('description', None)
                    for method, info in endpoint.items():
                        params = info.get('parameters', [])
                        params = list(global_parameters + params)
                        if params:

                            def parameter_key(i):
                                return (i.get('$ref',
                                              None), i.get('name', None),
                                        i.get('in', None))

                            # parameter is overriden based on $ref/in/name value
                            # last value (local definition) takes precedence
                            params_map = {parameter_key(p): p for p in params}
                            params = params_map.values()
                            for param in params:
                                if param.get('$ref'):
                                    for section in param.get('$ref').split(
                                            '/'):
                                        param = param.get(
                                            section
                                        ) if not section == '#' else spec

                                if param.get('in') != 'path':
                                    # Note that for completion mechanism, only
                                    # name/node_type is used
                                    # Parameters from methods/location
                                    # are merged
                                    full_path = path_tokens + [param['name']]
                                    self.root.add_path(*full_path,
                                                       node_type='file')
        elif not self.url:
            self.url = 'http://localhost:8000'