Skip to content

Commit 75adf89

Browse files
authored
Feature/closes 87 (#120)
close #87
1 parent 60cd9ec commit 75adf89

23 files changed

+136
-59
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ venv/
1919
978*/
2020
map.md
2121
repo/
22-
toc.md
22+
toc*.md
2323
*.yaml
2424
safaribooks/
2525

demo.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ done <<< "$content"
4747

4848

4949
ls -1R 0to100
50+
cp toc.md toc_0to100.md
5051
}
5152

5253
function 0to100_sb {
@@ -66,6 +67,8 @@ function 0to100_sb {
6667
./main_sb.py refresh_toc
6768

6869
ls -1R 978*
70+
cp toc.md toc_0to100_sb.md
71+
6972
}
7073

7174
setup

zero_to_one_hundred/factories/sb_factory.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import argparse
12
from enum import Enum
23

34
from zero_to_one_hundred.configs.sb_config_map import SBConfigMap
@@ -27,9 +28,21 @@ def __init__(
2728
self.process_fs = process_fs
2829

2930
def get_processor(self, args):
30-
cmd = args[1]
31+
parser = argparse.ArgumentParser(description="Run 0to100_sb.")
32+
valid_cmds = list(p.name for p in self.SUPPORTED_PROCESSOR)
33+
parser.add_argument(
34+
"cmd",
35+
type=str,
36+
help=f'command, must be {" ".join(valid_cmds)}',
37+
choices=valid_cmds,
38+
)
39+
parser.add_argument("p1", type=str, help="arg p1", nargs="?", default=None)
40+
41+
args = parser.parse_args(args[1:])
42+
cmd = args.cmd
43+
p1 = args.p1
3144
if cmd == SBFactory.SUPPORTED_PROCESSOR.snatch_book.name:
32-
http_url = args[2]
45+
http_url = p1
3346
yield self.snatch_book_processor(http_url)
3447
yield self.refresh_toc_processor()
3548
elif cmd == SBFactory.SUPPORTED_PROCESSOR.refresh_toc.name:

zero_to_one_hundred/factories/ztoh_factory.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import argparse
12
from enum import Enum
23

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

3637
def get_processor(self, args):
37-
cmd = args[1]
38+
parser = argparse.ArgumentParser(description="Run 0to100.")
39+
valid_cmds = list(p.name for p in self.SUPPORTED_PROCESSOR)
40+
parser.add_argument(
41+
"cmd",
42+
type=str,
43+
help=f'command, must be {" ".join(valid_cmds)}',
44+
choices=valid_cmds,
45+
)
46+
parser.add_argument("p1", type=str, help="arg p1", nargs="?", default=None)
47+
48+
args = parser.parse_args(args[1:])
49+
cmd = args.cmd
50+
p1 = args.p1
51+
3852
if cmd == ZTOHFactory.SUPPORTED_PROCESSOR.create_section.name:
39-
yield self.create_section_processor(args[2])
53+
yield self.create_section_processor(p1)
4054
yield self.refresh_map_processor()
4155
elif cmd == ZTOHFactory.SUPPORTED_PROCESSOR.done_section.name:
42-
yield self.done_section_processor(args[2])
56+
yield self.done_section_processor(p1)
4357
yield self.refresh_map_processor()
4458
elif cmd == ZTOHFactory.SUPPORTED_PROCESSOR.refresh_map.name:
4559
yield self.refresh_map_processor()

zero_to_one_hundred/factories/ztoh_factory_provider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ def provide(self) -> ZTOHFactory:
1818
config_map_type = config_map.get_type
1919
if config_map_type == ZTOH_MAP:
2020
return ZTOHFactory(config_map, self.persist_fs, self.process_fs)
21-
raise NotImplementedError(f"NotImplementedError {config_map_type}")
21+
raise NotImplementedError(
22+
f"NotImplementedError {config_map_type}, check the files contents of {config_map.map_yaml_path}"
23+
)

zero_to_one_hundred/models/map.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_sections(self):
3535
res = sorted(self.sections, key=lambda s: s.get_readme_md_time())
3636
return res
3737

38-
def asMarkDown(self) -> str:
38+
def as_mark_down(self) -> str:
3939
lf_char = "\n"
4040

4141
def get_legend_as_md(self):
@@ -50,7 +50,7 @@ def get_legend_as_md(self):
5050
5151
{get_legend_as_md(self)}
5252
53-
{lf_char.join((section.asMarkDown() for section in self.get_sections()))}
53+
{lf_char.join((section.as_mark_down() for section in self.get_sections()))}
5454
"""
5555
return txt.replace(" ", "")
5656

zero_to_one_hundred/models/meta_book.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,27 @@ def write(self):
8888
self.persist_fs.make_dirs(self.config_map.get_download_engine_books_path)
8989
self.persist_fs.make_dirs(self.contents_path)
9090
except Exception as e:
91-
Validator.print_DDD(e)
91+
Validator.print_e(e)
9292
try:
9393
self.write_img()
9494
except Exception as e:
95-
Validator.print_DDD(e)
95+
Validator.print_e(e)
9696
try:
9797
self.write_epub()
9898
except Exception as e:
99-
Validator.print_DDD(e)
99+
Validator.print_e(e)
100100
try:
101101
self.write_metadata()
102102
except Exception as e:
103-
Validator.print_DDD(e)
103+
Validator.print_e(e)
104104
try:
105105
self.write_pdf(self.path_epub)
106106
except Exception as e:
107-
Validator.print_DDD(e)
107+
Validator.print_e(e)
108108
try:
109109
self.write_splitter_pdf(self.path_pdf, self.config_map.get_split_pdf_pages)
110110
except Exception as e:
111-
Validator.print_DDD(e)
111+
Validator.print_e(e)
112112

113113
@classmethod
114114
def get_isbn(cls, http_url):
@@ -135,3 +135,10 @@ def path_as_md(self, a_path):
135135
use relative path and convert " " to %20
136136
"""
137137
return a_path.replace(" ", "%20")
138+
139+
@property
140+
def get_matching_icon_as_md(self):
141+
icons = self.config_map.get_legend_icons
142+
143+
res = [i.icon for i in icons if re.search(i.regex, self.http_url)]
144+
return " ".join(res)

zero_to_one_hundred/models/metadata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(
3030
self.metadata: dict = self.read()
3131

3232
def __repr__(self):
33-
return f"MetaBook {self.isbn} {self.http_url} {self.asMarkDown()}"
33+
return f"MetaBook {self.isbn} {self.http_url} {self.as_mark_down()}"
3434

3535
@staticmethod
3636
def get_page_perc(metadata_dict: dict):
@@ -79,7 +79,7 @@ def get_metadata(self):
7979
sorted_dict = dict(sorted(metadata_dict.items()))
8080
return sorted_dict
8181

82-
def asMarkDown(self) -> str:
82+
def as_mark_down(self) -> str:
8383
# handle nasty URL in MD
8484
m: dict = self.get_metadata()
8585
url = m.get("url")

zero_to_one_hundred/models/readme_md.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(
2626
def __repr__(self):
2727
return f"ReadMeMD {self.readme_md} {self.http_url} {self.dir_name}"
2828

29-
def asMarkDown(self):
29+
def as_mark_down(self):
3030
return f"ReadMeMD {self.readme_md}, {self.dir_name} {self.http_url}"
3131

3232
def write(self, txt=None):

zero_to_one_hundred/models/section.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(
3838
def __repr__(self):
3939
return f"Section {self.http_url} {self.dir_readme_md} {self.is_done} {self.dir_name}"
4040

41-
def asMarkDown(self):
41+
def as_mark_down(self):
4242
return (
4343
"1. "
4444
+ self.get_id_name
@@ -177,7 +177,7 @@ def get_header(line):
177177
if len(not_null) > 1: # take first one header found
178178
res = not_null[1]
179179
except Exception as e:
180-
Validator.print_DDD(e)
180+
Validator.print_e(e)
181181
res = "FIXME: "
182182
return res
183183

zero_to_one_hundred/models/toc.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def build_from_dirs(
4343
logging.info(res)
4444
return res
4545

46-
def asMarkDown(self):
46+
def as_mark_down(self):
4747
def flatten_meta_book(meta_book: MetaBook):
4848
logging.info(f"flatten_meta_book {meta_book}")
4949
txt = "|".join(
@@ -53,11 +53,22 @@ def flatten_meta_book(meta_book: MetaBook):
5353
f"[`xyz`]({meta_book.contents_path_as_md})",
5454
f"{meta_book.metadata.as_mark_down()}",
5555
f"{meta_book.metadata.status}",
56+
f"{meta_book.get_matching_icon_as_md}",
5657
]
5758
)
5859

5960
return "|" + txt + "|"
6061

62+
lf_char = "\n"
63+
64+
def get_legend_as_md(self):
65+
txt: str = """
66+
## legend:
67+
"""
68+
txt += lf_char
69+
txt += self.config_map.get_legend_icons_as_md
70+
return txt
71+
6172
flattened_meta_book = [flatten_meta_book(mb) for mb in self.meta_books]
6273
backslash_n_char = "\n"
6374

@@ -67,13 +78,15 @@ def flatten_meta_book(meta_book: MetaBook):
6778
# TOC
6879
## `{len(self.meta_books)}` metabook
6980
### {self.process_fs.get_now()}
70-
| ISBN | img | `meta-contents` | `json-contents` | `status` |
71-
|--- |--- |--- |--- |--- |
81+
{get_legend_as_md(self)}
82+
83+
| ISBN | img | `meta-contents` | `json-contents` | `status` | `icons`
84+
|--- |--- |--- |--- |--- |--- |
7285
{backslash_n_char.join(flattened_meta_book)}
7386
"""
7487
)
7588
return md
7689

7790
def write(self):
78-
md = self.asMarkDown()
91+
md = self.as_mark_down()
7992
return self.persist_fs.write_file(self.readme_md, md)

zero_to_one_hundred/processors/refresh_links_processor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ def process(self):
3737
try:
3838
s.refresh_links()
3939
except Exception as e:
40-
Validator.print_DDD(e)
40+
Validator.print_e(e)

zero_to_one_hundred/processors/refresh_map_processor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ def process(self):
3232
self.persist_fs.list_dirs(self.config_map.get_repo_path),
3333
),
3434
)
35-
map.write(map.asMarkDown())
35+
map.write(map.as_mark_down())

zero_to_one_hundred/runner.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# pylint: disable=W0106,R1710
22
from typing import List
3+
from typing import Union, TypeVar
34

45
from zero_to_one_hundred.exceptions.errors import SomeError
56
from zero_to_one_hundred.factories.a_factory import AFactory
@@ -15,16 +16,21 @@ def run_core(argv: List[str], factory_provider: AFactoryProvider):
1516
factory_provider (AFactoryProvider): a factory_type
1617
1718
"""
18-
factory: AFactory
19+
20+
T = TypeVar("T", bound=AFactory)
21+
factory: Union[AFactory, T]
1922
try:
2023
factory = factory_provider.provide()
2124
[processor.process() for processor in factory.get_processor(argv) if processor]
2225
except SomeError as e:
23-
Validator.print_DDD(e)
26+
Validator.print_e(e)
2427
return
2528
except FileNotFoundError as e:
26-
Validator.print_DDD(e)
29+
Validator.print_e(e)
30+
return
31+
except NotImplementedError as e:
32+
Validator.print_e(e)
2733
return
2834
except Exception as e:
29-
Validator.print_DDD(e)
35+
Validator.print_e(e)
3036
factory.help_processor().process()

zero_to_one_hundred/tests/test_ztoh/test_map.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# pylint: disable=W0102
1111

1212

13-
def test_asMarkDown(
13+
def test_as_mark_down(
1414
get_config_map: ZTOHConfigMap,
1515
persist_fs,
1616
process_fs,
@@ -21,7 +21,7 @@ def test_asMarkDown(
2121
for http_url in http_urls
2222
]
2323
actual = Map(get_config_map, persist_fs, sections=sections)
24-
current = actual.asMarkDown()
24+
current = actual.as_mark_down()
2525
expected = """
2626
# map toc.md, 2
2727
## legend:
@@ -32,7 +32,7 @@ def test_asMarkDown(
3232
assert str_relaxed(current) == str_relaxed(expected)
3333

3434

35-
def test_asMarkDown_0(
35+
def test_as_mark_down_0(
3636
get_config_map_sorted_0: ZTOHConfigMap,
3737
persist_fs,
3838
process_fs,
@@ -47,7 +47,7 @@ def test_asMarkDown_0(
4747
for http_url in http_urls
4848
]
4949
actual = Map(get_config_map_sorted_0, persist_fs, sections=sections)
50-
current = actual.asMarkDown()
50+
current = actual.as_mark_down()
5151
expected = """
5252
# map toc.md, 3
5353
## legend:
@@ -60,7 +60,7 @@ def test_asMarkDown_0(
6060
assert str_relaxed(current) == str_relaxed(expected)
6161

6262

63-
def test_asMarkDown_1(
63+
def test_as_mark_down_1(
6464
get_config_map_sorted_1: ZTOHConfigMap,
6565
persist_fs,
6666
process_fs,
@@ -75,7 +75,7 @@ def test_asMarkDown_1(
7575
for http_url in http_urls
7676
]
7777
actual = Map(get_config_map_sorted_1, persist_fs, sections=sections)
78-
current = actual.asMarkDown()
78+
current = actual.as_mark_down()
7979
expected = """
8080
# map toc.md, 3
8181
## legend:
@@ -100,7 +100,7 @@ def test_write(
100100
for http_url in http_urls
101101
]
102102
actual = Map(get_config_map, persist_fs, sections=sections)
103-
txt = actual.asMarkDown()
103+
txt = actual.as_mark_down()
104104
with Patcher(allow_root_user=False) as patcher:
105105
res = actual.write(txt)
106106
assert res > 0

zero_to_one_hundred/tests/test_ztoh/test_readme_md.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ def test_refresh_links(get_config_map, persist_fs, process_fs, http_url_1):
1212
)
1313

1414

15-
def test_asMarkDown(get_config_map, persist_fs, process_fs, http_url_1):
15+
def test_as_mark_down(get_config_map, persist_fs, process_fs, http_url_1):
1616
actual = ReadMeMD(
1717
get_config_map,
1818
persist_fs,
1919
process_fs,
2020
Section.from_http_url_to_dir,
2121
http_url_1,
2222
)
23-
current = actual.asMarkDown()
23+
current = actual.as_mark_down()
2424
assert (
2525
current
2626
== "ReadMeMD ./0to100/https§§§cloud.google.com§abc/readme.md, https§§§cloud.google.com§abc https://cloud.google.com/abc"

zero_to_one_hundred/tests/test_ztoh/test_section.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ def test_gcp_get_format_as_md(get_gcp_config_map, persist_fs, process_fs):
9393
assert actual.get_matching_icon_as_md == """:snake:"""
9494

9595

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

0 commit comments

Comments
 (0)