<commit_before># canaryd
# File: canaryd/settings.py
# Desc: settings for canaryd/canaryctl

import platform

from ConfigParser import (
    DuplicateSectionError,
    Error as ConfigParserError,
    RawConfigParser,
)
from os import environ, geteuid, makedirs, path
from shutil import copytree

from canaryd.packages import click, six  # noqa

from canaryd.exceptions import ConfigError
from canaryd.log import logger

API_BASE = environ.get(
    'API_BASE',
    'https://api.servicecanary.com',
)

API_VERSION = int(environ.get(
    'API_VERSION',
    1,
))

API_KEY = environ.get(
    'API_KEY',
    None,
)

SERVER_ID = environ.get(
    'SERVER_ID',
    None,
)


class CanarydSettings(object):
    api_base = API_BASE
    api_version = API_VERSION

    log_file = None
    debug = False

    collect_interval_s = 30

    # API access key
    api_key = API_KEY

    # ID of (hopefully this) server attached to this key
    server_id = SERVER_ID

    def __init__(self, **kwargs):
        self.update(kwargs)

    def update(self, data):
        for key, value in six.iteritems(data):
            setattr(self, key, value)

        logger.debug('Settings updated: {0}'.format(data))


def get_config_directory():
    # If we're non-root, just use the users config dir (~/.config, etc)
    if geteuid() > 0:
        config_directory = click.get_app_dir('canaryd')

        return config_directory

    # If we're root on OSX
    if platform.system() == 'Darwin':
        return path.join('/', 'Library', 'Application Support', 'canaryd')

    # Elsewhere on *nix (no Windows support _yet_)
    return path.join('/', 'etc', 'canaryd')


def get_config_file():
    return path.join(get_config_directory(), 'canaryd.conf')


def get_settings(config_file=None):
    '''
    Load the config from the filesystem if provided, with defaults.<commit_msg>Auto log to /var/log/canaryd.log if root and the directory exists.<commit_after># canaryd
# File: canaryd/settings.py
# Desc: settings for canaryd/canaryctl

import platform

from ConfigParser import (
    DuplicateSectionError,
    Error as ConfigParserError,
    RawConfigParser,
)
from os import environ, geteuid, makedirs, path
from shutil import copytree

from canaryd.packages import click, six  # noqa

from canaryd.exceptions import ConfigError
from canaryd.log import logger

API_BASE = environ.get(
    'API_BASE',
    'https://api.servicecanary.com',
)

API_VERSION = int(environ.get(
    'API_VERSION',
    1,
))

API_KEY = environ.get(
    'API_KEY',
    None,
)

SERVER_ID = environ.get(
    'SERVER_ID',
    None,
)


class CanarydSettings(object):
    api_base = API_BASE
    api_version = API_VERSION

    log_file = None
    debug = False

    collect_interval_s = 30

    # API access key
    api_key = API_KEY

    # ID of (hopefully this) server attached to this key
    server_id = SERVER_ID

    def __init__(self, **kwargs):
        self.update(kwargs)

        # If no log file specified, we're root and /var/log exists, use that
        if (
            self.log_file is None
            and geteuid() <= 0
            and path.exists(path.join('/', 'var', 'log'))
        ):
            logger.debug('Root user, so setting log file to /var/log/canaryd.log')
            self.log_file = path.join('/', 'var', 'log', 'canaryd.log')

    def update(self, data):
        for key, value in six.iteritems(data):
            setattr(self, key, value)

        logger.debug('Settings updated: {0}'.format(data))


def get_config_directory():
    # If we're non-root, just use the users config dir (~/.config, etc)
    if geteuid() > 0:
        config_directory = click.get_app_dir('canaryd')

        return config_directory

    # If we're root on OSX
    if platform.system() == 'Darwin':
        return path.join('/', 'Library', 'Application Support', 'canaryd')

    # Elsewhere on *nix (no Windows support _yet_)
    return path.join('/', 'etc', 'canaryd')


def get_config_file():
    return path.join(get_config_directory(), 'canaryd.conf')


def get_settings(config_file=None):
    '''
    Load the config from the filesystem if provided, with defaults.
{'fraction_non_alphanumeric': 0.11049188272352799, 'fraction_numerical': 0.002665374363944754, 'mean_word_length': 3.0155642023346303, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '2441548', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1324, 'n_words': 378}