Skip to content

Commit

Permalink
Merge pull request #23 from scaife-viewer/core/collections-sort
Browse files Browse the repository at this point in the history
Add hooks to customize sorting of CTS collections
  • Loading branch information
jacobwegner authored Oct 27, 2020
2 parents edf95ce + 5dde588 commit 3871fae
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
7 changes: 7 additions & 0 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ This package was extracted from
## Settings

### ALLOW_TRAILING_COLON

Default: `False`

When `False`, to maintain compatability with the MyCapitain resolver,
the trailing colon will be stripped from URNs.

### HOOKSET

Default: `"scaife_viewer.core.hooks.DefaultHookSet"`

The path to a hookset that can be used to customize package functionality.
25 changes: 25 additions & 0 deletions core/scaife_viewer/core/conf.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import importlib

from django.conf import settings # noqa
from django.core.exceptions import ImproperlyConfigured

from appconf import AppConf


def load_path_attr(path):
i = path.rfind(".")
module, attr = path[:i], path[i + 1 :]
try:
mod = importlib.import_module(module)
except ImportError as e:
raise ImproperlyConfigured("Error importing {0}: '{1}'".format(module, e))
try:
attr = getattr(mod, attr)
except AttributeError:
raise ImproperlyConfigured(
"Module '{0}' does not define a '{1}'".format(module, attr)
)
return attr


class CoreAppConf(AppConf):
ALLOW_TRAILING_COLON = False

# Other
HOOKSET = "scaife_viewer.core.hooks.DefaultHookSet"

class Meta:
prefix = "scaife_viewer_core"

def configure_hookset(self, value):
return load_path_attr(value)()
17 changes: 11 additions & 6 deletions core/scaife_viewer/core/cts/collections.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from functools import lru_cache, partial
from operator import attrgetter

from django.conf import settings

from MyCapytain.common.constants import RDF_NAMESPACES
from MyCapytain.resources.collections.cts import XmlCtsTextInventoryMetadata
from MyCapytain.resources.prototypes.cts import inventory as cts

from ..hooks import hookset
from .capitains import default_resolver
from .passage import Passage
from .reference import URN
Expand Down Expand Up @@ -41,11 +41,14 @@ def __repr__(self):
return f"<cts.TextInventory at {hex(id(self))}>"

def text_groups(self):
for urn in sorted(self.metadata.textgroups.keys()):
text_group_urns = self.metadata.textgroups.keys()
text_groups = []
for urn in text_group_urns:
text_group = TextGroup(urn, self.metadata.textgroups[urn])
if next(text_group.works(), None) is None:
continue
yield text_group
text_groups.append(text_group)
yield from hookset.sort_text_groups(text_groups)


class Collection:
Expand Down Expand Up @@ -84,11 +87,13 @@ def __repr__(self):

def works(self):
children = self.metadata.works
for urn in sorted(children.keys()):
works = []
for urn in children.keys():
work = Work(urn, children[urn])
if next(work.texts(), None) is None:
continue
yield work
works.append(work)
yield from hookset.sort_works(works)

def as_json(self, **kwargs) -> dict:
return {
Expand Down Expand Up @@ -116,7 +121,7 @@ def texts(self):
if metadata.citation is None:
continue
texts.append(resolve_collection(metadata.TYPE_URI)(urn, metadata))
yield from sorted(texts, key=attrgetter("kind", "label"))
yield from hookset.sort_texts(texts)

def as_json(self, **kwargs) -> dict:
return {
Expand Down
19 changes: 19 additions & 0 deletions core/scaife_viewer/core/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class DefaultHookSet:
def sort_text_groups(self, text_groups):
return sorted(text_groups, key=lambda tg: tg.urn)

def sort_works(self, works):
return sorted(works, key=lambda w: w.urn)

def sort_texts(self, texts):
return sorted(texts, key=lambda t: (t.kind, t.label))


class HookProxy:
def __getattr__(self, attr):
from .conf import settings # noqa; avoids race condition

return getattr(settings.SCAIFE_VIEWER_CORE_HOOKSET, attr)


hookset = HookProxy()

0 comments on commit 3871fae

Please sign in to comment.