<commit_before>import os
import sys
import logging
from termcolor import colored

from .config import Config
from .exceptions import MoxieException


def is_root():
    return os.getuid() == 0


def init_logging(args):
    logging.basicConfig(level=logging.INFO)

    if hasattr(logging, args['--loglevel'].upper()):
        logging.getLogger().setLevel(getattr(logging, args['--loglevel'].upper()))
    else:
        sys.stderr.write("No such log level '%s', defaulting to 'INFO'\n", args['--loglevel'])


def check_status(config):
    # calculate max host and port width for alignment
    width = max([len("{0}:{1}".format(r.destination, port)) for r in config.routes for port in r.ports])

    # check status of routes
    for route in config.routes:
        for port in route.ports:
            aligned_host = "{0}:{1}".format(route.destination, port).ljust(width, ' ')

            try:
                if route.status(port):
                    sys.stdout.write("{0} {1}\n".format(aligned_host, colored('OK', 'green')))
                else:
                    sys.stdout.write("{0} {1}\n".format(aligned_host, "OFF"))
            except MoxieException as e:
                sys.stdout.write("{0} {1} ({2})\n".format(aligned_host, colored('ISSUE', 'red'), e.message))

    return 0


def start_proxying(args, config):
    started_a_group = False

    # look for specified groups
    if args['<destinations>']:
        for group in config.groups:
            if group.name in args['<destinations>']:
                group.start()
                started_a_group = True

    # loop through configured routes
    for route in config.routes:
        # filter by destination hostname, if specified
        if args['<destinations>'] and route.destination not in args['<destinations>']:
            continue

        route.start()

    return 0


def stop_proxying(args, config):
    stopped_a_group = False

    # look for specified groups
    if args['<destinations>']:
        for group in config.groups:
            if group.name in args['<destinations>']:
                group.stop()
                stopped_a_group = True

    # loop through configured routes
    for route in config.routes:
        # filter by destination hostname, if specified
        if args['<destinations>'] and route.destination not in args['<destinations>']:
            continue

        route.stop()

    return 0


def add_destination(args, config):
    if not config.default_proxy and not args['--proxy']:
        sys.stderr.write("No default proxy set.\n\nInclude a --proxy argument, or set a default proxy in your moxie config.\n")
        return 1

    if not config.default_ports and not args['<ports>']:
        sys.stderr.write("No ports specified.\n\nInclude port(s) in the command line, or set default ports in your moxie config!\n")
        return 1

    try:
        int_ports = map(int, args['<ports>'])
    except:
        sys.stderr.write("Invalid ports: {0}\n".format(args['<ports>']))
        return 1

    if len(args['--proxy']) > 0:
        proxy = args['--proxy'][0]
    else:
        proxy = None

    result = config.add_route(
        destination=args['<destination>'],
        ports=int_ports,
        proxy=proxy
    )

    if result:
        config.save(os.path.expanduser(args['--config']))
        sys.stderr.write("Added route. Run \"moxie start\" to start proxying.\n")
        return 0
    else:
        sys.stderr.write("Failed to add route.\n")
        return 1


def remove_destination(args, config):
    result = config.remove_route(args['<destination>'])

    if result:
        config.save(os.path.expanduser(args['--config']))
        sys.stderr.write("Removed route")
        return 0
    else:
        sys.stderr.write("Failed to remove route")
        return 1

def create_or_update_group(args, config):
    result = config.create_or_update_group(args['<group>'], args['<destinations>'])

    if result:
        config.save(os.path.expanduser(args['--config']))
        sys.stderr.write('Added group')
        return 0
    else:
        sys.stderr.write('Failed to create/update group')
        return 1

def remove_group(args, config):
    result = config.remove_group(args['<group>'])

    if result:
        config.save(os.path.expanduser(args['--config']))
        sys.stderr.write('Removed group')
        return 0
    else:
        sys.stderr.write('Failed to remove group')
        return 1
<commit_msg>Add missing newlines in logging<commit_after>import os
import sys
import logging
from termcolor import colored

from .config import Config
from .exceptions import MoxieException


def is_root():
    return os.getuid() == 0


def init_logging(args):
    logging.basicConfig(level=logging.INFO)

    if hasattr(logging, args['--loglevel'].upper()):
        logging.getLogger().setLevel(getattr(logging, args['--loglevel'].upper()))
    else:
        sys.stderr.write("No such log level '%s', defaulting to 'INFO'\n", args['--loglevel'])


def check_status(config):
    # calculate max host and port width for alignment
    width = max([len("{0}:{1}".format(r.destination, port)) for r in config.routes for port in r.ports])

    # check status of routes
    for route in config.routes:
        for port in route.ports:
            aligned_host = "{0}:{1}".format(route.destination, port).ljust(width, ' ')

            try:
                if route.status(port):
                    sys.stdout.write("{0} {1}\n".format(aligned_host, colored('OK', 'green')))
                else:
                    sys.stdout.write("{0} {1}\n".format(aligned_host, "OFF"))
            except MoxieException as e:
                sys.stdout.write("{0} {1} ({2})\n".format(aligned_host, colored('ISSUE', 'red'), e.message))

    return 0


def start_proxying(args, config):
    started_a_group = False

    # look for specified groups
    if args['<destinations>']:
        for group in config.groups:
            if group.name in args['<destinations>']:
                group.start()
                started_a_group = True

    # loop through configured routes
    for route in config.routes:
        # filter by destination hostname, if specified
        if args['<destinations>'] and route.destination not in args['<destinations>']:
            continue

        route.start()

    return 0


def stop_proxying(args, config):
    stopped_a_group = False

    # look for specified groups
    if args['<destinations>']:
        for group in config.groups:
            if group.name in args['<destinations>']:
                group.stop()
                stopped_a_group = True

    # loop through configured routes
    for route in config.routes:
        # filter by destination hostname, if specified
        if args['<destinations>'] and route.destination not in args['<destinations>']:
            continue

        route.stop()

    return 0


def add_destination(args, config):
    if not config.default_proxy and not args['--proxy']:
        sys.stderr.write("No default proxy set.\n\nInclude a --proxy argument, or set a default proxy in your moxie config.\n")
        return 1

    if not config.default_ports and not args['<ports>']:
        sys.stderr.write("No ports specified.\n\nInclude port(s) in the command line, or set default ports in your moxie config!\n")
        return 1

    try:
        int_ports = map(int, args['<ports>'])
    except:
        sys.stderr.write("Invalid ports: {0}\n".format(args['<ports>']))
        return 1

    if len(args['--proxy']) > 0:
        proxy = args['--proxy'][0]
    else:
        proxy = None

    result = config.add_route(
        destination=args['<destination>'],
        ports=int_ports,
        proxy=proxy
    )

    if result:
        config.save(os.path.expanduser(args['--config']))
        sys.stderr.write("Added route. Run \"moxie start\" to start proxying.\n")
        return 0
    else:
        sys.stderr.write("Failed to add route.\n")
        return 1


def remove_destination(args, config):
    result = config.remove_route(args['<destination>'])

    if result:
        config.save(os.path.expanduser(args['--config']))
        sys.stderr.write("Removed route\n")
        return 0
    else:
        sys.stderr.write("Failed to remove route\n")
        return 1

def create_or_update_group(args, config):
    result = config.create_or_update_group(args['<group>'], args['<destinations>'])

    if result:
        config.save(os.path.expanduser(args['--config']))
        sys.stderr.write('Added group\n')
        return 0
    else:
        sys.stderr.write('Failed to create/update group\n')
        return 1

def remove_group(args, config):
    result = config.remove_group(args['<group>'])

    if result:
        config.save(os.path.expanduser(args['--config']))
        sys.stderr.write('Removed group\n')
        return 0
    else:
        sys.stderr.write('Failed to remove group\n')
        return 1

{'fraction_non_alphanumeric': 0.130083127387104, 'fraction_numerical': 0.006515389800044934, 'mean_word_length': 2.4709551656920077, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, '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': '565117', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2559, 'n_words': 696}