Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import os
import xml.etree.ElementTree as ET
import sys
from urllib.parse import quote
except ImportError:
from urllib import quote
ns = {
'rdf': '',
'z': '',
'dc': '',
'link': '',
'dcterms': '',
for prefix, url in ns.items():
ET.register_namespace(prefix, url)
extensions = ['.' + ext for ext in sys.argv[1:]]
def elementID(path):
return '#' + quote(path, safe='')
rdf = ET.Element('{%s}RDF' % (ns['rdf'],))
for path, dirs, files in os.walk("."):
path = path[2:] # removes './'
if path != '':
coll = ET.SubElement(rdf, '{%s}Collection' % (ns['z'],),)
coll.set('{%s}about' % (ns['rdf'],), elementID(path))
ET.SubElement(coll, '{%s}title' % (ns['dc'],)).text = os.path.basename(path)
elt = ET.SubElement(coll, '{%s}type' % (ns['rdf'],))
elt.set('{%s}resource' % (ns['rdf'],), '')
for d in dirs:
elt = ET.SubElement(coll, '{%s}hasPart' % (ns['dcterms'],))
elt.set('{%s}resource' % (ns['rdf'],), elementID(os.path.join(path, d)))
for f in files:
if not os.path.splitext(f)[1] in extensions: continue
elt = ET.SubElement(coll, '{%s}hasPart' % (ns['dcterms'],))
elt.set('{%s}resource' % (ns['rdf'],), elementID(os.path.join(path, f)))
for f in files:
if not os.path.splitext(f)[1] in extensions: continue
att = ET.SubElement(rdf, '{%s}Attachment' % (ns['z'],))
att.set('{%s}about' % (ns['rdf'],), elementID(os.path.join(path, f)))
ET.SubElement(att, '{%s}itemType' % (ns['z'],)).text = 'attachment'
elt = ET.SubElement(att, '{%s}resource' % (ns['rdf'],))
elt.set('{%s}resource' % (ns['rdf'],), os.path.join(path, f))
ET.SubElement(att, '{%s}title' % (ns['dc'],)).text = f
#ET.SubElement(att, '{%s}type' % (ns['link'],)).text =
with open('attachments.rdf', 'wb') as f:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.