| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- Mode: python; indent-tabs-mode: nil -*- |
| 3 |
|
| 4 |
# Copyright (c) 2014 Shaun McCance <[email protected]> |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
|
| 24 |
import argparse |
| 25 |
import os |
| 26 |
import sys |
| 27 |
|
| 28 |
import mallard.ducktype |
| 29 |
|
| 30 |
if __name__ == '__main__': |
| 31 |
argparser = argparse.ArgumentParser() |
| 32 |
argparser.add_argument('-o', '--output', nargs='?', |
| 33 |
help='specify the output file or directory') |
| 34 |
argparser.add_argument('files', nargs='+', |
| 35 |
help='list of input duck files') |
| 36 |
args = argparser.parse_args() |
| 37 |
if args.output is not None: |
| 38 |
if len(args.files) > 1 and not os.path.isdir(args.output): |
| 39 |
sys.stderr.write('Output must be a directory for multiple files\n') |
| 40 |
sys.exit(1) |
| 41 |
for file in args.files: |
| 42 |
try: |
| 43 |
parser = mallard.ducktype.DuckParser() |
| 44 |
parser.parse_file(file) |
| 45 |
parser.finish() |
| 46 |
except mallard.ducktype.SyntaxError as e: |
| 47 |
sys.stderr.write(e.fullmessage + '\n') |
| 48 |
basename = os.path.basename(file) |
| 49 |
if basename.endswith('.duck'): |
| 50 |
basename = basename[:-5] |
| 51 |
if args.output is None: |
| 52 |
outfile = os.path.join(os.path.dirname(file), basename + '.page') |
| 53 |
elif os.path.isdir(args.output): |
| 54 |
# FIXME: try to recreate directory structure? |
| 55 |
outfile = os.path.join(args.output, basename + '.page') |
| 56 |
else: |
| 57 |
outfile = args.output |
| 58 |
parser.document.write_xml(outfile) |