Source code for vdat.config.entry_point

"""Implementation of the ``vdat_config`` entry point"""
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import argparse
from copy import copy as ccopy
import os

import pkg_resources
import six

try:
    input = raw_input
except NameError:
    pass

# The ``CONFIG_FILES`` are scanned to look for some placeholder to replace at
# run time
COPY_FILES = ["vdat_setting.cfg", "tabs.yml", 'fplane.txt',
              'vdat_commands.yml', 'buttons.yml']
"""Files copied from the package"""
COPY_DIR = ["extra_files"]
"""The files in the following subdirectories are copied too"""


# command line code to get the configuration files
[docs]def main(argv=None): """Main function of the implementation of the ``vdat_config`` executable Parameters ---------- argv : list command line, if None taken from ``sys.argv`` """ args = parse(argv=argv) if args.subparser_name == 'copy': copy(args)
[docs]def parse(argv=None): """Create the parser and parse the command line arguments Parameters ---------- argv : list command line, if None taken from ``sys.argv`` Returns ------- Namespace parsed command line """ parser = argparse.ArgumentParser(description="""Deal with the vdat configuration files""") subparser = parser.add_subparsers(title='Subcommands', dest='subparser_name', description="""Type '%(prog)s cmd -h' for detailed information about the subcommands""") description = ("Copy the files '{}' into the given" " directory".format("', '".join(COPY_FILES))) copy = subparser.add_parser('copy', description=description, help='Copy the configuration files') copy.add_argument('-t', '--to-dir', default='.', help="Directory where to copy the data") copy.add_argument('-f', '--force', action='store_true', help="Force overwriting existing configuration files. If" " this option is used, all local modifications will be" " lost") return parser.parse_args(args=argv)
[docs]def _encode(str_): """In python 3 encodes the string into bytes, in python 2 returns the input Parameters ---------- str_ : string type string to encode Returns bytes """ if six.PY3 and isinstance(str_, six.string_types): return str_.encode() else: return str_
[docs]def copy(args): """Copy the configuration files Parameters ---------- args : Namespace arguments to make the copy run """ written_files = [] _COPY_FILES = ccopy(COPY_FILES) # loop the directories, create them on the receiver end if necessary and # add the contained files to the list of the ones to copy for dir_ in COPY_DIR: try: os.mkdir(os.path.join(args.to_dir, dir_)) except OSError: pass for fname in pkg_resources.resource_listdir(__name__, dir_): _COPY_FILES.append(os.path.join(dir_, fname)) for cf in _COPY_FILES: ofile = os.path.join(args.to_dir, cf) if os.path.exists(ofile) and not args.force: overwrite = False msg = "Do you want to overwrite file '{}' (y/[n])?".format(cf) while True: try: msg answer = input(msg) if not answer: answer = 'n' if answer.lower() == 'y': overwrite = True break elif answer.lower() == 'n': break else: continue except EOFError: break if not overwrite: continue ifile = pkg_resources.resource_string(__name__, cf) with open(ofile, 'wb') as of: ifile = ifile.replace(_encode('<++>CONFIG_DIR<++>'), _encode(os.path.abspath(args.to_dir))) of.write(ifile) written_files.append(cf) if written_files: msg = "File(s) '{}' copied to directory '{}'" print(msg.format("', '".join(written_files), args.to_dir)) else: msg = "No file copied to directory '{}'" print(msg.format(args.to_dir))
if __name__ == '__main__': main()