Refactoring: enable formatting with autopep8

This commit is contained in:
IRBorisov 2024-05-24 19:06:39 +03:00
parent 9e4f7ca2f2
commit 18c09ecd93
22 changed files with 116 additions and 78 deletions

11
.vscode/settings.json vendored
View File

@ -4,6 +4,7 @@
".pytest_cache/": true
},
"typescript.tsdk": "rsconcept/frontend/node_modules/typescript/lib",
"eslint.workingDirectories": ["rsconcept/frontend"],
"isort.args": [
"--line-length",
"100",
@ -12,10 +13,16 @@
"--project",
"apps"
],
"eslint.workingDirectories": ["rsconcept/frontend"],
"autopep8.args": [
"--max-line-length",
"120",
"--aggressive",
"--ignore",
"E303"
],
"[python]": {
"editor.formatOnSave": false,
"editor.defaultFormatter": "ms-python.autopep8",
"editor.formatOnSave": true,
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.codeActionsOnSave": {

View File

@ -7,6 +7,7 @@ ItemType = TypeVar("ItemType")
class Graph(Generic[ItemType]):
''' Directed graph. '''
def __init__(self, graph: Optional[dict[ItemType, list[ItemType]]] = None):
if graph is None:
self.outputs: dict[ItemType, list[ItemType]] = {}

View File

@ -1,41 +1,54 @@
''' Utility: Text messages. '''
# pylint: skip-file
def constituentaNotOwned(title: str):
return f'Конституента не принадлежит схеме: {title}'
def substitutionNotInList():
return 'Отождествляемая конституента отсутствует в списке'
def schemaNotOwned():
return 'Нет доступа к схеме'
def renameTrivial(name: str):
return f'Имя должно отличаться от текущего: {name}'
def substituteTrivial(name: str):
return f'Отождествление конституенты с собой не корректно: {name}'
def substituteDouble(name: str):
return f'Повторное отождествление: {name}'
def aliasTaken(name: str):
return f'Имя уже используется: {name}'
def pyconceptFailure():
return 'Invalid data response from pyconcept'
def typificationInvalidStr():
return 'Invalid typification string'
def libraryTypeUnexpected():
return 'Attempting to use invalid adaptor for non-RSForm item'
def exteorFileVersionNotSupported():
return 'Некорректный формат файла Экстеор. Сохраните файл в новой версии'
def invalidPosition():
return 'Invalid position: should be positive integer'
def constituentaNoStructure():
return 'Указанная конституента не обладает теоретико-множественной типизацией'

View File

@ -29,6 +29,7 @@ _INSERT_LAST: int = -1
class RSForm:
''' RSForm is math form of conceptual schema. '''
def __init__(self, item: LibraryItem):
if item.item_type != LibraryItemType.RSFORM:
raise ValueError(msg.libraryTypeUnexpected())
@ -426,20 +427,21 @@ class RSForm:
class SemanticInfo:
''' Semantic information derived from constituents. '''
def __init__(self, schema: RSForm):
self._graph = schema._graph_formal()
self._items = list(
schema.constituents() \
.only('id', 'alias', 'cst_type', 'definition_formal') \
schema.constituents()
.only('id', 'alias', 'cst_type', 'definition_formal')
.order_by('order')
)
self._cst_by_alias = {cst.alias: cst for cst in self._items}
self._cst_by_ID = {cst.id: cst for cst in self._items}
self.info = {
cst.id: {
'is_simple' : False, \
'is_template' : False, \
'parent' : cst.id, \
'is_simple': False,
'is_template': False,
'parent': cst.id,
'children': []
}
for cst in self._items
@ -483,7 +485,7 @@ class SemanticInfo:
dependencies = self._graph.inputs[target.id]
has_complex_dependency = any(
self.is_template(cst_id) and \
self.is_template(cst_id) and
not self.is_simple_expression(cst_id) for cst_id in dependencies
)
if has_complex_dependency:
@ -551,12 +553,13 @@ class SemanticInfo:
class _OrderManager:
''' Ordering helper class '''
def __init__(self, schema: RSForm):
self._semantic = schema.semantic()
self._graph = schema._graph_formal()
self._items = list(
schema.constituents() \
.only('id', 'order', 'alias', 'cst_type', 'definition_formal') \
schema.constituents()
.only('id', 'order', 'alias', 'cst_type', 'definition_formal')
.order_by('order')
)
self._cst_by_ID = {cst.id: cst for cst in self._items}
@ -579,8 +582,8 @@ class _OrderManager:
result = [cst for cst in self._items if cst.cst_type == CstType.BASE]
result = result + [cst for cst in self._items if cst.cst_type == CstType.CONSTANT]
kernel = [
cst.id for cst in self._items if \
cst.cst_type in [CstType.STRUCTURED, CstType.AXIOM] or \
cst.id for cst in self._items if
cst.cst_type in [CstType.STRUCTURED, CstType.AXIOM] or
self._cst_by_ID[self._semantic.parent(cst.id)].cst_type == CstType.STRUCTURED
]
kernel = kernel + self._graph.expand_inputs(kernel)
@ -604,7 +607,6 @@ class _OrderManager:
result.append(child)
self._items = result
@transaction.atomic
def _save_order(self) -> None:
order = 1

View File

@ -150,7 +150,7 @@ def generate_structure(alias: str, expression: str, parse: dict) -> list:
parent_type = ast[parent_index]['typeID']
parent_text = generated[parent_index]['text']
parent_is_boolean = generated[parent_index]['is_boolean']
assert(parent_type in [TokenType.BOOLEAN, TokenType.DECART])
assert parent_type in [TokenType.BOOLEAN, TokenType.DECART]
if parent_is_boolean:
if parent_type == TokenType.BOOLEAN:

View File

@ -65,6 +65,7 @@ class ErrorDescriptionSerializer(serializers.Serializer):
child=serializers.CharField()
)
class NodeDataSerializer(serializers.Serializer):
''' Serializer: Node data. '''
dataType = serializers.CharField()

View File

@ -12,6 +12,7 @@ _TRS_VERSION_MIN = 16
_TRS_VERSION = 16
_TRS_HEADER = 'Exteor 4.8.13.1000 - 30/05/2022'
class FileSerializer(serializers.Serializer):
''' Serializer: File input. '''
file = serializers.FileField(allow_empty_file=False)
@ -25,6 +26,7 @@ class RSFormUploadSerializer(serializers.Serializer):
class RSFormTRSSerializer(serializers.Serializer):
''' Serializer: TRS file production and loading for RSForm. '''
def to_representation(self, instance: RSForm) -> dict:
result = self._prepare_json_rsform(instance)
items = instance.constituents().order_by('order')

View File

@ -10,6 +10,7 @@ from ..models import RSForm
class PyConceptAdapter:
''' RSForm adapter for interacting with pyconcept module. '''
def __init__(self, data: Union[RSForm, dict]):
try:
if 'items' in cast(dict, data):

View File

@ -14,6 +14,7 @@ class NewCstResponse(serializers.Serializer):
new_cst = serializers.IntegerField()
schema = RSFormParseSerializer()
class NewMultiCstResponse(serializers.Serializer):
''' Serializer: Create multiple cst response. '''
cst_list = serializers.ListField(
@ -21,6 +22,7 @@ class NewMultiCstResponse(serializers.Serializer):
)
schema = RSFormParseSerializer()
class NewVersionResponse(serializers.Serializer):
''' Serializer: Create cst response. '''
version = serializers.IntegerField()

View File

@ -8,6 +8,7 @@ from apps.rsform.models import Constituenta, CstType, LibraryItem, LibraryItemTy
class TestConstituenta(TestCase):
''' Testing Constituenta model. '''
def setUp(self):
self.schema1 = LibraryItem.objects.create(item_type=LibraryItemType.RSFORM, title='Test1')
self.schema2 = LibraryItem.objects.create(item_type=LibraryItemType.RSFORM, title='Test2')

View File

@ -6,6 +6,7 @@ from apps.rsform.models import LibraryItem, LibraryItemType, Subscription, User
class TestLibraryItem(TestCase):
''' Testing LibraryItem model. '''
def setUp(self):
self.user1 = User.objects.create(username='User1')
self.user2 = User.objects.create(username='User2')

View File

@ -7,6 +7,7 @@ from apps.rsform.models import Constituenta, CstType, RSForm, User
class TestRSForm(TestCase):
''' Testing RSForm wrapper. '''
def setUp(self):
self.user1 = User.objects.create(username='User1')
self.user2 = User.objects.create(username='User2')

View File

@ -8,7 +8,6 @@ from apps.rsform.utils import apply_pattern, fix_old_references
class TestUtils(unittest.TestCase):
''' Test various utility functions. '''
def test_apply_mapping_patter(self):
mapping = {'X101': 'X20'}
pattern = re.compile(r'(X[0-9]+)')

View File

@ -15,6 +15,7 @@ _REF_OLD_PATTERN = re.compile(r'@{([^0-9\-][^\}\|\{]*?)\|([^\}\|\{]*?)\|([^\}\|\
class ObjectOwnerOrAdmin(BasePermission):
''' Permission for object ownership restriction '''
def has_object_permission(self, request, view, obj):
if request.user == obj.owner:
return True
@ -25,6 +26,7 @@ class ObjectOwnerOrAdmin(BasePermission):
class IsClaimable(IsAuthenticated):
''' Permission for object ownership restriction '''
def has_object_permission(self, request, view, obj):
if not super().has_permission(request, view):
return False
@ -33,6 +35,7 @@ class IsClaimable(IsAuthenticated):
class SchemaOwnerOrAdmin(BasePermission):
''' Permission for object ownership restriction '''
def has_object_permission(self, request, view, obj):
if request.user == obj.schema.owner:
return True
@ -43,6 +46,7 @@ class SchemaOwnerOrAdmin(BasePermission):
class ItemOwnerOrAdmin(BasePermission):
''' Permission for object ownership restriction '''
def has_object_permission(self, request, view, obj):
if request.user == obj.item.owner:
return True

View File

@ -1,8 +1,10 @@
''' Utility: Text messages. '''
# pylint: skip-file
def passwordAuthFailed():
return 'Неизвестное сочетание имени пользователя и пароля'
def passwordsNotMatch():
return 'Введенные пароли не совпадают'

View File

@ -1,4 +1,4 @@
''' Models: User profile and Authentification. '''
''' Models: User profile and Authorization. '''
# Note: using User import to isolate original
# pylint: disable=unused-import,ungrouped-imports

View File

@ -23,6 +23,7 @@ def _get_secret(key: str, default):
return f.read()
return value
_TRUE_VARIANTS = [True, 'True', '1']
# Build paths inside the project like this: BASE_DIR / 'subdir'.