-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add test for dispatcher [ Fixed #111 ] - Fix a bug where TextInventoryCollection would export to TextInventoryCollection node in CTS XML [ Fixed #110 ] - Add Dispatcher and TextInventoryCollection to documentation and example [ Fixed #108 ]
- Loading branch information
1 parent
177846c
commit a80e99a
Showing
12 changed files
with
376 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ | |
""" | ||
|
||
__version__ = "2.0.0b6" | ||
__version__ = "2.0.0b7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,77 @@ | ||
from MyCapytain.errors import UndispatchedTextError | ||
|
||
|
||
class CollectionDispatcher: | ||
""" | ||
""" Collection Dispatcher provides a utility to divide automatically texts and collections \ | ||
into different collections | ||
:param collection: | ||
:param default_inventory_name: | ||
:param collection: The root collection | ||
:param default_inventory_name: The default name of the default collection | ||
""" | ||
def __init__(self, collection, default_inventory_name=None): | ||
self.collection = collection | ||
if default_inventory_name is None: | ||
default_inventory_name = list(self.collection.children.values())[0].id | ||
self.__methods__ = [(default_inventory_name, lambda x, **k: True)] | ||
self.__methods__ = [(default_inventory_name, lambda x, **kwargs: True)] | ||
|
||
@property | ||
def methods(self): | ||
""" List of methods to dispatch resources. | ||
Each element is a tuple with two elements : | ||
- First one is the inventory identifier to dispatch to | ||
- Second one is a function which, if returns true, will activate dispatching to given | ||
:rtype: List | ||
""" | ||
return self.__methods__ | ||
|
||
def add(self, func, inventory_name=None): | ||
def add(self, func, inventory_name): | ||
""" Register given function as a filter. | ||
If this function "func" returns True when given an object, said object will be dispatched to \ | ||
Collection(inventory_name) | ||
:param func: Callable | ||
:param inventory_name: Identifier of the collection to dispatch to | ||
""" | ||
self.methods.append((inventory_name, func)) | ||
|
||
def inventory(self, inventory_name): | ||
""" Decorator to register filters for given inventory. For a function "abc", it has the same effect | ||
:param inventory_name: | ||
:return: | ||
.. code-block:: python | ||
tic = TextInventoryCollection() | ||
latin = PrototypeTextInventory("urn:perseus:latinLit", parent=tic) | ||
latin.set_label("Classical Latin", "eng") | ||
dispatcher = CollectionDispatcher(tic) | ||
@dispatcher.inventory("urn:perseus:latinLit") | ||
def dispatchLatinLit(collection, path=None, **kwargs): | ||
if collection.id.startswith("urn:cts:latinLit:"): | ||
return True | ||
return False | ||
""" | ||
def decorator(f): | ||
self.add(func=f, inventory_name=inventory_name) | ||
return f | ||
return decorator | ||
|
||
def dispatch(self, collection, **kwargs): | ||
""" Dispatch a collection using internal filters | ||
:param collection: Collection object | ||
:param kwargs: Additional keyword arguments that could be used by internal filters | ||
:return: None | ||
:raises: | ||
""" | ||
for inventory, method in self.methods[::-1]: | ||
if method(collection, **kwargs) is True: | ||
collection.parent = self.collection.children[inventory] | ||
return | ||
raise Exception("Text not dispatched %s" % collection.id) | ||
raise UndispatchedTextError("Text not dispatched %s" % collection.id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from MyCapytain.resolvers.cts.local import CTSCapitainsLocalResolver | ||
from MyCapytain.resolvers.utils import CollectionDispatcher | ||
from MyCapytain.common.constants import Mimetypes, NS | ||
from MyCapytain.resources.collections.cts import TextInventory | ||
from MyCapytain.resources.prototypes.cts.inventory import TextInventoryCollection | ||
|
||
# We set up a main collection | ||
tic = TextInventoryCollection() | ||
# We register sub collection we want to dispatch to | ||
latin = TextInventory("urn:perseus:latinLit", parent=tic) | ||
latin.set_label("Classical Latin", "eng") | ||
farsi = TextInventory("urn:perseus:farsiLit", parent=tic) | ||
farsi.set_label("Farsi", "eng") | ||
gc = TextInventory("urn:perseus:greekLit", parent=tic) | ||
gc.set_label("Ancient Greek", "eng") | ||
gc.set_label("Grec Ancien", "fre") | ||
|
||
# We create the dispatcher with the root collection | ||
dispatcher = CollectionDispatcher(tic) | ||
|
||
# And we record function for each given repository | ||
# We could have two function dispatching for the same repository ! | ||
@dispatcher.inventory("urn:perseus:latinLit") | ||
def dispatchLatinLit(collection, path=None, **kwargs): | ||
if collection.id.startswith("urn:cts:latinLit:"): | ||
return True | ||
return False | ||
|
||
@dispatcher.inventory("urn:perseus:farsiLit") | ||
def dispatchfFarsiLit(collection, path=None, **kwargs): | ||
if collection.id.startswith("urn:cts:farsiLit:"): | ||
return True | ||
return False | ||
|
||
@dispatcher.inventory("urn:perseus:greekLit") | ||
def dispatchGreekLit(collection, path=None, **kwargs): | ||
if collection.id.startswith("urn:cts:greekLit:"): | ||
return True | ||
return False | ||
|
||
# We set up a resolver which parses local file | ||
NautilusDummy = CTSCapitainsLocalResolver( | ||
resource=[ | ||
"./tests/testing_data/latinLit2" | ||
], | ||
# We give it the dispatcher | ||
dispatcher=dispatcher | ||
) | ||
|
||
# If we want to read the main repository, we will have all children | ||
all = NautilusDummy.getMetadata() | ||
|
||
print(len(all.readableDescendants)) # 25 is the number of edition and translation | ||
print([m.id for m in all.members]) # Direct members are dispatched-in collections | ||
print( | ||
all["urn:cts:latinLit:phi1294"] == all["urn:perseus:latinLit"]["urn:cts:latinLit:phi1294"] | ||
) # Is true because they are dispatched this way | ||
|
||
try: | ||
all["urn:perseus:greekLit"]["urn:cts:latinLit:phi1294"] | ||
except KeyError: | ||
print("But this won't work because the object has been dispatched to latinLit !") | ||
|
||
print(len(all["urn:perseus:greekLit"].readableDescendants)) # Is 6 because there is 6 recorded texts in __cts__ | ||
print(len(all["urn:perseus:latinLit"].readableDescendants)) # Is 19 because there is 6 recorded texts in __cts__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.