-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from jedymatt/dev
Dev
- Loading branch information
Showing
29 changed files
with
1,128 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,3 +100,9 @@ Then run the test: | |
```shell | ||
pytest tests | ||
``` | ||
|
||
Run test with coverage | ||
|
||
```shell | ||
coverage run -m pytest | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
SQLAlchemy>=1.4 | ||
dataclasses>=0.8; python_version == "3.6" | ||
# dataclasses>=0.8; python_version == "3.6" | ||
PyYAML>=5.4 | ||
coverage>=6.2 | ||
tox | ||
pytest | ||
pylint | ||
autopep8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from setuptools import setup | ||
|
||
|
||
setup() | ||
from setuptools import setup | ||
|
||
|
||
setup() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
""" | ||
attribute module containing helper functions for instrumented attribute. | ||
""" | ||
|
||
from functools import lru_cache | ||
from inspect import isclass | ||
|
||
from sqlalchemy.orm import ColumnProperty, RelationshipProperty | ||
from sqlalchemy.orm.attributes import InstrumentedAttribute, get_attribute, set_attribute | ||
|
||
|
||
def instrumented_attribute(class_or_instance, key: str): | ||
""" | ||
Returns instrumented attribute from the class or instance. | ||
""" | ||
|
||
if isclass(class_or_instance): | ||
return getattr(class_or_instance, key) | ||
|
||
return getattr(class_or_instance.__class__, key) | ||
|
||
|
||
def attr_is_relationship(instrumented_attr: InstrumentedAttribute): | ||
""" | ||
Check if instrumented attribute property is a RelationshipProperty | ||
""" | ||
return isinstance(instrumented_attr.property, RelationshipProperty) | ||
|
||
|
||
def attr_is_column(instrumented_attr: InstrumentedAttribute): | ||
""" | ||
Check if instrumented attribute property is a ColumnProperty | ||
""" | ||
return isinstance(instrumented_attr.property, ColumnProperty) | ||
|
||
|
||
def set_instance_attribute(instance, key, value): | ||
""" | ||
Set attribute value of instance | ||
""" | ||
|
||
instr_attr: InstrumentedAttribute = getattr(instance.__class__, key) | ||
|
||
if attr_is_relationship(instr_attr) and instr_attr.property.uselist: | ||
get_attribute(instance, key).append(value) | ||
else: | ||
set_attribute(instance, key, value) | ||
|
||
@lru_cache() | ||
def foreign_key_column(instrumented_attr: InstrumentedAttribute): | ||
""" | ||
Returns the table name of the first foreignkey. | ||
""" | ||
return next(iter(instrumented_attr.foreign_keys)).column | ||
|
||
@lru_cache() | ||
def referenced_class(instrumented_attr: InstrumentedAttribute): | ||
""" | ||
Returns class that the attribute is referenced to. | ||
""" | ||
|
||
if attr_is_relationship(instrumented_attr): | ||
return instrumented_attr.mapper.class_ | ||
|
||
table_name = foreign_key_column(instrumented_attr).table.name | ||
|
||
return next(filter( | ||
lambda mapper: mapper.class_.__tablename__ == table_name, | ||
instrumented_attr.parent.registry.mappers | ||
)).class_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
MODEL_KEY = 'model' | ||
DATA_KEY = 'data' | ||
FILTER_KEY = 'filter' | ||
SOURCE_KEYS = [DATA_KEY, FILTER_KEY] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class DynamicSeeder: | ||
""" | ||
DynamicSeeder class | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,38 @@ | ||
class ClassNotFoundError(Exception): | ||
"""Raised when the class is not found""" | ||
pass | ||
|
||
|
||
class MissingKeyError(Exception): | ||
"""Raised when a required key is missing""" | ||
pass | ||
|
||
|
||
class MaxLengthExceededError(Exception): | ||
"""Raised when maximum length of data exceeded""" | ||
pass | ||
|
||
|
||
class InvalidTypeError(Exception): | ||
"""Raised when a type of data is not accepted""" | ||
pass | ||
|
||
|
||
class EmptyDataError(Exception): | ||
"""Raised when data is empty""" | ||
pass | ||
|
||
|
||
class InvalidKeyError(Exception): | ||
"""Raised when an invalid key is invoked""" | ||
pass | ||
|
||
|
||
class ParseError(Exception): | ||
"""Raised when parsing string fails""" | ||
pass | ||
|
||
|
||
class UnsupportedClassError(Exception): | ||
"""Raised when an unsupported class is invoked""" | ||
pass | ||
|
||
|
||
class NotInModuleError(Exception): | ||
"""Raised when a value is not found in module""" | ||
pass | ||
|
||
|
||
class InvalidModelPath(Exception): | ||
"""Raised when an invalid model path is invoked""" | ||
pass | ||
|
||
|
||
class UnsupportedClassError(Exception): | ||
"""Raised when an unsupported class is invoked""" | ||
pass |
Oops, something went wrong.