Parse comic book archive names using the simple ‘parse’ parser.
A more sophisticated library like pyparsing or rebulk might be able to build a faster, more powerful matching engine with fewer parsers with optional fields. But this brute force method with the parse library is effective, simple and easy to read and to contribute to.
Bases: IntegerField
A month or day, dumped as two digits so a date reads (1950-11-01).
Source code in comicbox/formats/filename/schema.py
103
104
105
106
107
108
109
110 | class DatePartField(IntegerField):
"""A month or day, dumped as two digits so a date reads `(1950-11-01)`."""
ZERO_FILL = 2
def __init__(self, **kwargs: Any) -> None:
"""Dump as a string, which is what the zero fill applies to."""
super().__init__(as_string=True, **kwargs)
|
Dump as a string, which is what the zero fill applies to.
Source code in comicbox/formats/filename/schema.py
| def __init__(self, **kwargs: Any) -> None:
"""Dump as a string, which is what the zero fill applies to."""
super().__init__(as_string=True, **kwargs)
|
Bases: BaseRenderModule
Filename Render module.
Source code in comicbox/formats/filename/schema.py
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 | class FilenameRenderModule(BaseRenderModule):
"""Filename Render module."""
@staticmethod
def _sanitize(fn: str) -> str:
"""Replace characters so the result is a single portable filename."""
for char, replacement in _FILENAME_REPLACEMENTS:
fn = fn.replace(char, replacement)
return fn
@override
@classmethod
def dumps(cls, obj: dict, *args: Any, **kwargs: Any) -> str:
"""Dump dict to filename string."""
data: dict = obj.get(FilenameSchema.ROOT_TAG, {})
fn = dict2comicfn(data, *args, **kwargs)
return cls._sanitize(fn)
@staticmethod
def _is_non_filename_format(s: str | bytes | bytearray) -> bool:
"""Detect if the input is xml, yaml or json."""
if not s:
return True
t = str(s).split("\n")[0].strip().lower()
return t.startswith(_OTHER_SCHEMA_STARTS) or t.endswith(_OTHER_SCHEMA_ENDS)
@override
@classmethod
def loads(
cls,
s: str | bytes | bytearray,
*args: Any,
**kwargs: Any,
) -> dict[str, dict] | None:
"""Load filename to dict."""
if cls._is_non_filename_format(s):
return None
if cleaned_s := cls.clean_string(s):
sub_data = comicfn2dict(cleaned_s, *args, **kwargs)
return {FilenameSchema.ROOT_TAG: sub_data}
return None
|
Dump dict to filename string.
Source code in comicbox/formats/filename/schema.py
| @override
@classmethod
def dumps(cls, obj: dict, *args: Any, **kwargs: Any) -> str:
"""Dump dict to filename string."""
data: dict = obj.get(FilenameSchema.ROOT_TAG, {})
fn = dict2comicfn(data, *args, **kwargs)
return cls._sanitize(fn)
|
Load filename to dict.
Source code in comicbox/formats/filename/schema.py
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 | @override
@classmethod
def loads(
cls,
s: str | bytes | bytearray,
*args: Any,
**kwargs: Any,
) -> dict[str, dict] | None:
"""Load filename to dict."""
if cls._is_non_filename_format(s):
return None
if cleaned_s := cls.clean_string(s):
sub_data = comicfn2dict(cleaned_s, *args, **kwargs)
return {FilenameSchema.ROOT_TAG: sub_data}
return None
|
Bases: BaseSchema
File name schema.
Source code in comicbox/formats/filename/schema.py
132
133
134
135
136
137
138
139
140
141
142
143 | class FilenameSchema(BaseSchema):
"""File name schema."""
ROOT_TAG: str = "comicfn2dict"
ROOT_KEYPATH: str = ROOT_TAG
comicfn2dict = Nested(FilenameSubSchema)
class Meta(BaseSchema.Meta):
"""Schema Options."""
render_module = FilenameRenderModule
|
Bases: Meta
Schema Options.
Source code in comicbox/formats/filename/schema.py
| class Meta(BaseSchema.Meta):
"""Schema Options."""
render_module = FilenameRenderModule
|
Bases: BaseSubSchema
File name sub schema.
Source code in comicbox/formats/filename/schema.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 | class FilenameSubSchema(BaseSubSchema):
"""File name sub schema."""
day = DatePartField(minimum=1, maximum=31)
ext = StringField()
issue = StringField()
issue_count = IntegerField(minimum=0)
month = DatePartField(minimum=1, maximum=12)
original_format = StringField()
publisher = StringField()
remainders = StringListField()
series = StringField()
scan_info = StringField()
title = StringField()
volume = IntegerField()
volume_count = IntegerField(minimum=0)
year = IntegerField()
|