-
Notifications
You must be signed in to change notification settings - Fork 9
/
dmGlobals.py
472 lines (377 loc) · 16.3 KB
/
dmGlobals.py
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# some variables for global use
import clr
import System
from System.IO import Path, FileInfo
import iniReadWrite
from System import Array
from System import StringSplitOptions
from iniReadWrite import *
from System.Collections.Generic import Dictionary
#clr.AddReference("ComicRack.Engine")
from cYo.Projects.ComicRack.Engine import MangaYesNo, YesNo
############Set Paths##############
FOLDER = FileInfo(__file__).DirectoryName + Path.DirectorySeparatorChar.ToString()
IMGFOLDER = FOLDER + 'images' + Path.DirectorySeparatorChar.ToString()
DATFILE = Path.Combine(FOLDER, 'dataMan.dat') #can load 1.24 text files or 2.3.1 xml files saves as xml
SAMPLEFILE = Path.Combine(FOLDER, 'dataManSample.dat')
INIFILE = Path.Combine(FOLDER, 'dataMan.ini')
USERINI = Path.Combine(FOLDER, 'user.ini')
BAKFILE = Path.Combine(FOLDER, 'dataMan.bak')
ERRFILE = Path.Combine(FOLDER, 'dataMan.err')
TMPFILE = Path.Combine(FOLDER, 'dataMan.tmp')
LOGFILE = Path.Combine(FOLDER, 'dataMan.log')
CHKFILE = Path.Combine(FOLDER, 'dataMan.chk') # will be created once the configuration is saved
GUIEXE = Path.Combine(FOLDER, 'crdmgui.exe')
##############End Set Paths##############
################Set Constant variables##############
CRLISTDELIMITER = ', '
GROUPHEADER = '#@ GROUP '
RULESETHEADER = '#@ NAME '
GROUPENDER = '#@ END_GROUP'
ENDER = '#@ END_RULES'
AUTHORHEADER = '#@ AUTHOR'
NOTESHEADER = '#@ NOTES'
########################### Images #############################
ICON_SMALL = Path.Combine(IMGFOLDER, 'dataMan16.ico')
ICON = Path.Combine(IMGFOLDER, 'dataMan.ico')
IMAGE = Path.Combine(IMGFOLDER, 'dataMan.png')
IMAGESEARCH = Path.Combine(IMGFOLDER, 'search.png')
IMAGEADD = Path.Combine(IMGFOLDER, 'yes.png')
IMAGEAPPLY = Path.Combine(IMGFOLDER, 'Apply.png')
IMAGETRASH = Path.Combine(IMGFOLDER, 'Trash.png')
IMAGEDELETE_SMALL = Path.Combine(IMGFOLDER, 'erase.png')
IMAGEDOWN = Path.Combine(IMGFOLDER, 'down.png')
IMAGELIGHTNING = Path.Combine(IMGFOLDER, 'lightning.png')
IMAGETEXT = Path.Combine(IMGFOLDER, 'text.png')
DONATE = 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=UQ7JZY366R85S'
WIKI = 'http://code.google.com/p/cr-data-manager/'
MANUAL = 'http://code.google.com/p/cr-data-manager/downloads/list'
COMPARE_CASE_SENSITIVE = ReadKeyAsBool(INIFILE, 'CaseSensitive')
#############End set constant variables##################
######################Read From dataman.ini###############################
VERSION = System.Version(ReadKey(INIFILE,'Version'))
DMLISTDELIMITER = ReadKey(INIFILE, 'ListDelimiter')
DATEFORMAT = ReadKey(INIFILE, 'DateTimeFormat')
####FIELDTYPES AND ALLOWED MODIFIERS#########
ALLOWEDKEYS = ReadKeyAsStringList(INIFILE, 'allowedKeys')
ALLOWEDVALS = ReadKeyAsStringList(INIFILE, 'allowedVals')
READONLYKEYS = ReadKeyAsStringList(INIFILE, 'ReadOnlyKeys')
ALLOWEDKEYMODIFIERS = ReadKeyAsStringList(INIFILE, 'allowedKeyModifiers')
ALLOWEDVALMODIFIERS = ReadKeyAsStringList(INIFILE, 'allowedValModifiers')
FIELDSSTRING = ReadKeyAsStringList(INIFILE, 'stringKeys')
ALLOWEDKEYMODIFIERSSTRING = ReadKeyAsStringList(INIFILE, 'allowedKeyModifiersString')
ALLOWEDVALMODIFIERSSTRING = ReadKeyAsStringList(INIFILE, 'allowedValModifiersString')
FIELDSLIST = ReadKeyAsStringList(INIFILE, 'multiValueKeys')
ALLOWEDKEYMODIFIERSLIST = ReadKeyAsStringList(INIFILE, 'allowedKeyModifiersMulti')
ALLOWEDVALMODIFIERSLIST = ReadKeyAsStringList(INIFILE, 'allowedValModifiersMulti')
FIELDSDATETIME = ReadKeyAsStringList(INIFILE, 'dateTimeKeys')
ALLOWEDKEYMODIFIERSDATETIME = ReadKeyAsStringList(INIFILE, 'allowedKeyModifierDateTime')
ALLOWEDVALMODIFIERSDATETIME = ReadKeyAsStringList(INIFILE, 'allowedValModifierDateTime')
FIELDSLANGUAGEISO = ReadKeyAsStringList(INIFILE, 'languageISOKeys')
ALLOWEDKEYMODIFIERSLANGUAGEISO = ReadKeyAsStringList(INIFILE, 'allowedKeyModifierLanguageISO')
ALLOWEDVALMODIFIERSLANGUAGEISO = ReadKeyAsStringList(INIFILE, 'allowedValModifierLanguageISO')
FIELDSMANGAYESNO = ReadKeyAsStringList(INIFILE, 'mangaYesNoKeys')
ALLOWEDKEYMODIFIERSMANGAYESNO = ReadKeyAsStringList(INIFILE, 'allowedKeyModifierMangaYesNo')
ALLOWEDVALMODIFIERSMANGAYESNO = ReadKeyAsStringList(INIFILE, 'allowedValModifierMangaYesNo')
FIELDSYESNO = ReadKeyAsStringList(INIFILE, 'yesNoKeys')
ALLOWEDKEYMODIFIERSYESNO = ReadKeyAsStringList(INIFILE, 'allowedKeyModifierYesNo')
ALLOWEDVALMODIFIERSYESNO = ReadKeyAsStringList(INIFILE, 'allowedValModifierYesNo')
FIELDSBOOL = ReadKeyAsStringList(INIFILE, 'boolKeys')
ALLOWEDKEYMODIFIERSBOOL = ReadKeyAsStringList(INIFILE, 'allowedKeyModifierBool')
ALLOWEDVALMODIFIERSBOOL = ReadKeyAsStringList(INIFILE, 'allowedValModifierBool')
ALLOWEDKEYMODIFIERSCUSTOM = ReadKeyAsStringList(INIFILE, 'allowedKeyModifierString')
ALLOWEDVALMODIFIERSCUSTOM = ReadKeyAsStringList(INIFILE, 'allowedValModifierString')
FIELDSNUMERIC = ReadKeyAsStringList(INIFILE, 'numericalKeys')
FIELDSPSUEDONUMERIC = ReadKeyAsStringList(INIFILE, 'pseudoNumericalKeys')
ALLOWEDKEYMODIFIERSNUMERIC = ReadKeyAsStringList(INIFILE, 'allowedKeyModifiersNumeric')
ALLOWEDVALMODIFIERSNUMERIC = ReadKeyAsStringList(INIFILE, 'allowedValModifiersNumeric')
REGEXVARREPLACEKEYS = ReadKeyAsStringList(INIFILE, 'regExVarReplaceFields')
MULTIPARAMMODIFIERS = ReadKeyAsStringList(INIFILE, 'multipleParamModifiers')
FIELDSMULTILINE = ReadKeyAsStringList(INIFILE, 'multiLineKeys')
####END FIELDTYPES AND ALLOWED MODIFIERS#########
#################End read From dataman.ini############################
#####################Read From User.ini#######################
BreakAfterFirstError = ReadKeyAsBool(USERINI, "BreakAfterFirstError")
TraceGeneralMessages = ReadKeyAsBool(USERINI, 'TraceGeneralMessages')
TraceFunctionMessages = ReadKeyAsBool(USERINI, 'TraceFunctionMessages')
SortLists = ReadKeyAsBool(USERINI, 'SortLists')
#############type conversion utilities ########################
def StringToDate(strDate):
if strDate == '':
return System.DateTime(1,1,1)
return System.DateTime.Parse(strDate)
def IsDateTime(value):
return isinstance(value, System.DateTime)
def StringToInt(strNumber):
if strNumber == '':
return int(-1)
return int(strNumber)
def IsInt(value):
return isinstance(value, System.Int32)
def StringToFloat(strFloat):
fValue = float(-1)
fValue = float(strFloat)
return fValue
def IsFloat(value):
return isinstance(value, float)
def CRStringToList(strList):
return strList.Split(Array[str](CRLISTDELIMITER), StringSplitOptions.RemoveEmptyEntries)
def DMStringToList(strList):
return strList.split(DMLISTDELIMITER)
def IsList(value):
return isinstance(value, list) or isinstance(value, Array[str])
#all of the below will throw errors if the conversion is not possible
#this behaviour is intended and should be caught in the calling function
#which should report errors at runtime or in the log
def StringToBool(strValue):
return bool.Parse(strValue)
def IsBool(value):
return isinstance(value, bool)
def StringToYesNo(strValue):
ynReturn = None
if strValue == 'Unknown' or strValue == '':
ynReturn = YesNo.Unknown
elif strValue.lower() == 'yes':
ynReturn = YesNo.Yes
elif strValue.lower() == 'no':
ynReturn = YesNo.No
if ynReturn == None:
raise dmConversionError(strValue, 'ComicRack.Engine.YesNo')
return ynReturn
def IsYesNo(value):
return isinstance(value, YesNo)
def StringToMangaYesNo(strValue):
ynReturn = None
if strValue.lower() == 'yes':
ynReturn = MangaYesNo.Yes
elif strValue.lower() == 'no':
ynReturn = MangaYesNo.No
elif strValue.lower() == 'yesandrighttoleft':
ynReturn = MangaYesNo.YesAndRightToLeft
if ynReturn == None:
raise dmConversionError(strValue, 'ComicRack.Engine.MangaYesNo')
return ynReturn
def IsMangaYesNo(value):
return isinstance(value, MangaYesNo)
def ToString(obj):
strReturn = ''
if isinstance(obj, YesNo):
if obj == YesNo.Unknown:
strReturn = ''
else:
strReturn = obj.ToString()
elif isinstance(obj, MangaYesNo):
if obj == MangaYesNo.Unknown:
strReturn = ''
else:
strReturn = obj.ToString()
pass
elif isinstance(obj, System.DateTime):
strReturn = obj.ToString(DATEFORMAT)
else:
strReturn = obj.ToString()
return strReturn
#############end conversion utilities###############
#######Field And Modifier helpers##############
def ValidModifiers(strKey, strRuleOrAction):
strListReturn = []
if strRuleOrAction.lower() == 'rule':
strListReturn = ValidKeyModifiers(strKey)
elif strRuleOrAction.lower() == 'action':
strListReturn = ValidValModifiers(strKey)
return strListReturn
def ValidKeyModifiers(strKey):
strListReturn = []
if KeyFieldType(strKey) == 'STRING':
strListReturn = ALLOWEDKEYMODIFIERSSTRING
elif KeyFieldType(strKey) == 'LIST':
strListReturn = ALLOWEDKEYMODIFIERSLIST
elif KeyFieldType(strKey) == 'DATETIME':
strListReturn = ALLOWEDKEYMODIFIERSDATETIME
elif KeyFieldType(strKey) == 'BOOL':
strListReturn = ALLOWEDKEYMODIFIERSBOOL
elif KeyFieldType(strKey) == 'YESNO':
strListReturn = ALLOWEDKEYMODIFIERSYESNO
elif KeyFieldType(strKey) == 'NUMERIC':
strListReturn = ALLOWEDKEYMODIFIERSNUMERIC
elif KeyFieldType(strKey) == 'PSUEDONUMERIC':
strListReturn = ALLOWEDKEYMODIFIERSNUMERIC
elif KeyFieldType(strKey) == 'LANGUAGEISO':
strListReturn = ALLOWEDKEYMODIFIERSLANGUAGEISO
return strListReturn
def ValidValModifiers(strKey):
strListReturn = []
if KeyFieldType(strKey) == 'STRING':
strListReturn = ALLOWEDVALMODIFIERSSTRING
elif KeyFieldType(strKey) == 'LIST':
strListReturn = ALLOWEDVALMODIFIERSLIST
elif KeyFieldType(strKey) == 'DATETIME':
strListReturn = ALLOWEDVALMODIFIERSDATETIME
elif KeyFieldType(strKey) == 'BOOL':
strListReturn = ALLOWEDVALMODIFIERSBOOL
elif KeyFieldType(strKey) == 'YESNO':
strListReturn = ALLOWEDVALMODIFIERSYESNO
elif KeyFieldType(strKey) == 'NUMERIC':
strListReturn = ALLOWEDVALMODIFIERSNUMERIC
elif KeyFieldType(strKey) == 'PSUEDONUMERIC':
strListReturn = ALLOWEDVALMODIFIERSNUMERIC
elif KeyFieldType(strKey) == 'LANGUAGEISO':
strListReturn = ALLOWEDVALMODIFIERSLANGUAGEISO
if strKey in REGEXVARREPLACEKEYS:
strListReturn.append('RegExVarReplace')
strListReturn.append('RegExVarAppend')
return strListReturn
def KeyFieldType(strKey):
strReturn = 'UNKNOWN'
if strKey in FIELDSSTRING:
strReturn = 'STRING'
elif strKey in FIELDSLIST:
strReturn = 'LIST'
elif strKey in FIELDSDATETIME:
strReturn = 'DATETIME'
elif strKey in FIELDSBOOL:
strReturn = 'BOOL'
elif strKey in FIELDSYESNO:
strReturn = 'YESNO'
elif strKey in FIELDSMANGAYESNO:
strReturn = 'MANGAYESNO'
elif strKey in FILEDSNUMERIC:
strReturn = 'NUMERIC'
elif strKey in FIELDSPSUEDONUMERIC:
strReturn = 'PSUEDONUMERIC'
elif strKey in FIELDSLANGUAGEISO:
strReturn = 'LANGUAGEISO'
return strReturn
def GetAvailableKeys(strRuleOrAction):
strListReturn = []
if strRuleOrAction.lower() == 'rule':
strListReturn.extend(ALLOWEDKEYS)
elif strRuleOrAction == 'action':
strListReturn.extend(ALLOWEDVALS)
strListReturn.extend(REGEXVARREPLACEKEYS)
return strListReturn
def GetAppendString(strKey):
if strKey in FIELDSMULTILINE:
return "\r\n"
else:
return ", "
pass
def GetValueType(strFieldName):
strReturn = unknown
#String
if strFieldName in FIELDSSTRING or strFieldName in FIELDSCUSTOM:
strReturn = 'string'
elif strFieldName in FIELDSNUMERIC:
strReturn = 'numeric'
elif strFieldName in FIELDSDATETIME:
strReturn = 'datetime'
elif strFieldName in FIELDSPSUEDONUMERIC:
strReturn = 'psuedonumeric'
elif strFieldName in FIELDSBOOL:
strReturn = 'bool'
elif strFieldName in FIELDSLIST:
strReturn = 'list'
elif strFieldName in FILEDSLANGUAGEISO:
strReturn = 'languageiso'
elif strFieldName in FIELDSYESNO:
strReturn = 'yesno'
elif strFieldName in FIELDSMANGAYESNO:
strReturn = 'mangayesno'
return strReturn
def ComicVineFields():
return ['Series','Volume','Number','Title','Published','ReleasedTime','AlternateSeries','Publisher','Imprint','Writer','Penciller','Colorist','Inker','Letterer','CoverArtist','Editor','Summary','Characters']
#######Field And Modifier helpers##############
def WriteStartTime():
WriteKey(USERINI, 'LastProcessStart', System.DateTime.Now.ToString('yyyy/MM/dd hh:mm:ss'))
def WriteEndTime():
WriteKey(USERINI, 'LastProcessEnd', System.DateTime.Now.ToString('yyyy/MM/dd hh:mm:ss'))
def WriteDurationtime(strDuration):
WriteKey(USERINI, 'LastProcessDuration', strDuration)
#################### Book Helpers #####################
def CompareValues(book, cmpDict):
"""Compares Values of book to a dictionary of values to see if any values have changed
Attributes:
book -- the comicbook object holding current values after process has run
cmpDict -- The Dictionary of values that before the process
"""
strReturn = ''
touchedFields = 0
for field in cmpDict:
bookValue = None
if field in ALLOWEDVALS:
bookValue = getattr(book, field)
else:
try:
bookValue = book.GetCustomValue(field)
except Exception as er:
bookValue = ''
if bookValue != cmpDict[field]:
touchedFields = touchedFields + 1
extrastring = ''
if not field in ALLOWEDVALS:
extrastring = 'Custom '
strReturn = strReturn + ' ' + extrastring + 'Field: ' + field
strReturn = strReturn + ' Previous Value: ' + cmpDict[field] + System.Environment.NewLine
strReturn = strReturn + ' New Value: ' + bookValue.ToString() + System.Environment.NewLine
return strReturn
def CreateBookDict(book):
dictReturn = dict()
for item in ALLOWEDVALS:
if item != 'Custom':
dictReturn[item] = getattr(book, item)
else:
customs = book.GetCustomValues()
for field in customs:
dictReturn[field.Key] = field.Value
pass
pass
return dictReturn
def AppendReport(strInitialReport, strExtendReport):
strReturn = ''
if strInitialReport == None or strInitialReport == '':
strReturn = strExtendReport
else:
strReturn = strInitialReport + System.Environment.NewLine + strExtendReport
return strReturn
def GetTouchCount(book, tmpDict):
touchedFields = 0
for field in tmpDict:
bookValue = None
if field in ALLOWEDVALS:
bookValue = getattr(book, field)
else:
try:
bookValue = book.GetCustomValue(field)
except Exception as er:
bookValue = ''
if bookValue != tmpDict[field]:
touchedFields = touchedFields + 1
return touchedFields
def IsCustomField(strFieldName):
bCustom = True
return bCustom
################ Errors ################
class dmException(Exception):
@property
def Message(self):
return self.msg
def __init__(self, strMessage):
self.msg = strMessage
class dmConversionError(dmException):
def __init__(self, value, type):
self.value = value
self.type = type
self.msg = 'Could not convert \'' + self.value + '\' to type \'' + self.type + '\'.'
pass
def GetStringAsList(strList):
return strList.Split(CRLISTDELIMITER)
class dmNodeCompileException(dmException):
def __init__(self, strMessage, dmNodeParent):
dmException.__init__(self, strMessage)
self.Parent = dmNodeParent
class dmGroupCompileException(dmNodeCompileException):
def __init__(strMessage, dmContainerParent):
dmNodeCompileException.__init__(self, strMessage ,dmNodeParent)
class dmCollectionCompileError(dmNodeCompileException):
def __init__(self, strMessage, dmNodeParent):
dmNodeCompileException.__init__(self, strMessage, dmNodeParent)