spiegel-keyman/linux/keyman-config/km-kvk2ldml
Eberhard Beilharz a2950bef7f
refactor(linux): Cleanup import of os.path
This change imports `os` instead of `os.path`. This is the
recommended way. It also reduces confusion whether to use
`os.path.whatever()` or just `path.whatever()`.
2021-08-04 12:24:07 +02:00

62 lines
2.2 KiB
Python
Executable file

#!/usr/bin/python3
import argparse
import logging
import os
import sys
from keyman_config import __version__
def main():
parser = argparse.ArgumentParser(
description='Convert a Keyman kvk on-screen keyboard file to an LDML file. Optionally ' +
'print the details of the kvk file.')
parser.add_argument('-p', "--print", help='print kvk details', action="store_true")
parser.add_argument('-k', "--keys", help='if printing also print all keys', action="store_true")
parser.add_argument('kvkfile', help='kvk file')
parser.add_argument('-o', '--output', metavar='LDMLFILE', help='output LDML file location')
parser.add_argument('--version', action='version', version='%(prog)s version ' + __version__)
parser.add_argument('-v', '--verbose', action='store_true', help='verbose logging')
parser.add_argument('-vv', '--veryverbose', action='store_true', help='very verbose logging')
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(message)s')
elif args.veryverbose:
logging.basicConfig(level=logging.DEBUG, format='%(levelname)s:%(message)s')
else:
logging.basicConfig(format='%(levelname)s:%(message)s')
from keyman_config.kvk2ldml import parse_kvk_file, print_kvk, convert_ldml, output_ldml
name, ext = os.path.splitext(args.kvkfile)
# Check if input file extension is kvk
if ext != ".kvk":
logging.error("km-kvk2ldml: error, input file %s is not a kvk file.", args.kvkfile)
logging.error("km-kvk2ldml [-h] [-k] [-p] [-o <ldml file>] <kvk file>")
sys.exit(2)
# Check if input kvk file exists
if not os.path.isfile(args.kvkfile):
logging.error("km-kvk2ldml: error, input file %s does not exist.", args.kvkfile)
logging.error("km-kvk2ldml [-h] [-k] [-p] [-o <ldml file>] <kvk file>")
sys.exit(2)
kvkData = parse_kvk_file(args.kvkfile)
if args.print:
print_kvk(kvkData, args.keys)
if args.output:
outputfile = args.output
else:
outputfile = name + ".ldml"
with open(outputfile, 'wb') as ldmlfile:
ldml = convert_ldml(kvkData)
output_ldml(ldmlfile, ldml)
if __name__ == "__main__":
main()