Skip to content

Commit 191f1df

Browse files
committed
implemented a caching mechanism to speed up debugging.
1 parent e0eefb7 commit 191f1df

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

cache.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import json
2+
import requests
3+
import os
4+
5+
if os.path.exists("cache.json"):
6+
with open("cache.json") as fo:
7+
cache = json.loads(fo.read())
8+
else:
9+
cache = {}
10+
11+
def fetch(url, isJSON=True):
12+
if not url in cache:
13+
if isJSON:
14+
cache[url] = requests.get(url).json()
15+
else:
16+
cache[url] = requests.get(url).text
17+
18+
with open("cache.json", "w") as fo:
19+
fo.write(json.dumps(cache))
20+
return cache[url]

debug.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
from templates import indexTemplate, pkgTemplate, moduleTemplate, toHtml
33
import requests
44
import re
5+
from cache import fetch
56

67
from generate import Module
78

89
def debug_module(pkg_name, module_name):
9-
all_pkgs = requests.get("http://package.elm-lang.org/all-packages").json()
10+
all_pkgs = fetch("http://package.elm-lang.org/all-packages")
1011
all_pkgs_dict = {p["name"]:p for p in all_pkgs}
1112
pkg_data = all_pkgs_dict[pkg_name]
1213

1314
jsonURL = "/".join(["http://package.elm-lang.org/packages", pkg_name, pkg_data["versions"][0], "documentation.json"])
14-
json_data = requests.get(jsonURL).json()
15+
json_data = fetch(jsonURL)
1516
json_data_dict = {m["name"]:m for m in json_data}
1617

1718

generate.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sqlite3
55

66
import json
7-
import requests
7+
from cache import fetch
88

99
from cgi import escape
1010

@@ -243,10 +243,10 @@ def docname(pkg, module=None):
243243
def generate_all():
244244
global pkgs
245245
print("feching all packages list ..."),
246-
all_pkgs = requests.get(pkgsURL+"all-packages").json()
246+
all_pkgs = fetch(pkgsURL+"all-packages")
247247
print("DONE!")
248248
print("feching new packages list ..."),
249-
new_pkgs = requests.get(pkgsURL+"new-packages").json()
249+
new_pkgs = fetch(pkgsURL+"new-packages")
250250
print("DONE!")
251251

252252
new_pkgs = list(set(new_pkgs))
@@ -270,7 +270,7 @@ def generate_all():
270270
print "Generating package: "+pkg_name+" [% 3d / %03d]..."%(idx, no_pkgs),
271271

272272
docURL = pkgsURL+"/".join(["packages", pkg_name, pkg_version, "documentation"])+".json"
273-
json = requests.get(docURL).json()
273+
json = fetch(docURL)
274274
# module = Module(json)
275275
links = []
276276
for module in json:
@@ -291,7 +291,7 @@ def generate_all():
291291
print "DONE!"
292292

293293
DEBUG = False
294-
DEBUG = True
294+
# DEBUG = True
295295

296296
if __name__ == '__main__':
297297
print("starting ...")

templates.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import markdown
66
import markdown.extensions.fenced_code
77

8-
import requests
8+
from cache import fetch
99

1010
gitHub = "https://github.com/"
1111
gitHubRaw = "https://raw.githubusercontent.com"
@@ -101,7 +101,7 @@ def moduleslist(this, options, items):
101101

102102

103103
def gitRM(this, name):
104-
readme = requests.get("/".join([gitHub, name, "raw/master", "README.md"])).text
104+
readme = fetch("/".join([gitHub, name, "raw/master", "README.md"]), False)
105105
result = toHtml(readme)
106106
return result
107107

0 commit comments

Comments
 (0)