-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter_validator.py
More file actions
49 lines (37 loc) · 1.87 KB
/
chapter_validator.py
File metadata and controls
49 lines (37 loc) · 1.87 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
from pydantic import Field
from novel_tools.framework import NovelData, Type
from .__validator__ import Validator, BaseOptions
class Options(BaseOptions):
discard_chapters: bool = Field(description='If set to True, restart indexing at the beginning of each new volume.')
volume_tag: str | None = Field(default=None, description='Only validates if the current volume is the given tag.')
class ChapterValidator(Validator):
"""
Validates a chapter, potentially within a volume.
"""
def __init__(self, args):
options = Options(**args)
self.init_fields(options)
self.discard_chapters = options.discard_chapters
self.volume_tag = options.volume_tag
self.curr_volume = None
self.validate_curr_volume = True
def check(self, data: NovelData) -> bool:
if data.type == Type.VOLUME_TITLE:
self.validate_curr_volume = self.volume_tag is None or self.volume_tag == data.get('tag')
self.curr_volume = data
if self.discard_chapters:
self.indices.clear()
self.curr_index = 0
return False
return self.validate_curr_volume and data.type == Type.CHAPTER_TITLE and data.index >= 0 and data.get(
'tag') == self.tag
def _duplicate_message(self, data: NovelData, corrected_index: int) -> str:
return f'Duplicate chapter{self.volume_message} - expected: {corrected_index}, actual: {self._format(data)}'
def _missing_message(self, data: NovelData, corrected_index: int) -> str:
return f'Missing chapter{self.volume_message} - expected: {corrected_index}, actual: {self._format(data)}'
@property
def volume_message(self):
if self.curr_volume:
volume_format = self.curr_volume.format('index = {index}, content = {content}')
return f' in volume ({volume_format})'
return ''