Skip to content

Commit

Permalink
Cross platform compatibility (keras-team#127)
Browse files Browse the repository at this point in the history
* Cross platform compatibility

* Changing paths to using Path instead of os.path.join

* Black formatting fix
  • Loading branch information
falaktheoptimist authored Jul 30, 2020
1 parent 811e0e1 commit 572de2b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
89 changes: 46 additions & 43 deletions scripts/autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import generate_tf_guides


EXAMPLES_GH_LOCATION = "keras-team/keras-io/blob/master/examples/"
GUIDES_GH_LOCATION = "keras-team/keras-io/blob/master/guides/"
EXAMPLES_GH_LOCATION = Path("keras-team") / "keras-io" / "blob" / "master" / "examples"
GUIDES_GH_LOCATION = Path("keras-team") / "keras-io" / "blob" / "master" / "guides"


class KerasIO:
Expand Down Expand Up @@ -80,21 +80,19 @@ def make_examples_master(self):
for fname in sorted(os.listdir(path)):
if fname.endswith(".py"): # e.g. examples/nlp/test.py
name = fname[:-3]
example_path = name.split('/')[-1]
example_path = name.split("/")[-1]
if example_path not in preexisting:
f = open(path / fname)
f.readline()
title_line = f.readline()
f.close()
assert title_line.startswith("Title: ")
title = title_line[len("Title: ") :]
children.append(
{"path": example_path, "title": title.strip()}
)
children.append({"path": example_path, "title": title.strip()})
entry["children"] = children

def make_md_sources(self):
print('Generating md sources')
print("Generating md sources")
if os.path.exists(self.md_sources_dir):
print("Clearing", self.md_sources_dir)
shutil.rmtree(self.md_sources_dir)
Expand Down Expand Up @@ -137,7 +135,8 @@ def preprocess_tutobook_md_source(
"[**View in Colab**](https://colab.research.google.com/github/"
+ github_repo_dir
+ "ipynb/"
+ name + ".ipynb"
+ name
+ ".ipynb"
+ ") "
'<span class="k-dot">•</span>'
'<img class="k-inline-icon" src="https://github.com/favicon.ico"/> '
Expand Down Expand Up @@ -210,8 +209,8 @@ def process_one_dir(src_dir, target_dir):

def add_example(self, path, working_dir=None):
"""e.g. add_example('vision/cats_and_dogs')"""
assert path.count("/") == 1
folder, name = path.split("/")
assert path.count(os.path.sep) == 1
folder, name = path.split(os.path.sep)
if name.endswith(".py"):
name = name[:-3]

Expand All @@ -233,8 +232,8 @@ def add_example(self, path, working_dir=None):
tutobooks.py_to_nb(py_path, nb_path, fill_outputs=False)
tutobooks.py_to_md(py_path, nb_path, md_path, img_dir, working_dir=working_dir)
md_content = open(md_path).read()
github_repo_dir = EXAMPLES_GH_LOCATION + folder + "/"
site_img_dir = "img/examples/" + folder + "/" + name
github_repo_dir = str(EXAMPLES_GH_LOCATION / folder)
site_img_dir = os.path.join("img", "examples", folder, name)
md_content = self.preprocess_tutobook_md_source(
md_content, name + ".py", github_repo_dir, img_dir, site_img_dir
)
Expand All @@ -244,7 +243,6 @@ def add_guide(self, name, working_dir=None):
"""e.g. add_guide('functional_api')"""
if name.endswith(".py"):
name = name[:-3]

ipynb_dir = Path(self.guides_dir) / "ipynb"
if not os.path.exists(ipynb_dir):
os.makedirs(ipynb_dir)
Expand All @@ -264,7 +262,7 @@ def add_guide(self, name, working_dir=None):
tutobooks.py_to_nb(py_path, nb_path, fill_outputs=False)
tutobooks.py_to_md(py_path, nb_path, md_path, img_dir, working_dir=working_dir)
md_content = open(md_path).read()
github_repo_dir = GUIDES_GH_LOCATION
github_repo_dir = str(GUIDES_GH_LOCATION)
site_img_dir = "img/guides/" + name
md_content = self.preprocess_tutobook_md_source(
md_content, name + ".py", github_repo_dir, img_dir, site_img_dir
Expand Down Expand Up @@ -297,7 +295,7 @@ def make_tutobook_sources(self, guides=True, examples=True):
target_dir=target_dir,
img_dir=img_dir,
site_img_dir="img/guides/",
github_repo_dir=GUIDES_GH_LOCATION,
github_repo_dir=str(GUIDES_GH_LOCATION),
)

# Examples
Expand All @@ -318,7 +316,7 @@ def make_tutobook_sources(self, guides=True, examples=True):
target_dir=target_dir, # e.g. examples/nlp/md
img_dir=img_dir, # e.g. examples/nlp/img
site_img_dir="img/examples/" + name, # e.g. img/examples/nlp
github_repo_dir=EXAMPLES_GH_LOCATION + name + "/",
github_repo_dir=str(EXAMPLES_GH_LOCATION / name),
)

def sync_tutobook_templates(self):
Expand Down Expand Up @@ -437,7 +435,8 @@ def make_md_source_for_entry(self, entry, path_stack, title_stack):
template_path = template_path.with_suffix(".md")

if os.path.exists(template_path):
template_file = open(template_path)
template_file = open(template_path, encoding="utf8")
print(template_path)
template = template_file.read()
template_file.close()
else:
Expand Down Expand Up @@ -510,7 +509,7 @@ def make_md_source_for_entry(self, entry, path_stack, title_stack):
self.make_md_source_for_entry(entry, path_stack[:], title_stack[:])

def render_md_sources_to_html(self):
print('Rendering md sources to HTML')
print("Rendering md sources to HTML")
base_template = jinja2.Template(open(Path(self.theme_dir) / "base.html").read())
docs_template = jinja2.Template(open(Path(self.theme_dir) / "docs.html").read())

Expand Down Expand Up @@ -544,7 +543,7 @@ def render_md_sources_to_html(self):
# Render as index.html
target_path = Path(target_dir) / "index.html"
relative_url = (str(target_dir) + "/").replace(self.site_dir, "/")
relative_url = relative_url.replace('//', '/')
relative_url = relative_url.replace("//", "/")
else:
# Render as fname_no_ext/index.tml
fname_no_ext = ".".join(fname.split(".")[:-1])
Expand Down Expand Up @@ -594,7 +593,7 @@ def render_md_sources_to_html(self):
}
)
save_file(target_path, html_page)
all_urls_list.append('https://keras.io' + relative_url)
all_urls_list.append("https://keras.io" + relative_url)

# Images & css
shutil.copytree(Path(self.theme_dir) / "css", Path(self.site_dir) / "css")
Expand Down Expand Up @@ -631,14 +630,14 @@ def render_md_sources_to_html(self):
"content": "<h1>404: Page not found</h1>",
"base_url": self.url,
}
)
),
}
)
save_file(Path(self.site_dir) / "404.html", page404)

# Tutobooks
self.sync_tutobook_media()
sitemap = '\n'.join(all_urls_list) + '\n'
sitemap = "\n".join(all_urls_list) + "\n"
save_file(Path(self.site_dir) / "sitemap.txt", sitemap)

def make(self):
Expand Down Expand Up @@ -672,7 +671,7 @@ def signal_handler(signal, frame):


def save_file(path, content):
f = open(path, "w")
f = open(path, "w", encoding="utf8")
f.write(content)
f.close()

Expand Down Expand Up @@ -788,15 +787,15 @@ def insert_title_ids_in_html(html):
for i in range(1, 5):
match = "<h" + str(i) + ">(.*?)</h" + str(i) + ">"
replace = (
"<h" +
str(i) +
r' id="' +
marker +
r"\1" +
marker_end +
r'">\1</h' +
str(i) +
">"
"<h"
+ str(i)
+ r' id="'
+ marker
+ r"\1"
+ marker_end
+ r'">\1</h'
+ str(i)
+ ">"
)
html = re.sub(match, replace, html)

Expand Down Expand Up @@ -847,7 +846,8 @@ def generate_md_toc(entries, url, depth=2):
else:
title_prefix = "- "
generated += title_prefix + "[{title}]({full_url})\n".format(
title=title, full_url=full_url)
title=title, full_url=full_url
)
if children:
assert path.endswith("/")
for child in children:
Expand All @@ -857,18 +857,21 @@ def generate_md_toc(entries, url, depth=2):
child_path = child["path"]
child_url = full_url + child_path
generated += "- [{child_title}]({child_url})\n".format(
child_title=child_title, child_url=child_url)
child_title=child_title, child_url=child_url
)
generated += "\n"
elif generate and print_generate:
for gen in generate:
obj = docstrings.import_object(gen)
obj_name = docstrings.get_name(obj)
obj_type = docstrings.get_type(obj)
link = "{full_url}/#{obj_name}-{obj_type}".format(
full_url=full_url, obj_name=obj_name, obj_type=obj_type).lower()
full_url=full_url, obj_name=obj_name, obj_type=obj_type
).lower()
name = gen.split(".")[-1]
generated += "- [{name} {obj_type}]({link})\n".format(
name=name, obj_type=obj_type, link=link)
name=name, obj_type=obj_type, link=link
)
generated += "\n"
return generated

Expand All @@ -882,13 +885,13 @@ def get_working_dir(arg):
if __name__ == "__main__":
keras_io = KerasIO(
master=MASTER,
url="/",
templates_dir="../templates/",
md_sources_dir="../sources/",
site_dir="../site/",
theme_dir="../theme/",
guides_dir="../guides/",
examples_dir="../examples/",
url=os.path.sep,
templates_dir=os.path.join("..", "templates"),
md_sources_dir=os.path.join("..", "sources"),
site_dir=os.path.join("..", "site"),
theme_dir=os.path.join("..", "theme"),
guides_dir=os.path.join("..", "guides"),
examples_dir=os.path.join("..", "examples"),
refresh_guides=False,
refresh_examples=False,
)
Expand Down
7 changes: 5 additions & 2 deletions scripts/tutobooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import json
import random
import shutil
import tempfile
from pathlib import Path

TIMEOUT = 60 * 60
Expand Down Expand Up @@ -214,7 +215,7 @@ def nb_to_md(nb_path, md_path, img_dir, working_dir=None):
original_img_dir = original_img_dir[:-1]
img_dir = os.path.abspath(img_dir)
nb_path = os.path.abspath(nb_path)
nb_fname = str(nb_path).split("/")[-1]
nb_fname = str(nb_path).split(os.path.sep)[-1]

del_working_dir = False
if working_dir is None:
Expand Down Expand Up @@ -319,7 +320,9 @@ def validate(py):
if line.endswith(" "):
raise ValueError("Found trailing space on line %d; line: `%s`" % (i, line))
# Validate style with black
fpath = "/tmp/" + str(random.randint(1e6, 1e7)) + ".py"

tmp = tempfile.gettempdir()
fpath = os.path.join(tmp, str(random.randint(1e6, 1e7)) + ".py")
f = open(fpath, "w")
pre_formatting = "\n".join(lines)
f.write(pre_formatting)
Expand Down

0 comments on commit 572de2b

Please sign in to comment.