• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SPF-OST / pytrnsys_gui / 11662562960

29 Oct 2024 03:09PM UTC coverage: 67.508% (-0.08%) from 67.591%
11662562960

push

github

web-flow
Merge pull request #564 from SPF-OST/560-black-change-line-length-to-pep8-standard-of-79-and-check-ci-reaction

changed line length in black to 79

1054 of 1475 new or added lines in 174 files covered. (71.46%)

150 existing lines in 74 files now uncovered.

10399 of 15404 relevant lines covered (67.51%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.95
/trnsysGUI/components/plugin/specification/load.py
1
import abc as _abc
1✔
2
import collections.abc as _cabc
1✔
3
import dataclasses as _dc
1✔
4
import pathlib as _pl
1✔
5
import pkgutil as _pu
1✔
6

7
import pydantic as _pc
1✔
8
import yaml as _yaml
1✔
9
import yaml.parser as _yp
1✔
10

11
import pytrnsys.utils.result as _res
1✔
12
import trnsysGUI as _trnsysGui
1✔
13

14
from . import model as _model
1✔
15
from .. import _paths
1✔
16

17

18
@_dc.dataclass
1✔
19
class ResourceLoaderBase(_abc.ABC):
1✔
20
    @_abc.abstractmethod
21
    def loadBytes(self, resourcePath: str) -> bytes:
22
        raise NotImplementedError()
23

24

25
class FileResourceLoader(ResourceLoaderBase):
1✔
26
    def loadBytes(self, resourcePath: str) -> bytes:
1✔
27
        path = _pl.Path(resourcePath)
1✔
28
        return path.read_bytes()
1✔
29

30

31
class PackageResourceLoader(ResourceLoaderBase):
1✔
32
    def loadBytes(self, resourcePath: str) -> bytes:
1✔
33
        data = _pu.get_data(_trnsysGui.__name__, resourcePath)
1✔
34
        if not data:
1✔
35
            raise ValueError("No data found at resource path.", resourcePath)
×
36

37
        return data
1✔
38

39

40
PACKAGE_RESOURCE_LOADER = PackageResourceLoader()
1✔
41

42

43
@_dc.dataclass
1✔
44
class Loader:
1✔
45
    baseResourcePath: str
1✔
46
    resourceLoader: ResourceLoaderBase
1✔
47

48
    @staticmethod
1✔
49
    def createPackageResourceLoader():
1✔
50
        loader = Loader(
1✔
51
            _paths.COMPONENTS_BASE_RESOURCE_PATH, PACKAGE_RESOURCE_LOADER
52
        )
53
        return loader
1✔
54

55
    def load(self, typeName: str) -> _res.Result[_model.Specification]:
1✔
56
        pluginPath = f"{self.baseResourcePath}/{typeName}"
1✔
57

58
        specificationResult = self._loadSpecification(pluginPath)
1✔
59
        if _res.isError(specificationResult):
1✔
60
            return _res.error(specificationResult)
1✔
61
        specification = _res.value(specificationResult)
1✔
62

63
        return specification
1✔
64

65
    def _loadSpecification(
1✔
66
        self, pluginResourcePath: str
67
    ) -> _res.Result[_model.Specification]:
68
        specResourcePath = f"{pluginResourcePath}/spec.yaml"
1✔
69
        bytesResult = self._loadData(specResourcePath)
1✔
70
        if _res.isError(bytesResult):
1✔
71
            return _res.error(bytesResult)
1✔
72
        _bytes = _res.value(bytesResult)
1✔
73

74
        dataResult = self._loadYaml(_bytes)
1✔
75
        if _res.isError(dataResult):
1✔
76
            return _res.error(dataResult).withContext(
1✔
77
                f"Syntax error in `{specResourcePath}`"
78
            )
79
        data = _res.value(dataResult)
1✔
80

81
        specificationResult = self._createSpecification(data)
1✔
82
        if _res.isError(specificationResult):
1✔
83
            return _res.error(specificationResult).withContext(
1✔
84
                f"Error in specification `{specResourcePath}`"
85
            )
86
        specification = _res.value(specificationResult)
1✔
87

88
        return specification
1✔
89

90
    def _loadData(self, resourcePath: str) -> _res.Result[bytes]:
1✔
91
        try:
1✔
92
            return self.resourceLoader.loadBytes(resourcePath)
1✔
93
        except FileNotFoundError:
1✔
94
            return _res.Error(f"`{resourcePath}` could not be found.")
1✔
95
        except PermissionError:
×
NEW
96
            return _res.Error(
×
97
                f"`{resourcePath}` could not be read. Maybe it's a directory instead of a file?"
98
            )
99

100
    @staticmethod
1✔
101
    def _loadYaml(_bytes: bytes) -> _res.Result[_cabc.Mapping]:
1✔
102
        try:
1✔
103
            return _yaml.safe_load(_bytes)
1✔
104
        except _yp.ParserError as parseError:
1✔
105
            return _res.Error(str(parseError))
1✔
106

107
    @staticmethod
1✔
108
    def _createSpecification(
1✔
109
        data: _cabc.Mapping,
110
    ) -> _res.Result[_model.Specification]:
111
        try:
1✔
112
            return _model.Specification(**data)
1✔
113
        except _pc.ValidationError as validationError:
1✔
114
            return _res.Error(str(validationError))
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc