<commit_before>from os.path import dirname, join, exists, sep
from os import listdir, environ, mkdir
from subprocess import call
import sys
import logging
import argparse
import os
#from devilryclient.restfulclient import RestfulFactory
from ConfigParser import ConfigParser


def helloworld():
    print "Hello world"


def showhelp():
    """
    Print the help menu
    """
    commands = getcommandlist()
    print 'Usage: {progname} <command>'.format(progname=sys.argv[0])
    print 'Commands:'
    for cmd in commands:
        print '     {}'.format(cmd[:-3]),
        print '     {}'.format(getcmdinfo(cmd))


def getcmdinfo(cmd):
    """
    :param cmd: Command (with .py ending)
    :return: Available info on cmd
    """
    path = join(getpluginsdir(), cmd)
    return "bla bla"


def getcommandlist():
    """
    :return: All available commands
    """
    filenames = listdir(join(dirname(__file__), 'plugins'))
    commands = [filename for filename in filenames if filename.endswith('.py')]
    return commands


def getthisdir():
    """
    :return: Current directory
    """
    return dirname(__file__)


def getpluginsdir():
    """
    :return: Plugins directory
    """
    return join(getthisdir(), "plugins")


def pathwithargs(path, args):
    """
    Append args to path as a list

    :param path: Path of file to be called
    :param args: Namespace object of additional arguments
    :return: Path and args as a list of strings
    """
    commands = [path]
    for arg in args:
        commands.append(arg)
    return commands


def execute(command, args):
    """
    Execute command by calling the corresponding py-file with args as arguments

    :param command: The command to be called
    :param args: Additional arguments
    """
    logging.warning('Hello from utils.py')
    path = join(getpluginsdir(), command + ".py")
    if exists(path):
        commands = pathwithargs(path, args)
        call(commands)
    else:
        #command not found in devilry pulgins, must be a local command in .devilry folder
        path = join(environ['HOME'], '.devilry', 'plugins', command + '.py')
        if exists(path):
            commands = pathwithargs(path, args)
            call(commands)
        else:
            showhelp()
            raise SystemExit()


def logging_startup(args):
    """
    Parse the arguments in args and atart logging based on these arguments.

    Raises: SystemExit if arguments cannot be parsed

    :param args: The logging arguments
    :return: other command-specific arguments
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('-q', action='store_true', default=False, help='Quiet mode')
    parser.add_argument('-v', action='store_true', default=False, help='Verbose mode')
    parser.add_argument('otherargs', nargs='*', help='Additional arguments')
    try:
        args = parser.parse_args(args)
    except:
        showhelp()
        raise SystemExit()

    log_level = logging.INFO
    if args.q:
        log_level = logging.WARNING
    elif args.v:
        log_level = logging.DEBUG
    logging.basicConfig(format='%(message)s', level=log_level)
    #retrun args that are needed for command
    return args.otherargs


def findconffolder():
    """
    :return: The path of the .devilry folder containing config-file
    """
    cwd = os.getcwd()
    while cwd != environ["HOME"]:
        if exists(join(cwd, '.devilry')):
            return join(cwd, '.devilry')
        else:
            cwd = dirname(cwd)

    raise ValueError(".devirly not found")


def create_folder(path):
    """
    :param folder_name: A string representing the node attribute which the folder should be named after
    """
    if not exists(path):
        logging.debug('INFO: Creating {}'.format(path))
        mkdir(path)
    return path


def get_config():
    """Return a ConfigParser object that can be used immediately"""
    config = ConfigParser()
    config.read(join(findconffolder(), 'config'))
    return config


def get_metadata():
    """Try to fetch .devilry/metadata. Raise an exception if it
    doesn't exist"""

    conf_dir = findconffolder()

    metadata_f = open(join(conf_dir, 'metadata'), 'r')
    metadata = eval(metadata_f.read())
    metadata_f.close()
    return metadata


def get_metadata_from_path(path, metadata=None):
    """Given a path, find the the context the path belongs to, and
    the metadata for that level.
<commit_msg>devilryclient.utils: Added get_metadata_from_path method<commit_after>from os.path import dirname, join, exists, sep
from os import listdir, environ, mkdir
from subprocess import call
import sys
import logging
import argparse
import os
#from devilryclient.restfulclient import RestfulFactory
from ConfigParser import ConfigParser


def helloworld():
    print "Hello world"


def showhelp():
    """
    Print the help menu
    """
    commands = getcommandlist()
    print 'Usage: {progname} <command>'.format(progname=sys.argv[0])
    print 'Commands:'
    for cmd in commands:
        print '     {}'.format(cmd[:-3]),
        print '     {}'.format(getcmdinfo(cmd))


def getcmdinfo(cmd):
    """
    :param cmd: Command (with .py ending)
    :return: Available info on cmd
    """
    path = join(getpluginsdir(), cmd)
    return "bla bla"


def getcommandlist():
    """
    :return: All available commands
    """
    filenames = listdir(join(dirname(__file__), 'plugins'))
    commands = [filename for filename in filenames if filename.endswith('.py')]
    return commands


def getthisdir():
    """
    :return: Current directory
    """
    return dirname(__file__)


def getpluginsdir():
    """
    :return: Plugins directory
    """
    return join(getthisdir(), "plugins")


def pathwithargs(path, args):
    """
    Append args to path as a list

    :param path: Path of file to be called
    :param args: Namespace object of additional arguments
    :return: Path and args as a list of strings
    """
    commands = [path]
    for arg in args:
        commands.append(arg)
    return commands


def execute(command, args):
    """
    Execute command by calling the corresponding py-file with args as arguments

    :param command: The command to be called
    :param args: Additional arguments
    """
    logging.warning('Hello from utils.py')
    path = join(getpluginsdir(), command + ".py")
    if exists(path):
        commands = pathwithargs(path, args)
        call(commands)
    else:
        #command not found in devilry pulgins, must be a local command in .devilry folder
        path = join(environ['HOME'], '.devilry', 'plugins', command + '.py')
        if exists(path):
            commands = pathwithargs(path, args)
            call(commands)
        else:
            showhelp()
            raise SystemExit()


def logging_startup(args):
    """
    Parse the arguments in args and atart logging based on these arguments.

    Raises: SystemExit if arguments cannot be parsed

    :param args: The logging arguments
    :return: other command-specific arguments
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('-q', action='store_true', default=False, help='Quiet mode')
    parser.add_argument('-v', action='store_true', default=False, help='Verbose mode')
    parser.add_argument('otherargs', nargs='*', help='Additional arguments')
    try:
        args = parser.parse_args(args)
    except:
        showhelp()
        raise SystemExit()

    log_level = logging.INFO
    if args.q:
        log_level = logging.WARNING
    elif args.v:
        log_level = logging.DEBUG
    logging.basicConfig(format='%(message)s', level=log_level)
    #retrun args that are needed for command
    return args.otherargs


def findconffolder():
    """
    :return: The path of the .devilry folder containing config-file
    """
    cwd = os.getcwd()
    while cwd != environ["HOME"]:
        if exists(join(cwd, '.devilry')):
            return join(cwd, '.devilry')
        else:
            cwd = dirname(cwd)

    raise ValueError(".devirly not found")


def create_folder(path):
    """
    :param folder_name: A string representing the node attribute which the folder should be named after
    """
    if not exists(path):
        logging.debug('INFO: Creating {}'.format(path))
        mkdir(path)
    return path


def get_config():
    """Return a ConfigParser object that can be used immediately"""
    config = ConfigParser()
    config.read(join(findconffolder(), 'config'))
    return config


def get_metadata():
    """Try to fetch .devilry/metadata. Raise an exception if it
    doesn't exist"""

    conf_dir = findconffolder()

    metadata_f = open(join(conf_dir, 'metadata'), 'r')
    metadata = eval(metadata_f.read())
    metadata_f.close()
    return metadata


def save_metadata(metadata):
    conf_dir = findconffolder()
    metadata_f = open(join(conf_dir, 'metadata'), 'w')
    metadata_f.write(str(metadata))
    metadata_f.close()


def get_metadata_from_path(path, metadata=None):
    """Given a path, find the the context the path belongs to, and
    the metadata for that level.

{'fraction_non_alphanumeric': 0.11178280940866124, 'fraction_numerical': 0.00043965706748735987, 'mean_word_length': 2.992540587977183, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6113942', 'n_tokens_mistral': 2662, 'n_tokens_neox': 2603, 'n_words': 845}