1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from librarian import DCNS, SSTNS
11 """ Finds out the text value of metadata element.
14 >>> p.text = u"Czajka, Radek"
15 >>> print text_value(p)
19 if hasattr(meta, 'text_value'):
20 return meta.text_value()
25 class Metadata(etree.ElementBase):
27 def about(cls, element):
33 if hasattr(self, '_about'):
36 return self.getparent()
38 def get(self, key, inherit=True):
39 """ Finds metadata by its element name. """
40 values = self.findall(key)
42 return [text_value(v) for v in values]
43 elif inherit and self.get_about().getparent() is not None:
44 return self.get_about().getparent().meta.get(key)
45 elif inherit and hasattr(self.get_about(), 'meta_context'):
46 return self.get_about().meta_context.get(key)
50 def get_one(self, *args, **kwargs):
51 values = self.get(*args, **kwargs)
61 return unicode(self.get(DCNS('creator'))[0])
67 return self.get(DCNS('identifier'))[0].slug()
72 dc_title = self.get(DCNS('title'), inherit=False)
74 return unicode(dc_title[0])
76 header = self.get_about().find(SSTNS('header'))
77 if header is not None:
78 # FIXME: This should be a simple text representation
84 class MetaItem(etree.ElementBase):
88 class Person(MetaItem):
90 return u" ".join(p.strip() for p in reversed(self.text.rsplit(u',', 1)))
93 class Identifier(MetaItem):
95 return self.text.rstrip('/').rsplit('/', 1)[-1]