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

SPF-OST / pytrnsys_gui / 11576810878

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

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

55.43
/trnsysGUI/project.py
1
__all__ = [
1✔
2
    "CreateProject",
3
    "LoadProject",
4
    "MigrateProject",
5
    "Project",
6
    "getProject",
7
    "getExistingEmptyDirectory",
8
    "getLoadOrMigrateProject",
9
]
10

11
import dataclasses as _dc
1✔
12
import pathlib as _pl
1✔
13
import typing as _tp
1✔
14

15
import PyQt5.QtWidgets as _qtw
1✔
16

17
import trnsysGUI.common.cancelled as _ccl
1✔
18
import trnsysGUI.messageBox as mb
1✔
19
from trnsysGUI import constants
1✔
20
from trnsysGUI.dialogs.startupDialog import StartupDialog
1✔
21
from trnsysGUI.recentProjectsHandler import RecentProjectsHandler
1✔
22

23

24
@_dc.dataclass
1✔
25
class CreateProject:
1✔
26
    jsonFilePath: _pl.Path
1✔
27

28

29
@_dc.dataclass
1✔
30
class LoadProject:
1✔
31
    jsonFilePath: _pl.Path
1✔
32

33

34
@_dc.dataclass
1✔
35
class MigrateProject:
1✔
36
    oldJsonFilePath: _pl.Path
1✔
37
    newProjectFolderPath: _pl.Path
1✔
38

39

40
Project = CreateProject | LoadProject | MigrateProject
1✔
41

42

43
def getProject() -> _ccl.MaybeCancelled[Project]:
1✔
44
    createOpenMaybeCancelled = StartupDialog.showDialogAndGetResult()
1✔
45

46
    while not _ccl.isCancelled(createOpenMaybeCancelled):
1✔
47
        createOpen = _ccl.value(createOpenMaybeCancelled)
1✔
48

49
        projectMaybeCancelled = (
1✔
50
            loadRecentProject(createOpen)
51
            if isinstance(createOpen, _pl.Path)
52
            else _getProjectInternal(
53
                constants.CreateNewOrOpenExisting(createOpen)
54
            )
55
        )
56
        if not _ccl.isCancelled(projectMaybeCancelled):
1✔
57
            project = _ccl.value(projectMaybeCancelled)
1✔
58
            assert isinstance(project, (CreateProject, LoadProject))
1✔
59
            RecentProjectsHandler.addProject(project.jsonFilePath)
1✔
60
            return _tp.cast(
1✔
61
                Project, project
62
            )  # Don't know why mypy requires this cast
63

64
        createOpenMaybeCancelled = StartupDialog.showDialogAndGetResult()
×
65

66
    return _ccl.CANCELLED
×
67

68

69
def _getProjectInternal(
1✔
70
    createOrOpenExisting: "constants.CreateNewOrOpenExisting",
71
) -> _ccl.MaybeCancelled[Project]:
72
    if createOrOpenExisting == constants.CreateNewOrOpenExisting.OPEN_EXISTING:
×
73
        return getLoadOrMigrateProject()
×
74

75
    if createOrOpenExisting == constants.CreateNewOrOpenExisting.CREATE_NEW:
×
76
        return getCreateProject()
×
77

NEW
78
    raise AssertionError(
×
79
        f"Unknown value for enum {constants.CreateNewOrOpenExisting}: {createOrOpenExisting}"
80
    )
81

82

83
def getCreateProject(
1✔
84
    startingDirectoryPath: _tp.Optional[_pl.Path] = None,
85
) -> _ccl.MaybeCancelled[CreateProject]:
NEW
86
    projectFolderPathMaybeCancelled = getExistingEmptyDirectory(
×
87
        startingDirectoryPath
88
    )
89
    if _ccl.isCancelled(projectFolderPathMaybeCancelled):
×
90
        return _ccl.CANCELLED
×
91
    projectFolderPath = _ccl.value(projectFolderPathMaybeCancelled)
×
92

93
    jsonFilePath = projectFolderPath / f"{projectFolderPath.name}.json"
×
94

95
    return CreateProject(jsonFilePath)
×
96

97

98
def getExistingEmptyDirectory(
1✔
99
    startingDirectoryPath: _tp.Optional[_pl.Path] = None,
100
) -> _ccl.MaybeCancelled[_pl.Path]:
101
    while True:
×
102
        selectedDirectoryPathString = _qtw.QFileDialog.getExistingDirectory(
×
103
            caption="Select new project directory",
104
            directory=str(startingDirectoryPath),
105
        )
106
        if not selectedDirectoryPathString:
×
107
            return _ccl.CANCELLED
×
108

109
        selectedDirectoryPath = _pl.Path(selectedDirectoryPathString)
×
110

111
        if _isEmptyDirectory(selectedDirectoryPath):
×
112
            return selectedDirectoryPath
×
113

NEW
114
        mb.MessageBox.create(
×
115
            messageText=constants.DIRECTORY_MUST_BE_EMPTY,
116
            buttons=[_qtw.QMessageBox.Ok],
117
        )
118

119

120
def _isEmptyDirectory(path: _pl.Path) -> bool:
1✔
121
    if not path.is_dir():
×
122
        return False
×
123
    containedFilesAndDirectories = list(path.iterdir())
×
124
    isDirectoryEmpty = len(containedFilesAndDirectories) == 0
×
125
    return isDirectoryEmpty
×
126

127

128
def getLoadOrMigrateProject() -> (
1✔
129
    _ccl.MaybeCancelled[LoadProject | MigrateProject]
130
):
NEW
131
    projectFolderPathString, _ = _qtw.QFileDialog.getOpenFileName(
×
132
        caption="Open diagram", filter="*.json"
133
    )
134
    if not projectFolderPathString:
×
135
        return _ccl.CANCELLED
×
136
    jsonFilePath = _pl.Path(projectFolderPathString)
×
137
    projectFolderPath = jsonFilePath.parent
×
138
    return checkIfProjectEnviromentIsValid(projectFolderPath, jsonFilePath)
×
139

140

141
def loadRecentProject(
1✔
142
    projectPath: _pl.Path,
143
) -> _ccl.MaybeCancelled[LoadProject | MigrateProject]:
144
    try:
1✔
145
        if not projectPath.exists():
1✔
146
            if (
×
147
                mb.MessageBox.create(
148
                    messageText=constants.RECENT_MOVED_OR_DELETED,
149
                    buttons=[_qtw.QMessageBox.Ok],
150
                )
151
                == _qtw.QMessageBox.Ok
152
            ):
153
                RecentProjectsHandler.removeProject(projectPath)
×
154
                return _ccl.CANCELLED
×
155
        else:
156
            return checkIfProjectEnviromentIsValid(
1✔
157
                projectPath.parent, projectPath
158
            )
159
    except TypeError:
×
160
        if (
×
161
            mb.MessageBox.create(
162
                messageText=constants.NO_RECENT_AVAILABLE,
163
                buttons=[_qtw.QMessageBox.Ok],
164
            )
165
            == _qtw.QMessageBox.Ok
166
        ):
167
            return _ccl.CANCELLED
×
168
    return _ccl.CANCELLED
×
169

170

171
def checkIfProjectEnviromentIsValid(
1✔
172
    projectFolderPath, jsonFilePath
173
) -> _ccl.MaybeCancelled[LoadProject | MigrateProject]:
174
    containingFolderIsCalledSameAsJsonFile = (
1✔
175
        projectFolderPath.name == jsonFilePath.stem
176
    )
177
    ddckFolder = projectFolderPath / "ddck"
1✔
178
    if not containingFolderIsCalledSameAsJsonFile or not ddckFolder.is_dir():
1✔
179
        oldJsonFilePath = jsonFilePath
1✔
180
        if (
1✔
181
            mb.MessageBox.create(
182
                messageText=constants.NO_PROPER_PROJECT_ENVIRONMENT
183
            )
184
            == _qtw.QMessageBox.Cancel
185
        ):
UNCOV
186
            return _ccl.CANCELLED
×
187
        maybeCancelled = getExistingEmptyDirectory(
1✔
188
            startingDirectoryPath=projectFolderPath.parent
189
        )
190
        if _ccl.isCancelled(maybeCancelled):
1✔
191
            return _ccl.CANCELLED
×
192
        newProjectFolderPath = _ccl.value(maybeCancelled)
1✔
193
        return MigrateProject(oldJsonFilePath, newProjectFolderPath)
1✔
194
    return LoadProject(jsonFilePath)
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