Skip to content

Commit

Permalink
Merge pull request #668 from Dessia-tech/testing
Browse files Browse the repository at this point in the history
Towards 0.16.0
  • Loading branch information
GhislainJ committed Feb 2, 2024
2 parents ffdd282 + e563267 commit 1b61ccb
Show file tree
Hide file tree
Showing 33 changed files with 1,239 additions and 411 deletions.
27 changes: 27 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,12 +6,34 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## 0.16.0

###

- Excel Reader

### Build

- Global : remove usage of pkg_resources to use importlib.resources instead

### Fix

- Serialization : Fix wrong path in serialization that result in pointers cycles in some cases
- Workflow : Fix export workflow in .py script

### Refactor

- Export: Add all export formats in zip file by iterating over _export_formats()
- Serialization : Mutualize DessiaObject and Regular objects pointer serialization


## 0.15.4

### Fix

- Workflow : Add display block class discovery


## 0.15.3

### Fix
Expand Down Expand Up @@ -54,9 +76,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- PhysicalObject: Pool to_stem and to_step_stream
- Workflow : Block input and output names have been enhanced
- Workflow : Inputs now have entries for Imposed Variable Values

### Fixed

- Document generator : Fix add_picture to .docx

### Removed

- Workflow : jointjs plot
Expand Down
22 changes: 8 additions & 14 deletions code_pylint.py
Expand Up @@ -29,29 +29,23 @@
"protected-access": 48, # Highly dependant on our "private" conventions. Keeps getting raised
"arguments-differ": 1,
"too-many-locals": 5, # Reduce by dropping vectored objects
"too-many-branches": 10, # Huge refactor needed. Will be reduced by schema refactor
"unused-argument": 4, # Some abstract functions have unused arguments (plot_data). Hence cannot decrease
"too-many-branches": 9, # Huge refactor needed. Will be reduced by schema refactor
"unused-argument": 3, # Some abstract functions have unused arguments (plot_data). Hence cannot decrease
"cyclic-import": 2, # Still work to do on Specific based DessiaObject
"too-many-arguments": 20, # Huge refactor needed
"too-many-arguments": 18, # Huge refactor needed
"too-few-public-methods": 3, # Abstract classes (Errors, Checks,...)
"too-many-return-statements": 8, # Huge refactor needed. Will be reduced by schema refactor
"import-outside-toplevel": 5, # TODO : will reduced in a future work (when tests are ready)
"too-many-instance-attributes": 7, # Huge refactor needed (workflow, etc...)
"broad-exception-caught": 8, # Necessary in order not to raise non critical errors. Will be reduced by schema refactor
"bare-except": 1, # Necessary in order not to raise non critical errors. Will be reduced by schema refactor
"too-many-instance-attributes": 6, # Huge refactor needed (workflow, etc...)
"broad-exception-caught": 9, # Necessary in order not to raise non critical errors.
"too-many-public-methods": 2, # Try to lower by splitting DessiaObject and Workflow
"too-many-statements": 1, # Will be solved by schema refactor and jsonchema removal
"undefined-loop-variable": 1, # Fearing to break the code by solving it
"attribute-defined-outside-init": 3, # For test purposes
}

ERRORS_WITHOUT_TIME_DECREASE = ['protected-access', 'arguments-differ', 'too-many-locals', 'too-many-branches',
'unused-argument', 'cyclic-import', 'too-many-arguments', 'too-few-public-methods',
'too-many-return-statements', 'import-outside-toplevel',
'too-many-instance-attributes', 'broad-except', 'bare-except',
"broad-exception-caught",
'too-many-public-methods', 'too-many-statements', 'undefined-loop-variable',
'attribute-defined-outside-init']
'too-many-return-statements', 'import-outside-toplevel', 'too-many-instance-attributes',
'bare-except', "broad-exception-caught", 'too-many-public-methods',
'undefined-loop-variable']

print("pylint version: ", __version__)

Expand Down
6 changes: 1 addition & 5 deletions coverage.py
Expand Up @@ -14,12 +14,8 @@
RATCHET_COVERAGE = 3.

untracked_modules = ["dessia_common/templates.py",
"dessia_common/utils.py",
"dessia_common/optimization.py",
'workflows/forms_simulation.py',
"utils/jsonschema.py",
"utils/serialization.py"
]
'workflows/forms_simulation.py']

print("untracked modules:", untracked_modules)

Expand Down
4 changes: 2 additions & 2 deletions dessia_common/__init__.py
Expand Up @@ -2,9 +2,9 @@
# -*- coding: utf-8 -*-
""" __init__ for dessia_common module. """

import pkg_resources
from importlib.metadata import version

__version__ = pkg_resources.require("dessia_common")[0].version
__version__ = version("dessia_common")

FLOAT_TOLERANCE = 1e-9
REF_MARKER = "$ref"
Expand Down
74 changes: 42 additions & 32 deletions dessia_common/core.py
Expand Up @@ -34,6 +34,7 @@
import dessia_common.files as dcf
from dessia_common.document_generator import DocxWriter
from dessia_common.decorators import get_decorated_methods, DISPLAY_DECORATORS
from dessia_common.excel_reader import ExcelReader


def __getattr__(name):
Expand Down Expand Up @@ -295,6 +296,26 @@ def from_json(cls, filepath: str):

return cls.dict_to_object(dict_)

@classmethod
def from_xlsx_stream(cls, stream: dcf.BinaryFile):
"""
Load object from a xlsx stream.
:param stream: either a string representing the stream
"""
reader = ExcelReader(stream)
return reader.read_workbook()

@classmethod
def from_xlsx(cls, filepath: str):
"""
Load object from a xlsx file.
:param filepath: either a string representing the filepath
"""
stream = dcf.BinaryFile.from_file(filepath=filepath)
return cls.from_xlsx_stream(stream=stream)

def check_list(self, level: str = 'error', check_platform: bool = True) -> dcc.CheckList:
""" Return a list of potential info, warning and issues on the instance, that might be user custom. """
check_list = dcc.CheckList([])
Expand Down Expand Up @@ -420,7 +441,7 @@ def _display_settings_from_decorator_name(cls, decorator_name: str):
settings.append(DisplaySetting(selector=selector, type_=type_, method=name,
serialize_data=serialize_data, load_by_default=load_by_default))
return settings

@classmethod
def _display_settings_from_decorators(cls) -> List[DisplaySetting]:
""" Return a list, computed from decorated functions, of objects describing how to call displays. """
Expand All @@ -432,7 +453,7 @@ def _display_from_selector(self, selector: str) -> DisplayObject:
track = ""
try:
data = attrmethod_getter(self, display_setting.method)(**display_setting.arguments)
except:
except Exception:
data = None
track = tb.format_exc()

Expand Down Expand Up @@ -557,13 +578,15 @@ def zip_settings(self):
Excel file stream generated by calling 'to_xlsx_stream' method.
JSON file stream generated by calling 'save_to_stream' method.
"""
excel_stream = dcf.BinaryFile("excel_export.xlsx")
self.to_xlsx_stream(excel_stream)

json_stream = dcf.JsonFile("json_export.json")
self.save_to_stream(json_stream)

return [excel_stream, json_stream]
streams = []
for export_format in self._export_formats():
if export_format.extension != "zip":
method_name = export_format.method_name
stream_class = dcf.StringFile if export_format.text else dcf.BinaryFile
stream = stream_class(filename=f"export.{export_format.extension}")
getattr(self, method_name)(stream)
streams.append(stream)
return streams

def to_zip_stream(self, archive: dcf.BinaryFile) -> List[dcf.BinaryFile]:
""" Generates a zipped archive of the streams returned by 'zip_settings' method. """
Expand Down Expand Up @@ -650,7 +673,10 @@ def to_step(self, filepath: str):
:param filepath: a str representing a filepath
"""
return self.volmdlr_volume_model().to_step(filepath=filepath)
if not filepath.endswith('.step'):
filepath += '.step'
with open(filepath, 'w', encoding='utf-8') as file:
self.to_step_stream(stream=file)

def to_step_stream(self, stream):
"""
Expand All @@ -673,13 +699,17 @@ def to_stl_stream(self, stream):
""" Export Object CAD to given stream as STL. """
return self.volmdlr_volume_model().to_stl_stream(stream=stream)

def to_stl(self, filepath):
def to_stl(self, filepath: str):
"""
Exports the CAD of the object to STL. Works if the class define a custom volmdlr model.
:param filepath: a str representing a filepath
"""
return self.volmdlr_volume_model().to_stl(filepath=filepath)
if not filepath.endswith('.stl'):
filepath += '.stl'

with open(filepath, 'wb', encoding='utf-8') as file:
self.to_stl_stream(stream=file)

def babylonjs(self, use_cdn=True, debug=False, **kwargs):
""" Show the 3D volmdlr of an object by calling volmdlr_volume_model method and plot in in browser. """
Expand All @@ -698,26 +728,6 @@ def save_babylonjs_to_file(self, filename: str = None, use_cdn: bool = True, deb
"""
self.volmdlr_volume_model(**kwargs).save_babylonjs_to_file(filename=filename, use_cdn=use_cdn, debug=debug)

def zip_settings(self):
"""
Returns a list of streams containing different representations of the object.
Excel file stream generated by calling 'to_xlsx_stream' method.
JSON file stream generated by calling 'save_to_stream' method.
STEP file stream generated by calling 'to_step_stream' method.
HTML file stream generated by calling 'to_html_stream' method.
STL file stream generated by calling 'to_stl_stream' method.
"""
streams = DessiaObject.zip_settings(self)
step_stream = dcf.StringFile("step_export.stp")
self.to_step_stream(step_stream)
html_stream = self.to_html_stream(dcf.StringFile(filename="html_export.html"))
stl_stream = dcf.BinaryFile("stl_export.stl")
self.to_stl_stream(stl_stream)

streams.extend([step_stream, html_stream, stl_stream])
return streams

def _export_formats(self) -> List[ExportFormat]:
""" Return a list of objects describing how to call 3D exports. """
formats = DessiaObject._export_formats(self)
Expand Down
20 changes: 17 additions & 3 deletions dessia_common/document_generator.py
Expand Up @@ -4,7 +4,9 @@

import re
from typing import List, Union

import docx
from docx.shared import Inches

from dessia_common.files import BinaryFile

Expand Down Expand Up @@ -53,8 +55,12 @@ def add_to_section(self, section):
""" Add header to section. """
super()._add_to_section(section=section, type_='header')

def add_picture(self, section, image_path: str, width: int = None, height: int = None):
def add_picture(self, section, image_path: str, width: float = None, height: float = None):
""" Add picture for header. """
if width:
width = Inches(width)
if height:
height = Inches(height)
super()._add_picture(section=section, type_='header', image_path=image_path, width=width, height=height)


Expand All @@ -70,8 +76,12 @@ def add_to_section(self, section):
""" Add footer to section. """
super()._add_to_section(section=section, type_='footer')

def add_picture(self, section, image_path: str, width: int = None, height: int = None):
def add_picture(self, section, image_path: str, width: float = None, height: float = None):
""" Add picture for footer. """
if width:
width = Inches(width)
if height:
height = Inches(height)
super()._add_picture(section=section, type_='footer', image_path=image_path, width=width, height=height)


Expand Down Expand Up @@ -243,8 +253,12 @@ def add_header_footer_picture(self, image_path: str, width: int = None, height:
self.section.add_picture_to_document(document=self.document, image_path=image_path, width=width, height=height)
return self

def add_picture(self, image_path: str, width: int = None, height: int = None) -> 'DocxWriter':
def add_picture(self, image_path: str, width: float = None, height: float = None) -> 'DocxWriter':
""" Add an image to the document. """
if width:
width = Inches(width)
if height:
height = Inches(height)
self.document.add_picture(image_path, width=width, height=height)
return self

Expand Down

0 comments on commit 1b61ccb

Please sign in to comment.