Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 05271fb

Browse files
committed
Documented utils
1 parent fcd824f commit 05271fb

File tree

1 file changed

+90
-8
lines changed

1 file changed

+90
-8
lines changed

runestone/server/utils.py

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
1-
import pdb
2-
import click
1+
# *******************************************
2+
# |docname| - reusable functions for rsmanage
3+
# *******************************************
4+
# These functions are used by the rsmanage command in RunestoneServer as well as
5+
# by the AuthorServer in its Celery worker tasks. There may be other places that
6+
# find these utils handy as well.
7+
#
8+
#
9+
# Imports
10+
# =======
11+
# These are listed in the order prescribed by `PEP 8`_.
12+
#
13+
# Standard library
14+
# ----------------
15+
16+
import os
317
import subprocess
418
import sys
5-
import os
6-
from pathlib import Path
719
import xml.etree.ElementTree as ET
820
from xml.etree import ElementInclude
21+
from pathlib import Path
22+
23+
# Third Party
24+
# -----------
25+
import click
926
from sqlalchemy import create_engine
1027
from sqlalchemy.exc import IntegrityError
28+
29+
# Local packages
30+
# --------------
1131
from runestone.pretext.chapter_pop import manifest_data_to_db
1232

1333

14-
def _build_runestone_book(course):
34+
# Build a Runestone Book
35+
# ----------------------
36+
def _build_runestone_book(course, click=click):
37+
"""
38+
Parameters:
39+
course: the name of the course to build.
40+
click: default is the click module otherwise an object that has an echo method
41+
"""
1542
try:
1643
if os.path.exists("pavement.py"):
1744
sys.path.insert(0, os.getcwd())
@@ -54,13 +81,25 @@ def _build_runestone_book(course):
5481
click.echo("Deploy failed, check the log to see what went wrong.")
5582

5683

57-
def _build_ptx_book(config, gen, manifest, course):
84+
# Build a PreTeXt Book
85+
# --------------------
86+
def _build_ptx_book(config, gen, manifest, course, click=click):
87+
"""
88+
Parameters:
89+
config : This originated as a config object from click -- a mock config will be provided by the AuthorServer
90+
gen: A flag to indicate whether or not we should build static assets
91+
manifest: the name of the manifest file
92+
course: the name of the course to build.
93+
click: default is the click module otherwise an object that has an echo method
94+
"""
5895
if not os.path.exists("project.ptx"):
5996
click.echo("PreTeXt books need a project.ptx file")
6097
sys.exit(1)
6198
else:
6299
main_file = check_project_ptx()
100+
# parse the main file, but this does not resolve any xi:includes
63101
tree = ET.parse(main_file)
102+
# The next two lines are needed to parse the entire tree
64103
root = tree.getroot()
65104
ElementInclude.include(root, base_url=main_file) # include all xi:include parts
66105
if gen:
@@ -96,7 +135,17 @@ def _build_ptx_book(config, gen, manifest, course):
96135
update_library(config, mpath, course)
97136

98137

99-
def process_manifest(cname, mpath):
138+
# Support Functions
139+
# -----------------
140+
141+
def process_manifest(cname, mpath, click=click):
142+
"""
143+
cname - the name of the course
144+
mpath - path to the runestone-manifest.xml file
145+
146+
Setup this book in the database and populate the questions table as well as
147+
The chapter and subchapter tables.
148+
"""
100149
click.echo("processing manifest...")
101150
if os.path.exists(mpath):
102151
manifest_data_to_db(cname, mpath)
@@ -107,6 +156,17 @@ def process_manifest(cname, mpath):
107156

108157

109158
def check_project_ptx():
159+
"""
160+
Verify that the PreTeXt project is set up for a Runestone build
161+
162+
Returns: Name of the main project file.
163+
164+
1. Is there a runestone target in project.ptx?
165+
2. Is the output dir set to published/basecourse
166+
3. Is the top level source file set properly
167+
4. TODO: Is the publisher file set (and present)
168+
169+
"""
110170
tree = ET.parse("project.ptx")
111171
targ = tree.find(".//target[@name='runestone']")
112172
if not targ:
@@ -126,6 +186,12 @@ def check_project_ptx():
126186

127187

128188
def extract_docinfo(tree, string, attr=None):
189+
"""
190+
Parameters:
191+
tree: The parsed document tree from ET
192+
string: The name of the element we are looking for
193+
Helper to get the contents of several tags from the docinfo element of a PreTeXt book
194+
"""
129195
el = tree.find(f"./{string}")
130196
if attr is not None and el is not None:
131197
print(f"{el.attrib[attr]=}")
@@ -138,6 +204,16 @@ def extract_docinfo(tree, string, attr=None):
138204

139205

140206
def update_library(config, mpath, course):
207+
"""
208+
Parameters:
209+
config : This originated as a config object from click -- a mock config will be provided by the AuthorServer
210+
mpath: Path to the runestone-manifest file which containes the library metadata
211+
course: the name of the course we are buildingn
212+
213+
Update the library table using meta data from the book
214+
215+
Returns: Nothing
216+
"""
141217
tree = ET.parse(mpath)
142218
docinfo = tree.find("./library-metadata")
143219
eng = create_engine(config.dburl)
@@ -171,7 +247,13 @@ def update_library(config, mpath, course):
171247

172248

173249
def populate_static(config, mpath: Path, course: str):
174-
250+
"""
251+
Copy the apropriate Javascript to the _static folder for PreTeXt books. This may
252+
involve downloading it from the Runestone CDN. PreTeXt does not include the current set
253+
of javascript files like the Runestone components release does, instead we supply it
254+
on runestone.academy/cdn/runestone so it can be used for generic html builds as well as
255+
builds on runestone.academy.
256+
"""
175257
# <runestone-services version="6.2.1"/>
176258
sdir = mpath.parent / "_static"
177259
current_version = ""

0 commit comments

Comments
 (0)