Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/closes 87 #120

Merged
merged 6 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ venv/
978*/
map.md
repo/
toc.md
toc*.md
*.yaml
safaribooks/

Expand Down
3 changes: 3 additions & 0 deletions demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ done <<< "$content"


ls -1R 0to100
cp toc.md toc_0to100.md
}

function 0to100_sb {
Expand All @@ -66,6 +67,8 @@ function 0to100_sb {
./main_sb.py refresh_toc

ls -1R 978*
cp toc.md toc_0to100_sb.md

}

setup
Expand Down
17 changes: 15 additions & 2 deletions zero_to_one_hundred/factories/sb_factory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import argparse
from enum import Enum

from zero_to_one_hundred.configs.sb_config_map import SBConfigMap
Expand Down Expand Up @@ -27,9 +28,21 @@ def __init__(
self.process_fs = process_fs

def get_processor(self, args):
cmd = args[1]
parser = argparse.ArgumentParser(description="Run 0to100_sb.")
valid_cmds = list(p.name for p in self.SUPPORTED_PROCESSOR)
parser.add_argument(
"cmd",
type=str,
help=f'command, must be {" ".join(valid_cmds)}',
choices=valid_cmds,
)
parser.add_argument("p1", type=str, help="arg p1", nargs="?", default=None)

args = parser.parse_args(args[1:])
cmd = args.cmd
p1 = args.p1
if cmd == SBFactory.SUPPORTED_PROCESSOR.snatch_book.name:
http_url = args[2]
http_url = p1
yield self.snatch_book_processor(http_url)
yield self.refresh_toc_processor()
elif cmd == SBFactory.SUPPORTED_PROCESSOR.refresh_toc.name:
Expand Down
20 changes: 17 additions & 3 deletions zero_to_one_hundred/factories/ztoh_factory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import argparse
from enum import Enum

from zero_to_one_hundred.configs.ztoh_config_map import ZTOHConfigMap
Expand Down Expand Up @@ -34,12 +35,25 @@ def __init__(
self.process_fs = process_fs

def get_processor(self, args):
cmd = args[1]
parser = argparse.ArgumentParser(description="Run 0to100.")
valid_cmds = list(p.name for p in self.SUPPORTED_PROCESSOR)
parser.add_argument(
"cmd",
type=str,
help=f'command, must be {" ".join(valid_cmds)}',
choices=valid_cmds,
)
parser.add_argument("p1", type=str, help="arg p1", nargs="?", default=None)

args = parser.parse_args(args[1:])
cmd = args.cmd
p1 = args.p1

if cmd == ZTOHFactory.SUPPORTED_PROCESSOR.create_section.name:
yield self.create_section_processor(args[2])
yield self.create_section_processor(p1)
yield self.refresh_map_processor()
elif cmd == ZTOHFactory.SUPPORTED_PROCESSOR.done_section.name:
yield self.done_section_processor(args[2])
yield self.done_section_processor(p1)
yield self.refresh_map_processor()
elif cmd == ZTOHFactory.SUPPORTED_PROCESSOR.refresh_map.name:
yield self.refresh_map_processor()
Expand Down
4 changes: 3 additions & 1 deletion zero_to_one_hundred/factories/ztoh_factory_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ def provide(self) -> ZTOHFactory:
config_map_type = config_map.get_type
if config_map_type == ZTOH_MAP:
return ZTOHFactory(config_map, self.persist_fs, self.process_fs)
raise NotImplementedError(f"NotImplementedError {config_map_type}")
raise NotImplementedError(
f"NotImplementedError {config_map_type}, check the files contents of {config_map.map_yaml_path}"
)
4 changes: 2 additions & 2 deletions zero_to_one_hundred/models/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_sections(self):
res = sorted(self.sections, key=lambda s: s.get_readme_md_time())
return res

def asMarkDown(self) -> str:
def as_mark_down(self) -> str:
lf_char = "\n"

def get_legend_as_md(self):
Expand All @@ -50,7 +50,7 @@ def get_legend_as_md(self):

{get_legend_as_md(self)}

{lf_char.join((section.asMarkDown() for section in self.get_sections()))}
{lf_char.join((section.as_mark_down() for section in self.get_sections()))}
"""
return txt.replace(" ", "")

Expand Down
19 changes: 13 additions & 6 deletions zero_to_one_hundred/models/meta_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,27 @@ def write(self):
self.persist_fs.make_dirs(self.config_map.get_download_engine_books_path)
self.persist_fs.make_dirs(self.contents_path)
except Exception as e:
Validator.print_DDD(e)
Validator.print_e(e)
try:
self.write_img()
except Exception as e:
Validator.print_DDD(e)
Validator.print_e(e)
try:
self.write_epub()
except Exception as e:
Validator.print_DDD(e)
Validator.print_e(e)
try:
self.write_metadata()
except Exception as e:
Validator.print_DDD(e)
Validator.print_e(e)
try:
self.write_pdf(self.path_epub)
except Exception as e:
Validator.print_DDD(e)
Validator.print_e(e)
try:
self.write_splitter_pdf(self.path_pdf, self.config_map.get_split_pdf_pages)
except Exception as e:
Validator.print_DDD(e)
Validator.print_e(e)

@classmethod
def get_isbn(cls, http_url):
Expand All @@ -135,3 +135,10 @@ def path_as_md(self, a_path):
use relative path and convert " " to %20
"""
return a_path.replace(" ", "%20")

@property
def get_matching_icon_as_md(self):
icons = self.config_map.get_legend_icons

res = [i.icon for i in icons if re.search(i.regex, self.http_url)]
return " ".join(res)
4 changes: 2 additions & 2 deletions zero_to_one_hundred/models/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(
self.metadata: dict = self.read()

def __repr__(self):
return f"MetaBook {self.isbn} {self.http_url} {self.asMarkDown()}"
return f"MetaBook {self.isbn} {self.http_url} {self.as_mark_down()}"

@staticmethod
def get_page_perc(metadata_dict: dict):
Expand Down Expand Up @@ -79,7 +79,7 @@ def get_metadata(self):
sorted_dict = dict(sorted(metadata_dict.items()))
return sorted_dict

def asMarkDown(self) -> str:
def as_mark_down(self) -> str:
# handle nasty URL in MD
m: dict = self.get_metadata()
url = m.get("url")
Expand Down
2 changes: 1 addition & 1 deletion zero_to_one_hundred/models/readme_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(
def __repr__(self):
return f"ReadMeMD {self.readme_md} {self.http_url} {self.dir_name}"

def asMarkDown(self):
def as_mark_down(self):
return f"ReadMeMD {self.readme_md}, {self.dir_name} {self.http_url}"

def write(self, txt=None):
Expand Down
4 changes: 2 additions & 2 deletions zero_to_one_hundred/models/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(
def __repr__(self):
return f"Section {self.http_url} {self.dir_readme_md} {self.is_done} {self.dir_name}"

def asMarkDown(self):
def as_mark_down(self):
return (
"1. "
+ self.get_id_name
Expand Down Expand Up @@ -177,7 +177,7 @@ def get_header(line):
if len(not_null) > 1: # take first one header found
res = not_null[1]
except Exception as e:
Validator.print_DDD(e)
Validator.print_e(e)
res = "FIXME: "
return res

Expand Down
21 changes: 17 additions & 4 deletions zero_to_one_hundred/models/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def build_from_dirs(
logging.info(res)
return res

def asMarkDown(self):
def as_mark_down(self):
def flatten_meta_book(meta_book: MetaBook):
logging.info(f"flatten_meta_book {meta_book}")
txt = "|".join(
Expand All @@ -53,11 +53,22 @@ def flatten_meta_book(meta_book: MetaBook):
f"[`xyz`]({meta_book.contents_path_as_md})",
f"{meta_book.metadata.as_mark_down()}",
f"{meta_book.metadata.status}",
f"{meta_book.get_matching_icon_as_md}",
]
)

return "|" + txt + "|"

lf_char = "\n"

def get_legend_as_md(self):
txt: str = """
## legend:
"""
txt += lf_char
txt += self.config_map.get_legend_icons_as_md
return txt

flattened_meta_book = [flatten_meta_book(mb) for mb in self.meta_books]
backslash_n_char = "\n"

Expand All @@ -67,13 +78,15 @@ def flatten_meta_book(meta_book: MetaBook):
# TOC
## `{len(self.meta_books)}` metabook
### {self.process_fs.get_now()}
| ISBN | img | `meta-contents` | `json-contents` | `status` |
|--- |--- |--- |--- |--- |
{get_legend_as_md(self)}

| ISBN | img | `meta-contents` | `json-contents` | `status` | `icons`
|--- |--- |--- |--- |--- |--- |
{backslash_n_char.join(flattened_meta_book)}
"""
)
return md

def write(self):
md = self.asMarkDown()
md = self.as_mark_down()
return self.persist_fs.write_file(self.readme_md, md)
2 changes: 1 addition & 1 deletion zero_to_one_hundred/processors/refresh_links_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ def process(self):
try:
s.refresh_links()
except Exception as e:
Validator.print_DDD(e)
Validator.print_e(e)
2 changes: 1 addition & 1 deletion zero_to_one_hundred/processors/refresh_map_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def process(self):
self.persist_fs.list_dirs(self.config_map.get_repo_path),
),
)
map.write(map.asMarkDown())
map.write(map.as_mark_down())
14 changes: 10 additions & 4 deletions zero_to_one_hundred/runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pylint: disable=W0106,R1710
from typing import List
from typing import Union, TypeVar

from zero_to_one_hundred.exceptions.errors import SomeError
from zero_to_one_hundred.factories.a_factory import AFactory
Expand All @@ -15,16 +16,21 @@ def run_core(argv: List[str], factory_provider: AFactoryProvider):
factory_provider (AFactoryProvider): a factory_type

"""
factory: AFactory

T = TypeVar("T", bound=AFactory)
factory: Union[AFactory, T]
try:
factory = factory_provider.provide()
[processor.process() for processor in factory.get_processor(argv) if processor]
except SomeError as e:
Validator.print_DDD(e)
Validator.print_e(e)
return
except FileNotFoundError as e:
Validator.print_DDD(e)
Validator.print_e(e)
return
except NotImplementedError as e:
Validator.print_e(e)
return
except Exception as e:
Validator.print_DDD(e)
Validator.print_e(e)
factory.help_processor().process()
14 changes: 7 additions & 7 deletions zero_to_one_hundred/tests/test_ztoh/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# pylint: disable=W0102


def test_asMarkDown(
def test_as_mark_down(
get_config_map: ZTOHConfigMap,
persist_fs,
process_fs,
Expand All @@ -21,7 +21,7 @@ def test_asMarkDown(
for http_url in http_urls
]
actual = Map(get_config_map, persist_fs, sections=sections)
current = actual.asMarkDown()
current = actual.as_mark_down()
expected = """
# map toc.md, 2
## legend:
Expand All @@ -32,7 +32,7 @@ def test_asMarkDown(
assert str_relaxed(current) == str_relaxed(expected)


def test_asMarkDown_0(
def test_as_mark_down_0(
get_config_map_sorted_0: ZTOHConfigMap,
persist_fs,
process_fs,
Expand All @@ -47,7 +47,7 @@ def test_asMarkDown_0(
for http_url in http_urls
]
actual = Map(get_config_map_sorted_0, persist_fs, sections=sections)
current = actual.asMarkDown()
current = actual.as_mark_down()
expected = """
# map toc.md, 3
## legend:
Expand All @@ -60,7 +60,7 @@ def test_asMarkDown_0(
assert str_relaxed(current) == str_relaxed(expected)


def test_asMarkDown_1(
def test_as_mark_down_1(
get_config_map_sorted_1: ZTOHConfigMap,
persist_fs,
process_fs,
Expand All @@ -75,7 +75,7 @@ def test_asMarkDown_1(
for http_url in http_urls
]
actual = Map(get_config_map_sorted_1, persist_fs, sections=sections)
current = actual.asMarkDown()
current = actual.as_mark_down()
expected = """
# map toc.md, 3
## legend:
Expand All @@ -100,7 +100,7 @@ def test_write(
for http_url in http_urls
]
actual = Map(get_config_map, persist_fs, sections=sections)
txt = actual.asMarkDown()
txt = actual.as_mark_down()
with Patcher(allow_root_user=False) as patcher:
res = actual.write(txt)
assert res > 0
Expand Down
4 changes: 2 additions & 2 deletions zero_to_one_hundred/tests/test_ztoh/test_readme_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ def test_refresh_links(get_config_map, persist_fs, process_fs, http_url_1):
)


def test_asMarkDown(get_config_map, persist_fs, process_fs, http_url_1):
def test_as_mark_down(get_config_map, persist_fs, process_fs, http_url_1):
actual = ReadMeMD(
get_config_map,
persist_fs,
process_fs,
Section.from_http_url_to_dir,
http_url_1,
)
current = actual.asMarkDown()
current = actual.as_mark_down()
assert (
current
== "ReadMeMD ./0to100/https§§§cloud.google.com§abc/readme.md, https§§§cloud.google.com§abc https://cloud.google.com/abc"
Expand Down
4 changes: 2 additions & 2 deletions zero_to_one_hundred/tests/test_ztoh/test_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def test_gcp_get_format_as_md(get_gcp_config_map, persist_fs, process_fs):
assert actual.get_matching_icon_as_md == """:snake:"""


def test_asMarkDown(get_config_map, persist_fs, process_fs, http_url_1):
def test_as_mark_down(get_config_map, persist_fs, process_fs, http_url_1):
actual = Section(get_config_map, persist_fs, process_fs, http_url_1)
current = actual.asMarkDown()
current = actual.as_mark_down()
assert str_relaxed(current) == str_relaxed(
"1. [`here`](./0to100/https§§§cloud.google.com§abc/readme.md) `wip`"
)
Loading
Loading