-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
146 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,76 @@ | ||
import json | ||
from typing import OrderedDict | ||
|
||
from zero_to_one_hundred.configs.sb_config_map import SBConfigMap | ||
from zero_to_one_hundred.repository.sb_persist_fs import SBPersistFS | ||
from zero_to_one_hundred.repository.sb_process_fs import SBProcessFS | ||
from zero_to_one_hundred.validator.validator import Validator | ||
from zero_to_one_hundred.views.markdown_renderer import MarkdownRenderer | ||
|
||
|
||
class Metadata: | ||
class Metadata(MarkdownRenderer): | ||
ONE_HUN_PER_TXT = "100.0%" | ||
|
||
DONE_TXT_AS_MD = '<span style="color:green">**DONE**</span>' | ||
WIP_TXT_AS_MD= '<span style="color:yellow">**WIP**</span>' | ||
|
||
|
||
def __init__( | ||
self, | ||
config_map: SBConfigMap, | ||
persist_fs: SBPersistFS, | ||
process_fs: SBProcessFS, | ||
get_isbn, | ||
http_url: str, | ||
page_curr=0, | ||
pages_tot=0, | ||
): | ||
self.config_map = config_map | ||
self.http_url = http_url | ||
self.persist_fs = persist_fs | ||
self.process_fs = process_fs | ||
self.page_curr = page_curr | ||
self.isbn = get_isbn(http_url) | ||
self.contents_path = persist_fs.abs_path(f"{self.isbn}") | ||
self.page_curr = page_curr | ||
self.pages_tot = pages_tot | ||
self.path_json = f"{self.contents_path}/{self.isbn}.json" | ||
self.metadata : dict = self.read() | ||
|
||
def __repr__(self): | ||
return f"Metadata {self.http_url}, {self.isbn} {self.contents_path}" | ||
|
||
@property | ||
def get_page_perc(self): | ||
perc = 0 | ||
if self.pages_tot > 0: | ||
perc = 100 * self.page_curr / self.pages_tot | ||
return str(round(perc, 1)) + "%" | ||
|
||
@staticmethod | ||
def get_page_perc(metadata_dict: dict): | ||
""" | ||
given metadata_dict, get values of pages and return metadata_dict, n/a if no valid values are present | ||
""" | ||
page_curr = int(metadata_dict.get('page_curr',"0")) | ||
pages_tot= int(metadata_dict.get('page_tot',"0")) | ||
perc = 0.0 | ||
if pages_tot > 0: | ||
perc = 100 * page_curr / pages_tot | ||
return str(round(perc, 1)) + "%" | ||
return "n/a" | ||
|
||
def write(self): | ||
self.write_json() | ||
|
||
def write_json(self): | ||
try: | ||
self.page_curr = self.persist_fs.read_pages_curr( | ||
f"{self.contents_path}/{self.isbn}.json" | ||
) | ||
except Exception as e: | ||
Validator.print_DDD(e) | ||
try: | ||
self.pages_tot = self.persist_fs.read_pages_tot( | ||
f"{self.contents_path}/{self.isbn}.pdf" | ||
) | ||
except Exception as e: | ||
Validator.print_DDD(e) | ||
|
||
txt = """ | ||
"isbn":"{isbn}", | ||
"url":"{url}", | ||
"page_curr":"{page_curr}", | ||
"pages_tot":"{pages_tot}", | ||
"page_perc":"{page_perc}" | ||
""".strip() | ||
txt = txt.format( | ||
isbn=self.isbn, | ||
url=self.http_url, | ||
page_curr=self.page_curr, | ||
pages_tot=self.pages_tot, | ||
page_perc=self.get_page_perc, | ||
) | ||
print(txt) | ||
self.persist_fs.write_json(self.path_json, "{" + txt + "}") | ||
txt = json.dump(self.get_metadata(), indent=4) | ||
self.persist_fs.write_json(self.path_json,txt ) | ||
|
||
def read_json(self): | ||
def read(self): | ||
json_data = self.persist_fs.read_file(self.path_json) | ||
lines = "{}" if json_data is None else json_data | ||
return json.dumps(json.loads("".join(lines)), indent=4) | ||
return json.loads("".join(lines)) | ||
|
||
@property | ||
def status(self): | ||
"""use relative folder to simplify the usage in browser""" | ||
return Metadata.DONE_TXT_AS_MD if Metadata.ONE_HUN_PER_TXT in self.metadata else Metadata.WIP_TXT_AS_MD | ||
|
||
|
||
def get_metadata(self) -> str: | ||
""" | ||
refresh info for the final dict(), keys are orderered so it looks better :) | ||
""" | ||
metadata_dict = self.metadata | ||
metadata_dict["isbn"]= self.isbn | ||
metadata_dict["url"]= self.http_url | ||
metadata_dict["pages_perc"]= self.get_page_perc(metadata_dict) | ||
sorted_dict = OrderedDict(sorted(metadata_dict.items())) | ||
return json.dumps(sorted_dict) | ||
|
||
def asMarkDown(self) -> str: | ||
return self.get_metadata() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters