|
| 1 | +#!/usr/bin/env python3 |
| 2 | +''' |
| 3 | + Copyright (C) 2012-2014 Povilas Kanapickas <povilas@radix.lt> |
| 4 | +
|
| 5 | + This file is part of cppreference-doc |
| 6 | +
|
| 7 | + This program is free software: you can redistribute it and/or modify |
| 8 | + it under the terms of the GNU General Public License as published by |
| 9 | + the Free Software Foundation, either version 3 of the License, or |
| 10 | + (at your option) any later version. |
| 11 | +
|
| 12 | + This program is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + GNU General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU General Public License |
| 18 | + along with this program. If not, see http://www.gnu.org/licenses/. |
| 19 | +''' |
| 20 | + |
| 21 | +import lxml.etree as e |
| 22 | + |
| 23 | +class LinkMap: |
| 24 | + def __init__(self): |
| 25 | + self.mapping = dict() |
| 26 | + |
| 27 | + def read(self, fn): |
| 28 | + root = e.parse(fn) |
| 29 | + el_files = root.xpath('/files/*') |
| 30 | + |
| 31 | + self.mapping = dict() |
| 32 | + |
| 33 | + for el_file in el_files: |
| 34 | + fn_from = el_file.get('from') |
| 35 | + fn_to = el_file.get('to') |
| 36 | + self.mapping[fn_from] = fn_to |
| 37 | + |
| 38 | + def write(self, fn): |
| 39 | + root = e.Element('files') |
| 40 | + |
| 41 | + for key in self.mapping: |
| 42 | + file_el = e.SubElement(root, 'file') |
| 43 | + file_el.set('from', key) |
| 44 | + file_el.set('to', self.mapping[key]) |
| 45 | + |
| 46 | + out = open(fn, 'w') |
| 47 | + out.write('<?xml version="1.0" encoding="UTF-8"?>') |
| 48 | + out.write(e.tostring(root, encoding=str, pretty_print=True)) |
| 49 | + out.close() |
| 50 | + |
| 51 | + # Returns None on failure |
| 52 | + def get_dest(self, target): |
| 53 | + if target in self.mapping: |
| 54 | + return self.mapping[target] |
| 55 | + return None |
| 56 | + |
| 57 | + def add_link(self, title, target): |
| 58 | + self.mapping[title] = target |
0 commit comments