commit 76fb890920353a77b875099749192b228a9811b4
parent c5e844b97f386dc531e67d3453ee1c8021574301
Author: lash <dev@holbrook.no>
Date: Sun, 6 Sep 2026 16:57:18 -0600
File stem search for metadata
Diffstat:
2 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/adesse/util/xmp.py b/adesse/util/xmp.py
@@ -20,13 +20,21 @@ def _parse_xmp(xmp_file, resource_file, search_dir=None):
r = r.find('rdf:RDF', namespaces={'rdf': str(RDF)})
f = None
+ cont = True
+ fp = None
for d in search_dir:
- fp = os.path.join(d, resource_file)
- try:
- f = open(fp, 'rb')
+ if not cont:
break
- except FileNotFoundError:
- pass
+ for p in resource_file:
+ fp = os.path.join(d, p)
+ logg.debug('trying file {}'.format(fp))
+ try:
+ f = open(fp, 'rb')
+ cont = False
+ logg.info('match on file {}'.format(fp))
+ break
+ except FileNotFoundError:
+ pass
if f == None:
raise FileNotFoundError()
@@ -41,10 +49,10 @@ def _parse_xmp(xmp_file, resource_file, search_dir=None):
for v in r.findall('rdf:Description', namespaces={'rdf': str(RDF)}):
v.set('{{{}}}about'.format(str(RDF)), uu)
- mime = mimetypes.guess_type(resource_file)
+ mime = mimetypes.guess_type(fp)
s = lxml.etree.tostring(r)
- return (uu, mime, s,)
+ return (uu, mime, s, fp,)
def _get_alt_lang(sbj, prd, o, lang=None):
@@ -116,11 +124,20 @@ def get_tags(sbj, o):
def attachment_from_xmp(xml_file, resource_file=None, search_dir=None, uuidns=None):
if search_dir == None:
search_dir = ['.']
+ if resource_file == None:
+ v = os.path.splitext(xml_file)[0]
+ resource_file = []
+ resource_file.append(v + '.xmp')
+ resource_file.append(v + '.xml')
+ logg.debug('default to xmp files {}'.format(resource_file))
+ elif isinstance(resource_file, str):
+ resource_file = [resource_file]
r = _parse_xmp(xml_file, resource_file=resource_file, search_dir=search_dir)
o = Graph()
z = r[0]
sbj = URIRef(z)
mime = r[1]
+ resource_file = r[3]
o.parse(r[2], format='xml')
(default_lang, langs) = scan_lang(sbj, o)
title = parse_title(sbj, o, lang=default_lang)
diff --git a/test/test_xmp.py b/test/test_xmp.py
@@ -24,7 +24,8 @@ class TestXmpLoad(unittest.TestCase):
def test_parse(self):
fp = os.path.join(thisdir, 'import.xmp')
- r = xmp.attachment_from_xmp(fp, 'import.pdf', search_dir=[thisdir], uuidns=self.uubase)
+ #r = xmp.attachment_from_xmp(fp, 'import.pdf', search_dir=[thisdir], uuidns=self.uubase)
+ r = xmp.attachment_from_xmp(fp, search_dir=[thisdir], uuidns=self.uubase)
z = Serializer()
r.apply(z)
print(z)