Skip to content

Commit

Permalink
produce MANIFEST_hidden.yml
Browse files Browse the repository at this point in the history
The LTI tool needs to know about hidden items so they can be picked.

The MANIFEST_hidden.yml includes all hidden items and is not included in
the zip file of the package.
  • Loading branch information
christianp committed Sep 27, 2024
1 parent c59bc0e commit 8f10143
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
26 changes: 22 additions & 4 deletions chirun/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self, args):
self.build_dir = (Path(args.build_path) if args.build_path is not None else (self.root_dir / 'build')).resolve()

self.hidden_paths = [] # A list of directories containing hidden items, filled in by process.FindHiddenItemsProcess.
self.hidden_files = [Path('MANIFEST_hidden.json')]

if args.veryverbose:
args.verbose = True
Expand Down Expand Up @@ -353,13 +354,20 @@ def package_zip(self):
if zipfile_name is None:
return

with zipfile.ZipFile(self.build_dir / zipfile_name, mode='w') as zf:
zipfile_path = self.build_dir / zipfile_name

if zipfile_path.exists():
zipfile_path.unlink()

with zipfile.ZipFile(zipfile_path, mode='w') as zf:
for d, dirs, files in os.walk(str(self.build_dir)):
if any(Path(d).is_relative_to(self.build_dir / p) for p in self.hidden_paths):
continue
for f in files:
p = self.build_dir.parent / d / f
fname = p.relative_to(self.build_dir)
if fname in self.hidden_files:
continue
if fname == zipfile_name:
continue
zf.write(p, fname)
Expand Down Expand Up @@ -414,12 +422,19 @@ def remove_hidden(items):
item['content'] = remove_hidden(item['content'])
return items

del manifest['args']
del manifest['static_dir']

manifest.update({
'structure': remove_hidden([item.content_tree() for item in self.structure]),
'zipfile': str(self.get_zipfile_name()),
'structure': [item.content_tree() for item in self.structure],
})

hidden_manifest = copy.deepcopy(manifest)

manifest.update({
'structure': remove_hidden(manifest['structure']),
})
del manifest['args']
del manifest['static_dir']

manifest_path = self.build_dir / 'MANIFEST.yml'

Expand All @@ -429,6 +444,9 @@ def remove_hidden(items):
with open(manifest_path.with_suffix('.json'), 'w') as f:
json.dump(manifest, f)

with open(self.build_dir / 'MANIFEST_hidden.json', 'w') as f:
json.dump(hidden_manifest, f)


def build_with_theme(self, theme):
"""
Expand Down
2 changes: 1 addition & 1 deletion chirun/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def toc_from_aux(self, splitlevel):
return self.toc

def read_aux_file(self, filename, splitlevel):
"""
r"""
Read a .aux file and save any table of contents entries found in it.
A TOC entry looks like::
Expand Down
5 changes: 5 additions & 0 deletions unittests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def get_manifest(self):
with open(self.build_dir / 'MANIFEST.json') as fp:
return json.load(fp)

@functools.cache
def get_hidden_manifest(self):
with open(self.build_dir / 'MANIFEST_hidden.json') as fp:
return json.load(fp)

class ExpectCrashTest(ChirunCompilationTest):
r"""
A test case which expects Chirun to quit with an error.
Expand Down
9 changes: 8 additions & 1 deletion unittests/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ def test_metadata(self):
def test_structure(self):
manifest = self.get_manifest()

self.assertEqual(len(manifest['structure']), 3, msg="There are two items in the structure.")
self.assertEqual(len(manifest['structure']), 3, msg="There are two items in the structure as well as the introduction.")
self.assertEqual([x['type'] for x in manifest['structure']], ['introduction', 'chapter', 'chapter'], msg="There is an introduction and two chapters.")

def test_hidden_structure(self):
manifest = self.get_hidden_manifest()

self.assertEqual(len(manifest['structure']), 4, msg="There are three items in the structure as well as the introduction.")
self.assertEqual([x['type'] for x in manifest['structure']], ['introduction', 'chapter', 'chapter', 'chapter'], msg="There is an introduction and three chapters.")
self.assertTrue(manifest['structure'][3]['is_hidden'], "The third chapter is hidden.")

def test_empty_links(self):
"""
Check that all links on the introduction page contain readable text.
Expand Down
4 changes: 4 additions & 0 deletions unittests/basic/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ structure:
- type: chapter
title: "Basic markdown document"
source: test.md
- type: chapter
title: "Basic hidden markdown document"
source: test.md
is_hidden: true
2 changes: 2 additions & 0 deletions unittests/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ def test_zipfile(self):
p = Path(dirpath, filename).relative_to(self.build_dir)
if p.suffix == '.zip':
continue
if p.name == 'MANIFEST_hidden.json':
continue
disk_filenames.append(str(p))

self.assertEqual(set(disk_filenames), set(zip_filenames))
Expand Down

0 comments on commit 8f10143

Please sign in to comment.