Added width and height rendering

This commit is contained in:
Sven Heidemann 2022-01-16 14:56:02 +01:00
parent 4b7f0e8231
commit 61b1838c40
6 changed files with 131 additions and 91 deletions

View File

@ -1,3 +1,5 @@
import traceback
from cpl_core.application import ApplicationABC from cpl_core.application import ApplicationABC
from cpl_core.configuration import ConfigurationABC from cpl_core.configuration import ConfigurationABC
from cpl_core.console import Console from cpl_core.console import Console
@ -54,13 +56,19 @@ class Application(ApplicationABC):
Console.write_line(f'Input path:', self._path) Console.write_line(f'Input path:', self._path)
Console.write_line(f'Output path:', self._file) Console.write_line(f'Output path:', self._file)
xml = ''
try:
classes: list[PythonClass] = self._parser.parse() classes: list[PythonClass] = self._parser.parse()
Console.write_line(f'Found {len(classes)} classes')
# self._console_output(classes) # self._console_output(classes)
xml = self._umlet_creator.generate_xml(classes) xml = self._umlet_creator.generate_xml(classes)
except Exception as e:
Console.error('Parsing failed', f'{e} -> {traceback.format_exc()}')
exit()
if not self._file.endswith('.uxf'): if not self._file.endswith('.uxf'):
Console.error(f'Unexpected file {self._file}') Console.error(f'Unexpected file {self._file}')
return exit()
with open(self._file, 'w+') as file: with open(self._file, 'w+') as file:
file.write(xml) file.write(xml)

View File

@ -16,11 +16,11 @@ class PythonClass:
return self._name return self._name
@property @property
def functions(self) -> list[PythonFunction]: def functions(self) -> List[PythonFunction]:
return self._functions return self._functions
@property @property
def attributes(self) -> list[PythonClassAttribute]: def attributes(self) -> List[PythonClassAttribute]:
return self._attributes return self._attributes
def add_function(self, func: PythonFunction): def add_function(self, func: PythonFunction):

View File

@ -27,22 +27,43 @@ class UMLClass:
def position(self) -> Position: def position(self) -> Position:
return self._position return self._position
@position.setter
def position(self, value: int):
self._position = value
@property @property
def dimension(self) -> Dimension: def dimension(self) -> Dimension:
return self._dimension return self._dimension
@dimension.setter
def dimension(self, value: int):
self._dimension = value
def as_xml(self) -> str: def as_xml(self) -> str:
px_per_line = 16
px_per_char = 2.9
self._dimension.height += (self._cls.attributes.count() + self._cls.functions.count()) * px_per_line
longest_line_length = self._dimension.width / px_per_char
attributes = '' attributes = ''
functions = '' functions = ''
if len(self._cls.attributes) > 0: if len(self._cls.attributes) > 0:
for atr in self._cls.attributes: for atr in self._cls.attributes:
attributes += f'{atr.access_modifier.value}{atr.name}: {atr.type}\n' attribute = f'{atr.access_modifier.value}{atr.name}: {atr.type}\n'
if len(attribute) > longest_line_length:
longest_line_length = len(attribute)
attributes += attribute
if len(self._cls.functions) > 0: if len(self._cls.functions) > 0:
for func in self._cls.functions: for func in self._cls.functions:
args = '' args = ''
functions += f'{func.access_modifier.value}{func.name}({args}): {func.return_type}\n' function = f'{func.access_modifier.value}{func.name}({args}): {func.return_type}\n'
if len(function) > longest_line_length:
longest_line_length = len(function)
functions += function
self._dimension.width = round(longest_line_length * px_per_char * px_per_char)
return f"""\ return f"""\
<element> <element>

View File

@ -1,5 +1,6 @@
from typing import Optional from typing import Optional
from cpl_core.console import Console
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
@ -20,7 +21,9 @@ class FunctionScannerService(FunctionScannerABC):
# async def xy(x: int, y: int) -> int: # async def xy(x: int, y: int) -> int:
# async def _xy(x: int, y: int) -> int: # async def _xy(x: int, y: int) -> int:
# async def __xy(x: int, y: int) -> int: # async def __xy(x: int, y: int) -> int:
if line.startswith('def') or line.startswith('async def'): if not line.startswith('def ') and not line.startswith('async def '):
return None
line = line.replace('\t', '') line = line.replace('\t', '')
# name # name
name = line.split('def ')[1] name = line.split('def ')[1]
@ -63,4 +66,3 @@ class FunctionScannerService(FunctionScannerABC):
return_type = return_type_str.split(':')[0] return_type = return_type_str.split(':')[0]
return PythonFunction(access_modifier, name, args, return_type) return PythonFunction(access_modifier, name, args, return_type)
return None

View File

@ -1,3 +1,4 @@
import traceback
from typing import Optional from typing import Optional
from cpl_core.console import Console from cpl_core.console import Console
@ -35,7 +36,11 @@ class PythonParserService(PythonParserABC):
is_function = False is_function = False
with open(file, 'r') as file_content: with open(file, 'r') as file_content:
cls: Optional[PythonClass] = None cls: Optional[PythonClass] = None
for line in file_content.readlines(): lines = file_content.readlines()
for i in range(len(lines)):
try:
line = lines[i]
line_with_tabs = line line_with_tabs = line
line = line.replace(' ', '') line = line.replace(' ', '')
line = line.replace('\t', '') line = line.replace('\t', '')
@ -83,5 +88,8 @@ class PythonParserService(PythonParserABC):
attribute = self._attribute_scanner.scan_line_for_attribute(line) attribute = self._attribute_scanner.scan_line_for_attribute(line)
if attribute is not None: if attribute is not None:
cls.add_attribute(attribute) cls.add_attribute(attribute)
except Exception as e:
Console.error(f'Parsing {file}@{i}', f'{e} -> {traceback.format_exc()}')
exit()
return classes return classes

View File

@ -16,14 +16,15 @@ class UmletCreatorService(UmletCreatorABC):
def generate_xml(self, classes: list[PythonClass]) -> str: def generate_xml(self, classes: list[PythonClass]) -> str:
xml_cls = '' xml_cls = ''
width = 400 default_width = 80
height = 300 default_height = 80
next_x = 10 next_x = 10
for cls in classes: for cls in classes:
uml_cls = UMLClass(cls, Position(next_x, 10), Dimension(width, height)) uml_cls = UMLClass(cls, Position(next_x, 10), Dimension(default_width, default_height))
# save class xml
xml_cls += uml_cls.as_xml() xml_cls += uml_cls.as_xml()
next_x += width + 10 next_x += round(uml_cls.dimension.width, -1) + 10
return f"""\ return f"""\
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>