# HG changeset patch # User Paul Boddie # Date 1543177692 -3600 # Node ID 104491ce1ed4d3398018b2b939eb85457791f4cd # Parent 032af926246bf75fcd480ec5aab2c77331409259 Tidied up and added copyright and licensing information. diff -r 032af926246b -r 104491ce1ed4 convert.py --- a/convert.py Sun Nov 25 16:57:15 2018 +0100 +++ b/convert.py Sun Nov 25 21:28:12 2018 +0100 @@ -1,10 +1,51 @@ #!/usr/bin/env python +""" +Moin wiki format converter. + +Copyright (C) 2018 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + from moinformat import make_parser, make_serialiser, Metadata, parse, serialise from os.path import split import sys +# Long messages. + +message_all_with_filenames = """\ +Using --all overrides any indicated pagenames. Either --all or the filenames +should be omitted.""" + +message_explicit_pagenames = """\ +Explicit pagenames (indicated using --pagename) are only to be specified when +providing filenames without an input directory (indicated using --input-dir). + +To indicate pagenames within an input directory, omit any --pagename flags.""" + + + +# Options management. + def getmapping(mappings): + + """ + Return the given 'mappings' - a collection of key-then-value items - as a + dictionary. + """ + mapping = {} key = None @@ -18,9 +59,22 @@ return mapping def getvalue(values, default=None): + + """ + Return the first value from 'values' or 'default' if 'values' is empty or + the first value tests as false. + """ + return values and values[0] or default + + +# Main program. + def main(): + + "Interpret program options and perform the conversion." + dirname, progname = split(sys.argv[0]) args = sys.argv[1:] @@ -182,18 +236,12 @@ if input_dir: if pagenames: - print >>sys.stderr, """\ -Explicit pagenames (indicated using --pagename) are only to be specified when -providing filenames without an input directory (indicated using --input-dir). - -To indicate pagenames within an input directory, omit any --pagename flags.""" + print >>sys.stderr, message_explicit_pagenames sys.exit(1) if all: if filenames: - print >>sys.stderr, """\ -Using --all overrides any indicated pagenames. Either --all or the filenames -should be omitted.""" + print >>sys.stderr, message_all_with_filenames sys.exit(1) else: filenames = input.all()