Skip to content

Commit f8d5574

Browse files
committed
feat(cache): introduce new subcommand cache, which is the combination of fetch + parse.
1 parent b732c01 commit f8d5574

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

bin/cppref

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,34 @@ class CppRef:
128128
else:
129129
Utils.write_man3(man3.joinpath(f"{source}{r.id}.3.gz"), document)
130130

131+
def cache(self, force: bool = False, timeout: float = 10000, limit: int = 5):
132+
source = ConfContext.read_source()
133+
man3 = ConfContext.man3_root()
134+
try:
135+
records = Utils.query(source, ConfContext.dbfile())
136+
except AssertionError as e:
137+
return print(f"Unexpected Error: {e}", file=sys.stderr)
138+
if not force:
139+
records = list(filter(lambda r: not man3.joinpath(f"{source}{r.id}.3.gz").exists(), records)) # fmt: off
140+
records = list(records)
141+
records = list(records)
142+
if (length := len(records)) == 0:
143+
return print("Nothing to fetch.", file=sys.stderr)
144+
145+
pbar, process = tqdm(total=length), Utils.html_handler(source)
146+
147+
def on_success(record: Record, resp: str):
148+
document = process(resp, record)
149+
Utils.write_man3(man3.joinpath(f"{source}{record.id}.3.gz"), document)
150+
pbar.update()
151+
152+
def on_failed(record: Record, exec: Exception):
153+
print(f"Error={type(exec).__name__}({exec}): {record}", file=sys.stderr)
154+
pbar.update()
155+
156+
man3.mkdir(parents=True, exist_ok=True)
157+
asyncio.run(Utils.afetch(*records, timeout=timeout, limit=limit, on_success=on_success, on_failed=on_failed)) # fmt: off
158+
131159

132160
if __name__ == "__main__":
133161
import fire

0 commit comments

Comments
 (0)