Fixed parsing

This commit is contained in:
Sven Heidemann 2022-01-16 02:06:01 +01:00
parent 416940fd10
commit 4b14443fa1
9 changed files with 75 additions and 9 deletions

View File

@ -5,11 +5,13 @@ from cpl_core.configuration import ConfigurationABC, ConsoleArgument
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
from cpl_core.environment import ApplicationEnvironment from cpl_core.environment import ApplicationEnvironment
from py_to_uxf_core.abc.attribute_scanner_abc import AttributeScannerABC
from py_to_uxf_core.abc.class_scanner_abc import ClassScannerABC from py_to_uxf_core.abc.class_scanner_abc import ClassScannerABC
from py_to_uxf_core.abc.file_scanner_abc import FileScannerABC from py_to_uxf_core.abc.file_scanner_abc import FileScannerABC
from py_to_uxf_core.abc.function_scanner_abc import FunctionScannerABC from py_to_uxf_core.abc.function_scanner_abc import FunctionScannerABC
from py_to_uxf_core.abc.python_parser_abc import PythonParserABC from py_to_uxf_core.abc.python_parser_abc import PythonParserABC
from py_to_uxf_core.abc.umlet_creator_abc import UmletCreatorABC from py_to_uxf_core.abc.umlet_creator_abc import UmletCreatorABC
from py_to_uxf_core.service.attribute_scanner_service import AttributeScannerService
from py_to_uxf_core.service.class_scanner_service import ClassScannerService from py_to_uxf_core.service.class_scanner_service import ClassScannerService
from py_to_uxf_core.service.file_scanner_service import FileScannerService from py_to_uxf_core.service.file_scanner_service import FileScannerService
from py_to_uxf_core.service.function_scanner_service import FunctionScannerService from py_to_uxf_core.service.function_scanner_service import FunctionScannerService
@ -32,6 +34,7 @@ class Startup(StartupABC):
services.add_transient(FileScannerABC, FileScannerService) services.add_transient(FileScannerABC, FileScannerService)
services.add_transient(ClassScannerABC, ClassScannerService) services.add_transient(ClassScannerABC, ClassScannerService)
services.add_transient(FunctionScannerABC, FunctionScannerService) services.add_transient(FunctionScannerABC, FunctionScannerService)
services.add_transient(AttributeScannerABC, AttributeScannerService)
services.add_singleton(PythonParserABC, PythonParserService) services.add_singleton(PythonParserABC, PythonParserService)
services.add_singleton(UmletCreatorABC, UmletCreatorService) services.add_singleton(UmletCreatorABC, UmletCreatorService)

View File

@ -0,0 +1,12 @@
from abc import ABC, abstractmethod
from py_to_uxf_core.model.python_class_attribute import PythonClassAttribute
class AttributeScannerABC(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
def scan_line_for_attribute(self, line: str) -> PythonClassAttribute: pass

View File

@ -1,7 +1,7 @@
from enum import Enum from enum import Enum
class FunctionAccessModifierEnum(Enum): class AccessModifierEnum(Enum):
public = '+' public = '+'
private = '-' private = '-'

View File

@ -1,5 +1,6 @@
from cpl_query.extension import List from cpl_query.extension import List
from py_to_uxf_core.model.python_class_attribute import PythonClassAttribute
from py_to_uxf_core.model.python_function import PythonFunction from py_to_uxf_core.model.python_function import PythonFunction
@ -8,6 +9,7 @@ class PythonClass:
def __init__(self, name): def __init__(self, name):
self._name = name self._name = name
self._functions = List(PythonFunction) self._functions = List(PythonFunction)
self._attributes = List(PythonClassAttribute)
@property @property
def name(self) -> str: def name(self) -> str:
@ -19,3 +21,6 @@ class PythonClass:
def add_function(self, func: PythonFunction): def add_function(self, func: PythonFunction):
self._functions.append(func) self._functions.append(func)
def add_attribute(self, attribute: PythonClassAttribute):
self._attributes.append(attribute)

View File

@ -0,0 +1,27 @@
from py_to_uxf_core.model.access_modifier_enum import AccessModifierEnum
class PythonClassAttribute:
def __init__(self, access_modifier: AccessModifierEnum, name: str, type: str):
self._access_modifier = access_modifier
self._name = name
self._type = type
@property
def access_modifier(self) -> AccessModifierEnum:
return self._access_modifier
@property
def name(self) -> str:
return self._name
@property
def type(self) -> str:
return self._type
def __str__(self):
return f'{self._name}: {self._type}'
def __repr__(self):
return f'{self._name}: {self._type}'

View File

@ -1,12 +1,12 @@
from cpl_query.extension import List from cpl_query.extension import List
from py_to_uxf_core.model.function_access_modifier_enum import FunctionAccessModifierEnum from py_to_uxf_core.model.access_modifier_enum import AccessModifierEnum
from py_to_uxf_core.model.python_function_argument import PythonFunctionArgument from py_to_uxf_core.model.python_function_argument import PythonFunctionArgument
class PythonFunction: class PythonFunction:
def __init__(self, access_modifier: FunctionAccessModifierEnum, name: str, args: List[PythonFunctionArgument], return_type: str): def __init__(self, access_modifier: AccessModifierEnum, name: str, args: List[PythonFunctionArgument], return_type: str):
self._access_modifier = access_modifier self._access_modifier = access_modifier
self._name = name self._name = name
self._args = args self._args = args

View File

@ -0,0 +1,11 @@
from py_to_uxf_core.abc.attribute_scanner_abc import AttributeScannerABC
from py_to_uxf_core.model.python_class_attribute import PythonClassAttribute
class AttributeScannerService(AttributeScannerABC):
def __init__(self):
AttributeScannerABC.__init__(self)
def scan_line_for_attribute(self, line: str) -> PythonClassAttribute:
pass

View File

@ -4,7 +4,7 @@ import value as value
from cpl_query.extension import List from cpl_query.extension import List
from py_to_uxf_core.abc.function_scanner_abc import FunctionScannerABC from py_to_uxf_core.abc.function_scanner_abc import FunctionScannerABC
from py_to_uxf_core.model.function_access_modifier_enum import FunctionAccessModifierEnum from py_to_uxf_core.model.access_modifier_enum import AccessModifierEnum
from py_to_uxf_core.model.python_function import PythonFunction from py_to_uxf_core.model.python_function import PythonFunction
from py_to_uxf_core.model.python_function_argument import PythonFunctionArgument from py_to_uxf_core.model.python_function_argument import PythonFunctionArgument
@ -25,17 +25,17 @@ class FunctionScannerService(FunctionScannerABC):
# name # name
name = line.split('def ')[1] name = line.split('def ')[1]
name = name.split('(')[0] name = name.split('(')[0]
access_modifier = FunctionAccessModifierEnum.public access_modifier = AccessModifierEnum.public
args = List() args = List()
return_type = 'None' return_type = 'None'
# access_modifier # access_modifier
if name == '__init__' or name == '__repr__' or name == '__str__': if name == '__init__' or name == '__repr__' or name == '__str__':
access_modifier = FunctionAccessModifierEnum.public access_modifier = AccessModifierEnum.public
elif name.startswith('_'): elif name.startswith('_'):
access_modifier = FunctionAccessModifierEnum.protected access_modifier = AccessModifierEnum.protected
elif name.startswith('__'): elif name.startswith('__'):
access_modifier = FunctionAccessModifierEnum.private access_modifier = AccessModifierEnum.private
# args # args
args_str = line.split('(')[1] args_str = line.split('(')[1]

View File

@ -2,6 +2,7 @@ from typing import Optional
from cpl_core.console import Console from cpl_core.console import Console
from py_to_uxf_core.abc.attribute_scanner_abc import AttributeScannerABC
from py_to_uxf_core.abc.class_scanner_abc import ClassScannerABC from py_to_uxf_core.abc.class_scanner_abc import ClassScannerABC
from py_to_uxf_core.abc.file_scanner_abc import FileScannerABC from py_to_uxf_core.abc.file_scanner_abc import FileScannerABC
from py_to_uxf_core.abc.function_scanner_abc import FunctionScannerABC from py_to_uxf_core.abc.function_scanner_abc import FunctionScannerABC
@ -15,13 +16,15 @@ class PythonParserService(PythonParserABC):
self, self,
file_scanner: FileScannerABC, file_scanner: FileScannerABC,
class_scanner: ClassScannerABC, class_scanner: ClassScannerABC,
function_scanner: FunctionScannerABC function_scanner: FunctionScannerABC,
attribute_scanner: AttributeScannerABC
): ):
PythonParserABC.__init__(self) PythonParserABC.__init__(self)
self._file_scanner = file_scanner self._file_scanner = file_scanner
self._class_scanner = class_scanner self._class_scanner = class_scanner
self._function_scanner = function_scanner self._function_scanner = function_scanner
self._attribute_scanner = attribute_scanner
def parse(self): def parse(self):
files = self._file_scanner.scan_files() files = self._file_scanner.scan_files()
@ -67,3 +70,8 @@ class PythonParserService(PythonParserABC):
if func is not None: if func is not None:
cls.add_function(func) cls.add_function(func)
Console.write_line('fu:', func.access_modifier.value, func.name, func.args, func.return_type) Console.write_line('fu:', func.access_modifier.value, func.name, func.args, func.return_type)
attribute = self._attribute_scanner.scan_line_for_attribute(line)
if attribute is not None:
cls.add_attribute(attribute)
Console.write_line('at:', attribute.name, attribute.type)