2 # -*- coding: utf-8 -*-
7 from librarian import html
8 from slughifi import slughifi
11 BOOK_URL = 'http://wolnelektury.pl/katalog/lektura/'
14 if __name__ == '__main__':
15 # Parse commandline arguments
16 usage = """Usage: %prog [options] SOURCE [SOURCE...]
17 Generate slugs for SOURCE."""
19 parser = optparse.OptionParser(usage=usage)
21 parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
22 help='print status messages to stdout')
24 options, input_filenames = parser.parse_args()
26 if len(input_filenames) < 1:
31 for input_filename in input_filenames:
35 doc = etree.parse(input_filename)
37 title = doc.find('//{http://purl.org/dc/elements/1.1/}title').text
38 except AttributeError:
39 print '%s:error:book title not found, skipping' % input_filename
44 parent_url = doc.find('//{http://purl.org/dc/elements/1.1/}relation.isPartOf').text
45 parent = parent_url.rsplit('/', 1)[1] + ' '
46 except AttributeError:
49 book_url = doc.find('//{http://purl.org/dc/elements/1.1/}identifier.url')
51 book_description = doc.find('//{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description')
52 book_url = etree.SubElement(book_description, '{http://purl.org/dc/elements/1.1/}identifier.url')
53 elif book_url.text.startswith('http://'):
54 print '%s:notice:book already has identifier starting with http://, skipping' % input_filename
56 book_url.text = BOOK_URL + slughifi(parent + title)
58 doc.write(input_filename, xml_declaration=True, pretty_print=True, encoding='utf-8')