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)
62 return unicode(self.get(DCNS('creator'))[0])
68 return self.get(DCNS('identifier'))[0].slug()
73 dc_title = self.get(DCNS('title'), inherit=False)
75 return unicode(dc_title[0])
77 header = self.get_about().find(SSTNS('header'))
78 if header is not None:
79 # FIXME: This should be a simple text representation
85 class MetaItem(etree.ElementBase):
89 class Person(MetaItem):
91 return u" ".join(p.strip() for p in reversed(self.text.rsplit(u',', 1)))
94 class Identifier(MetaItem):
96 return self.text.rstrip('/').rsplit('/', 1)[-1]