+ args = self.args[0] if len(self.args) == 1 else self.args
+ try:
+ message = unicode(args)
+ except UnicodeDecodeError:
+ message = unicode(args, encoding='utf-8', errors='ignore')
+ return message
+
+class ParseError(UnicodeException):
+ pass
pass
class NoDublinCore(ValidationError):
"""There's no DublinCore section, and it's required."""
pass
pass
class NoDublinCore(ValidationError):
"""There's no DublinCore section, and it's required."""
pass
RDFNS = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
DCNS = XMLNamespace('http://purl.org/dc/elements/1.1/')
XINS = XMLNamespace("http://www.w3.org/2001/XInclude")
XHTMLNS = XMLNamespace("http://www.w3.org/1999/xhtml")
NCXNS = XMLNamespace("http://www.daisy.org/z3986/2005/ncx/")
OPFNS = XMLNamespace("http://www.idpf.org/2007/opf")
RDFNS = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
DCNS = XMLNamespace('http://purl.org/dc/elements/1.1/')
XINS = XMLNamespace("http://www.w3.org/2001/XInclude")
XHTMLNS = XMLNamespace("http://www.w3.org/1999/xhtml")
NCXNS = XMLNamespace("http://www.daisy.org/z3986/2005/ncx/")
OPFNS = XMLNamespace("http://www.idpf.org/2007/opf")
- _re_wl_uri = re.compile('http://wolnelektury.pl/katalog/lektura/'
- '(?P<slug>[-a-z]+)(/(?P<lang>[a-z]{3})/?)?')
+ _re_wl_uri = re.compile(r'http://(www\.)?wolnelektury.pl/katalog/lektura/'
+ '(?P<slug>[-a-z0-9]+)/?$')
- def __init__(self, uri=None):
- if uri is not None:
- self.uri = uri
- match = self._re_wl_uri.match(uri)
- assert match
- self.slug = match.group('slug')
- self.language = match.group('lang') or self.DEFAULT_LANGUAGE
+ def __init__(self, uri):
+ uri = unicode(uri)
+ self.uri = uri
+ self.slug = uri.rstrip('/').rsplit('/', 1)[-1]
- def from_slug_and_lang(cls, slug, lang):
- """Contructs an URI from slug and language code.
+ def strict(cls, uri):
+ match = cls._re_wl_uri.match(uri)
+ if not match:
+ raise ValidationError(u'Invalid URI (%s). Should match: %s' % (
+ uri, cls._re_wl_uri.pattern))
+ return cls(uri)
- >>> WLURI.from_slug_and_lang('a-slug', WLURI.DEFAULT_LANGUAGE).uri
- 'http://wolnelektury.pl/katalog/lektura/a-slug/'
- >>> WLURI.from_slug_and_lang('a-slug', 'deu').uri
- 'http://wolnelektury.pl/katalog/lektura/a-slug/deu/'
+ @classmethod
+ def from_slug(cls, slug):
+ """Contructs an URI from slug.
+
+ >>> WLURI.from_slug('a-slug').uri
+ u'http://wolnelektury.pl/katalog/lektura/a-slug/'
- def __eq__(self, other):
- return self.slug, self.language == other.slug, other.language
-
- def filename_stem(self):
- stem = self.slug
- if self.language != self.DEFAULT_LANGUAGE:
- stem += '_' + self.language
- return stem
+ def __str__(self):
+ return self.uri
- def validate_language(self, language):
- if language != self.language:
- raise ValidationError("Incorrect language definition in URI")
+ def __eq__(self, other):
+ return self.slug == other.slug
- def by_slug_and_lang(self, slug, lang=None):
- fname = WLURI.from_slug_and_lang(slug, lang).filename_stem() + '.xml'
+ def by_slug(self, slug):
+ fname = slug + '.xml'
DCNS('subject.type'): [u'Unknown'],
DCNS('subject.genre'): [u'Unknown'],
DCNS('date'): ['1970-01-01'],
DCNS('subject.type'): [u'Unknown'],
DCNS('subject.genre'): [u'Unknown'],
DCNS('date'): ['1970-01-01'],
# DCNS('date'): [creation_date],
DCNS('publisher'): [u"Fundacja Nowoczesna Polska"],
DCNS('description'):
# DCNS('date'): [creation_date],
DCNS('publisher'): [u"Fundacja Nowoczesna Polska"],
DCNS('description'):
if not os.path.isdir(dirname):
os.makedirs(dirname)
shutil.copy(self.get_filename(), path)
if not os.path.isdir(dirname):
os.makedirs(dirname)
shutil.copy(self.get_filename(), path)