Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
obar1 committed Aug 20, 2024
1 parent 289b445 commit 88482ca
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 24 deletions.
8 changes: 4 additions & 4 deletions demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ function setup {
function setup0to100 {
rm -rf 0to100/

cp ./zero_to_one_hundred/tests/resources/gcp_map.yaml map.yaml
cp ./zero_to_one_hundred/tests/test_ztoh/resources/gcp_map.yaml map.yaml
}

function setup0to100_sb {
rm -rf 978*/

cp ./zero_to_one_hundred/tests_sb/resources/map.yaml .
cp ./zero_to_one_hundred/tests/tests_sb/resources/map.yaml map.yaml

# safari books from lorenzodifuccia
git clone https://github.com/lorenzodifuccia/safaribooks.git
Expand Down Expand Up @@ -50,12 +50,12 @@ function 0to100_sb {
setup0to100_sb

./main_sb.py help

./main_sb.py snatch_book https://learning.oreilly.com/course/clean-code-fundamentals/9780134661742
echo 'add any metadata you like'
echo '{"title": "Clean Code Fundamentals"}'> 9780134661742/9780134661742.json
./main_sb.py refresh_toc

./main_sb.py snatch_book https://learning.oreilly.com/library/view/rewire-your-brain/9781119895947
echo 'pretend book was read fully and get % calc for free :P'
echo '{"page_curr": "100", "page_tot": "100", "url":"https://www.oreilly.com/library/view/rewire-your-brain/9781119895947"}' > 9781119895947/9781119895947.json
Expand Down
2 changes: 1 addition & 1 deletion zero_to_one_hundred/models/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __repr__(self):
def get_sections(self):

def order_by_date(sections):
return sorted(sections, key=lambda s: s.dir_readme_md_ts)
return sorted(sections, key=lambda s: s.get_readme_md_time())

if self.config_map.get_repo_sorted == 'abc':
return sorted(self.sections, key=str)
Expand Down
3 changes: 3 additions & 0 deletions zero_to_one_hundred/models/readme_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ def read(self):
data = self.persist_fs.read_file(self.readme_md)
lines = "FIXME:" if data is None else data
return lines

def get_latest_ts(self):
pass
13 changes: 9 additions & 4 deletions zero_to_one_hundred/models/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ def __init__(
self.dir_readme_md = (
config_map.get_repo_path + "/" + self.dir_name + "/readme.md"
)
self.dir_readme_md_ts =datetime.utcnow()

self.is_done = is_done

def __repr__(self):
Expand Down Expand Up @@ -80,16 +78,19 @@ def from_http_url_to_dir(cls, http_url):
.replace("\\", "§")
)

def write(self):
def write(self,txt:str):
return self.persist_fs.make_dirs(
self.config_map.get_repo_path + "/" + self.dir_name
txt
)

def write_done_section(self):
return self.persist_fs.done_section(
self.config_map.get_repo_path + "/" + self.dir_name
)

def get_readme_md_time(self):
return self.persist_fs.get_biz_ts( self.config_map.get_repo_path + "/" + self.dir_name)

@classmethod
def from_http_url_to_dir_to(cls, dir_name):
return dir_name.replace("§", "/").replace("https///", "https://")
Expand All @@ -98,6 +99,9 @@ def from_http_url_to_dir_to(cls, dir_name):
def done_section_status(cls, persist_fs, repo_path, dir_name):
return persist_fs.done_section_status(repo_path, dir_name)




@classmethod
def build_from_http(cls, config_map, http_url, persist_fs, process_fs):
return Section(config_map, persist_fs, process_fs, http_url)
Expand Down Expand Up @@ -256,3 +260,4 @@ def __eq__(self, other):
and other.dir_readme_md == self.dir_readme_md
and other.is_done == self.is_done
)

3 changes: 2 additions & 1 deletion zero_to_one_hundred/processors/create_section_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def process(self):
self.http_url,
is_done=False,
)
section.write()
txt = self.config_map.get_repo_path + "/" + self.dir_name
section.write(txt)
readme_md: ReadMeMD = ReadMeMD(
self.config_map,
self.persist_fs,
Expand Down
4 changes: 2 additions & 2 deletions zero_to_one_hundred/processors/refresh_map_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(

def process(self):
"""Scan the repo and for each new_section add it to the map, save the map file."""
actual: Map = Map(
map: Map = Map(
self.config_map,
self.persist_fs,
Map.build_from_dirs(
Expand All @@ -33,4 +33,4 @@ def process(self):
self.persist_fs.list_dirs(self.config_map.get_repo_path),
),
)
actual.write(actual.asMarkDown())
map.write(map.asMarkDown())
10 changes: 7 additions & 3 deletions zero_to_one_hundred/repository/a_persist_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def create_empty_file(cls, filename):
@classmethod
def make_dirs(cls, path):
print(f"make_dirs {path}")
return os.makedirs(path, 0o777, True)
try:
os.makedirs(path, 0o777, True)
return True
except FileExistsError:
return False

@classmethod
def read_file(cls, filename) -> List[str] | None:
Expand All @@ -57,8 +61,8 @@ def read_file(cls, filename) -> List[str] | None:
try:
with open(filename, mode="r", encoding="UTF-8") as f:
lines = f.readlines()
except:
pass # we dont care
except Exception as e:
print(e) # we dont care
return lines

@classmethod
Expand Down
37 changes: 37 additions & 0 deletions zero_to_one_hundred/repository/ztoh_persist_fs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pylint: disable=W0108
from datetime import datetime
import os


Expand Down Expand Up @@ -31,3 +32,39 @@ def done_section_status(cls, abs_repo_path, path):
if exists:
return True
return False

def get_biz_ts(cls, path):
print(f"path {path}")
exists = os.path.exists(path)
print(f"exists {exists}")
ts_format = '%Y-%m-%d %H:%M:%S'
if exists:
modification_time = os.path.getmtime(path)
modification_time_date = datetime.fromtimestamp(modification_time)

return modification_time_date.strftime(ts_format





















)
return datetime.now().strftime(ts_format)


20 changes: 20 additions & 0 deletions zero_to_one_hundred/tests/test_ztoh/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ def test_asMarkDown_0(get_config_map_sorted_0: ZTOHConfigMap, persist_fs, proces
1.[`here`](./0to100/https§§§cloud.google.com§efg/readme.md) :footprints:
1.[`here`](./0to100/https§§§cloud.google.com§zzz/readme.md) :footprints:
"""
assert str_relaxed(current) == str_relaxed(expected)

def test_asMarkDown_1(get_config_map_sorted_1: ZTOHConfigMap, persist_fs, process_fs, http_urls =['https://cloud.google.com/abc','https://cloud.google.com/zzz', 'https://cloud.google.com/efg']):
sections = [Section(get_config_map_sorted_1, persist_fs, process_fs, http_url, False) for http_url in http_urls]
actual = Map(get_config_map_sorted_1, persist_fs, sections=sections)
current = actual.asMarkDown()
expected = """
# map toc.md, 3
## legend:
| 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:
"""
assert str_relaxed(current) == str_relaxed(expected)

Expand Down
12 changes: 10 additions & 2 deletions zero_to_one_hundred/tests/test_ztoh/test_section.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from zero_to_one_hundred.models.section import Section
from pyfakefs.fake_filesystem_unittest import Patcher
import os


def test_init(get_config_map, persist_fs, process_fs, http_url_1):
Expand All @@ -9,11 +11,17 @@ def test_init(get_config_map, persist_fs, process_fs, http_url_1):
actual.dir_readme_md
== get_config_map.get_repo_path + "/" + "https§§§cloud.google.com§abc/readme.md"
)

res = actual.get_readme_md_time(persist_fs, actual.dir_readme_md)
assert res is not None

def test_write(get_config_map, persist_fs, process_fs, http_url_1):
actual = Section(get_config_map, persist_fs, process_fs, http_url_1)

txt = get_config_map.get_repo_path + r"/" + actual.dir_name
txt = os.path.abspath(txt)
with Patcher(allow_root_user=False) as patcher:
res= actual.write(txt)
assert res is True
assert os.path.exists(txt)

def test_build_from_dir(
get_config_map, persist_fs, process_fs
Expand Down
13 changes: 6 additions & 7 deletions zero_to_one_hundred/tests/test_ztoh/test_ztoh_config_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# pylint: disable=W0621,W0613


def test_pass_config_map(get_config_map: ZTOHConfigMap):
def test_config_map(get_config_map: ZTOHConfigMap):
actual = get_config_map
assert actual.get_type == ZTOH_MAP
assert actual.get_repo_path is not None
Expand All @@ -19,29 +19,28 @@ def test__repr__(get_config_map: ZTOHConfigMap, get_map_yaml_path: str):
)


def test_pass_gcp_config_map(get_gcp_config_map: ZTOHConfigMap):
def test_gcp_config_map(get_gcp_config_map: ZTOHConfigMap):
actual = get_gcp_config_map
assert actual.get_type == ZTOH_MAP
assert actual.get_repo_path is not None
assert actual.get_repo_map_md == "toc.md"
assert actual.get_repo_legend_type == "gcp"


def test_pass_datacamp_config_map(get_datacamp_config_map: ZTOHConfigMap):
def test_datacamp_config_map(get_datacamp_config_map: ZTOHConfigMap):
actual = get_datacamp_config_map
assert actual.get_type == ZTOH_MAP
assert actual.get_repo_path is not None
assert actual.get_repo_map_md == "toc.md"
assert actual.get_repo_legend_type == "datacamp"


def test_uns(get_unsupported_config_map: ZTOHConfigMap):
# with pytest.raises(NotImplementedError):
def test_unsupported_config_map(get_unsupported_config_map: ZTOHConfigMap):
actual = get_unsupported_config_map
assert actual.get_type == "not-a-map"


def test_pass_config_map(get_config_map_sorted_0: ZTOHConfigMap):
def test_config_map_sorted_0(get_config_map_sorted_0: ZTOHConfigMap):
actual = get_config_map_sorted_0
assert actual.get_type == ZTOH_MAP
assert actual.get_repo_path is not None
Expand All @@ -50,7 +49,7 @@ def test_pass_config_map(get_config_map_sorted_0: ZTOHConfigMap):
assert actual.get_repo_legend_type is None


def test_pass_config_map(get_config_map_sorted_1: ZTOHConfigMap):
def test_config_map_sorted_1(get_config_map_sorted_1: ZTOHConfigMap):
actual = get_config_map_sorted_1
assert actual.get_type == ZTOH_MAP
assert actual.get_repo_path is not None
Expand Down

0 comments on commit 88482ca

Please sign in to comment.