Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcommandLineParser.py
More file actions
Latest commit
134 lines (105 loc) · 5.21 KB
/
Copy pathcommandLineParser.py
File metadata and controls
134 lines (105 loc) · 5.21 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
from __future__ importunicode_literals
importre
try:
fromtypingimportOptional
except:
pass
classAriaStatusUpdate:
regexAriaCompletionStatus=re.compile(r"#[0-9a-zA-Z]+\s([^/]+/[^/]+)\((100|\d\d|\d)%\)")
regexAriaETA=re.compile(r"ETA:([^\]]+)")
regexAriaConnectionAndSpeed=re.compile(r'CN:([^D]+)DL:([^E]+)', re.IGNORECASE)
regexAriaChecksumError=re.compile(r'Checksum error detected\.\s*file=(.*)', re.IGNORECASE)
def__init__(self, amountCompletedString, percentCompleted, numConnections, speed, ETAString):
self.amountCompletedString=amountCompletedString
self.percentCompleted=percentCompleted
self.ETAString=ETAString
self.speed=speed
self.numConnections=numConnections
#Note: Sometimes can get lines like "99% 35615" without the - [filename] part. This will be missed by this parser.
classSevenZipStatusUpdate:
#Note: extracted file count is OPTIONAL - if few files, 7zip omits it. In that case, match[2] = None
regexSevenZipPercentComplete=re.compile(r"(100|\d\d|\d)%") # use with .search()
regexSevenZipFileCountAndName=re.compile(r"^\s*\d+ - *.*\s*$") # use with .match()
regexSevenZipFileCountOnly=re.compile(r"^\s*\d+\s*$") # use with .match()
regexSevenZipExtractionStarted=re.compile(r"^\s*Extracting archive:\s*.*") # use with .match()
def__init__(self, percentCompleted, numItemsCompleted, currentlyProcessingFileName):
self.percentCompleted=percentCompleted
self.numItemsCompleted=numItemsCompleted
self.currentlyProcessingFilename=currentlyProcessingFileName
classSeventhModStatusUpdate:
regexSeventhModStatus=re.compile(r"<<< \s*Status:\s*(100|\d\d|\d)%\s*(.+) >>>")
def__init__(self, overallPercentage, currentTask):
# type: (int, str) -> None
self.overallPercentage=overallPercentage
self.currentTask=currentTask
# parse a line like: [#7f0d78 27MiB/910MiB(3%) CN:8 DL:4.2MiB ETA:3m27s]
# or: [#99893f 1.1MiB/910MiB(0%) CN:8 DL:1.1MiB ETA:12m50s]
deftryGetAriaStatusUpdate(ariaStatusUpdateString):
# type: (str) -> Optional[AriaStatusUpdate]
match=AriaStatusUpdate.regexAriaCompletionStatus.search(ariaStatusUpdateString)
ifnotmatchorlen(match.groups()) <2:
returnNone
amountCompletedString=match.group(1)
percentCompleted=int(match.group(2))
ETAString="N/A"
match=AriaStatusUpdate.regexAriaETA.search(ariaStatusUpdateString)
ifmatch:
ETAString=match.groups()[0]
# Search for num connections and speed separately so they don't cause the ETA search to fail
match=AriaStatusUpdate.regexAriaConnectionAndSpeed.search(ariaStatusUpdateString)
ifmatch:
numConnections=match.groups()[0]
speed=match.groups()[1]
else:
numConnections="N/A"
speed="N/A"
returnAriaStatusUpdate(amountCompletedString, percentCompleted, numConnections, speed, ETAString)
deftryGetAriaChecksumError(ariaChecksumErrorString):
# type: (str) -> Optional[str]
"""
Matches a message like:
"Checksum error detected. file=Umineko Question (Ch. 1-4) Downloads/Umineko-Graphics-1080p-v2.7z"
"""
match=AriaStatusUpdate.regexAriaChecksumError.search(ariaChecksumErrorString)
returnmatch.groups()[0] ifmatchisnotNoneelseNone
#if none of the other types of lines match, and you see a percent number (eg 54%), assume it's 7zip
deftryGetSevenZipPercent(sevenZipStatusUpdateString):
# type: (str) -> Optional[str]
match=SevenZipStatusUpdate.regexSevenZipPercentComplete.search(sevenZipStatusUpdateString)
ifnotmatchorlen(match.groups()) <1:
returnNone
returnint(match.groups()[0])
#look for 10211 - HigurashiEp02_Data\StreamingAs . ctrum\ps3\s02\02\130200358.txt
#or: 99% 10339 - HigurashiEp02_Data\StreamingAs . ctrum\ps3\s02\02\130200486.txt
deftryGetSevenZipFilecountAndFileNameString(sevenZipStatusUpdateString):
# type: (str) -> Optional[str]
#NOTE: 'match' is used here, not 'search'
ifSevenZipStatusUpdate.regexSevenZipFileCountAndName.match(sevenZipStatusUpdateString):
returnsevenZipStatusUpdateString
returnNone
deftryGetSevenZipFileCount(sevenZipStatusUpdateString):
# type: (str) -> Optional[str]
# NOTE: 'match' is used here, not 'search'
ifSevenZipStatusUpdate.regexSevenZipFileCountOnly.match(sevenZipStatusUpdateString):
returnsevenZipStatusUpdateString
returnNone
deftryGetSevenZipExtractionStarted(sevenZipStatusUpdateString):
# type: (str) -> Optional[str]
# NOTE: 'match' is used here, not 'search'
ifSevenZipStatusUpdate.regexSevenZipExtractionStarted.match(sevenZipStatusUpdateString):
returnsevenZipStatusUpdateString
returnNone
deftryGetSevenZipTestArchive(sevenZipStatusUpdateString):
# type: (str) -> Optional[str]
ifsevenZipStatusUpdateString.lstrip().startswith('Testing archive:'):
returnsevenZipStatusUpdateString
returnNone
deftryGetOverallStatus(overallStatusString):
match=SeventhModStatusUpdate.regexSeventhModStatus.search(overallStatusString)
ifnotmatchorlen(match.groups()) <2:
returnNone
returnSeventhModStatusUpdate(overallPercentage=int(match.group(1)), currentTask=match.group(2))
# Print a status update which will be recognized by the command line parser
defprintSeventhModStatusUpdate(overallPercentage, currentTask):
# type: (int, str) -> None
print("<<< Status: {}% {} >>>".format(overallPercentage, currentTask))