-from dcparser import (as_person, as_date, Field, WorkInfo, DCNS)
+from operator import and_
+
+from .dcparser import Field, WorkInfo, DCNS
from librarian import (RDFNS, ValidationError, NoDublinCore, ParseError, WLURI)
from xml.parsers.expat import ExpatError
from os import path
from librarian import (RDFNS, ValidationError, NoDublinCore, ParseError, WLURI)
from xml.parsers.expat import ExpatError
from os import path
@classmethod
def from_slug(cls, slug):
uri = 'http://wolnelektury.pl/katalog/obraz/%s/' % slug
return cls(uri)
@classmethod
def from_slug(cls, slug):
uri = 'http://wolnelektury.pl/katalog/obraz/%s/' % slug
return cls(uri)
Field(DCNS('subject.period'), 'epochs', salias='epoch', multiple=True),
Field(DCNS('subject.type'), 'kinds', salias='kind', multiple=True),
Field(DCNS('subject.genre'), 'genres', salias='genre', multiple=True, required=False),
Field(DCNS('subject.period'), 'epochs', salias='epoch', multiple=True),
Field(DCNS('subject.type'), 'kinds', salias='kind', multiple=True),
Field(DCNS('subject.genre'), 'genres', salias='genre', multiple=True, required=False),
Field(DCNS('format.dimensions'), 'dimensions', required=False),
Field(DCNS('format.checksum.sha1'), 'sha1', required=True),
Field(DCNS('description.medium'), 'medium', required=False),
Field(DCNS('description.dimensions'), 'original_dimensions', required=False),
Field(DCNS('format'), 'mime_type', required=False),
Field(DCNS('format.dimensions'), 'dimensions', required=False),
Field(DCNS('format.checksum.sha1'), 'sha1', required=True),
Field(DCNS('description.medium'), 'medium', required=False),
Field(DCNS('description.dimensions'), 'original_dimensions', required=False),
Field(DCNS('format'), 'mime_type', required=False),
- Field(DCNS('identifier.url'), 'url', WLPictureURI,
- strict=as_wlpictureuri_strict),
- )
+ Field(DCNS('identifier.url'), 'url', WLPictureURI, strict=as_wlpictureuri_strict)
+ )
class ImageStore(object):
EXT = ['gif', 'jpeg', 'png', 'swf', 'psd', 'bmp'
class ImageStore(object):
EXT = ['gif', 'jpeg', 'png', 'swf', 'psd', 'bmp'
- 'tiff', 'tiff', 'jpc', 'jp2', 'jpf', 'jb2', 'swc',
- 'aiff', 'wbmp', 'xbm']
+ 'tiff', 'tiff', 'jpc', 'jp2', 'jpf', 'jb2', 'swc',
+ 'aiff', 'wbmp', 'xbm']
MIME = ['image/gif', 'image/jpeg', 'image/png',
'application/x-shockwave-flash', 'image/psd', 'image/bmp',
'image/tiff', 'image/tiff', 'application/octet-stream',
MIME = ['image/gif', 'image/jpeg', 'image/png',
'application/x-shockwave-flash', 'image/psd', 'image/bmp',
'image/tiff', 'image/tiff', 'application/octet-stream',
'application/x-shockwave-flash', 'image/iff', 'image/vnd.wap.wbmp', 'image/xbm']
def __init__(self, dir_):
'application/x-shockwave-flash', 'image/iff', 'image/vnd.wap.wbmp', 'image/xbm']
def __init__(self, dir_):
- def from_string(cls, xml, *args, **kwargs):
- return cls.from_file(StringIO(xml), *args, **kwargs)
+ def from_bytes(cls, xml, *args, **kwargs):
+ return cls.from_file(six.BytesIO(xml), *args, **kwargs)
@classmethod
def from_file(cls, xmlfile, parse_dublincore=True, image_store=None):
# first, prepare for parsing
@classmethod
def from_file(cls, xmlfile, parse_dublincore=True, image_store=None):
# first, prepare for parsing
data = data.decode('utf-8')
data = data.replace(u'\ufeff', '')
# assume images are in the same directory
data = data.decode('utf-8')
data = data.replace(u'\ufeff', '')
# assume images are in the same directory
image_store = ImageStore(path.dirname(xmlfile.name))
try:
parser = etree.XMLParser(remove_blank_text=False)
image_store = ImageStore(path.dirname(xmlfile.name))
try:
parser = etree.XMLParser(remove_blank_text=False)
- except (ExpatError, XMLSyntaxError, XSLTApplyError), e:
+ except (ExpatError, XMLSyntaxError, XSLTApplyError) as e:
return self.image_store.path(self.slug, self.mime_type)
def image_file(self, *args, **kwargs):
return self.image_store.path(self.slug, self.mime_type)
def image_file(self, *args, **kwargs):
- return open(self.image_path, *args, **kwargs)
+ return open(self.image_path, 'rb', *args, **kwargs)
def get_sem_coords(self, sem):
area = sem.find("div[@type='rect']")
def get_sem_coords(self, sem):
area = sem.find("div[@type='rect']")
def has_all_props(node, props):
return reduce(and_, map(lambda prop: prop in node.attrib, props))
def has_all_props(node, props):
return reduce(and_, map(lambda prop: prop in node.attrib, props))
- if has_all_props(area, ['x1', 'x2', 'y1', 'y2']) == False:
+ if not has_all_props(area, ['x1', 'x2', 'y1', 'y2']):
def n(prop): return int(area.get(prop))
return [[n('x1'), n('y1')], [n('x2'), n('y2')]]
def n(prop): return int(area.get(prop))
return [[n('x1'), n('y1')], [n('x2'), n('y2')]]
def partiter(self):
"""
Iterates the parts of this picture and returns them and their metadata
"""
# omg no support for //sem[(@type='theme') or (@type='object')] ?
def partiter(self):
"""
Iterates the parts of this picture and returns them and their metadata
"""
# omg no support for //sem[(@type='theme') or (@type='object')] ?
- for part in list(self.edoc.iterfind("//sem[@type='theme']")) + list(self.edoc.iterfind("//sem[@type='object']")):
- pd = {}
- pd['type'] = part.get('type')
+ for part in list(self.edoc.iterfind("//sem[@type='theme']")) +\
+ list(self.edoc.iterfind("//sem[@type='object']")):
+ pd = {'type': part.get('type')}
pd['object'] = part.attrib['type'] == 'object' and want_unicode(part.attrib.get('object', u'')) or None
pd['themes'] = part.attrib['type'] == 'theme' and [part.attrib.get('theme', u'')] or []
yield pd
pd['object'] = part.attrib['type'] == 'object' and want_unicode(part.attrib.get('object', u'')) or None
pd['themes'] = part.attrib['type'] == 'theme' and [part.attrib.get('theme', u'')] or []
yield pd