Skip to content

Commit 377ec43

Browse files
authored
Extractor refactor (#3)
Don't import the extractor when the metadata file already exists.
1 parent f6610de commit 377ec43

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

hyperdiv_docs/docs_metadata.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
import json
3+
import pathlib
4+
5+
metadata = None
6+
json_path = pathlib.Path(os.path.dirname(__file__), "docs_metadata.json")
7+
8+
9+
def get_docs_metadata():
10+
global metadata
11+
12+
if metadata:
13+
return metadata
14+
15+
if json_path.exists():
16+
with open(json_path) as f:
17+
metadata = json.loads(f.read())
18+
return metadata
19+
else:
20+
from .extractor.main import extract
21+
22+
metadata = extract()
23+
return metadata
24+
25+
26+
def create_docs_metadata():
27+
"""
28+
(Re)-creates the stored JSON file containing docs metadata.
29+
"""
30+
from .extractor.main import extract
31+
32+
if json_path.exists():
33+
os.unlink(json_path)
34+
data = extract()
35+
with open(json_path, "w") as f:
36+
f.write(json.dumps(data, indent=2))

hyperdiv_docs/extractor/main.py

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,15 @@
1-
import os
2-
import json
31
import inspect
4-
import pathlib
52
import hyperdiv as hd
63
from hyperdiv.prop_types import HyperdivType
74
from .extractor import Extractor
85

9-
extracted = None
10-
json_path = pathlib.Path(os.path.dirname(__file__), "extracted.json")
11-
126

137
def extract():
148
"""
159
Extracts metadata from the Hyperdiv repo, which is used to
1610
dynamically render the docs components and prop types pages.
17-
18-
If a json file exists at `json_path`, this function will load that
19-
json file. If not, it creates an Extractor and extracts docs
20-
metadata from the hyperdiv repo, starting with all the components
21-
and types exported by hyperdiv at the top level.
22-
23-
The output is cached in the global variable `extracted` and
24-
returned on subsequent calls.
2511
"""
26-
global extracted
27-
28-
# If globally cached, return.
29-
if extracted:
30-
return extracted
31-
32-
# If the json file exists, load it, cache it, and return.
33-
if json_path.exists():
34-
with open(json_path) as f:
35-
extracted = json.loads(f.read())
36-
return extracted
3712

38-
# Otherwise extract metadata from the Hyperdiv repo.
3913
ctx = Extractor()
4014

4115
# Iterate over all the attributes exported by `hyperdiv`
@@ -78,17 +52,4 @@ def extract():
7852
for typ in ctx.types.values():
7953
ctx.extract_prop_type(typ)
8054

81-
# Cache the output
82-
extracted = ctx.output
83-
return extracted
84-
85-
86-
def create_json_file():
87-
"""
88-
(Re)-creates the stored JSON file containing docs metadata.
89-
"""
90-
if os.path.exists(json_path):
91-
os.unlink(json_path)
92-
data = extract()
93-
with open(json_path, "w") as f:
94-
f.write(json.dumps(data, indent=2))
55+
return ctx.output

hyperdiv_docs/pages/reference/components.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ...code_examples import docs_markdown
44
from ...utils import render_value
55
from ...page import page
6-
from ...extractor.main import extract
6+
from ...docs_metadata import get_docs_metadata
77

88

99
def render_methods(methods):
@@ -79,7 +79,7 @@ def render_slots(slots):
7979

8080
@router.route("/reference/components/{component_name}")
8181
def reference_component(component_name):
82-
data = extract()
82+
data = get_docs_metadata()
8383
component = data["components"].get(component_name)
8484
if not component:
8585
router.render_not_found()

hyperdiv_docs/pages/reference/prop_types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
from ...router import router
33
from ...code_examples import docs_markdown
44
from ...page import page
5-
from ...extractor.main import extract
5+
from ...docs_metadata import get_docs_metadata
66

77

88
@router.route("/reference/prop-types")
99
def prop_types():
10-
data = extract()
10+
data = get_docs_metadata()
1111

1212
top_level_types = []
1313
concrete_types = []
@@ -97,7 +97,7 @@ def prop_type(prop_type_name):
9797
)
9898
return
9999

100-
data = extract()
100+
data = get_docs_metadata()
101101
if prop_type_name not in data["prop_types"]:
102102
router.render_not_found()
103103
return

0 commit comments

Comments
 (0)