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

117 change the legend logic to use regex in map #118

Merged
merged 16 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ test:
testint:
bash demo.sh 0to100 && bash demo.sh 0to100_sb


format:
black zero_to_one_hundred

lint:
pylint --disable=C0116,C0115,W0702,C0114,C0301,C0103,C0209,R0913,R0902,R0903,E1101,W0612,W0718,R0801,W0150,W0613 zero_to_one_hundred

refactor: format lint
pr: format lint test testint install
obar1 marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 14 additions & 10 deletions demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,20 @@ function 0to100 {
setup0to100

./main.py help

url=https://cloud.google.com/docs/
./main.py create_section "$url"

url=https://docs.getdbt.com/docs/introduction
./main.py create_section "$url"

url=https://cloud.google.com/docs/
./main.py done_section "$url"

content=$(cat << 'EOF'
https://www.cloudskillsboost.google/paths/16
https://www.cloudskillsboost.google/games/4424/labs/28651
https://www.cloudskillsboost.google/course_templates/3
https://www.cloudskillsboost.google/games/4422
https://storage.googleapis.com/cloud-training/cls-html5-courses/T-BQRS-I/M1/index.html

EOF
)
while IFS= read -r section || [[ -n "$section" ]]; do
./main.py create_section "$section"
done <<< "$content"


ls -1R 0to100
obar1 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
43 changes: 38 additions & 5 deletions zero_to_one_hundred/configs/a_config_map.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pylint: disable=W0246
import os
from abc import ABC
from enum import Enum
from dataclasses import dataclass

from zero_to_one_hundred.exceptions.errors import SomeError
from zero_to_one_hundred.repository.a_persist_fs import APersistFS
Expand All @@ -10,14 +10,18 @@
class AConfigMap(ABC):
MAP_YAML_PATH = "MAP_YAML_PATH"

class SUPPORTED_EXTRA_MAP(Enum):
gcp = 1
datacamp = 2
@dataclass
class LegendIcons:
name: str
icon: str
regex: str

def __init__(self, persist_fs: APersistFS):
self.map_yaml_path = os.getenv(AConfigMap.MAP_YAML_PATH)
if self.map_yaml_path is None:
raise SomeError(f"map_yaml_path {self.map_yaml_path} is not valid")
raise SomeError(
f"map_yaml_path {self.map_yaml_path} is not valid, please set it in the env"
)
self.persist_fs = persist_fs

def __repr__(self):
Expand All @@ -32,3 +36,32 @@ def load(self):
@property
def get_type(self):
return self.load["type"]

@property
def get_legend_icons(self):
legend = self.load.get("legend")
if legend:
return [
AConfigMap.LegendIcons(
name=icon_data["name"],
icon=icon_data["icon"],
regex=icon_data["regex"],
)
for icon_data in legend.get("icons", [])
if isinstance(icon_data, dict)
]
return []

@property
def get_legend_type(self) -> str | None:
return (
None
if self.load.get("legend") is None
else self.load.get("legend").get("type")
)

@property
def get_legend_icons_as_md(self):
icons = self.get_legend_icons
res = [f"`{i.name}` {i.icon}" for i in icons]
return "" if res == "" else "**legend_icons**\n" + "\n".join(res)
4 changes: 0 additions & 4 deletions zero_to_one_hundred/configs/ztoh_config_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,3 @@ def get_repo_map_md(self):
@property
def get_repo_sorted(self) -> bool:
return self.load["repo"].get("sorted")

@property
def get_repo_legend_type(self) -> str | None:
return self.load["repo"].get("legend_type")
35 changes: 6 additions & 29 deletions zero_to_one_hundred/models/map.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import List

from zero_to_one_hundred.configs.a_config_map import AConfigMap
from zero_to_one_hundred.configs.ztoh_config_map import ZTOHConfigMap
from zero_to_one_hundred.models.section import Section
from zero_to_one_hundred.repository.ztoh_persist_fs import ZTOHPersistFS
Expand All @@ -26,14 +25,13 @@ def __repr__(self):
return f"Map {str(self.sections)}"

def get_sections(self):

res :List[Section] = self.sections
res: List[Section] = self.sections
if self.config_map.get_repo_sorted == "abc":
print('*** abc')
res= sorted(self.sections, key=lambda s: s.dir_name)
print("*** abc")
res = sorted(self.sections, key=lambda s: s.dir_name)
if self.config_map.get_repo_sorted == "00:00:00":
print('*** 00:00:00')
res = sorted(self.sections, key=lambda s: s.get_readme_md_time())
print("*** 00:00:00")
res = sorted(self.sections, key=lambda s: s.get_readme_md_time())
return res

def asMarkDown(self) -> str:
Expand All @@ -42,30 +40,9 @@ def asMarkDown(self) -> str:
def get_legend_as_md(self):
txt: str = """
## legend:

| footprints | completed |
|---|---|
| :footprints: | :green_heart: |
"""
txt += lf_char

match self.config_map.get_repo_legend_type:
case AConfigMap.SUPPORTED_EXTRA_MAP.gcp.name:
txt += """
> extra
>
| quest | lab | template | game | course |
|---|---|---|----|---|
| :cyclone: | :floppy_disk: | :whale: | :snake: | :pushpin: |""".strip()
case AConfigMap.SUPPORTED_EXTRA_MAP.datacamp.name:
txt += """
> extra
>
| projects | tutorial | course |
|---|---|---|
| :cyclone: | :floppy_disk: | :whale: |""".strip()
case _:
txt += lf_char
txt += self.config_map.get_legend_icons_as_md
return txt

txt = f"""{f"# map {self.readme_md}, {len(self.sections)}"}
Expand Down
40 changes: 9 additions & 31 deletions zero_to_one_hundred/models/section.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pylint: disable= R0904
import re

from zero_to_one_hundred.configs.a_config_map import AConfigMap
from zero_to_one_hundred.configs.ztoh_config_map import ZTOHConfigMap
from zero_to_one_hundred.models.readme_md import ReadMeMD
from zero_to_one_hundred.repository.ztoh_persist_fs import ZTOHPersistFS
Expand Down Expand Up @@ -46,7 +46,7 @@ def asMarkDown(self):
+ self.dir_readme_md
+ ")"
+ self.get_done_as_md
+ self.get_format_as_md
+ self.get_matching_icon_as_md
)

@property
Expand All @@ -55,7 +55,7 @@ def get_http_url(self):

@property
def get_done_as_md(self):
return " :green_heart:" if self.is_done else " :footprints:"
return "`done`" if self.is_done else "`wip`"

@property
def get_dir_name(self):
Expand Down Expand Up @@ -86,9 +86,7 @@ def write_done_section(self):
)

def get_readme_md_time(self):
return self.persist_fs.get_biz_ts(
self.dir_readme_md
)
return self.persist_fs.get_biz_ts(self.dir_readme_md)

@classmethod
def from_http_url_to_dir_to(cls, dir_name):
Expand Down Expand Up @@ -215,31 +213,11 @@ def is_datacamp_course(self):
return "courses" in self.http_url and "app.datacamp.com" in self.http_url

@property
def get_format_as_md(self):
a = []
match self.config_map.get_repo_legend_type:
case AConfigMap.SUPPORTED_EXTRA_MAP.gcp.name:
a = [
":cyclone:" if self.is_gcp_quest else None,
":floppy_disk:" if self.is_gcp_lab else None,
":whale:" if self.is_gcp_template else None,
":snake:" if self.is_gcp_game else None,
":pushpin:",
]
case AConfigMap.SUPPORTED_EXTRA_MAP.datacamp.name:
a = [
":cyclone:" if self.is_datacamp_project else None,
":floppy_disk:" if self.is_datacamp_tutorial else None,
":whale:" if self.is_datacamp_course else None,
":pushpin:",
]
case _:
a = []
try:
res = next(item for item in a if item is not None)
except StopIteration:
return ""
return res
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)

def __eq__(self, other):
if other is self:
Expand Down
1 change: 0 additions & 1 deletion zero_to_one_hundred/repository/ztoh_persist_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def done_section_status(cls, abs_repo_path, path):

@classmethod
def get_biz_ts(cls, path):

# print(f"path {path}")
exists = os.path.exists(path)
# print(f"exists {exists}")
Expand Down
8 changes: 7 additions & 1 deletion zero_to_one_hundred/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import traceback
from typing import List

from zero_to_one_hundred.exceptions.errors import SomeError
from zero_to_one_hundred.factories.a_factory import AFactory
from zero_to_one_hundred.factories.a_factory_provider import AFactoryProvider

Expand All @@ -19,7 +20,12 @@ def run_core(argv: List[str], factory_provider: AFactoryProvider):
try:
factory = factory_provider.provide()
[processor.process() for processor in factory.get_processor(argv) if processor]

except SomeError as se:
print(se)
return
except FileNotFoundError as se:
print(se)
return
obar1 marked this conversation as resolved.
Show resolved Hide resolved
except Exception as e:
print(e)
traceback.print_exc()
Expand Down
13 changes: 12 additions & 1 deletion zero_to_one_hundred/tests/test_ztoh/resources/datacamp_map.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@ type: ztoh-map
repo:
path: "./0to100"
map_md: "toc.md"
legend_type: "datacamp"
legend:
type: "datacamp"
icons:
- name: Project
icon: ":cyclone:"
regex: "project"
- name: Tutorial
icon: ":floppy_disk:"
regex: "tutorial"
- name: Course
icon: ":whale:"
regex: "course"
22 changes: 20 additions & 2 deletions zero_to_one_hundred/tests/test_ztoh/resources/gcp_map.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,23 @@ type: ztoh-map
repo:
path: "./0to100"
map_md: "toc.md"
legend_type: "gcp"
sorted: "abc" # oder alph
legend:
type: "gcp"
icons:
- name: Path
icon: ":cyclone:"
regex: "path"
- name: Lab
icon: ":floppy_disk:"
regex: "lab"
- name: Template
icon: ":whale:"
regex: "template"
- name: Game
icon: ":snake:"
regex: "game"
- name: Course
icon: ":pushpin:"
regex: "course"

sorted: "abc" # oder alph
32 changes: 11 additions & 21 deletions zero_to_one_hundred/tests/test_ztoh/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ def test_asMarkDown(
expected = """
# map toc.md, 2
## legend:

| footprints | completed |
|---|---|
| :footprints: | :green_heart: |

1.[`here`](./0to100/https§§§cloud.google.com§zzz/readme.md) :footprints:
1.[`here`](./0to100/https§§§cloud.google.com§abc/readme.md) :footprints:
**legend_icons**
1.[`here`](./0to100/https§§§cloud.google.com§zzz/readme.md) `wip`
1.[`here`](./0to100/https§§§cloud.google.com§abc/readme.md) `wip`
"""
assert str_relaxed(current) == str_relaxed(expected)

Expand All @@ -55,14 +51,11 @@ def test_asMarkDown_0(
expected = """
# map toc.md, 3
## legend:
**legend_icons**

| footprints | completed |
|---|---|
| :footprints: | :green_heart: |

1.[`here`](./0to100/https§§§cloud.google.com§abc/readme.md) :footprints:
1.[`here`](./0to100/https§§§cloud.google.com§efg/readme.md) :footprints:
1.[`here`](./0to100/https§§§cloud.google.com§zzz/readme.md) :footprints:
1.[`here`](./0to100/https§§§cloud.google.com§abc/readme.md) `wip`
1.[`here`](./0to100/https§§§cloud.google.com§efg/readme.md) `wip`
1.[`here`](./0to100/https§§§cloud.google.com§zzz/readme.md) `wip`

"""
assert str_relaxed(current) == str_relaxed(expected)
Expand All @@ -87,14 +80,11 @@ def test_asMarkDown_1(
expected = """
# map toc.md, 3
## legend:
**legend_icons**

| footprints | completed |
|---|---|
| :footprints: | :green_heart: |

1.[`here`](./0to100/https§§§cloud.google.com§abc/readme.md) :footprints:
1.[`here`](./0to100/https§§§cloud.google.com§zzz/readme.md) :footprints:
1.[`here`](./0to100/https§§§cloud.google.com§efg/readme.md) :footprints:
1.[`here`](./0to100/https§§§cloud.google.com§abc/readme.md) `wip`
1.[`here`](./0to100/https§§§cloud.google.com§zzz/readme.md) `wip`
1.[`here`](./0to100/https§§§cloud.google.com§efg/readme.md) `wip`


"""
Expand Down
Loading
Loading