<commit_before># -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Red Hat
# Licensed under The MIT License (MIT)
# http://opensource.org/licenses/MIT
#

from __future__ import print_function

import sys
import argparse
import beanbag
import os
import os.path
import logging
import imp

# The client supports Bash completion if argcomplete Python package is
# installed. To enable it, run this in your terminal (assuming pdc is somewhere
# on path).
#
#     eval "$(register-python-argcomplete pdc)"
#
# This is only a temporary solution, when the client is packaged, a completion
# file should be shipped with it and installed to /etc/bash_completion.d/.
try:
    import argcomplete
except ImportError:
    class argcomplete(object):
        @classmethod
        def autocomplete(*args):
            pass

import pdc_client
from pdc_client.utils import pretty_print


# A list of paths to directories where plugins should be loaded from.
# The purpose of the plugins is to extend the default behaviour.
LOCAL_DIR = os.path.join(os.path.dirname(__file__), 'plugins')
INSTALLED_DIR = os.path.join('/usr/share/pdc-client', 'plugins')
PLUGIN_DIRS = [LOCAL_DIR] if not os.path.exists(INSTALLED_DIR) else [INSTALLED_DIR]

DEFAULT_PLUGINS = [
    'build_image_rtt_tests.py',
    'build_images.py',
    'component.py',
    'compose_image_rtt_tests.py',
    'compose.py',
    'compose_full_import.py',
    'compose_tree_locations.py',
    'contact.py',
    'image.py',
    'permission.py',
    'release.py',
    'repo.py',
    'rpm.py'
]

CONFIG_PLUGINS_KEY_NAME = 'plugins'


class Runner(object):
    def __init__(self):
        self.raw_plugins = []
        self.plugins = []
        self.logger = logging.getLogger('pdc')

    def load_plugins(self):
        config = None
        server = None
        plugins_set = set(DEFAULT_PLUGINS)
        args = sys.argv[1:]
        try:
            idx = args.index("-s")
            server = args[idx + 1]
        except ValueError:
            pass
        except IndexError:
            pass
        if server:
            config = pdc_client.read_config_file(server)
        if config and config.get(CONFIG_PLUGINS_KEY_NAME):
            plugins = config.get(CONFIG_PLUGINS_KEY_NAME)
            if not isinstance(plugins, list):
                raise TypeError('Plugins must be a list')
            plugins_set.update(set(plugins))

        for dir in PLUGIN_DIRS:
            self.logger.debug('Loading plugins from {0}'.format(dir))
            for name in os.listdir(dir):
                if not name.endswith('.py') or name not in plugins_set:
                    continue
                file, pathname, description = imp.find_module(name[:-3], [dir])<commit_msg>Support option --server as well.<commit_after># -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Red Hat
# Licensed under The MIT License (MIT)
# http://opensource.org/licenses/MIT
#

from __future__ import print_function

import sys
import argparse
import beanbag
import os
import os.path
import logging
import imp

# The client supports Bash completion if argcomplete Python package is
# installed. To enable it, run this in your terminal (assuming pdc is somewhere
# on path).
#
#     eval "$(register-python-argcomplete pdc)"
#
# This is only a temporary solution, when the client is packaged, a completion
# file should be shipped with it and installed to /etc/bash_completion.d/.
try:
    import argcomplete
except ImportError:
    class argcomplete(object):
        @classmethod
        def autocomplete(*args):
            pass

import pdc_client
from pdc_client.utils import pretty_print


# A list of paths to directories where plugins should be loaded from.
# The purpose of the plugins is to extend the default behaviour.
LOCAL_DIR = os.path.join(os.path.dirname(__file__), 'plugins')
INSTALLED_DIR = os.path.join('/usr/share/pdc-client', 'plugins')
PLUGIN_DIRS = [LOCAL_DIR] if not os.path.exists(INSTALLED_DIR) else [INSTALLED_DIR]

DEFAULT_PLUGINS = [
    'build_image_rtt_tests.py',
    'build_images.py',
    'component.py',
    'compose_image_rtt_tests.py',
    'compose.py',
    'compose_full_import.py',
    'compose_tree_locations.py',
    'contact.py',
    'image.py',
    'permission.py',
    'release.py',
    'repo.py',
    'rpm.py'
]

CONFIG_PLUGINS_KEY_NAME = 'plugins'


class Runner(object):
    def __init__(self):
        self.raw_plugins = []
        self.plugins = []
        self.logger = logging.getLogger('pdc')

    def load_plugins(self):
        config = None
        server = None
        idx_s, idx_server = (None, None)
        plugins_set = set(DEFAULT_PLUGINS)
        args = sys.argv[1:]
        try:
            idx_s = args.index('-s')
        except (ValueError, IndexError):
            pass
        try:
            idx_server = args.index('--server')
        except (ValueError, IndexError):
            pass
        try:
            server = args[max(idx_s, idx_server) + 1]
        except TypeError:
            pass

        if server:
            config = pdc_client.read_config_file(server)
        if config and config.get(CONFIG_PLUGINS_KEY_NAME):
            plugins = config.get(CONFIG_PLUGINS_KEY_NAME)
            if not isinstance(plugins, list):
                raise TypeError('Plugins must be a list')
            plugins_set.update(set(plugins))

        for dir in PLUGIN_DIRS:
            self.logger.debug('Loading plugins from {0}'.format(dir))
            for name in os.listdir(dir):
                if not name.endswith('.py') or name not in plugins_set:
                    continue
                file, pathname, description = imp.find_module(name[:-3], [dir])
{'fraction_non_alphanumeric': 0.10143642489803156, 'fraction_numerical': 0.0031920553289590354, 'mean_word_length': 2.6107554417413574, 'pattern_counts': {'":': 0, '<': 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': '1264400', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1679, 'n_words': 477}