Skip to content
Snippets Groups Projects
Commit ea8c8383 authored by Empiriker's avatar Empiriker
Browse files

refactor progress indicator

parent ffe7de7e
No related branches found
No related tags found
No related merge requests found
...@@ -6,35 +6,34 @@ import re ...@@ -6,35 +6,34 @@ import re
import threading import threading
import time import time
from typing import List
DUMPS_DIR = "dumps" DUMPS_DIR = "dumps"
def start_progress_indicator(): def start_progress_indicator(is_done: List[bool], msg: str = ""):
time_elapsed_indicator.stop = False is_done[0] = False
def time_elapsed_indicator():
start_time = time.time()
while not is_done[0]:
elapsed_time = time.time() - start_time
print(f"\r{msg} Time elapsed: {elapsed_time:.0f} seconds", end="")
time.sleep(1)
print(f"{msg} Time elapsed: {elapsed_time:.0f} seconds") # type: ignore
indicator_thread = threading.Thread(target=time_elapsed_indicator) indicator_thread = threading.Thread(target=time_elapsed_indicator)
indicator_thread.start() indicator_thread.start()
return indicator_thread return indicator_thread
def stop_progress_indicator(indicator_thread): def stop_progress_indicator(indicator_thread, is_done: List[bool]):
time_elapsed_indicator.stop = True is_done[0] = True
indicator_thread.join() indicator_thread.join()
def time_elapsed_indicator():
start_time = time.time()
while not time_elapsed_indicator.stop:
elapsed_time = time.time() - start_time
print(f"\rTime elapsed: {elapsed_time:.2f} seconds", end="")
time.sleep(1)
print(
"\rTime elapsed: {elapsed_time:.2f} seconds".format(elapsed_time=elapsed_time) # type: ignore
)
def get_most_recent_file(directory, lang_code): def get_most_recent_file(directory, lang_code):
pattern = re.compile( pattern = re.compile(
r"" + lang_code + r"wiktionary-(\d+)-pages-articles-multistream.xml.bz2" r"" + lang_code + r"wiktionary-(\d+)-pages-articles-multistream.xml.bz2"
...@@ -53,20 +52,23 @@ def get_most_recent_file(directory, lang_code): ...@@ -53,20 +52,23 @@ def get_most_recent_file(directory, lang_code):
def load_templates(wiktlang: str): def load_templates(wiktlang: str):
print(f"Loading templates for {wiktlang}...") dump_file = get_most_recent_file(DUMPS_DIR, wiktlang)
if not dump_file:
raise ValueError(f"Could not find dump file for {wiktlang}.")
print(f"Loading templates from {dump_file}...")
indicator_thread = start_progress_indicator() is_done = [False]
indicator_thread = start_progress_indicator(
is_done, msg=f"Loading templates for {wiktlang}..."
)
wxr = get_wiktextract_context(wiktlang) wxr = get_wiktextract_context(wiktlang)
wxr.wtp.db_conn.execute("DELETE FROM pages") wxr.wtp.db_conn.execute("DELETE FROM pages")
wxr.wtp.db_conn.commit() wxr.wtp.db_conn.commit()
dump_file = get_most_recent_file(DUMPS_DIR, wiktlang)
if not dump_file:
raise ValueError(f"Could not find dump file for {wiktlang}.")
parse_wiktionary( parse_wiktionary(
wxr, wxr,
dump_file, dump_file,
...@@ -80,6 +82,6 @@ def load_templates(wiktlang: str): ...@@ -80,6 +82,6 @@ def load_templates(wiktlang: str):
) )
wxr.wtp.db_conn.commit() wxr.wtp.db_conn.commit()
stop_progress_indicator(indicator_thread) stop_progress_indicator(indicator_thread, is_done)
print("Done loading templates.") print(f"Done loading templates for {wiktlang}.")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment