Skip to content

Commit

Permalink
synthesize an edition id
Browse files Browse the repository at this point in the history
  • Loading branch information
eshellman committed Sep 24, 2015
1 parent eba8764 commit 8b78f7d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
21 changes: 17 additions & 4 deletions gitenberg/metadata/pandata.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def __init__(self, datafile):
self.metadata = yaml.safe_load( r.content)
else:
self.metadata = yaml.safe_load(file(datafile, 'r').read())
self.set_edition_id()

def __getattr__(self, name):
if name in PANDATA_STRINGFIELDS:
Expand All @@ -79,11 +80,24 @@ def __getattr__(self, name):
return self.metadata.get(name, {})
return self.metadata.get(name, None)

def set_edition_id(self):
# set a (hopefully globally unique) edition identifier
if not self.metadata.has_key('edition_identifiers'):
self.metadata['edition_identifiers'] = {}
base=self.url
if not base:
try:
base = unicode(self.identifiers.keys[0])+':'+unicode(self.identifiers.values[0])
except:
base = u'repo:' + unicode(self._repo)
self.metadata['edition_identifiers']['edition_id'] = base + '#' + self._edition

def agents(self, agent_type):
agents = self.metadata.get(agent_type, [])
return agents if agents else []

# the edition should be able to report ebook downloads, which should have format and url attributes
# TODO - fill in URL based on a standard place in repo
def downloads(self):
return []

Expand Down Expand Up @@ -112,7 +126,6 @@ def get_by_isbn(isbn):


def get_one_identifier(self, id_name):
print 'in get 1'
if self.metadata.get(id_name,''):
return get_one(self.metadata[id_name])
if self.identifiers.get(id_name,''):
Expand All @@ -123,15 +136,14 @@ def get_one_identifier(self, id_name):

@property
def isbn(self):
print 'in isbn'
return self.get_one_identifier('isbn')

@property
def _edition(self):
if self.metadata.get("_edition", ''):
return self.metadata["_edition"]
return unicode(self.metadata["_edition"])
elif self.get_one_identifier('isbn'):
return self.get_one_identifier('isbn') #use first isbn if available
return unicode(self.get_one_identifier('isbn')) #use first isbn if available
elif self._repo:
return edition_name_from_repo(self._repo)
else:
Expand All @@ -143,4 +155,5 @@ def get_edition_list(self):
new_self = Pandata(self)
for key in edition.keys():
new_self.metadata[key] = edition[key]
new_self.set_edition_id()
yield new_self
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

setup(
name='gitenberg.metadata',
version='0.1.9',
version='0.1.10',
description="metadata development - formats and machinery for GITenberg",
long_description=readme + '\n\n' + history,
author="Eric Hellman",
Expand Down
2 changes: 2 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def test_editions(self):
self.assertEqual(ed2.publisher, "Recovering the Classics")
self.assertEqual(ed2.isbn, "9781111122223")
self.assertEqual(ed1.isbn, "")
self.assertEqual(ed1.edition_identifiers['edition_id'], "repo:Space-Viking_20728#default")
self.assertEqual(ed2.edition_identifiers['edition_id'], u'repo:Space-Viking_20728#9781111122223')


if __name__ == '__main__':
Expand Down

0 comments on commit 8b78f7d

Please sign in to comment.