def update_codegen_ignore(spec):
    with io.open(CODEGEN_IGNORE_PATH) as f:
        ignore_lines = set(f.read().splitlines())

    import kubernetes
    k8s_apis = dir(kubernetes.client.apis)
    k8s_models = dir(kubernetes.client.models)

    api_modules = set()
    model_modules = set()

    for path in list(spec['paths'].values()):
        for op_name in list(path.keys()):
            if op_name != 'parameters':
                op = path[op_name]
                for tag in op['tags']:
                    tag = '_'.join([string_utils.camel_case_to_snake(x) for x in tag.split('_')])
                    api_modules.add(tag + '_api')

    for model in list(spec['definitions'].keys()):
        module_name = '_'.join([string_utils.camel_case_to_snake(x) for x in model.split('.')])
        model_modules.add(module_name)

    for api_module in api_modules:
        suffix = api_module.split('_')[-1]
        if suffix in ['0', 'pre012'] or api_module in k8s_apis:
            ignore_lines.add('client/apis/{}.py'.format(api_module))

    for model_module in model_modules:
        if model_module in k8s_models:
            ignore_lines.add('client/models/{}.py'.format(model_module))

    with io.open(CODEGEN_IGNORE_PATH, mode='w') as f:
        f.write('\n'.join(sorted(ignore_lines)))
    def test_cannot_handle_non_string_objects(self):
        with self.assertRaises(TypeError) as raised:
            # noinspection PyTypeChecker
            camel_case_to_snake(None)

        self.assertEqual(str(raised.exception), 'Expected "str", received "NoneType"')

        with self.assertRaises(TypeError) as raised:
            # noinspection PyTypeChecker
            camel_case_to_snake(False)

        self.assertEqual(str(raised.exception), 'Expected "str", received "bool"')

        with self.assertRaises(TypeError) as raised:
            # noinspection PyTypeChecker
            camel_case_to_snake(0)

        self.assertEqual(str(raised.exception), 'Expected "str", received "int"')

        with self.assertRaises(TypeError) as raised:
            # noinspection PyTypeChecker
            camel_case_to_snake([])

        self.assertEqual(str(raised.exception), 'Expected "str", received "list"')

        with self.assertRaises(TypeError) as raised:
            # noinspection PyTypeChecker
            camel_case_to_snake({'a': 1})

        self.assertEqual(str(raised.exception), 'Expected "str", received "dict"')
Esempio n. 3
0
def index(request):
    '''
    Supports the index of a users account and the post for a single account
    '''
    user = request.user
    if request.method == "POST":
        if teller_permission(user):
            data = decode_json_content(request.body)
            data = {camel_case_to_snake(key): data[key] for key in data}
            data["user_id"] = data["user"]
            del data["user"]
            data['creator'] = user
            data['balance'] = float(data.get('balance', 0))
            Account.objects.create(**data)
            return JsonResponse({}, status=201)
        else:
            return JsonResponse({}, status=403)

    if user.is_anonymous:
        return JsonResponse([], status=200, safe=False)
    serialized_q = serialize_accounts(
        user.account_set.extra(select={
            'lower_name': 'lower(name)'
        }).order_by('lower_name').all())
    return JsonResponse(serialized_q, status=200, safe=False)
Esempio n. 4
0
def create_user(request):
    """
    Creates a user based on a request
    """
    try:
        data = decode_json_content(request.body)
        if "password2" in data:
            del data["password2"]

        data = {
            camel_case_to_snake(key): data[key]
            for key in data if data[key]
        }

        role = None
        if data.get('role') and not manager_permission(request.user):
            return JsonResponse({}, status=403)
        elif data.get('role'):
            role = data['role']
            del data['role']
        user = User.objects.create_user(**data)

        if role in PERMISSION_CLASSES:
            user.user_permissions.add(PERMISSION_CLASSES[role])
        return JsonResponse({}, status=200)
    except TypeError as e:
        return JsonResponse({}, status=422)
Esempio n. 5
0
def post(request):
    user = request.user
    if user.is_anonymous:
        account_ids = []
    else:
        account_ids = user.account_set.values_list('id', flat=True)
    data = decode_json_content(request.body)
    data = {
        camel_case_to_snake(key): data[key]
        for key in data
    }
    data['creator'] = user
    data['amount'] = float(data['amount'])

    if "account_to" in data:
        data["account_to_id"] = data["account_to"]
        del data["account_to"]
    if "account_from" in data:
        data["account_from_id"] = data["account_from"]
        del data["account_from"]

    if (
        teller_permission(user) or
        (
            data.get("transaction_type") == "transfer" and
            int(data.get("account_to_id")) in account_ids and
            int(data.get("account_from_id")) in account_ids
        )
    ):
        Transaction.objects.create(**data)
        return JsonResponse({}, status=201)
    else:
        return JsonResponse({}, status=403)
Esempio n. 6
0
def create_meta_table_data(engine, dataset_schema: DatasetSchema):
    session = sessionmaker(bind=engine)()
    ds_content = {
        camel_case_to_snake(k): v
        for k, v in dataset_schema.items() if k != "tables"
    }
    ds_content["contact_point"] = str(ds_content.get("contact_point", ""))
    ds_transformer = transformer_factory(models.Dataset)
    dataset = models.Dataset(**ds_transformer(ds_content))
    session.add(dataset)

    for table_data in dataset_schema["tables"]:
        table_content = {
            camel_case_to_snake(k): v
            for k, v in table_data.items() if k != "schema"
        }

        table = models.Table(
            **{
                **table_content,
                **{
                    f: table_data["schema"].get(f)
                    for f in ("required", "display")
                },
            })
        table.dataset_id = dataset.id
        session.add(table)

        for field_name, field_value in table_data["schema"][
                "properties"].items():
            field_content = {
                k.replace("$", ""): v
                for k, v in field_value.items() if k not in {"$comment"}
            }
            field_content["name"] = field_name
            try:
                field = models.Field(**field_content)
            except TypeError as e:
                raise NotImplementedError(
                    f'Import failed: at "{field_name}": {field_value!r}:\n{e}'
                ) from e

            field.table_id = table.id
            field.dataset_id = dataset.id
            session.add(field)

    session.commit()
Esempio n. 7
0
 def get_base_model_name_snake(self, model_name):
     """
     Return base model name with API version removed, and remainder converted to snake case
     :param model_name: string
     :return: string
     """
     result = self.get_base_model_name(model_name)
     return string_utils.camel_case_to_snake(result)
Esempio n. 8
0
 def __get_models_for_package(model_package, api_version, requested_models):
     models = []
     for model, model_class in inspect.getmembers(model_package):
         if model == 'V1ProjectRequest':
             continue
         if 'kind' in dir(model_class) and ('metadata' in dir(model_class)
                                            or 'spec' in dir(model_class)):
             # models with a 'kind' are top-level objects that we care about
             matches = VERSION_RX.match(model)
             if not matches:
                 # exclude unversioned models
                 continue
             model_api = matches.group(0)
             base_model_name = model.replace(model_api, '')
             model_name_snake = string_utils.camel_case_to_snake(
                 base_model_name).lower()
             model_api_snake = string_utils.camel_case_to_snake(
                 model_api).lower()
             if requested_models and \
                base_model_name not in requested_models and \
                model_name_snake not in requested_models:
                 continue
             if api_version:
                 # only include models for the requested API version
                 if model_api == api_version.capitalize():
                     models.append({
                         'model': model,
                         'model_api': model_api,
                         'model_api_snake': model_api_snake,
                         'base_model_name': base_model_name,
                         'model_name_snake': model_name_snake
                     })
             else:
                 models.append({
                     'model': model,
                     'model_api': model_api,
                     'model_api_snake': model_api_snake,
                     'base_model_name': base_model_name,
                     'model_name_snake': model_name_snake
                 })
     return models
def add_labels_to_domain_ontology(dg: Graph, attr_str: str) -> Graph:
    query_string = 'select * where {?x rdf:type ' + attr_str + ' }'
    query_result = dg.query(query_string)
    for row in query_result:
        attr_uri = row[0]
        if attr_uri.rfind('#') == -1:
            attr_name = attr_uri[attr_uri.rfind('/') + 1:]
        else:
            attr_name = attr_uri[attr_uri.rfind('#') + 1:]
        attr_name = string_utils.camel_case_to_snake(attr_name, ' ')
        dg.add((URIRef(attr_uri), RDFS.label, Literal(attr_name)))
    return dg
Esempio n. 10
0
    def __init__(self,
                 project_id,
                 table,
                 select=None,
                 where=None,
                 order_by=None,
                 limit=None,
                 offset=None,
                 as_list=False,
                 reidentify=False,
                 all_in=[],
                 *args,
                 **kwargs):
        self.as_list = as_list
        self.reidentify = reidentify
        self._project_id = project_id
        table = string_utils.camel_case_to_snake(table)
        if type(select) is str:
            query = "select %s from %s " % (select, table)
        elif type(select) is list:
            query = "select %s from %s " % (', '.join(select), table)
        else:
            query = "select * from %s " % table

        if where:
            if type(where) is dict:
                where_clauses = []
                for k, v in where.items():
                    if type(v) is str:
                        where_clauses.append("%s = '%s'" % (k, v))
                    else:
                        where_clauses.append("%s = %s" % (k, v))
                query = query + "where " + " and ".join(where_clauses)
            else:
                query = query + "where %s " % where

        if where and all_in:
            query = query + "and person_id in " + '(' + ','.join(all_in) + ')'
        elif all_in:
            query = query + "where person_id in " + '(' + ','.join(
                all_in) + ')'

        if order_by:
            query = query + " order by %s " % order_by

        if limit and type(limit) in (str, int):
            query = query + " limit %s " % limit

        if offset and type(offset) in (str, int):
            query = query + " offset %s " % offset
        print(query)
        self.query = query
Esempio n. 11
0
    def __init__(self, model, api_version):
        self.model = model
        self.api_version = api_version

        self.module_name = '{}_{}_{}'.format(self.module_prefix, self.api_version.lower(),
                                             string_utils.camel_case_to_snake(self.model))
        self.module_file = self.module_name + '.yml'

        try:
            helper_class = self.helper_class
            self.helper = helper_class(self.api_version, self.model, debug=True)
        except KubernetesException:
            raise
def update_codegen_ignore(spec, output_path):
    import io
    import os
    import string_utils
    import kubernetes

    codegen_ignore_path = os.path.join(os.path.dirname(output_path),
                                       '.swagger-codegen-ignore')
    ignore_lines = set()

    k8s_apis = dir(kubernetes.client.apis)
    k8s_models = dir(kubernetes.client.models)

    api_modules = set()
    model_modules = set()

    for path in list(spec['paths'].values()):
        for op_name in list(path.keys()):
            if op_name != 'parameters':
                op = path[op_name]
                for tag in op.get('tags', []):
                    tag = '_'.join([
                        string_utils.camel_case_to_snake(x)
                        for x in tag.split('_')
                    ])
                    api_modules.add(tag + '_api')

    for model in list(spec['definitions'].keys()):
        module_name = '_'.join(
            [string_utils.camel_case_to_snake(x) for x in model.split('.')])
        model_modules.add(module_name)

    for api_module in api_modules:
        suffix = api_module.split('_')[-1]
        if suffix in ['0', 'pre012'] or api_module in k8s_apis:
            ignore_lines.add('client/apis/{}.py'.format(api_module))
            print(
                "Skipping generation of client/apis/{}.py".format(api_module))
        else:
            print("Not skipping generation of client/apis/{}.py".format(
                api_module))

    for model_module in model_modules:
        if model_module in k8s_models:
            ignore_lines.add('client/models/{}.py'.format(model_module))
            print("Skipping generation of client/models/{}.py".format(
                model_module))
        else:
            print("Not skipping generation of client/models/{}.py".format(
                model_module))

    for module in list(ignore_lines):
        module_name = module.split('/')[-1].split('.')[0]
        test_module_name = 'test_{}'.format(module_name)
        docs_module_name = "".join(
            map(lambda x: x.capitalize(), module_name.split('_')))
        ignore_lines.add("test/{}.py".format(test_module_name))
        ignore_lines.add('docs/{}.md'.format(docs_module_name))
        print("Skipping generation of test/{}.py".format(test_module_name))
        print("Skipping generation of docs/{}.md".format(docs_module_name))

    ignore_lines = ignore_lines.union(DEFAULT_CODEGEN_IGNORE_LINES)

    with open(codegen_ignore_path, 'w') as f:
        f.write('\n'.join(sorted(ignore_lines)))
Esempio n. 13
0
 def attribute_to_snake(name):
     """ Convert an object property name from camel to snake """
     result = string_utils.camel_case_to_snake(name)
     if result.endswith('_i_p'):
         result = re.sub(r'_i_p$', '_ip', result)
     return result
 def test_returns_same_string_if_all_uppercase(self):
     s = 'UPPERCASE'
     self.assertEqual(camel_case_to_snake(s), s)
 def test_returns_lowercase_string_for_single_word(self):
     s = 'Hello'
     self.assertEqual(camel_case_to_snake(s), s.lower())
Esempio n. 16
0
def snake_case_model(model):
    return '_'.join(
        [string_utils.camel_case_to_snake(x) for x in model.split('.')])
 def test_returns_words_divided_by_underscores_for_each_camel_word(self):
     s = 'CamelCaseStringToTest'
     self.assertEqual(camel_case_to_snake(s), 'camel_case_string_to_test')
 def test_returns_words_divided_by_underscores_for_each_camel_word_even_for_articles(self):
     s = 'ThisIsACamelStringTestB'
     self.assertEqual(camel_case_to_snake(s), 'this_is_a_camel_string_test_b')
 def test_returns_same_string_if_all_lowercase(self):
     s = 'lower'
     self.assertEqual(camel_case_to_snake(s), s)
 def test_handles_acronyms_gracefully(self):
     s = 'SPAAppsAreVeryPopularOnTheWEBToday'
     self.assertEqual(camel_case_to_snake(s), 'spa_apps_are_very_popular_on_the_web_today')
 def test_should_use_provided_separator(self):
     s = 'CamelCaseString'
     self.assertEqual(camel_case_to_snake(s, '_'), 'camel_case_string')
     self.assertEqual(camel_case_to_snake(s, '||'), 'camel||case||string')
     self.assertEqual(camel_case_to_snake(s, ' '), 'camel case string')
 def test_should_return_same_string_if_contains_spaces(self):
     s = 'This Is Not A Camel Case String! butThisOneIs'
     self.assertEqual(camel_case_to_snake(s), s)