Example #1
0
 def __new__(cls, classname, bases, dict_):
     tablename = dict_.get('__tablename__')
     if not tablename and not dict_.get('__table__') \
        and _DeclarativeMeta.has_primary_key(dict_):
         dict_['__tablename__'] = strings.pluralize(
             strings.snakecase(classname))
     return DeclarativeMeta.__new__(cls, classname, bases, dict_)
Example #2
0
def deserialize(obj, class_, attributes, strategies=None, snakecase=True,
                **kwargs):
    """Deserializes a dict into an object of type class_.

    Can be seen as the inverse of serialize().

    Args:
        obj (dict): The structure to deserialize
        class_ (class): The type that the object should be deserialized into
        attributes (tuple): A tuple of attributes that should be set
        strategies (dict): Key/value pairs of strategies to deal with objects
        snakecase (bool): snake_case the attribute names
        kwargs: Passed to the strategy when it's called
    """
    deserialized = class_()
    for attr in attributes:
        if snakecase:
            # camelcase the required attr
            attr = strings.camelcase(attr, uppercase=False)
        value = obj.get(attr)
        if value is None:
            continue
        if strategies and attr in strategies:
            value = strategies[attr](value, **kwargs)
        if snakecase:
            attr = strings.snakecase(attr)
        setattr(deserialized, attr, value)
    return deserialized
Example #3
0
def deserialize(obj,
                class_,
                attributes,
                strategies=None,
                snakecase=True,
                **kwargs):
    """Deserializes a dict into an object of type class_.

    Can be seen as the inverse of serialize().

    Args:
        obj (dict): The structure to deserialize
        class_ (class): The type that the object should be deserialized into
        attributes (tuple): A tuple of attributes that should be set
        strategies (dict): Key/value pairs of strategies to deal with objects
        snakecase (bool): snake_case the attribute names
        kwargs: Passed to the strategy when it's called
    """
    deserialized = class_()
    for attr in attributes:
        if snakecase:
            # camelcase the required attr
            attr = strings.camelcase(attr, uppercase=False)
        value = obj.get(attr)
        if value is None:
            continue
        if strategies and attr in strategies:
            value = strategies[attr](value, **kwargs)
        if snakecase:
            attr = strings.snakecase(attr)
        setattr(deserialized, attr, value)
    return deserialized
Example #4
0
def save(model, items, fixtures_config):
    model_name = strings.pluralize(
        strings.snakecase(model.__name__))
    path = '{}/{}.json'.format(
        fixtures_config['path'],
        model_name)
    with open(path, 'w') as file:
        file.write(items)
    return imports.get_qualified_name(model), path
Example #5
0
 def _generate_expands(self, expands=None):
     output = {}
     for expand in expands or []:
         m = re.search(r'(\w+)\((.*)\)', expand)
         if not m:
             output[expand] = None
             continue
         output[strings.snakecase(m.group(1))] = [
             v.strip() for v in split_attributes(m.group(2))
         ]
     return output
Example #6
0
 def _cleaned_attribute_names(self, accepted, requested, override):
     override = set([
         strings.snakecase(attr)
         for attr in set(override).difference(accepted)
     ])
     return accepted.union(requested.intersection(override))
Example #7
0
 def test_underscore(self):
     assert strings.snakecase('SomethingElse') == 'something_else'
Example #8
0
 def test_underscore(self):
     assert strings.snakecase('SomethingElse') == 'something_else'
Example #9
0
 def cased_name(cls):
     if not cls.name:
         cls.name = cls.__name__
     return strings.snakecase(cls.name)