-
Notifications
You must be signed in to change notification settings - Fork 2
/
jobObjects.py
419 lines (334 loc) · 10.5 KB
/
jobObjects.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
# job objects
#Copyright (C) 2023 yamahubuki <[email protected]>
import namedPipe
import os
import queue
import subprocess
import uuid
import constants
import events
import globalVars
import popplerUtil
from enum import IntFlag, auto
from logging import getLogger
next_job_id = 1
class job():
def __init__(self, path, temp, source, engine):
global next_job_id
self._path = path
self._id = next_job_id
next_job_id += 1
self._temp = temp
self._source = source
self._engine = engine
if path:
self._name = os.path.basename(path)
else:
self._name = "file-%d" % self._id
self.onEvent = None
self.log = getLogger("%s.job-%s" % (constants.APP_NAME, self.getId()))
self.log.info("created job named %s" % (self.getName()))
self.convertQueue = queue.Queue()
self.processQueue = queue.Queue()
self.processedItem = [virtualAllPageItem(self)]
self.processedItemCount = 0
self.status = jobStatus(0)
self.totalCount = 0
self.selectedPage = 0
def setOnEvent(self, callback):
assert callable(callback)
self.onEvent = callback
def cancel(self):
if self.isDone():
return
self.raiseStatusFlag(jobStatus.CANCEL)
def getName(self):
return self._name
def getPath(self):
return self._path
def getSelectedPage(self):
return self.selectedPage
def hasCancelFlag(self):
if self.getStatus() & jobStatus.CANCEL:
return True
return False
def isDone(self):
return (self.status & jobStatus.PROCESS_COMPLETE) > 0
def setName(self, name):
self._name = name
self.onEvent(events.job.NAME_CHANGED, job = self)
def setSelectedPage(self, page):
self.selectedPage = page
def addCreatedItem(self, item):
self.log.debug("item added")
self.convertQueue.put(item)
self.onEvent(events.item.ADDED, job = self, item = item)
def getConvertItem(self):
item = self.convertQueue.get(block=True)
if not self.getStatus() & jobStatus.CONVERT_STARTED:
self.onEvent(events.job.CONVERT_STARTED, job = self)
self.raiseStatusFlag(jobStatus.CONVERT_STARTED)
self.onEvent(events.item.CONVERT_STARTED, job = self, item = item)
return item
def addConvertedItem(self, item):
self.processQueue.put(item)
self.totalCount += 1
self.onEvent(events.item.CONVERTED, job = self, item = item)
globalVars.jobList[0].addConvertedItem(item)
def getProcessItem(self):
item = self.processQueue.get(block=True)
if not self.getStatus() & jobStatus.PROCESS_STARTED:
self.onEvent(events.job.PROCESS_STARTED, job = self)
self.raiseStatusFlag(jobStatus.PROCESS_STARTED)
self.onEvent(events.item.PROCESS_STARTED, job = self, item = item)
return item
def addProcessedItem(self, item):
self.log.debug("item processed")
item.setPageNumber(len(self.getProcessedItems()))
self.processedItem.append(item)
if item.isDone():
self.processedItemCount += 1
globalVars.jobList[0].addProcessedItem(self,item)
self.onEvent(events.item.PROCESSED, job = self, item = item)
def endSource(self):
self.convertQueue.put(None)
self.raiseStatusFlag(jobStatus.SOURCE_END)
self.onEvent(events.job.SOURCE_END, job = self)
def endConvert(self):
self.processQueue.put(None)
self.raiseStatusFlag(jobStatus.CONVERT_COMPLETE)
self.log.info("convert completed")
self.onEvent(events.job.CONVERT_COMPLETED, job = self)
def endEngine(self):
self.raiseStatusFlag(jobStatus.PROCESS_COMPLETE)
self.log.debug("process completed")
self.onEvent(events.job.PROCESS_COMPLETED, job = self)
self.lowerStatusFlag(jobStatus.CANCEL) # 完了したのでキャンセルフラグはもういらない
def getProcessedItems(self):
return self.processedItem
def getProcessedCount(self):
return self.processedItemCount
def getTotalCount(self):
return self.totalCount
def raiseStatusFlag(self, flag):
assert isinstance(flag, jobStatus)
self.log.debug("raised %s" % (flag))
self.status |= flag
self.onEvent(events.job.STATUS_CHANGED, job = self)
def lowerStatusFlag(self, flag):
assert isinstance(flag, jobStatus)
self.log.debug("lower %s" % (flag))
self.status &= -1-flag
self.onEvent(events.job.STATUS_CHANGED, job = self)
def getStatus(self):
return self.status
def getStatusString(self):
if self.status == 0:
return _("待機中")
if self.isDone():
if self.processedItemCount == self.getTotalCount() and self.convertQueue.empty():
return _("完了")
else:
return _("取消済")
if self.hasCancelFlag():
return _("取消中")
if not (self.status & jobStatus.SOURCE_END):
return _("待機中")
if not (self.status & jobStatus.CONVERT_STARTED):
return _("待機中")
if not (self.status & jobStatus.CONVERT_COMPLETE):
return _("準備中")
if not (self.status & jobStatus.PROCESS_STARTED):
return _("認識待ち")
else:
return _("認識中")
def getId(self):
return self._id
#
# for view
#
def __getitem__(self, key):
if key == 0:
return self.getName()
if key == 1:
return self.getStatusString()
if key == 2:
return "%d/%d" % (self.getProcessedCount(), self.getTotalCount())
if key == 3:
return type(self._engine).getName()
raise KeyError
def __setitem__(self, key, value):
raise NotImplementedError
def __len__(self):
return 4
class virtualAllItemJob(job):
"""
App.pyにてリスト先頭にInsertされ、「すべて」として表示される架空のジョブ
"""
def __init__(self):
self._path = None
self._id = 0
self._temp = None
self._name = _("(すべて)")
self.onEvent = None
self.log = getLogger("%s.job-%s" % (constants.APP_NAME, self.getId()))
self.log.info("created job named %s" % (self.getName()))
self.processedItem = [virtualAllPageItem(self)]
self.selectedPage = 0
def getPath(self):
raise NotImplementedError
def addCreatedItem(self, item):
raise NotImplementedError
def getConvertItem(self):
raise NotImplementedError
def addConvertedItem(self, item):
self.onEvent(events.item.CONVERTED, job = self, item = item)
def getProcessItem(self):
raise NotImplementedError
def addProcessedItem(self, parentJob, item):
# 全体の中でのインデックスを数えてくる
# まずは対象ジョブより上に対象ジョブ以外のジョブがあれば、そいつらのページ全部
idx = 0
for i in globalVars.jobList:
if i == parentJob:
break;
idx += i.getProcessedCount()
# 対象ジョブの対象ページより前のページ
idx += parentJob.getProcessedCount() - 1
self.processedItem.insert(idx,item)
self.onEvent(events.item.PROCESSED, job = self, item = item)
def endSource(self):
raise NotImplementedError
def endConvert(self):
raise NotImplementedError
def endEngine(self):
raise NotImplementedError
def getProcessedCount(self):
ret = 0
for job in globalVars.jobList[1:]:
ret += job.getProcessedCount()
return ret
def getTotalCount(self):
ret = 0
for job in globalVars.jobList[1:]:
ret += job.getTotalCount()
return ret
def raiseStatusFlag(self, flag):
raise NotImplementedError
def lowerStatusFlag(self, flag):
raise NotImplementedError
def getStatus(self):
raise NotImplementedError
def getStatusString(self):
raise NotImplementedError
def __getitem__(self, key):
# ステータスとエンジン名は表示できない
if key == 1:
return ""
if key == 2:
return "%d/%d" % (self.getProcessedCount(), self.getTotalCount())
if key == 3:
return ""
return super().__getitem__(key)
class item:
def __init__(self, path):
self.path = path # 画像パス
self.done = False # 認識完了していればTrue。認識待ちやキャンセルズミはFalse
self.pageNumber = 1
self.cursorPos = 0
self.text = "" # 認識結果テキスト
def getPath(self):
return self.path
def getFormat(self):
if not hasattr(self, "format"):
self._register_format()
return self.format
def hasGrayScaleFile(self):
return hasattr(self, "grayScaledPath")
def setGrayScaleFile(self, path):
self.grayScaledPath = path
def getGrayScaleFile(self):
return self.grayScaledPath
def _register_format(self):
ext = os.path.splitext(self.getPath())[1][1:]
self.format = constants.EXT_TO_FORMAT.get(ext.lower(), constants.FORMAT_UNKNOWN)
if self.format == constants.FORMAT_PDF_UNKNOWN:
info = popplerUtil.getInfo(self.getPath())
if ("Pages" in info) and int(info["Pages"]) > 1:
self.format = constants.FORMAT_PDF_MULTI_PAGE
return
# 単一ページの場合は埋め込みテキストの含まれるPDFであるか判定
pipeServer = namedPipe.Server(constants.PIPE_NAME_PREFIX + "PdfTextCheck" + str(uuid.uuid4()))
pipeServer.start()
subprocess.run((os.getcwd() + "/poppler/bin/pdftotext", "-enc", "UTF-8", self.getPath(), pipeServer.getFullName()))
list = pipeServer.getNewMessageList()
pipeServer.exit()
pipeServer.close()
text = list[0]
if len(text) > 0:
self.format = constants.FORMAT_PDF_TEXT
else:
self.format = constants.FORMAT_PDF_IMAGE
def getCursorPos(self):
return self.cursorPos
def getPageNumber(self):
return self.pageNumber
def getText(self):
return self.text
def isDone(self):
return self.done
def setCursorPos(self, pos):
self.cursorPos = pos
def setPageNumber(self, pageNumber):
self.pageNumber = pageNumber
def setText(self, text):
self.text = text
self.done = True
def __getitem__(self, key):
if key == 0:
name = _("%dページ" % self.getPageNumber())
if not self.isDone():
name += _("(未処理)")
return name
raise KeyError
def __setitem__(self, key, value):
raise NotImplementedError
def __len__(self):
return 1
class virtualAllPageItem(item):
def __init__(self, job):
self.job = job
self.pageNumber = -1
self.cursorPos = 0
def getPath(self):
raise NotImplementedError
def getFormat(self):
raise NotImplementedError
def hasGrayScaleFile(self):
raise NotImplementedError
def setGrayScaleFile(self, path):
raise NotImplementedError
def getGrayScaleFile(self):
raise NotImplementedError
def _register_format(self):
raise NotImplementedError
def setText(self, text):
raise NotImplementedError
def getText(self):
ret = ""
for item in self.job.getProcessedItems()[1:]:
ret += item.getText()+"\n"
return ret
def isDone(self):
raise NotImplementedError
def __getitem__(self, key):
if key == 0:
return _("(すべて)")
raise KeyError
class jobStatus(IntFlag):
SOURCE_END = auto() # ソース処理完了
CONVERT_STARTED = auto() # 変換処理開始
CONVERT_COMPLETE = auto() # 変換処理完了
PROCESS_STARTED = auto() #認識処理開始
PROCESS_COMPLETE = auto() # 認識処理完了
CANCEL = auto() # キャンセルの要求がなされた