Skip to content

Commit 6ac24ee

Browse files
feat: Refactoring diff fpaths for remove and add, docs and readme
1 parent 1ef9dc2 commit 6ac24ee

File tree

3 files changed

+103
-78
lines changed

3 files changed

+103
-78
lines changed

config/readme-config-condensed.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ categories:
7474
- quickstart-text-to-image-search
7575
features: []
7676
installation: []
77+
new-page-test: []
7778
old-welcome: []
7879
quick-tour: []
7980
quickstart: []

config/readme-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,15 @@ categories:
634634
slug: installation
635635
title: Installation
636636
updatedAt: '2022-04-06T02:13:18.408Z'
637+
new-page-test:
638+
_id: 624d060acca16400352eba8c
639+
createdAt: '2022-04-06T03:16:26.257Z'
640+
excerpt: ''
641+
hidden: true
642+
order: 999
643+
slug: new-page-test
644+
title: New page test
645+
updatedAt: '2022-04-06T03:16:26.257Z'
637646
old-welcome:
638647
_id: 623a96a3032c43002ef2c657
639648
createdAt: '2021-11-07T00:21:21.957Z'

src/rdme_sync/config/sync.py

Lines changed: 93 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,50 @@
2121
ROOT_PATH = Path(__file__).parent.resolve() / ".." / ".." / ".."
2222

2323

24+
def get_diff_fpaths(
25+
ddiff: DeepDiff, diff_type: Literal["removed", "added"], fpath: Path
26+
):
27+
"""This function returns a list of file paths that have been removed or added
28+
29+
Parameters
30+
----------
31+
ddiff : DeepDiff
32+
DeepDiff object
33+
diff_type : Literal['removed', 'added']
34+
either 'removed' or 'added'
35+
fpath : Path
36+
Path
37+
38+
"""
39+
ddiff_fpaths = []
40+
ddiff_items = [i for k, v in ddiff.items() for i in ddiff[k] if diff_type in k]
41+
logging.debug(f"{len(ddiff_items)} items {diff_type} in this repo...")
42+
for d in ddiff_items:
43+
path = [
44+
i
45+
for i in d.path(output_format="list")
46+
if i not in ["categories"]
47+
if isinstance(i, str)
48+
]
49+
50+
## Traversing tree diff
51+
for node in [d.t1, d.t2]:
52+
if isinstance(node, list):
53+
fname = [f"{path[-1]}.md"]
54+
path.pop()
55+
break
56+
if str(node) != "not present":
57+
fname = [f"{node}.md"]
58+
break
59+
path += fname
60+
61+
if ddiff_items:
62+
NEW_DOC_FPATH = fpath / "/".join(path)
63+
logging.debug(f"\n{NEW_DOC_FPATH} ...")
64+
ddiff_fpaths.append(NEW_DOC_FPATH)
65+
return ddiff_fpaths
66+
67+
2468
def get_config_diff(docs_config: Dict, readme_config: Dict, fpath: Path) -> List[Path]:
2569
"""This function compares the configs in the docs and the configs in the readme
2670
@@ -42,43 +86,14 @@ def get_config_diff(docs_config: Dict, readme_config: Dict, fpath: Path) -> List
4286
view="tree",
4387
)
4488

45-
paths = []
4689
if ddiff:
4790
logging.debug(f"---------")
4891
logging.debug(f"Deepdiff: \n{ddiff} ...")
4992

50-
new_doc_items = [
51-
i for k, v in ddiff.items() for i in ddiff[k] if "removed" in k
52-
]
53-
## TODO: Add new page in repo if created in ReadMe
54-
new_readme_items = [
55-
i for k, v in ddiff.items() for i in ddiff[k] if "added" in k
56-
]
57-
logging.debug(f"{len(new_doc_items)} items added")
58-
for d in new_doc_items:
59-
path = [
60-
i
61-
for i in d.path(output_format="list")
62-
if i not in ["categories"]
63-
if isinstance(i, str)
64-
]
65-
66-
## Traversing tree diff
67-
for node in [d.t1, d.t2]:
68-
if isinstance(node, list):
69-
fname = [f"{path[-1]}.md"]
70-
path.pop()
71-
break
72-
if str(node) != "not present":
73-
fname = [f"{node}.md"]
74-
break
75-
76-
path += fname
77-
78-
DIFF_FPATH = fpath / "/".join(path)
79-
logging.debug(f"\n{DIFF_FPATH} ...")
80-
paths.append(DIFF_FPATH)
81-
return paths
93+
new_doc_fpaths = get_diff_fpaths(ddiff, "removed", fpath)
94+
new_readme_fpaths = get_diff_fpaths(ddiff, "added", fpath)
95+
96+
return new_doc_fpaths, new_readme_fpaths
8297

8398

8499
def get_frontmatter(fpath: Path) -> Dict:
@@ -188,56 +203,56 @@ def main(args):
188203
docs_condensed_config = docs_config.condensed_config
189204
readme_condensed_config = readme_config.condensed_config
190205

191-
new_fpaths = get_config_diff(
206+
new_doc_fpaths, new_readme_fpaths = get_config_diff(
192207
docs_config=docs_condensed_config,
193208
readme_config=readme_condensed_config,
194209
fpath=DOCS_PATH,
195210
)
196211

197-
if not new_fpaths:
198-
logging.debug(f"No new updates in config ...")
199-
else:
200-
for path in new_fpaths:
201-
category = str(path).split("/docs")[1].split("/")[1]
202-
parent = path.parent.name
203-
logging.debug(f"Category: {category} Parent: {parent}")
204-
205-
# ## TODO: read slug from fname - in case slug is not the same
206-
post = get_frontmatter(path)
207-
208-
## Creating new frontmatter if none exists
209-
210-
## Getting max page order
211-
if category == parent:
212-
config_dict = readme_config.config["categories"][category]
213-
page_orders = readme_config.get_page_orders(
214-
config_dict, category=True
215-
)
216-
else:
217-
config_dict = readme_config.config["categories"][category][parent]
218-
page_orders = readme_config.get_page_orders(
219-
config_dict, category=False
220-
)
221-
max_page_order = max(page_orders)
222-
223-
## Creating new page
224-
args = {
225-
"title": post["title"],
226-
"type": "basic",
227-
"category_slug": category,
228-
"parent_slug": parent,
229-
"hidden": post["hidden"],
230-
"order": max_page_order + 1,
231-
}
232-
logging.debug(
233-
f"Creating new ReadMe config page... \n\t{path}\n{json.dumps(args)}"
234-
)
235-
# readme_config.create(**args)
236-
237-
logging.debug(f"Rebuilding config ... ")
238-
239-
# # ## TODO: Inserting new item in readme config with new category only instead rebuilding full config
240-
# readme_config.build()
212+
# if not new_doc_fpaths and not new_readme_fpaths:
213+
# logging.debug(f"No new updates in config ...")
214+
# elif new_doc_fpaths:
215+
# for path in new_doc_fpaths:
216+
# category = str(path).split("/docs")[1].split("/")[1]
217+
# parent = path.parent.name
218+
# logging.debug(f"Category: {category} Parent: {parent}")
219+
220+
# # ## TODO: read slug from fname - in case slug is not the same
221+
# post = get_frontmatter(path)
222+
223+
# ## Creating new frontmatter if none exists
224+
225+
# ## Getting max page order
226+
# if category == parent:
227+
# config_dict = readme_config.config["categories"][category]
228+
# page_orders = readme_config.get_page_orders(
229+
# config_dict, category=True
230+
# )
231+
# else:
232+
# config_dict = readme_config.config["categories"][category][parent]
233+
# page_orders = readme_config.get_page_orders(
234+
# config_dict, category=False
235+
# )
236+
# max_page_order = max(page_orders)
237+
238+
# ## Creating new page
239+
# args = {
240+
# "title": post["title"],
241+
# "type": "basic",
242+
# "category_slug": category,
243+
# "parent_slug": parent,
244+
# "hidden": post["hidden"],
245+
# "order": max_page_order + 1,
246+
# }
247+
# logging.debug(
248+
# f"Creating new ReadMe config page... \n\t{path}\n{json.dumps(args)}"
249+
# )
250+
# readme_config.create(**args)
251+
252+
# logging.debug(f"Rebuilding config ... ")
253+
254+
# # # ## TODO: Inserting new item in readme config with new category only instead rebuilding full config
255+
# # readme_config.build()
241256

242257

243258
if __name__ == "__main__":

0 commit comments

Comments
 (0)