-
-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathprojectdata.py
More file actions
384 lines (316 loc) · 11.4 KB
/
Copy pathprojectdata.py
File metadata and controls
384 lines (316 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"""
novelWriter – Project Data Class
================================
File History:
Created: 2022-10-30 [2.0rc2] NWProjectData
This file is a part of novelWriter
Copyright (C) 2022 Veronica Berglyd Olsen and novelWriter contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations
import logging
import uuid
from datetime import date
from typing import TYPE_CHECKING, Any
from novelwriter.common import (
checkBool, checkInt, checkStringNone, checkUuid, isHandle,
makeFileNameSafe, simplified
)
from novelwriter.core.status import NWStatus
if TYPE_CHECKING: # pragma: no cover
from novelwriter.core.project import NWProject
logger = logging.getLogger(__name__)
class NWProjectData:
"""Core: Project Data Class
The class holds all project data from the main XML file, aside from
the list of project items.
"""
def __init__(self, project: NWProject) -> None:
self._project = project
# Project Meta
self._uuid = ""
self._name = ""
self._author = ""
self._saveCount = 0
self._autoCount = 0
self._editTime = 0
# Project Settings
self._doBackup = True
self._projGoal = 1
self._projDeadline = date.today()
self._sessGoal = 1
self._sessGoalAuto = False
self._language = None
self._spellCheck = False
self._spellLang = None
# Project Dictionaries
self._initCounts = [0, 0]
self._currCounts = [0, 0]
self._lastHandle: dict[str, str | None] = {
"editor": None,
"viewer": None,
"novelTree": None,
"outline": None,
}
self._autoReplace: dict[str, str] = {}
self._titleFormat: dict[str, str] = {
"title": "%title%",
"chapter": "%title%",
"unnumbered": "%title%",
"scene": "* * *",
"section": "",
}
self._status = NWStatus(NWStatus.STATUS)
self._import = NWStatus(NWStatus.IMPORT)
return
##
# Properties
##
@property
def uuid(self) -> str:
"""Return the project ID."""
return self._uuid
@property
def name(self) -> str:
"""Return the project name."""
return self._name
@property
def fileSafeName(self) -> str:
"""Return the project name in a file name safe format."""
return makeFileNameSafe(self._name)
@property
def author(self) -> str:
"""Return the project author."""
return self._author
@property
def saveCount(self) -> int:
"""Return the count of project saves."""
return self._saveCount
@property
def autoCount(self) -> int:
"""Return the count of project auto-saves."""
return self._autoCount
@property
def editTime(self) -> int:
"""Return the number of seconds the project has been edited."""
return self._editTime
@property
def doBackup(self) -> bool:
"""Return the backup setting."""
return self._doBackup
@property
def projGoal(self) -> int:
"""Return the project goal."""
return self._projGoal
@property
def projDeadline(self) -> date | None:
"""Return the project deadline."""
return self._projDeadline
@property
def sessGoal(self) -> int:
"""Return the session goal."""
return self._sessGoal
@property
def sessGoalAuto(self) -> bool:
"""Return the automatic session goal setting."""
return self._sessGoalAuto
@property
def language(self) -> str | None:
"""Return the project language setting."""
return self._language
@property
def spellCheck(self) -> bool:
"""Return the spell check enabled setting."""
return self._spellCheck
@property
def spellLang(self) -> str | None:
"""Return the spell check language."""
return self._spellLang
@property
def initCounts(self) -> tuple[int, int]:
"""Return the initial count of words for novel and note
documents.
"""
return self._initCounts[0], self._initCounts[1]
@property
def currCounts(self) -> tuple[int, int]:
"""Return the current count of words for novel and note
documents.
"""
return self._currCounts[0], self._currCounts[1]
@property
def lastHandle(self) -> dict[str, str | None]:
"""Return the dictionary of last used handles for various
components of the GUI.
"""
return self._lastHandle
@property
def autoReplace(self) -> dict[str, str]:
"""Return the auto-replace dictionary."""
return self._autoReplace
@property
def itemStatus(self) -> NWStatus:
"""Return the status settings object."""
return self._status
@property
def itemImport(self) -> NWStatus:
"""Return the importance settings object."""
return self._import
##
# Methods
##
def incSaveCount(self) -> None:
"""Increment the save count by one."""
self._saveCount += 1
self._project.setProjectChanged(True)
return
def incAutoCount(self) -> None:
"""Increment the auto save count by one."""
self._autoCount += 1
self._project.setProjectChanged(True)
return
##
# Getters
##
def getLastHandle(self, component: str) -> str | None:
"""Retrieve the last used handle for a given component."""
return self._lastHandle.get(component, None)
##
# Setters
##
def setUuid(self, value: Any) -> None:
"""Set the project id."""
value = checkUuid(value, "")
if not value:
self._uuid = str(uuid.uuid4())
elif value != self._uuid:
self._uuid = value
self._project.setProjectChanged(True)
return
def setName(self, value: str | None) -> None:
"""Set a new project name."""
if value != self._name:
self._name = simplified(str(value or ""))
self._project.setProjectChanged(True)
return
def setAuthor(self, value: str | None) -> None:
"""Set the author value."""
if value != self._author:
self._author = simplified(str(value or ""))
self._project.setProjectChanged(True)
return
def setSaveCount(self, value: Any) -> None:
"""Set the save count from last session."""
self._saveCount = checkInt(value, 0)
self._project.setProjectChanged(True)
return
def setAutoCount(self, value: Any) -> None:
"""Set the auto save count from last session."""
self._autoCount = checkInt(value, 0)
self._project.setProjectChanged(True)
return
def setEditTime(self, value: Any) -> None:
"""Set the edit time from last session."""
self._editTime = checkInt(value, 0)
self._project.setProjectChanged(True)
return
def setDoBackup(self, value: Any) -> None:
"""Set the do write backup flag."""
if value != self._doBackup:
self._doBackup = checkBool(value, False)
self._project.setProjectChanged(True)
return
def setProjGoal(self, value: Any) -> None:
"""Set the project goal."""
if value != self._projGoal:
self._projGoal = checkInt(value, self._projGoal)
self._project.setProjectChanged(True)
return
def setProjDeadline(self, value: Any) -> None:
"""Set the project deadline."""
if value != self._projDeadline and isinstance(value, date):
self._projDeadline = value
self._project.setProjectChanged(True)
return
def setSessGoal(self, value: Any) -> None:
"""Set the session goal."""
if value != self._sessGoal:
self._sessGoal = checkInt(value, self._sessGoal)
self._project.setProjectChanged(True)
return
def setSessGoalAuto(self, value: Any) -> None:
"""Set the session goal."""
if value != self._sessGoalAuto:
self._sessGoalAuto = checkBool(value, False)
self._project.setProjectChanged(True)
return
def setLanguage(self, value: str | None) -> None:
"""Set the project language."""
if value != self._language:
self._language = checkStringNone(value, None)
self._project.setProjectChanged(True)
return
def setSpellCheck(self, value: Any) -> None:
"""Set the spell check flag."""
if value != self._spellCheck:
self._spellCheck = checkBool(value, False)
self._project.setProjectChanged(True)
return
def setSpellLang(self, value: str | None) -> None:
"""Set the spell check language."""
if value != self._spellLang:
self._spellLang = checkStringNone(value, None)
self._project.setProjectChanged(True)
return
def setLastHandle(self, value: str | None, component: str) -> None:
"""Set a last used handle into the handle registry for a given
component.
"""
if isinstance(component, str):
self._lastHandle[component] = checkStringNone(value, None)
self._project.setProjectChanged(True)
return
def setLastHandles(self, value: dict) -> None:
"""Set the full last handles dictionary to a new set of values.
This is intended to be used at project load.
"""
if isinstance(value, dict):
for key, entry in value.items():
if key in self._lastHandle:
self._lastHandle[key] = str(entry) if isHandle(entry) else None
self._project.setProjectChanged(True)
return
def setInitCounts(self, novel: Any = None, notes: Any = None) -> None:
"""Set the word count totals for novel and note files."""
if novel is not None:
self._initCounts[0] = checkInt(novel, 0)
self._currCounts[0] = checkInt(novel, 0)
if notes is not None:
self._initCounts[1] = checkInt(notes, 0)
self._currCounts[1] = checkInt(notes, 0)
return
def setCurrCounts(self, novel: Any = None, notes: Any = None) -> None:
"""Set the word count totals for novel and note files."""
if novel is not None:
self._currCounts[0] = checkInt(novel, 0)
if notes is not None:
self._currCounts[1] = checkInt(notes, 0)
return
def setAutoReplace(self, value: dict) -> None:
"""Set the auto-replace dictionary."""
if isinstance(value, dict):
self._autoReplace = {}
for key, entry in value.items():
if isinstance(entry, str):
self._autoReplace[key] = simplified(entry)
self._project.setProjectChanged(True)
return