Skip to content

Commit

Permalink
Add --clean flag for create_hugo_yaml.py (re #316)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Bollmann committed May 11, 2019
1 parent 705cec0 commit 6100ce9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bin/build_hugo
Expand Up @@ -32,7 +32,7 @@ if ! [ -x "$(command -v hugo)" ]; then
exit 1
fi

python3 $DIR/create_hugo_yaml.py
python3 $DIR/create_hugo_yaml.py --clean
python3 $DIR/create_hugo_pages.py --clean
python3 $DIR/create_bibtex.py --clean
cd $DIR/../hugo
Expand Down
52 changes: 20 additions & 32 deletions bin/create_hugo_yaml.py
Expand Up @@ -15,14 +15,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Usage: create_hugo_yaml.py [--importdir=DIR] [--exportdir=DIR] [--debug] [--dry-run]
"""Usage: create_hugo_yaml.py [--importdir=DIR] [--exportdir=DIR] [-c] [--debug] [--dry-run]
Creates YAML files containing all necessary Anthology data for the static website generator.
Options:
--importdir=DIR Directory to import XML files from. [default: {scriptdir}/../data/]
--exportdir=DIR Directory to write YAML files to. [default: {scriptdir}/../hugo/data/]
--debug Output debug-level log messages.
-c, --clean Delete existing files in target directory before generation.
-n, --dry-run Do not write YAML files (useful for debugging).
-h, --help Display this helpful text.
"""
Expand All @@ -41,15 +42,10 @@

from anthology import Anthology
from anthology.utils import SeverityTracker
from create_hugo_pages import check_directory


def export_anthology(anthology, outdir, dryrun=False):
# Create directories
for subdir in ("", "papers", "people"):
target_dir = "{}/{}".format(outdir, subdir)
if not os.path.isdir(target_dir):
os.mkdir(target_dir)

def export_anthology(anthology, outdir, clean=False, dryrun=False):
# Prepare paper index
papers = defaultdict(dict)
for id_, paper in anthology.papers.items():
Expand All @@ -65,13 +61,11 @@ def export_anthology(anthology, outdir, dryrun=False):
del data["xml_abstract"]
if "author" in data:
data["author"] = [
anthology.people.resolve_name(name, id_)
for name, id_ in data["author"]
anthology.people.resolve_name(name, id_) for name, id_ in data["author"]
]
if "editor" in data:
data["editor"] = [
anthology.people.resolve_name(name, id_)
for name, id_ in data["editor"]
anthology.people.resolve_name(name, id_) for name, id_ in data["editor"]
]
papers[paper.top_level_id][paper.full_id] = data

Expand All @@ -88,12 +82,7 @@ def export_anthology(anthology, outdir, dryrun=False):
reverse=True,
)
data["coauthors"] = sorted(
[
[co_id, count]
for (co_id, count) in anthology.people.get_coauthors(
id_
)
],
[[co_id, count] for (co_id, count) in anthology.people.get_coauthors(id_)],
key=lambda p: p[1],
reverse=True,
)
Expand All @@ -107,16 +96,9 @@ def export_anthology(anthology, outdir, dryrun=False):
key=lambda p: p[1],
reverse=True,
)
variants = [
n
for n in anthology.people.get_used_names(id_)
if n != name
]
variants = [n for n in anthology.people.get_used_names(id_) if n != name]
if len(variants) > 0:
data["variant_entries"] = [
name.as_dict()
for name in variants
]
data["variant_entries"] = [name.as_dict() for name in variants]
people[id_[0]][id_] = data

# Prepare volume index
Expand All @@ -131,13 +113,11 @@ def export_anthology(anthology, outdir, dryrun=False):
data["papers"] = volume.paper_ids
if "author" in data:
data["author"] = [
anthology.people.resolve_name(name, id_)
for name, id_ in data["author"]
anthology.people.resolve_name(name, id_) for name, id_ in data["author"]
]
if "editor" in data:
data["editor"] = [
anthology.people.resolve_name(name, id_)
for name, id_ in data["editor"]
anthology.people.resolve_name(name, id_) for name, id_ in data["editor"]
]
volumes[volume.full_id] = data

Expand Down Expand Up @@ -167,6 +147,12 @@ def export_anthology(anthology, outdir, dryrun=False):

# Dump all
if not dryrun:
# Create directories
for subdir in ("", "papers", "people"):
target_dir = "{}/{}".format(outdir, subdir)
if not check_directory(target_dir, clean=clean):
return

progress = tqdm(total=len(papers) + len(people) + 7)
for top_level_id, paper_list in papers.items():
with open("{}/papers/{}.yaml".format(outdir, top_level_id), "w") as f:
Expand Down Expand Up @@ -212,7 +198,9 @@ def export_anthology(anthology, outdir, dryrun=False):
log.info("Reading the Anthology data...")
anthology = Anthology(importdir=args["--importdir"])
log.info("Exporting to YAML...")
export_anthology(anthology, args["--exportdir"], dryrun=args["--dry-run"])
export_anthology(
anthology, args["--exportdir"], clean=args["--clean"], dryrun=args["--dry-run"]
)

if tracker.highest >= log.ERROR:
exit(1)
Empty file removed hugo/data/.keep
Empty file.

0 comments on commit 6100ce9

Please sign in to comment.