2 # -*- coding: utf-8 -*-
4 # This file is part of Librarian.
6 # Copyright © 2008,2009,2010 Fundacja Nowoczesna Polska <fundacja@nowoczesnapolska.org.pl>
8 # For full list of contributors see AUTHORS file.
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU Affero General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Affero General Public License for more details.
20 # You should have received a copy of the GNU Affero General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 from lxml import etree
27 from librarian import html
28 from slughifi import slughifi
31 BOOK_URL = 'http://wolnelektury.pl/katalog/lektura/'
34 if __name__ == '__main__':
35 # Parse commandline arguments
36 usage = """Usage: %prog [options] SOURCE [SOURCE...]
37 Generate slugs for SOURCE."""
39 parser = optparse.OptionParser(usage=usage)
41 parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
42 help='print status messages to stdout')
43 parser.add_option('-f', '--force', action='store_true', dest='force', default=False,
44 help='overwrite current identifiers')
46 options, input_filenames = parser.parse_args()
48 if len(input_filenames) < 1:
53 for input_filename in input_filenames:
57 doc = etree.parse(input_filename)
59 title = doc.find('//{http://purl.org/dc/elements/1.1/}title').text
60 except AttributeError:
61 print '%s:error:Book title not found. Skipping.' % input_filename
66 parent_url = doc.find('//{http://purl.org/dc/elements/1.1/}relation.isPartOf').text
67 parent = parent_url.rsplit('/', 1)[1] + ' '
68 except AttributeError:
71 print '%s:error:Invalid parent URL "%s". Skipping.' % (input_filename, parent_url)
73 book_url = doc.find('//{http://purl.org/dc/elements/1.1/}identifier.url')
75 book_description = doc.find('//{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description')
76 book_url = etree.SubElement(book_description, '{http://purl.org/dc/elements/1.1/}identifier.url')
77 if not options.force and book_url.text.startswith('http://'):
78 print '%s:Notice:Book already has identifier URL "%s". Skipping.' % (input_filename, book_url.text)
81 book_url.text = BOOK_URL + slughifi(parent + title)[:60]
83 doc.write(input_filename, xml_declaration=True, pretty_print=True, encoding='utf-8')