# Path: f8a_worker/defaults.py
# class F8AConfiguration(object):
#     def _make_postgres_string(password):
#     def is_local_deployment(cls):
#     def _rate_limit_exceeded(cls, headers):
#     def _decide_token_usage(cls):
#     def select_random_github_token(cls):
#     def libraries_io_project_url(cls, ecosystem, name):
#     def dependency_check_script_path(self):
#     BIGQUERY_JSON_KEY = environ.get('GITHUB_CONSUMER_KEY', 'not-set')
#     BROKER_CONNECTION = "amqp://guest@{host}:{port}".format(
#         host=environ.get('RABBITMQ_SERVICE_SERVICE_HOST', 'coreapi-broker'),
#         port=environ.get('RABBITMQ_SERVICE_SERVICE_PORT', '5672'))
#     GIT_USER_NAME = environ.get('GIT_USER_NAME', 'f8a')
#     GIT_USER_EMAIL = environ.get('GIT_USER_EMAIL', 'f8a@f8a')
#     GITHUB_TOKEN = environ.get('GITHUB_TOKEN', 'not-set').split(',')
#     GITHUB_API = "https://api.github.com/"
#     LIBRARIES_IO_TOKEN = environ.get('LIBRARIES_IO_TOKEN', 'not-set')
#     LIBRARIES_IO_API = 'https://libraries.io/api'
#     NPMJS_CHANGES_URL = environ.get('NPMJS_CHANGES_URL',
#                                     "https://skimdb.npmjs.com/registry/"
#                                     "_changes?descending=true&include_docs=true&feed=continuous")
#     UNQUOTED_POSTGRES_CONNECTION = _make_postgres_string(environ.get('POSTGRESQL_PASSWORD', ''))
#     POSTGRES_CONNECTION = _make_postgres_string(
#         quote(environ.get('POSTGRESQL_PASSWORD', ''), safe=''))
#     WORKER_DATA_DIR = environ.get('WORKER_DATA_DIR', 'not-set')
#     NPM_DATA_DIR = path.join(environ.get('HOME', '.npm'))
#     SCANCODE_LICENSE_SCORE = environ.get('SCANCODE_LICENSE_SCORE', '20')  # scancode's default is 0
#     SCANCODE_TIMEOUT = environ.get('SCANCODE_TIMEOUT', '120')  # scancode's default is 120
#     SCANCODE_PROCESSES = environ.get('SCANCODE_PROCESSES', '1')  # scancode's default is 1
#     SCANCODE_PATH = environ.get('SCANCODE_PATH', '/opt/scancode-toolkit/')
#     SCANCODE_IGNORE = ['*.pyc', '*.so', '*.dll', '*.rar', '*.jar',
#                        '*.zip', '*.tar', '*.tar.gz', '*.tar.xz', '*.png']  # don't scan binaries
#     AWS_S3_REGION = environ.get('AWS_S3_REGION')
#     AWS_S3_ACCESS_KEY_ID = environ.get('AWS_S3_ACCESS_KEY_ID')
#     AWS_S3_SECRET_ACCESS_KEY = environ.get('AWS_S3_SECRET_ACCESS_KEY')
#     S3_ENDPOINT_URL = environ.get('S3_ENDPOINT_URL')
#     DEPLOYMENT_PREFIX = environ.get('DEPLOYMENT_PREFIX')
#     BAYESIAN_SYNC_S3 = int(environ.get('BAYESIAN_SYNC_S3', 0)) == 1
#     AWS_SQS_ACCESS_KEY_ID = environ.get('AWS_SQS_ACCESS_KEY_ID')
#     AWS_SQS_SECRET_ACCESS_KEY = environ.get('AWS_SQS_SECRET_ACCESS_KEY')
#     CELERY_RESULT_BACKEND = environ.get('CELERY_RESULT_BACKEND')
#     AWS_SQS_REGION = environ.get('AWS_SQS_REGION')
#     JAVANCSS_PATH = environ.get('JAVANCSS_PATH')
#     OWASP_DEP_CHECK_PATH = environ.get('OWASP_DEP_CHECK_PATH')
#         USAGE_THRESHOLD = int(environ.get("LOW_USAGE_THRESHOLD", "5000"))
#         USAGE_THRESHOLD = 5000
#         POPULARITY_THRESHOLD = int(environ.get("LOW_POPULARITY_THRESHOLD", "5000"))
#         POPULARITY_THRESHOLD = 5000
#     BAYESIAN_GREMLIN_HTTP_SERVICE_HOST = environ.get("BAYESIAN_GREMLIN_HTTP_SERVICE_HOST",
#                                                      "localhost")
#     BAYESIAN_GREMLIN_HTTP_SERVICE_PORT = environ.get("BAYESIAN_GREMLIN_HTTP_SERVICE_PORT", "8182")


import os
import logging
from urllib.parse import quote
from celery.signals import setup_logging
from f8a_worker.defaults import configuration

class CelerySettings(object):
    """Setup Celery."""

    _DEFAULT_SQS_REGION = 'us-east-1'
    _DEFAULT_RESULT_BACKEND = 'db+' + configuration.POSTGRES_CONNECTION

    # Generic worker options
    timezone = 'UTC'
    task_acks_late = True
    result_backend = configuration.CELERY_RESULT_BACKEND or _DEFAULT_RESULT_BACKEND

    # do not retry on connection errors, rather let OpenShift kill the worker
    broker_connection_retry = False

    # Set up message broker
    if _use_sqs():
        broker_url = 'sqs://{aws_access_key_id}:{aws_secret_access_key}@'.format(
            aws_access_key_id=quote(configuration.AWS_SQS_ACCESS_KEY_ID, safe=''),
            aws_secret_access_key=quote(configuration.AWS_SQS_SECRET_ACCESS_KEY, safe=''),
        )
        broker_transport_options = {
            # number of seconds to wait for the worker to acknowledge the task before the message is
            # redelivered to another worker
            'visibility_timeout': 1800,
            # number of seconds for polling, the more frequent we poll, the more money we pay
            'polling_interval': 2,
            # 'queue_name_prefix': 'bayesian-',
            # see amazon_endpoints.js based on
            # http://docs.aws.amazon.com/general/latest/gr/rande.html#sqs_region

# Path: f8a_worker/defaults.py
# class F8AConfiguration(object):
#     def _make_postgres_string(password):
#     def is_local_deployment(cls):
#     def _rate_limit_exceeded(cls, headers):
#     def _decide_token_usage(cls):
#     def select_random_github_token(cls):
#     def libraries_io_project_url(cls, ecosystem, name):
#     def dependency_check_script_path(self):
#     BIGQUERY_JSON_KEY = environ.get('GITHUB_CONSUMER_KEY', 'not-set')
#     BROKER_CONNECTION = "amqp://guest@{host}:{port}".format(
#         host=environ.get('RABBITMQ_SERVICE_SERVICE_HOST', 'coreapi-broker'),
#         port=environ.get('RABBITMQ_SERVICE_SERVICE_PORT', '5672'))
#     GIT_USER_NAME = environ.get('GIT_USER_NAME', 'f8a')
#     GIT_USER_EMAIL = environ.get('GIT_USER_EMAIL', 'f8a@f8a')
#     GITHUB_TOKEN = environ.get('GITHUB_TOKEN', 'not-set').split(',')
#     GITHUB_API = "https://api.github.com/"
#     LIBRARIES_IO_TOKEN = environ.get('LIBRARIES_IO_TOKEN', 'not-set')
#     LIBRARIES_IO_API = 'https://libraries.io/api'
#     NPMJS_CHANGES_URL = environ.get('NPMJS_CHANGES_URL',
#                                     "https://skimdb.npmjs.com/registry/"
#                                     "_changes?descending=true&include_docs=true&feed=continuous")
#     UNQUOTED_POSTGRES_CONNECTION = _make_postgres_string(environ.get('POSTGRESQL_PASSWORD', ''))
#     POSTGRES_CONNECTION = _make_postgres_string(
#         quote(environ.get('POSTGRESQL_PASSWORD', ''), safe=''))
#     WORKER_DATA_DIR = environ.get('WORKER_DATA_DIR', 'not-set')
#     NPM_DATA_DIR = path.join(environ.get('HOME', '.npm'))
#     SCANCODE_LICENSE_SCORE = environ.get('SCANCODE_LICENSE_SCORE', '20')  # scancode's default is 0
#     SCANCODE_TIMEOUT = environ.get('SCANCODE_TIMEOUT', '120')  # scancode's default is 120
#     SCANCODE_PROCESSES = environ.get('SCANCODE_PROCESSES', '1')  # scancode's default is 1
#     SCANCODE_PATH = environ.get('SCANCODE_PATH', '/opt/scancode-toolkit/')
#     SCANCODE_IGNORE = ['*.pyc', '*.so', '*.dll', '*.rar', '*.jar',
#                        '*.zip', '*.tar', '*.tar.gz', '*.tar.xz', '*.png']  # don't scan binaries
#     AWS_S3_REGION = environ.get('AWS_S3_REGION')
#     AWS_S3_ACCESS_KEY_ID = environ.get('AWS_S3_ACCESS_KEY_ID')
#     AWS_S3_SECRET_ACCESS_KEY = environ.get('AWS_S3_SECRET_ACCESS_KEY')
#     S3_ENDPOINT_URL = environ.get('S3_ENDPOINT_URL')
#     DEPLOYMENT_PREFIX = environ.get('DEPLOYMENT_PREFIX')
#     BAYESIAN_SYNC_S3 = int(environ.get('BAYESIAN_SYNC_S3', 0)) == 1
#     AWS_SQS_ACCESS_KEY_ID = environ.get('AWS_SQS_ACCESS_KEY_ID')
#     AWS_SQS_SECRET_ACCESS_KEY = environ.get('AWS_SQS_SECRET_ACCESS_KEY')
#     CELERY_RESULT_BACKEND = environ.get('CELERY_RESULT_BACKEND')
#     AWS_SQS_REGION = environ.get('AWS_SQS_REGION')
#     JAVANCSS_PATH = environ.get('JAVANCSS_PATH')
#     OWASP_DEP_CHECK_PATH = environ.get('OWASP_DEP_CHECK_PATH')
#         USAGE_THRESHOLD = int(environ.get("LOW_USAGE_THRESHOLD", "5000"))
#         USAGE_THRESHOLD = 5000
#         POPULARITY_THRESHOLD = int(environ.get("LOW_POPULARITY_THRESHOLD", "5000"))
#         POPULARITY_THRESHOLD = 5000
#     BAYESIAN_GREMLIN_HTTP_SERVICE_HOST = environ.get("BAYESIAN_GREMLIN_HTTP_SERVICE_HOST",
#                                                      "localhost")
#     BAYESIAN_GREMLIN_HTTP_SERVICE_PORT = environ.get("BAYESIAN_GREMLIN_HTTP_SERVICE_PORT", "8182")


# Path: f8a_worker/celery_settings.py
import os
import logging
from urllib.parse import quote
from celery.signals import setup_logging
from f8a_worker.defaults import configuration


class CelerySettings(object):
    """Setup Celery."""

    _DEFAULT_SQS_REGION = 'us-east-1'
    _DEFAULT_RESULT_BACKEND = 'db+' + configuration.POSTGRES_CONNECTION

    # Generic worker options
    timezone = 'UTC'
    task_acks_late = True
    result_backend = configuration.CELERY_RESULT_BACKEND or _DEFAULT_RESULT_BACKEND

    # do not retry on connection errors, rather let OpenShift kill the worker
    broker_connection_retry = False

    # Set up message broker
    if _use_sqs():
        broker_url = 'sqs://{aws_access_key_id}:{aws_secret_access_key}@'.format(
            aws_access_key_id=quote(configuration.AWS_SQS_ACCESS_KEY_ID, safe=''),
            aws_secret_access_key=quote(configuration.AWS_SQS_SECRET_ACCESS_KEY, safe=''),
        )
        broker_transport_options = {
            # number of seconds to wait for the worker to acknowledge the task before the message is
            # redelivered to another worker
            'visibility_timeout': 1800,
            # number of seconds for polling, the more frequent we poll, the more money we pay
            'polling_interval': 2,
            # 'queue_name_prefix': 'bayesian-',
            # see amazon_endpoints.js based on
            # http://docs.aws.amazon.com/general/latest/gr/rande.html#sqs_region

            'region': configuration.AWS_SQS_REGION or _DEFAULT_SQS_REGION