예제 #1
0
파일: _ods.py 프로젝트: peopledoc/cutplace
def main(arguments):
    assert arguments is not None

    _FORMAT_CSV = "csv"
    _FORMAT_DOCBOOK = "docbook"
    _FORMAT_RST = "rst"
    _FORMATS = [_FORMAT_CSV, _FORMAT_DOCBOOK, _FORMAT_RST]
    _FORMAT_TO_SUFFIX_MAP = {
                             _FORMAT_CSV: ".csv",
                             _FORMAT_DOCBOOK: ".xml",
                             _FORMAT_RST: ".rst"
                             }
    usage = "usage: %prog [options] ODS-FILE [OUTPUT-FILE]"
    parser = optparse.OptionParser(usage)
    parser.set_defaults(format=_FORMAT_CSV, id="insert-id", sheet=1, title="Insert Title")
    parser.add_option("-f", "--format", metavar="FORMAT", type="choice", choices=_FORMATS, dest="format",
                      help="output format: %s (default: %%default)" % _tools.humanReadableList(_FORMATS))
    parser.add_option("-i", "--id", metavar="ID", dest="id", help="XML ID table can be referenced with (default: %default)")
    parser.add_option("-1", "--heading", action="store_true", dest="firstRowIsHeading", help="render first row as heading")
    parser.add_option("-s", "--sheet", metavar="SHEET", type="long", dest="sheet", help="sheet to convert (default: %default)")
    parser.add_option("-t", "--title", metavar="TITLE", dest="title", help="title to be used for XML table (default: %default)")
    options, others = parser.parse_args(arguments)

    # TODO: If no output file is specified, derive name from input file.
    if options.sheet < 1:
        _log.error("option --sheet is %d but must be at least 1" % options.sheet)
        sys.exit(1)
    elif len(others) in [1, 2]:
        sourceFilePath = others[0]
        if len(others) == 2:
            targetFilePath = others[1]
        else:
            assert options.format in _FORMAT_TO_SUFFIX_MAP
            suffix = _FORMAT_TO_SUFFIX_MAP[options.format]
            targetFilePath = _tools.withSuffix(sourceFilePath, suffix)
        _log.info("convert %r to %r using format %r" % (sourceFilePath, targetFilePath, options.format))
        try:
            if options.format == _FORMAT_CSV:
                if options.firstRowIsHeading:
                    _log.error("option --heading can not be used with --format=csv")
                    sys.exit(1)
                toCsv(sourceFilePath, targetFilePath, sheet=options.sheet)
            elif options.format == _FORMAT_DOCBOOK:
                # FIXME: Add support for --heading with DocBook.
                assert not options.firstRowIsHeading
                toDocBookXml(sourceFilePath, targetFilePath, xmlId=options.id, title=options.title, sheet=options.sheet)
            elif options.format == _FORMAT_RST:
                toRst(sourceFilePath, targetFilePath, firstRowIsHeading=options.firstRowIsHeading, sheet=options.sheet)
            else:  # pragma: no cover
                raise NotImplementedError(u"format=%r" % (options.format))
        except EnvironmentError, error:
            _log.error("cannot convert ods to csv: %s" % error)
            sys.exit(1)
        except Exception, error:
            _log.error("cannot convert ods to csv: %s" % error, exc_info=1)
            sys.exit(1)
예제 #2
0
 def _testWithSuffix(self, expectedPath, pathToTest, suffixToTest):
     actualPath = _tools.withSuffix(pathToTest, suffixToTest)
     self.assertEqual(expectedPath, actualPath)
예제 #3
0
 def _testWithSuffix(self, expectedPath, pathToTest, suffixToTest):
     actualPath = _tools.withSuffix(pathToTest, suffixToTest)
     self.assertEqual(expectedPath, actualPath)