<commit_before>#!/usr/bin/env python
import argparse
import logging
import os
import shutil
import sys

# Location of saved templates
SAVE_DIR = os.path.expanduser("~") + "/.recycle/"

try:
    input = raw_input
except NameError:
    pass


def init():
    if not os.path.isdir(SAVE_DIR):
        os.makedirs(SAVE_DIR)

    logging.basicConfig(level=logging.DEBUG,
                        format="%(asctime)s %(levelname)-8s %(message)s",
                        datefmt="%m-%d %H:%M",
                        filename=SAVE_DIR + "recycle.log",
                        filemode="w")
    console = logging.StreamHandler()

    # INFO or higher goes to console
    console.setLevel(logging.INFO)
    formatter = logging.Formatter("%(levelname)-8s %(message)s")
    console.setFormatter(formatter)

    logging.getLogger("").addHandler(console)
    logging.debug("Using Python version " + sys.version)


def handle_new(name, files):
    save_path = SAVE_DIR + name

    if os.path.isdir(save_path):
        # Boilerplate with that name already exists
        logging.debug(save_path + " already exists. Asking to overwrite...")

        res = ""
        while res != "y" and res != "n":
            res = input("Template '" + name + "' already exists. "
                        "Do you want to replace its contents? (y/n) ")
            res = res.lower()

        if res == "n":
            logging.debug("Overwrite denied. Aborting")
            return
        else:
            logging.debug("Overwrite approved. Deleting " + save_path)
            handle_delete(name)

    assert not os.path.isdir(save_path)

    files = os.path.abspath(files)
    logging.debug("Creating new template '" + name + "' from " + files)

    if os.path.isdir(files):
        shutil.copytree(files, save_path)
    elif os.path.isfile(files):
        os.makedirs(save_path)
        shutil.copy(files, save_path)
    else:
        logging.error("Source '" + files + "' not found!")
        return

    assert os.path.isdir(save_path)
    logging.debug("Boilerplate created!")


def handle_use(name):
    save_path = SAVE_DIR + name

    if os.path.isdir(save_path):
        logging.debug("Using template '" + name + "'")

        contents = os.listdir(save_path)
        for obj in contents:
            path = os.path.join(save_path, obj)

            if os.path.isdir(path):
                dest = os.path.join(os.getcwd(), obj)
                shutil.copytree(path, dest)
            elif os.path.isfile(path):
                shutil.copy(path, os.getcwd())
            else:
                logging.debug("Skipping template content '" + path + "'")
    else:
        logging.error("No template with the name '" + name + "'  was found!")


def handle_list():
    assert os.path.isdir(SAVE_DIR)

    names = next(os.walk(SAVE_DIR))[1]
    for line in names:
        if line.startswith(SAVE_DIR):
            line = line[len(SAVE_DIR):-1]
        print(line)


def handle_delete(name):
    save_path = SAVE_DIR + name

    if os.path.isdir(save_path):
        shutil.rmtree(save_path)
    else:
        logging.error("No template with the name '" + name + "'  was found!")

    assert not os.path.isdir(save_path)


def parseargs():
    parser = argparse.ArgumentParser()

    subparsers = parser.add_subparsers()

    new_parser = subparsers.add_parser(
        "new", help="Create a new template or overwrite an existing one")<commit_msg>Added support for globbing. Should close #8<commit_after>#!/usr/bin/env python
import argparse
import logging
import os
import shutil
import sys
import glob

# Location of saved templates
SAVE_DIR = os.path.expanduser("~") + "/.recycle/"

try:
    input = raw_input
except NameError:
    pass


def copy(contents, dest):
    if not os.path.isdir(dest):
        os.makedirs(dest)

    for obj in contents:
        if os.path.isdir(obj):
            folderName = os.path.basename(os.path.normpath(obj))
            shutil.copytree(obj, os.path.join(dest, folderName))
        elif os.path.isfile(obj):
            shutil.copy(obj, dest)
        else:
            raise IOError("Source doest not exist!")

def getSavePath(templateName):
    return os.path.join(SAVE_DIR, templateName)

def init():
    if not os.path.isdir(SAVE_DIR):
        os.makedirs(SAVE_DIR)

    logging.basicConfig(level=logging.DEBUG,
                        format="%(asctime)s %(levelname)-8s %(message)s",
                        datefmt="%m-%d %H:%M",
                        filename=SAVE_DIR + "recycle.log",
                        filemode="w")
    console = logging.StreamHandler()

    # INFO or higher goes to console
    console.setLevel(logging.INFO)
    formatter = logging.Formatter("%(levelname)-8s %(message)s")
    console.setFormatter(formatter)

    logging.getLogger("").addHandler(console)
    logging.debug("Using Python version " + sys.version)


def handle_new(name, files):
    save_path = getSavePath(name)

    fileList = [os.path.abspath(f) for f in glob.glob(files)]
    if len(fileList) is 0:
        logging.error("No files found matching '" + files + "'")
        return

    if os.path.isdir(save_path):
        # Boilerplate with that name already exists
        logging.debug(save_path + " already exists. Asking to overwrite...")

        res = ""
        while res != "y" and res != "n":
            res = input("Template '" + name + "' already exists. "
                        "Do you want to replace its contents? (y/n) ")
            res = res.lower()

        if res == "n":
            logging.debug("Overwrite denied. Aborting")
            return
        else:
            logging.debug("Overwrite approved. Deleting " + save_path)
            handle_delete(name)

    assert not os.path.isdir(save_path)

    logging.debug("Creating new template '" + name + "' from " + files)

    try:
        copy(fileList, save_path)
    except IOError as e:
        logging.error(e.strerror)

    assert os.path.isdir(save_path)
    logging.debug("Boilerplate created!")


def handle_use(name):
    save_path = getSavePath(name)

    if os.path.isdir(save_path):
        logging.debug("Using template '" + name + "'")

        contents = os.listdir(save_path)
        contentPaths = [os.path.join(save_path, c) for c in contents]

        try:
            copy(contentPaths, os.getcwd())
        except IOError as e:
            logging.error("Apparently your current directory doesn't exist...")
    else:
        logging.error("No template with the name '" + name + "'  was found!")


def handle_list():
    assert os.path.isdir(SAVE_DIR)

    names = next(os.walk(SAVE_DIR))[1]
    for line in names:
        if line.startswith(SAVE_DIR):
            line = line[len(SAVE_DIR):-1]
        print(line)


def handle_delete(name):
    save_path = getSavePath(name)

    if os.path.isdir(save_path):
        shutil.rmtree(save_path)
    else:
        logging.error("No template with the name '" + name + "'  was found!")

    assert not os.path.isdir(save_path)


def parseargs():
    parser = argparse.ArgumentParser()

    subparsers = parser.add_subparsers()

    new_parser = subparsers.add_parser(
        "new", help="Create a new template or overwrite an existing one")
{'fraction_non_alphanumeric': 0.12277777777777778, 'fraction_numerical': 0.001388888888888889, 'mean_word_length': 2.3337962962962964, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '306500', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2206, 'n_words': 521}