Skip to content

Commit d9d6fa5

Browse files
committed
Read, store and display readme files
1 parent 6e6f3d9 commit d9d6fa5

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

w3modmanager/domain/mod/fetcher.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,33 @@ def __lt__(self, other: object) -> bool:
274274
return False
275275

276276

277+
@dataclass
278+
class ReadmeFile(DataClassJsonMixin):
279+
source: Path = field(metadata=JsonConfig(encoder=str, decoder=Path))
280+
content: str
281+
282+
def __repr__(self) -> str:
283+
return '\'%s\'' % str(self.source)
284+
285+
def __str__(self) -> str:
286+
return str(self.source)
287+
288+
def __eq__(self, other: object) -> bool:
289+
if isinstance(other, ReadmeFile):
290+
return self.source == other.source
291+
if isinstance(other, str):
292+
return self.source == Path(other)
293+
return False
294+
295+
def __hash__(self) -> int:
296+
return hash(self.source)
297+
298+
def __lt__(self, other: object) -> bool:
299+
if isinstance(other, ReadmeFile):
300+
return self.source < other.source
301+
return False
302+
303+
277304
@dataclass
278305
class BundledFile(DataClassJsonMixin):
279306
source: Path = field(metadata=JsonConfig(encoder=str, decoder=Path))
@@ -439,6 +466,24 @@ def fetchBinFiles(path: Path, onlyUngrouped: bool = False) -> \
439466
return (bins, user, inpu)
440467

441468

469+
def fetchReadmeFiles(path: Path, onlyUngrouped: bool = False) -> list[ReadmeFile]:
470+
contents = []
471+
dirs = [path]
472+
for check in dirs:
473+
for file in sorted(
474+
f for f in check.iterdir()
475+
if f.is_file() and f.suffix.lower() in ('.txt', '.md')
476+
):
477+
relpath: Path = file.relative_to(path)
478+
if re.match(r'^(.*readme.*)\.(txt|md)', file.name, re.IGNORECASE):
479+
contents.append(ReadmeFile(relpath, util.readText(file)))
480+
dirs += sorted(
481+
d for d in check.iterdir()
482+
if d.is_dir() and not onlyUngrouped
483+
)
484+
return contents
485+
486+
442487
def fetchContentFiles(path: Path) -> list[ContentFile]:
443488
contents = []
444489
dirs = [path]

w3modmanager/domain/mod/mod.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Mod(DataClassJsonMixin):
4040
settings: list[UserSettings] = field(default_factory=list)
4141
inputs: list[InputSettings] = field(default_factory=list)
4242
bundled: list[BundledFile] = field(default_factory=list)
43+
readmes: list[ReadmeFile] = field(default_factory=list)
4344

4445
dataversion: int = 1
4546

@@ -106,6 +107,10 @@ def menuFiles(self) -> list[BinFile]:
106107
def bundledFiles(self) -> list[BundledFile]:
107108
return self.bundled
108109

110+
@property
111+
def readmeFiles(self) -> list[ReadmeFile]:
112+
return self.readmes
113+
109114

110115
@classmethod
111116
async def fromDirectory(
@@ -132,6 +137,7 @@ async def fromDirectory(
132137
size = 0
133138
files, settings, inputs = fetchBinFiles(check)
134139
contents = fetchContentFiles(check)
140+
readmes = fetchReadmeFiles(check)
135141
bundled = []
136142
for _file in files:
137143
size += check.joinpath(_file.source).stat().st_size
@@ -151,7 +157,8 @@ async def fromDirectory(
151157
settings=settings,
152158
inputs=inputs,
153159
contents=contents,
154-
bundled=bundled
160+
bundled=bundled,
161+
readmes=readmes
155162
))
156163
continue
157164
# fetch dlc dirs
@@ -161,6 +168,7 @@ async def fromDirectory(
161168
size = 0
162169
files, settings, inputs = fetchBinFiles(check)
163170
contents = fetchContentFiles(check)
171+
readmes = fetchReadmeFiles(check)
164172
bundled = []
165173
for _file in files:
166174
size += check.joinpath(_file.source).stat().st_size
@@ -180,7 +188,8 @@ async def fromDirectory(
180188
settings=settings,
181189
inputs=inputs,
182190
contents=contents,
183-
bundled=bundled
191+
bundled=bundled,
192+
readmes=readmes
184193
))
185194
continue
186195
# fetch unspecified mod or doc dirs
@@ -190,6 +199,7 @@ async def fromDirectory(
190199
size = 0
191200
files, settings, inputs = fetchBinFiles(check)
192201
contents = fetchContentFiles(check)
202+
readmes = fetchReadmeFiles(check)
193203
bundled = []
194204
for _file in files:
195205
size += check.joinpath(_file.source).stat().st_size
@@ -209,7 +219,8 @@ async def fromDirectory(
209219
settings=settings,
210220
inputs=inputs,
211221
contents=contents,
212-
bundled=bundled
222+
bundled=bundled,
223+
readmes=readmes
213224
))
214225
continue
215226
if recursive:
@@ -226,6 +237,7 @@ async def fromDirectory(
226237
size = 0
227238
for file in files:
228239
size += commonroot.joinpath(file.source).stat().st_size
240+
readmes = fetchReadmeFiles(commonroot, onlyUngrouped=True)
229241
mods.append(cls(
230242
package,
231243
filename=name,
@@ -236,7 +248,8 @@ async def fromDirectory(
236248
size=size,
237249
files=files,
238250
settings=settings,
239-
inputs=inputs
251+
inputs=inputs,
252+
readmes=readmes
240253
))
241254
# fetch patch files
242255
if len(mods) == 1 and mods[0].filename == 'mod0000____CompilationTrigger':

w3modmanager/ui/graphical/detailswindow.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(self, parent: QWidget, mod: Mod, model: Model) -> None:
4040
self.info.setReadOnly(True)
4141
self.info.setLineWrapMode(QTextEdit.LineWrapMode.WidgetWidth)
4242
for field in fields(mod):
43-
if field.name in ['files', 'contents', 'settings', 'inputs', 'bundled']:
43+
if field.name in ['files', 'contents', 'settings', 'inputs', 'bundled', 'readmes']:
4444
continue
4545
self.info.append(f'<strong>{field.name}</strong>: {getattr(mod, field.name)}')
4646
self.info.moveCursor(QTextCursor.MoveOperation.Start)
@@ -206,6 +206,25 @@ def __init__(self, parent: QWidget, mod: Mod, model: Model) -> None:
206206
self.bundled.verticalScrollBar().setValue(self.bundled.verticalScrollBar().minimum())
207207
gbBundledLayout.addWidget(self.bundled)
208208

209+
readmes = mod.readmeFiles
210+
if readmes:
211+
gbReadmes = QGroupBox('Readmes')
212+
gbReadmes.setMinimumHeight(160)
213+
innerLayout.addWidget(gbReadmes)
214+
innerLayout.setStretchFactor(gbReadmes, 2)
215+
gbReadmesLayout = QVBoxLayout(gbReadmes)
216+
217+
self.readmes = QTextEdit(gbReadmes)
218+
self.readmes.setTextInteractionFlags(Qt.TextInteractionFlag.NoTextInteraction)
219+
self.readmes.setReadOnly(True)
220+
self.readmes.setLineWrapMode(QTextEdit.LineWrapMode.WidgetWidth)
221+
for readmeFile in readmes:
222+
self.readmes.append(f'<p><pre>{readmeFile.content}</pre></p>')
223+
self.readmes.moveCursor(QTextCursor.MoveOperation.Start)
224+
self.readmes.setSizeAdjustPolicy(QTextEdit.SizeAdjustPolicy.AdjustToContents)
225+
self.readmes.verticalScrollBar().setValue(self.readmes.verticalScrollBar().minimum())
226+
gbReadmesLayout.addWidget(self.readmes)
227+
209228
# Setup
210229

211230
self.setMinimumSize(QSize(800, 600))

0 commit comments

Comments
 (0)