Source code for pyutil.dlink2

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""Utilize argparse, pathlib and IPython to generate symlinks.

=========================
Directory Linker Rewrite
=========================

.. module:: dlink2

.. highlight:: python

.. versionchanged:: Added argparse

This is a rewrite of a script I've had for years, so I decided to go above
and beyond.


See Also
---------
:func:`IPython.utils.path.ensure_dir_exists()` : function
    Check for a dir and create it if it doesn't exist.

"""
import argparse
import logging
import sys
from pathlib import Path

try:
    from IPython.core.error import UsageError
    from IPython.utils.path import ensure_dir_exists
except (ImportError, ModuleNotFoundError):

    class UsageError(Exception):
        pass

    # TODO
    # def ensure_dir_exists(dir):

from pyutil.__about__ import __version__


[docs]class PermissionsError(OSError): """Symlinking error typically from Windows.""" pass
def _parse_arguments(): parser = argparse.ArgumentParser( prog='Directory Linker 2.0', description="Iterate over a `dest` folder" " and create symlinks in directory " "`source`. If `source` is not provided use" " current working directory." ) parser.add_argument( "destination", metavar="destination", nargs='?', type=Path, help="Files to symlink to." ) parser.add_argument( "-s", "--source_directory", metavar="SOURCE_DIRECTORY", dest='source', nargs='?', default=Path().cwd(), help="Where to create the symlinks. Defaults to the cwd." ) parser.add_argument( '-g', '--glob-pattern', metavar='GLOB_PATTERN', help='Filter files in the destination dir with a glob pattern.' ) parser.add_argument( '-r', '--recursive', action='store_true', default=False, help= "Whether to recursively symlink the child directories below the destination folder as well." ) parser.add_argument( '-V', '--version', action='version', version='%(prog)s' + __version__ ) if len(sys.argv) == 1: parser.print_help() sys.exit() else: return parser
[docs]def generate_dest(dest, glob_pattern=None): """Return a generator for all the files in the destination directory.""" if not ensure_dir_exists(dest): logging.error('%s' % dest, exc_info=1) if glob_pattern: yield Path(dest).glob(glob_pattern) else: yield Path(dest).glob('*')
[docs]def get_basenames(directory): """Get the basenames of all the files in a directory.""" return [i.stem + i.suffix for i in directory.iterdir()]
[docs]def main(): """Set up the module.""" logging.basicConfig(level=logging.INFO) user_arguments = _parse_arguments() args = user_arguments.parse_args() dest = args.destination if not dest.is_dir(): sys.exit("Provided target not a directory. Exiting.") try: src = args.source except (IndexError, AttributeError) as e: raise UsageError(e) try: glob_search = args.glob_pattern except (AttributeError): glob_search = None try: recursive = args.recursive except AttributeError: recursive = False dlink(dest, src, is_recursive=recursive, glob_pattern=glob_search)
if __name__ == "__main__": main()