<commit_before># Django settings for qanda project.

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Chicago'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique, and don't share it with anybody.
SECRET_KEY = '@k2qrg!q(gf=za^tqc=&0aak6&&=p@zz3df9krxq+s0(ji#tp$'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django_notify.middleware.NotificationsMiddleware',
)

ROOT_URLCONF = 'qanda.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'qanda.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

TEMPLATE_CONTEXT_PROCESSORS={
    'django_notify.context_processors.notifications',
}

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}
<commit_msg>add south<commit_after># Django settings for qanda project.

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Chicago'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique, and don't share it with anybody.
SECRET_KEY = '@k2qrg!q(gf=za^tqc=&0aak6&&=p@zz3df9krxq+s0(ji#tp$'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django_notify.middleware.NotificationsMiddleware',
)

ROOT_URLCONF = 'qanda.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'qanda.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'south',
)

TEMPLATE_CONTEXT_PROCESSORS={
    'django_notify.context_processors.notifications',
}

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}
<|endoftext|>


<commit_before>
from sigtools import *
from MLab import *

modedict = {'valid':0, 'same':1, 'full':2}

def convolveND(volume,kernel,mode='full'):
    """out = convolveND(in,kernel{,mode}) returns the true convolution of an N-D
    input (in) with the N-D kernel (with index reversal) and zero-padded edges.
    The size of the return depends on the third argument, mode:

    'valid' (0):  The output consists only of those elements that do not rely
                  on the zero-padding.
    'same'  (1):  The output is the same size as the input centered with respect
                  to the 'full' output.
    'full'  (2):  The output is the full discrete linear convolution with the
                  input.  (Default)
    """
    volume = asarray(volume)
    kernel = asarray(kernel)
    if (product(kernel.shape) > product(volume.shape)):
        temp = kernel
        kernel = volume
        volume = temp
        del temp

    slice_obj = [slice(None,None,-1)]*len(kernel.shape)
    try:
        val = modedict[mode]
    except KeyError:
        val = mode

    return correlateND(volume,kernel[slice_obj],val)

def medfiltND(volume,kernel_size=None):
    """out = medfiltND(input{,kernel_size}) returns a median filtered version of
    input where the median is taken over a window of size kernel_size whose
    elements should be odd (kernel_size defaults to 3 along each axis).
    """
    volume = asarray(volume)
    if kernel_size == None:
        kernel_size = [3] * len(volume.shape)
    kernel_size = asarray(kernel_size)

    domain = ones(kernel_size)

    numels = product(kernel_size)
    order = numels/2
    if not (numels % 2):    # Even number in window
        return (order_filterND(volume,domain,order-1) + order_filterND(volume,domain,order))
    else:                   # Odd number in window
        return order_filterND(volume,domain,order)

def wienerND(im,mysize=None,noise=None):
    """out = wienerND(im{,kernel_size,noise_power}) returns a wiener filtered
    version of im with optional kernel size (default is 3 along each axis)
    and noise power given.
    """
    im = asarray(im)
    if mysize == None:
        mysize = [3] * len(im.shape)
    mysize = asarray(mysize);

    # Estimate the local mean
    lMean = correlateND(im,ones(mysize),1) / prod(mysize)

    # Estimate the local variance
    lVar = correlateND(im**2,ones(mysize),1) / prod(mysize) - lMean**2

    # Estimate the noise power if needed.
    if noise==None:
        noise = mean(ravel(lVar))

    # Compute result
    # f = lMean + (maximum(0, lVar - noise) ./
    #               maximum(lVar, noise)) * (im - lMean)
    #
    out = im - lMean
    im = lVar - noise
    im = maximum(im,0)
    lVar = maximum(lVar,noise)
    out = out / lVar
    out = out * im
    out = out + lMean

    return out

def test():
    a = [3,4,5,6,5,4]
    b = [1,2,3]
    c = convolveND(a,b)
    if (product(equal(c,[3,10,22,28,32,32,23,12]))==0):
        print "Error in convolveND."

    f = [[3,4,5],[2,3,4],[1,2,5]]
    d = medfiltND(f)
    if (product(ravel(equal(d,[[0,3,0],[2,3,3],[0,2,0]])))==0):
        print "Error in medfiltND."

    g = array([[5,6,4,3],[3,5,6,2],[2,3,5,6],[1,6,9,7]],'d')
    correct = array([[2.16374269,3.2222222222, 2.8888888889, 1.6666666667],[2.666666667, 4.33333333333, 4.44444444444, 2.8888888888],[2.222222222, 4.4444444444, 5.4444444444, 4.801066874837],[1.33333333333, 3.92735042735, 6.0712560386, 5.0404040404]])
    h = wienerND(g)
    if (abs(product(ravel(h-correct)))> 1e-7):
        print "Error in wienerND."

    return

if __name__ == "__main__":
    test()

<commit_msg>Changed setup.py and slightly modified signaltools.py<commit_after>
from sigtools import *
import MLab

modedict = {'valid':0, 'same':1, 'full':2}

def convolveND(volume,kernel,mode='full'):
    """out = convolveND(in,kernel{,mode}) returns the true convolution of an N-D
    input (in) with the N-D kernel (with index reversal) and zero-padded edges.
    The size of the return depends on the third argument, mode:

    'valid' (0):  The output consists only of those elements that do not rely
                  on the zero-padding.
    'same'  (1):  The output is the same size as the input centered with respect
                  to the 'full' output.
    'full'  (2):  The output is the full discrete linear convolution with the
                  input.  (Default)
    """
    volume = MLab.asarray(volume)
    kernel = MLab.asarray(kernel)
    if (MLab.product(kernel.shape) > MLab.product(volume.shape)):
        temp = kernel
        kernel = volume
        volume = temp
        del temp

    slice_obj = [slice(None,None,-1)]*len(kernel.shape)
    try:
        val = modedict[mode]
    except KeyError:
        val = mode

    return correlateND(volume,kernel[slice_obj],val)

def medfiltND(volume,kernel_size=None):
    """out = medfiltND(input{,kernel_size}) returns a median filtered version of
    input where the median is taken over a window of size kernel_size whose
    elements should be odd (kernel_size defaults to 3 along each axis).
    """
    volume = MLab.asarray(volume)
    if kernel_size == None:
        kernel_size = [3] * len(volume.shape)
    kernel_size = MLab.asarray(kernel_size)

    domain = MLab.ones(kernel_size)

    numels = MLab.product(kernel_size)
    order = numels/2
    if not (numels % 2):    # Even number in window
        return (order_filterND(volume,domain,order-1) + order_filterND(volume,domain,order))
    else:                   # Odd number in window
        return order_filterND(volume,domain,order)

def wienerND(im,mysize=None,noise=None):
    """out = wienerND(im{,kernel_size,noise_power}) returns a wiener filtered
    version of im with optional kernel size (default is 3 along each axis)
    and noise power given.
    """
    im = asarray(im)
    if mysize == None:
        mysize = [3] * len(im.shape)
    mysize = MLab.asarray(mysize);

    # Estimate the local mean
    lMean = correlateND(im,ones(mysize),1) / MLab.prod(mysize)

    # Estimate the local variance
    lVar = correlateND(im**2,ones(mysize),1) / MLab.prod(mysize) - lMean**2

    # Estimate the noise power if needed.
    if noise==None:
        noise = MLab.mean(ravel(lVar))

    # Compute result
    # f = lMean + (maximum(0, lVar - noise) ./
    #               maximum(lVar, noise)) * (im - lMean)
    #
    out = im - lMean
    im = lVar - noise
    im = MLab.maximum(im,0)
    lVar = MLab.maximum(lVar,noise)
    out = out / lVar
    out = out * im
    out = out + lMean

    return out

def test():
    a = [3,4,5,6,5,4]
    b = [1,2,3]
    c = convolveND(a,b)
    if (MLab.product(equal(c,[3,10,22,28,32,32,23,12]))==0):
        print "Error in convolveND."

    f = [[3,4,5],[2,3,4],[1,2,5]]
    d = medfiltND(f)
    if (MLab.product(ravel(equal(d,[[0,3,0],[2,3,3],[0,2,0]])))==0):
        print "Error in medfiltND."

    g = MLab.array([[5,6,4,3],[3,5,6,2],[2,3,5,6],[1,6,9,7]],'d')
    correct = MLab.array([[2.16374269,3.2222222222, 2.8888888889, 1.6666666667],[2.666666667, 4.33333333333, 4.44444444444, 2.8888888888],[2.222222222, 4.4444444444, 5.4444444444, 4.801066874837],[1.33333333333, 3.92735042735, 6.0712560386, 5.0404040404]])
    h = wienerND(g)
    if (MLab.abs(MLab.product(MLab.ravel(h-correct)))> 1e-7):
        print "Error in wienerND."

    return

if __name__ == "__main__":
    test()

<|endoftext|>


<commit_before>"""
Django settings for backend project.

Generated by 'django-admin startproject' using Django 1.11.1.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ')cgk!kr7s)f^cw&0a6g^mpkfh^qy#-jolr!qk9h2=v8mgss_$l'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'backend.unimeet.gr']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'unimeet'
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'backend.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'backend.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

PASSWORD_HASHERS = [
    'django.contrib.auth.hashers.PBKDF2PasswordHasher',
    'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
    'django.contrib.auth.hashers.Argon2PasswordHasher',
    'django.contrib.auth.hashers.SHA1PasswordHasher',
    'django.contrib.auth.hashers.MD5PasswordHasher',
    'django.contrib.auth.hashers.UnsaltedSHA1PasswordHasher',
    'django.contrib.auth.hashers.UnsaltedMD5PasswordHasher',
    'django.contrib.auth.hashers.CryptPasswordHasher',
]

# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'

AUTH_USER_MODEL = 'unimeet.User'

SESSION_COOKIE_DOMAIN = '.unimeet.gr'

CORS_ORIGIN_REGEX_WHITELIST = (r'^((http|https)?://)?(\w+\.)?unimeet\.gr$', )
CORS_ALLOW_CREDENTIALS = True
<commit_msg>Make ALLOWED_HOSTS use services.json conf<commit_after>"""
Django settings for backend project.

Generated by 'django-admin startproject' using Django 1.11.1.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os
import json
from urlparse import urlparse

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ')cgk!kr7s)f^cw&0a6g^mpkfh^qy#-jolr!qk9h2=v8mgss_$l'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

with open(os.path.join(BASE_DIR, '..', 'config', 'services.json')) as json_services_file:
    json_services = json.load(json_services_file)

backend_service_url = json_services['backend']['url']
backend_service_hostname = urlparse(backend_service_url).hostname

ALLOWED_HOSTS = ['127.0.0.2', 'localhost', backend_service_hostname]

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'unimeet'
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'backend.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'backend.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

PASSWORD_HASHERS = [
    'django.contrib.auth.hashers.PBKDF2PasswordHasher',
    'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
    'django.contrib.auth.hashers.Argon2PasswordHasher',
    'django.contrib.auth.hashers.SHA1PasswordHasher',
    'django.contrib.auth.hashers.MD5PasswordHasher',
    'django.contrib.auth.hashers.UnsaltedSHA1PasswordHasher',
    'django.contrib.auth.hashers.UnsaltedMD5PasswordHasher',
    'django.contrib.auth.hashers.CryptPasswordHasher',
]

# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'

AUTH_USER_MODEL = 'unimeet.User'

SESSION_COOKIE_DOMAIN = '.unimeet.gr'

CORS_ORIGIN_REGEX_WHITELIST = (r'^((http|https)?://)?(\w+\.)?unimeet\.gr$', )
CORS_ALLOW_CREDENTIALS = True
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import re
from datetime import datetime
import pytz
from functools import wraps
from neomodel import db as transaction
from py2neo.error import GraphError
from py2neo.cypher.error.schema import ConstraintViolation
from neomodel.exception import RequiredProperty
from neomodel.exception import UniqueProperty
from restapi.resources.exceptions import RestApiException
from ...rest.definition import EndpointResource
from commons import htmlcodes as hcodes

from commons.logs import get_logger
logger = get_logger(__name__)

__author__ = "Mattia D'Antonio (m.dantonio@cineca.it)"

class GraphBaseOperations(EndpointResource):

    def initGraph(self):
        self.graph = self.global_get_service('neo4j')
        self._current_user = self.getLoggedUserInstance()

    def getSingleLinkedNode(self, relation):

        nodes = relation.all()
        if len(nodes) <= 0:
            return None
        return nodes[0]

    def getLoggedUserInstance(self):
        user = self.get_current_user()
        if user is None:
            return None
        try:
            return self.graph.User.nodes.get(email=user.email)
        except self.graph.User.DoesNotExist:
            return None

    def getNode(self, Model, identifier, field='accession'):

        try:

            if field == 'accession':
                return Model.nodes.get(accession=identifier)

            if field == 'id':
                return Model.nodes.get(id=identifier)

            if field == 'uuid':
                return Model.nodes.get(uuid=identifier)

            if field == 'taxon_id':
                return Model.nodes.get(taxon_id=identifier)

            return Model.nodes.get(accession=identifier)

        except Model.DoesNotExist:
            return None

    def countNodes(self, type):
        query = "MATCH (a: " + type + ") RETURN count(a) as count"

        records = self.graph.cypher(query)
        for record in records:
            if (record is None):
                return 0
            if (record.count is None):
                return 0

        return record.count

    def getCurrentDate(self):
        return datetime.now(pytz.utc)

    # HANDLE INPUT PARAMETERS

    @staticmethod
    def createUniqueIndex(*var):

        separator = "#_#"
        return separator.join(var)

    def readProperty(self, schema, values, checkRequired=True):

        properties = {}
        for field in schema:
            if 'islink' in field:
                continue

            k = field["key"]
            if k in values:
                properties[k] = values[k]

            # this field is missing but required!
            elif checkRequired and field["required"] == "true":
                raise myGraphError(
                    'Missing field: %s' % k,
                    status_code=hcodes.HTTP_BAD_REQUEST)

        return properties

    def updateProperties(self, instance, schema, properties):

        for field in schema:
            if 'islink' in field:
                continue
            key = field["key"]
            if key in properties:
                instance.__dict__[key] = properties[key]

    def parseAutocomplete(
            self, properties, key, id_key='value', split_char=None):
        value = properties.get(key, None)

        ids = []

        if value is None:
            return ids

        # Multiple autocomplete
        if type(value) is list:
            for v in value:
                if v is None:
                    return None
                if id_key in v:
                    ids.append(v[id_key])
                else:
                    ids.append(v)
            return ids

        # Single autocomplete
        if id_key in value:
            return [value[id_key]]

        # Command line input
        if split_char is None:
            return [value]

        return value.split(split_char)

class myGraphError(RestApiException):
    status_code = None

    def __init__(self, exception, status_code=hcodes.HTTP_BAD_NOTFOUND):
        super(myGraphError).__init__()
        self.status_code = status_code

def returnError(self, label, error, code=hcodes.HTTP_BAD_NOTFOUND):
    error = str(error)
    logger.error(error)
    return self.force_response(errors={label: error}, code=code)

def graph_transactions(func):
    @wraps(func)
    def wrapper(self, *args, **kwargs):
        try:

            logger.verbose("Neomodel transaction BEGIN")
            transaction.begin()

            out = func(self, *args, **kwargs)

            logger.verbose("Neomodel transaction COMMIT")
            transaction.commit()

            return out
        except Exception as e:
            logger.verbose("Neomodel transaction ROLLBACK")
            try:
                # Starting from neo4j 2.3.0 ClientErrors automatically
                # rollback transactions and raise a 404 error:
                # HTTP DELETE returned response 404
                # https://github.com/neo4j/neo4j/issues/5806

                transaction.rollback()
            except Exception as rollback_exp:
                logger.warning(
                    "Exception raised during rollback: %s" % rollback_exp)
            raise e

    return wrapper

def catch_graph_exceptions(func):
    @wraps(func)
    def wrapper(self, *args, **kwargs):

        try:
            return func(self, *args, **kwargs)
        except (myGraphError) as e:
            if e.status_code == hcodes.HTTP_BAD_FORBIDDEN:
                label = 'Forbidden'
            elif e.status_code == hcodes.HTTP_BAD_NOTFOUND:
                label = 'Not found'
            else:
                label = 'Bad request'
            return returnError(self, label, e, code=e.status_code)

        except (UniqueProperty) as e:

            prefix = "Node [0-9]+ already exists with label"
            regExpr = "%s (.+) and property (.+)" % prefix
            m = re.search(regExpr, str(e))
            if m:
                node = m.group(1)
                prop = m.group(2)
                parsedError = "A %s already exist with %s" % (node, prop)
            else:
                parsedError = e

            return returnError(
                self, 'Duplicated property',
                parsedError, code=hcodes.HTTP_BAD_CONFLICT)
        except ConstraintViolation as e:
            return returnError(self, 'DB', e)
        except (GraphError) as e:
            # Also returned for duplicated fields...
            # UniqueProperty not catched?
            return returnError(self, 'DB', e)
        except (RequiredProperty) as e:
            return returnError(self, 'DB', e)

    return wrapper
<commit_msg>Bug fix: do not import neomodel before the neo4j connection init<commit_after># -*- coding: utf-8 -*-

import re
from datetime import datetime
import pytz
from functools import wraps
from py2neo.error import GraphError
from py2neo.cypher.error.schema import ConstraintViolation
from restapi.resources.exceptions import RestApiException
from ...rest.definition import EndpointResource
from commons import htmlcodes as hcodes

from commons.logs import get_logger
logger = get_logger(__name__)

__author__ = "Mattia D'Antonio (m.dantonio@cineca.it)"

class GraphBaseOperations(EndpointResource):

    def initGraph(self):
        self.graph = self.global_get_service('neo4j')
        self._current_user = self.getLoggedUserInstance()

    def getSingleLinkedNode(self, relation):

        nodes = relation.all()
        if len(nodes) <= 0:
            return None
        return nodes[0]

    def getLoggedUserInstance(self):
        user = self.get_current_user()
        if user is None:
            return None
        try:
            return self.graph.User.nodes.get(email=user.email)
        except self.graph.User.DoesNotExist:
            return None

    def getNode(self, Model, identifier, field='accession'):

        try:

            if field == 'accession':
                return Model.nodes.get(accession=identifier)

            if field == 'id':
                return Model.nodes.get(id=identifier)

            if field == 'uuid':
                return Model.nodes.get(uuid=identifier)

            if field == 'taxon_id':
                return Model.nodes.get(taxon_id=identifier)

            return Model.nodes.get(accession=identifier)

        except Model.DoesNotExist:
            return None

    def countNodes(self, type):
        query = "MATCH (a: " + type + ") RETURN count(a) as count"

        records = self.graph.cypher(query)
        for record in records:
            if (record is None):
                return 0
            if (record.count is None):
                return 0

        return record.count

    def getCurrentDate(self):
        return datetime.now(pytz.utc)

    # HANDLE INPUT PARAMETERS

    @staticmethod
    def createUniqueIndex(*var):

        separator = "#_#"
        return separator.join(var)

    def readProperty(self, schema, values, checkRequired=True):

        properties = {}
        for field in schema:
            if 'islink' in field:
                continue

            k = field["key"]
            if k in values:
                properties[k] = values[k]

            # this field is missing but required!
            elif checkRequired and field["required"] == "true":
                raise myGraphError(
                    'Missing field: %s' % k,
                    status_code=hcodes.HTTP_BAD_REQUEST)

        return properties

    def updateProperties(self, instance, schema, properties):

        for field in schema:
            if 'islink' in field:
                continue
            key = field["key"]
            if key in properties:
                instance.__dict__[key] = properties[key]

    def parseAutocomplete(
            self, properties, key, id_key='value', split_char=None):
        value = properties.get(key, None)

        ids = []

        if value is None:
            return ids

        # Multiple autocomplete
        if type(value) is list:
            for v in value:
                if v is None:
                    return None
                if id_key in v:
                    ids.append(v[id_key])
                else:
                    ids.append(v)
            return ids

        # Single autocomplete
        if id_key in value:
            return [value[id_key]]

        # Command line input
        if split_char is None:
            return [value]

        return value.split(split_char)

class myGraphError(RestApiException):
    status_code = None

    def __init__(self, exception, status_code=hcodes.HTTP_BAD_NOTFOUND):
        super(myGraphError).__init__()
        self.status_code = status_code

def returnError(self, label, error, code=hcodes.HTTP_BAD_NOTFOUND):
    error = str(error)
    logger.error(error)
    return self.force_response(errors={label: error}, code=code)

def graph_transactions(func):
    @wraps(func)
    def wrapper(self, *args, **kwargs):
        try:
            from neomodel import db as transaction

            logger.verbose("Neomodel transaction BEGIN")
            transaction.begin()

            out = func(self, *args, **kwargs)

            logger.verbose("Neomodel transaction COMMIT")
            transaction.commit()

            return out
        except Exception as e:
            logger.verbose("Neomodel transaction ROLLBACK")
            try:
                # Starting from neo4j 2.3.0 ClientErrors automatically
                # rollback transactions and raise a 404 error:
                # HTTP DELETE returned response 404
                # https://github.com/neo4j/neo4j/issues/5806

                transaction.rollback()
            except Exception as rollback_exp:
                logger.warning(
                    "Exception raised during rollback: %s" % rollback_exp)
            raise e

    return wrapper

def catch_graph_exceptions(func):
    @wraps(func)
    def wrapper(self, *args, **kwargs):

        from neomodel.exception import RequiredProperty
        from neomodel.exception import UniqueProperty

        try:
            return func(self, *args, **kwargs)
        except (myGraphError) as e:
            if e.status_code == hcodes.HTTP_BAD_FORBIDDEN:
                label = 'Forbidden'
            elif e.status_code == hcodes.HTTP_BAD_NOTFOUND:
                label = 'Not found'
            else:
                label = 'Bad request'
            return returnError(self, label, e, code=e.status_code)

        except (UniqueProperty) as e:

            prefix = "Node [0-9]+ already exists with label"
            regExpr = "%s (.+) and property (.+)" % prefix
            m = re.search(regExpr, str(e))
            if m:
                node = m.group(1)
                prop = m.group(2)
                parsedError = "A %s already exist with %s" % (node, prop)
            else:
                parsedError = e

            return returnError(
                self, 'Duplicated property',
                parsedError, code=hcodes.HTTP_BAD_CONFLICT)
        except ConstraintViolation as e:
            return returnError(self, 'DB', e)
        except (GraphError) as e:
            # Also returned for duplicated fields...
            # UniqueProperty not catched?
            return returnError(self, 'DB', e)
        except (RequiredProperty) as e:
            return returnError(self, 'DB', e)

    return wrapper
<|endoftext|>


<commit_before>#
# Copyright (c) 2005-2007 rPath, Inc.
#
# All Rights Reserved
#

from mint import database

(RDT_STRING,
 RDT_BOOL,
 RDT_INT,
 RDT_ENUM,
 RDT_TROVE)= range(5)

class GenericDataTable(database.DatabaseTable):
    name = None

    def __init__(self, db):
        if not self.name:
            return NotImplementedError
        self.lowered = self.name[0].lower() + self.name[1:]
        self.front = self.lowered.replace('Data', '')
        self.fields = ['%sId' % self.front, 'name', 'value', 'dataType']
        return database.DatabaseTable.__init__(self, db)

    def setDataValue(self, id, name, value, dataType):
        # do any data conversions necessary to safely store value as a string
        if dataType == RDT_BOOL:
            value=str(int(value))
        elif dataType == RDT_INT:
            value=str(value)

        cu = self.db.cursor()

        # audited for SQL injection
        cu.execute("DELETE FROM %s WHERE %sId=? AND name=?" % (self.name, self.front), id, name)
        # audited for SQL injection
        cu.execute("INSERT INTO %s (%sId, name, value, dataType) VALUES(?, ?, ?, ?)" % (self.name, self.front),
                   (id, name, value, dataType))
        self.db.commit()
        return True

    def getDataValue(self, id, name):
        # this function returns a tuple: isPresent, value to avoid
        # passing None to indicate that no value is set, since we don't
        # allow our XMLRPC server to pass None values.
        cu = self.db.cursor()
        # audited for SQL injection
        cu.execute("SELECT value, dataType FROM %s WHERE %sId=? AND name=?" % (self.name, self.front), (id, name))
        res = cu.fetchall()
        if len(res) != 1:
            return False, 0
        value, dataType = res[0]
        if dataType == RDT_BOOL:
            value=bool(int(value))
        elif dataType == RDT_INT:
            value=int(value)
        return True, value

    def getDataDict(self, id):
        cu = self.db.cursor()
        # audited for SQL injection
        cu.execute("SELECT name, value, dataType FROM %s WHERE %sId=?" % (self.name, self.front), id)
        dataDict = {}
        for name, value, dataType in cu.fetchall():
            if dataType == RDT_BOOL:
                value=bool(int(value))
            elif dataType == RDT_INT:
                value=int(value)
            dataDict[name] = value
        return dataDict

    def removeDataValue(self, id, name):
        cu = self.db.cursor()

        cu.execute("DELETE FROM %s WHERE %sId=? AND name=?" % (self.name, self.front), id, name)
        self.db.commit()

        return True

class JobDataTable(GenericDataTable):
    name = "JobData"

class UserDataTable(GenericDataTable):
    name = "UserData"

# XXX This table is deprecated in favor of BuildDataTable
class ReleaseDataTable(GenericDataTable):
    name = "ReleaseData"

class BuildDataTable(GenericDataTable):
    name = "BuildData"

<commit_msg>Add hook to not commit on data table updates (RBL-3030)<commit_after>#
# Copyright (c) 2005-2007 rPath, Inc.
#
# All Rights Reserved
#

from mint import database

(RDT_STRING,
 RDT_BOOL,
 RDT_INT,
 RDT_ENUM,
 RDT_TROVE)= range(5)

class GenericDataTable(database.DatabaseTable):
    name = None

    def __init__(self, db):
        if not self.name:
            return NotImplementedError
        self.lowered = self.name[0].lower() + self.name[1:]
        self.front = self.lowered.replace('Data', '')
        self.fields = ['%sId' % self.front, 'name', 'value', 'dataType']
        return database.DatabaseTable.__init__(self, db)

    def setDataValue(self, id, name, value, dataType, commit=True):
        # do any data conversions necessary to safely store value as a string
        if dataType == RDT_BOOL:
            value=str(int(value))
        elif dataType == RDT_INT:
            value=str(value)

        cu = self.db.cursor()

        # audited for SQL injection
        cu.execute("DELETE FROM %s WHERE %sId=? AND name=?" % (self.name, self.front), id, name)
        # audited for SQL injection
        cu.execute("INSERT INTO %s (%sId, name, value, dataType) VALUES(?, ?, ?, ?)" % (self.name, self.front),
                   (id, name, value, dataType))
        if commit:
            self.db.commit()
        return True

    def getDataValue(self, id, name):
        # this function returns a tuple: isPresent, value to avoid
        # passing None to indicate that no value is set, since we don't
        # allow our XMLRPC server to pass None values.
        cu = self.db.cursor()
        # audited for SQL injection
        cu.execute("SELECT value, dataType FROM %s WHERE %sId=? AND name=?" % (self.name, self.front), (id, name))
        res = cu.fetchall()
        if len(res) != 1:
            return False, 0
        value, dataType = res[0]
        if dataType == RDT_BOOL:
            value=bool(int(value))
        elif dataType == RDT_INT:
            value=int(value)
        return True, value

    def getDataDict(self, id):
        cu = self.db.cursor()
        # audited for SQL injection
        cu.execute("SELECT name, value, dataType FROM %s WHERE %sId=?" % (self.name, self.front), id)
        dataDict = {}
        for name, value, dataType in cu.fetchall():
            if dataType == RDT_BOOL:
                value=bool(int(value))
            elif dataType == RDT_INT:
                value=int(value)
            dataDict[name] = value
        return dataDict

    def removeDataValue(self, id, name, commit=True):
        cu = self.db.cursor()

        cu.execute("DELETE FROM %s WHERE %sId=? AND name=?" % (self.name, self.front), id, name)
        if commit:
            self.db.commit()

        return True

class JobDataTable(GenericDataTable):
    name = "JobData"

class UserDataTable(GenericDataTable):
    name = "UserData"

# XXX This table is deprecated in favor of BuildDataTable
class ReleaseDataTable(GenericDataTable):
    name = "ReleaseData"

class BuildDataTable(GenericDataTable):
    name = "BuildData"

<|endoftext|>


<commit_before>from mod_base import *
import re, string, random

# Try to respond to verbal queries without (too) fancy logic.
# This is mainly just an experiment for fun,
# and for learning list comprehensions and regex.

class Context:
    def __init__(self, sentence):
        self.sentence = sentence
        self.words = self.GetWords(sentence)
        self.reply = ""
        self.context_values = {}

    def AssignContext(self, name, value):
        self.context_values[name] = value

    def ReplaceWords(self, replacements):
        i = 0
        new = []
        for word in self.words:
            if word.lower() in replacements.keys():
                repl = replacements[word.lower()]
                parts = repl.split(" ")
                for part in parts:
                    new.append(part)
            else:
                new.append(word)
            i += 1
        self.words = new

    def GetWords(self, data):
        words = re.findall('[\#\w\-]*', data)
        words = map(lambda s:s.strip(), words)
        words = [word for word in words if word != ""]
        return words

    def SetReply(self, reply):
        self.reply = reply

    def ChooseReply(self, replys):
        self.reply = random.choice(replys)
        print self.reply

    def ReplaceReply(self):
        for key in self.context_values.keys():
            self.reply = self.reply.replace(key, self.context_values[key])

    def GetReply(self):
        return self.reply

class UnknownCMD(Listener):
    """Intercept unknown commands and try to respond. Experimental!"""
    def init(self):
        self.events = [IRC_EVT_UNKNOWN_CMD]
        self.happy_smileys = [":-)", ":)", ":D", ":>", "=)", "xD", ":P", "=P", "=]", ":]", "=}", ":}", "(:", ":3"]
        self.sad_smileys = [
            ("you listening", "yep, all the time [happy_smiley]"),":(", ";(", "=(", ":[", ";[", "=[", ":{", "={", ":/", ":\\", ";_;"]

        self.replacements = {
            # Questions
            "whos": "who is",
            "whats": "what is",
            "wat":"what",
            "wut":"what",
            "wuts": "what is",
            "whens": "when is",
            "whr": "where",
            "wheres": "where is",
            "whys": "why is",
            "why's": "why is",
            "hows": "how is",
            "abt": "about",

            "gimme": "give me",

            # Abbreviations
            "r": "are",
            "u": "you",
            "yo": "you",
            "ur": "your",
            "youre": "you are",
            "doin": "doing",
            "kool'": "cool",
            "livin": "living",
            "thnx": "thanks",
            "thx": "thanks",
            "plz": "please",
            "m/f": "male or female",
            "f/m": "male or female",
            "m / f": "male or female",
            "f / m": "male or female",
            "wbu": "what about you",
            "frm": "from",
            "m or f": "male of female",
            "f or m": "male of female",

            "lvl": "level",

            "hii": "hi",
            "heyy": "hey",
            "heya": "hey",

            "wassup": "what is up",
            "/w": "with",

            "im": "i am",
            "som": "some",
            "drinkin": "drinking",
            "wanna": "want to",
            "coz": "because",
        }

        self.responses = [
            ("what your name", "My name is [my_nick]"),
            ("what are you", "I'm an IRC bot. What about you, mortal?"),
            ("what you do", "say help or cmds to know what I do"),
            ("what commands", "you can use the following commands: [command_names]"),
            ("what time", "the time is [current_time]"),
            ("who are you", [
                "I'm [my_nick]",
                "My name is [my_nick]",
            ]),
            ("who you trust", "I trust [trusted_users]"),
            ("who is [my_nick]", "hey thats me [happy_smiley]"),
            ("who is [known_nick]", "[known_nick] is my friend"),
            ("who is", "I don't know"),
            ("been to [unknown_channel]", "No i haven't"),
            ("been to [known_channel]", "Sure, [known_channel] is one of my favourite places"),
            ("what about [known_channel]", "I like it [happy_smiley]"),
            ("why are you", "I like to be"),
            ("are you friend", "yeah, ofcourse I am"),
            ("are you at [known_channel]", "yeah, I am"),
            ("are you bot", "yep, call me [my_nick]"),
            ("are you on [known_channel]", "Yeah, I'm on [known_channel]"),
            ("are you on [unknown_channel]", "No, I'm not there"),
            ("you listening", "yep, all the time [happy_smiley]"),
            ("where you live", "I live at [source_link]"),
            ("how old you", "I'm eternal and immortal, but i've been here since [time_connected]"),
            ("how many channels on", "I'm on [channel_count] channels"),
            ("where you", "I'm at [channel_names]"),
            ("where we", "we're at [current_win]"),
            ("give me", "sorry, I can't"),
            ("channels you on", "I'm hanging out on [channel_names]"),
            ("many users", "I know [user_count] users"),
            ("many channels", "I know [channel_count] channels"),
            ("am i vip", "[user_authed]"),
            ("like me", ["ofcourse.", "sure [happy_smiley]", "yeah [user_nick]"]),
            ("my level", "your level is [user_level]"),
            ("trust me", "[trust]"),
            ("do trust [authed_nick]", "ofcourse I trust [authed_nick]"),
            ("thanks", "no problem [user_nick] [happy_smiley]"),
            ("thank you", "your welcome [happy_smiley]"),
            ("sorry", "oh thats ok [user_nick]"),
            ("what up", "just stalking..."),
            ("nope", "not nope!"),
            ("use you", "say help and I'll tell you"),
            ("hate you", "[user_nick] makes me sad [sad_smiley]"),
            ("you stupid", "sry im not so smart [sad_smiley]"),
            ("beer", [
                        "yeah, I like beer",
                        "drink it!",
                        "if drinking beer, remember the Ballmer peak!",
                        "beer every day [happy_smiley]",
            ]),
            ("hello", "hi [user_nick], how are you?"),
            ("hi", "greetings [user_nick] [happy_smiley] can I help you?"),
            ("you", "me? dunno."),
            ("me", "i don't know about you"),
            ("nom", "so munchy, so nom [happy_smiley]"),
            ("doge", "yeah me so doge"),
            ("if", "maybe!"),
            ("here", "or maybe there? or somewhere else..."),
            ("what", "no idea"),
            ("party", "party so hard!"),
            ("dafuq", "dafuq you back"),
        ]

        # Substitutions for replys
        self.reply_subst = {
            "user_count": lambda event: len(self.bot.users),
            "channel_count": lambda event: len([win for win in self.bot.windows if win.zone == IRC_ZONE_CHANNEL]),
            "time_connected": lambda event: time_stamp_numeric(self.bot.connection_time),
            "channel_names": lambda event: ", ".join([win.name for win in self.bot.windows if win.zone == IRC_ZONE_CHANNEL]),
            "command_names": lambda event: ", ".join(self.bot.GetCommandsByPermission(event.user.GetPermission())),
            "current_win": lambda event: event.win.GetName(),
            "my_nick": lambda event: self.bot.me.GetNick(),
            "user_nick": lambda event: event.user.GetNick(),
            "user_level": lambda event: event.user.GetPermission(),
            "user_authed": lambda event: "yes" if event.user.IsAuthed() else "no",
            "trust": lambda event: "yes sir!" if event.user.IsAuthed() else "nah, you better auth if you want trust",
            "current_time": lambda event: time_stamp_short(),
            "source_link": "https://github.com/richrd/bx",
            "trusted_users": lambda event: ", ".join([user.nick for user in self.bot.users if user.IsAuthed() and user.IsOnline()] or "nobody"),
            "happy_smiley": lambda event: random.choice(self.happy_smileys),
            "sad_smiley": lambda event: random.choice(self.sad_smileys),
        }

        # Substitution tests for inputs
        self.input_subst = {
            "known_nick": lambda word: word in [user.GetNick() for user in self.bot.users],
            "authed_nick": lambda word: word in [user.GetNick() for user in self.bot.users if user.IsAuthed()],
            "known_channel": lambda word: word in [win.name for win in self.bot.windows if win.zone == IRC_ZONE_CHANNEL],
            "unknown_channel": lambda word: self.bot.IsChannelName(word) and (not word in [win.GetName() for win in self.bot.windows if win.zone == IRC_ZONE_CHANNEL]),
            "my_nick": lambda word: word == self.bot.me.GetNick(),
        }

    def event(self, event):
        data = event.cmd
        if event.cmd_args:
            data += " " + event.cmd_args
        sentences = re.findall('[\w\d][\w\d\s]*[^\.\!\?]*[\.\!\?]*', data)
        matched = False
        if sentences:
            for sentence in sentences:
                context = Context(sentence)
                match = self.Match(context)
                if match:
                    matched = True
                    context.ReplaceReply()
                    reply = context.GetReply()
                    reply = self.SubstituteReply(reply, event)
                    event.win.Send(reply)
        return matched

    def Match(self, context):
        context.ReplaceWords(self.replacements)
        for data_in, data_out in self.responses:
            words_in = data_in.split(" ")
            if self.WordsInSequence(context, words_in):
                if type(data_out) == type([]):
                    context.ChooseReply(data_out)
                else:
                    context.SetReply(data_out)
                return True
        return False

    def WordsInSequence(self, context, sequence):
        i = 0
        for word in context.words:
            if i == len(sequence):
                break
            word = word.strip()

            if word == sequence[i]:
                i += 1
            else:
                found_var = self.FindInputSubst(sequence[i], word)
                if found_var:
                    context.AssignContext(sequence[i], word)
                    i += 1

        if i == len(sequence):
            return True
        return False

    def FindInputSubst(self, key, word):
        for subst in self.input_subst.keys():
            s = "["+subst+"]"
            if s == key:
                return self.input_subst[subst](word)
                return True
        return False

    def SubstituteReply(self, reply, event):
        for key in self.reply_subst.keys():
            subst = self.reply_subst[key]
            if type(subst) != type(""):
                subst = str(subst(event))
            reply = reply.replace("["+key+"]", subst)
        return reply

module = {
    "class": UnknownCMD,
    "type": MOD_LISTENER,
}
<commit_msg>unknowncmd.py: added responding to messages containing only the bots nick<commit_after>from mod_base import *
import re, string, random

# Try to respond to verbal queries without (too) fancy logic.
# This is mainly just an experiment for fun,
# and for learning list comprehensions and regex.

class Context:
    def __init__(self, sentence):
        self.sentence = sentence
        self.words = self.GetWords(sentence)
        self.reply = ""
        self.context_values = {}

    def AssignContext(self, name, value):
        self.context_values[name] = value

    def ReplaceWords(self, replacements):
        i = 0
        new = []
        for word in self.words:
            if word.lower() in replacements.keys():
                repl = replacements[word.lower()]
                parts = repl.split(" ")
                for part in parts:
                    new.append(part)
            else:
                new.append(word)
            i += 1
        self.words = new or [""]

    def GetWords(self, data):
        words = re.findall('[\#\w\-]*', data)
        words = map(lambda s:s.strip(), words)
        words = [word for word in words if word != ""]
        return words

    def SetReply(self, reply):
        self.reply = reply

    def ChooseReply(self, replys):
        self.reply = random.choice(replys)

    def ReplaceReply(self):
        for key in self.context_values.keys():
            self.reply = self.reply.replace(key, self.context_values[key])

    def GetReply(self):
        return self.reply

class UnknownCMD(Listener):
    """Intercept unknown commands and try to respond. Experimental!"""
    def init(self):
        self.events = [IRC_EVT_UNKNOWN_CMD]
        self.happy_smileys = [":-)", ":)", ":D", ":>", "=)", "xD", ":P", "=P", "=]", ":]", "=}", ":}", "(:", ":3"]
        self.sad_smileys = [
            ("you listening", "yep, all the time [happy_smiley]"),":(", ";(", "=(", ":[", ";[", "=[", ":{", "={", ":/", ":\\", ";_;"]

        self.replacements = {
            # Questions
            "whos": "who is",
            "whats": "what is",
            "wat":"what",
            "wut":"what",
            "wuts": "what is",
            "whens": "when is",
            "whr": "where",
            "wheres": "where is",
            "whys": "why is",
            "why's": "why is",
            "hows": "how is",
            "abt": "about",

            "gimme": "give me",

            # Abbreviations
            "r": "are",
            "u": "you",
            "yo": "you",
            "ur": "your",
            "youre": "you are",
            "doin": "doing",
            "kool'": "cool",
            "livin": "living",
            "thnx": "thanks",
            "thx": "thanks",
            "plz": "please",
            "m/f": "male or female",
            "f/m": "male or female",
            "m / f": "male or female",
            "f / m": "male or female",
            "wbu": "what about you",
            "frm": "from",
            "m or f": "male of female",
            "f or m": "male of female",

            "lvl": "level",

            "hii": "hi",
            "heyy": "hey",
            "heya": "hey",

            "wassup": "what is up",
            "/w": "with",

            "im": "i am",
            "som": "some",
            "drinkin": "drinking",
            "wanna": "want to",
            "coz": "because",
        }

        self.responses = [
            ("what your name", "My name is [my_nick]"),
            ("what are you", "I'm an IRC bot. What about you, mortal?"),
            ("what you do", "say help or cmds to know what I do"),
            ("what commands", "you can use the following commands: [command_names]"),
            ("what time", "the time is [current_time]"),
            ("who are you", [
                "I'm [my_nick]",
                "My name is [my_nick]",
            ]),
            ("who you trust", "I trust [trusted_users]"),
            ("who is [my_nick]", "hey thats me [happy_smiley]"),
            ("who is [known_nick]", "[known_nick] is my friend"),
            ("who is", "I don't know"),
            ("been to [unknown_channel]", "No i haven't"),
            ("been to [known_channel]", "Sure, [known_channel] is one of my favourite places"),
            ("what about [known_channel]", "I like it [happy_smiley]"),
            ("why are you", "I like to be"),
            ("are you friend", "yeah, ofcourse I am"),
            ("are you at [known_channel]", "yeah, I am"),
            ("are you bot", "yep, call me [my_nick]"),
            ("are you on [known_channel]", "Yeah, I'm on [known_channel]"),
            ("are you on [unknown_channel]", "No, I'm not there"),
            ("you listening", "yep, all the time [happy_smiley]"),
            ("where you live", "I live at [source_link]"),
            ("how old you", "I'm eternal and immortal, but i've been here since [time_connected]"),
            ("how many channels on", "I'm on [channel_count] channels"),
            ("where you", "I'm at [channel_names]"),
            ("where we", "we're at [current_win]"),
            ("give me", "sorry, I can't"),
            ("channels you on", "I'm hanging out on [channel_names]"),
            ("many users", "I know [user_count] users"),
            ("many channels", "I know [channel_count] channels"),
            ("am i vip", "[user_authed]"),
            ("like me", ["ofcourse.", "sure [happy_smiley]", "yeah [user_nick]"]),
            ("my level", "your level is [user_level]"),
            ("trust me", "[trust]"),
            ("do trust [authed_nick]", "ofcourse I trust [authed_nick]"),
            ("thanks", "no problem [user_nick] [happy_smiley]"),
            ("thank you", "your welcome [happy_smiley]"),
            ("sorry", "oh thats ok [user_nick]"),
            ("what up", "just stalking..."),
            ("nope", "not nope!"),
            ("use you", "say help and I'll tell you"),
            ("hate you", "[user_nick] makes me sad [sad_smiley]"),
            ("you stupid", "sry im not so smart [sad_smiley]"),
            ("beer", [
                        "yeah, I like beer",
                        "drink it!",
                        "if drinking beer, remember the Ballmer peak!",
                        "beer every day [happy_smiley]",
            ]),
            ("hello", "hi [user_nick], how are you?"),
            ("hi", "greetings [user_nick] [happy_smiley] can I help you?"),
            ("you", "me? dunno."),
            ("me", "i don't know about you"),
            ("nom", "so munchy, so nom [happy_smiley]"),
            ("doge", "yeah me so doge"),
            ("if", "maybe!"),
            ("here", "or maybe there? or somewhere else..."),
            ("what", "no idea"),
            ("party", "party so hard!"),
            ("dafuq", "dafuq you back"),
            ("", [
                "What [user_nick]?",
                "Yeah?",
                "Hi [user_nick]",
                ]),
        ]

        # Substitutions for replys
        self.reply_subst = {
            "user_count": lambda event: len(self.bot.users),
            "channel_count": lambda event: len([win for win in self.bot.windows if win.zone == IRC_ZONE_CHANNEL]),
            "time_connected": lambda event: time_stamp_numeric(self.bot.connection_time),
            "channel_names": lambda event: ", ".join([win.name for win in self.bot.windows if win.zone == IRC_ZONE_CHANNEL]),
            "command_names": lambda event: ", ".join(self.bot.GetCommandsByPermission(event.user.GetPermission())),
            "current_win": lambda event: event.win.GetName(),
            "my_nick": lambda event: self.bot.me.GetNick(),
            "user_nick": lambda event: event.user.GetNick(),
            "user_level": lambda event: event.user.GetPermission(),
            "user_authed": lambda event: "yes" if event.user.IsAuthed() else "no",
            "trust": lambda event: "yes sir!" if event.user.IsAuthed() else "nah, you better auth if you want trust",
            "current_time": lambda event: time_stamp_short(),
            "source_link": "https://github.com/richrd/bx",
            "trusted_users": lambda event: ", ".join([user.nick for user in self.bot.users if user.IsAuthed() and user.IsOnline()] or "nobody"),
            "happy_smiley": lambda event: random.choice(self.happy_smileys),
            "sad_smiley": lambda event: random.choice(self.sad_smileys),
        }

        # Substitution tests for inputs
        self.input_subst = {
            "known_nick": lambda word: word in [user.GetNick() for user in self.bot.users],
            "authed_nick": lambda word: word in [user.GetNick() for user in self.bot.users if user.IsAuthed()],
            "known_channel": lambda word: word in [win.name for win in self.bot.windows if win.zone == IRC_ZONE_CHANNEL],
            "unknown_channel": lambda word: self.bot.IsChannelName(word) and (not word in [win.GetName() for win in self.bot.windows if win.zone == IRC_ZONE_CHANNEL]),
            "my_nick": lambda word: word == self.bot.me.GetNick(),
        }

    def event(self, event):
        data = event.cmd
        if event.cmd_args:
            data += " " + event.cmd_args
        sentences = re.findall('[\w\d][\w\d\s]*[^\.\!\?]*[\.\!\?]*', data) or [""]
        matched = False
        for sentence in sentences:
            context = Context(sentence)
            match = self.Match(context)
            if match:
                matched = True
                context.ReplaceReply()
                reply = context.GetReply()
                reply = self.SubstituteReply(reply, event)
                event.win.Send(reply)
        return matched

    def Match(self, context):
        context.ReplaceWords(self.replacements)
        for data_in, data_out in self.responses:
            words_in = data_in.split(" ")
            if self.WordsInSequence(context, words_in):
                if type(data_out) == type([]):
                    context.ChooseReply(data_out)
                else:
                    context.SetReply(data_out)
                return True
        return False

    def WordsInSequence(self, context, sequence):
        i = 0
        for word in context.words:
            if i == len(sequence):
                break
            word = word.strip()

            if word == sequence[i]:
                i += 1
            else:
                found_var = self.FindInputSubst(sequence[i], word)
                if found_var:
                    context.AssignContext(sequence[i], word)
                    i += 1

        if i == len(sequence):
            return True
        return False

    def FindInputSubst(self, key, word):
        for subst in self.input_subst.keys():
            s = "["+subst+"]"
            if s == key:
                return self.input_subst[subst](word)
                return True
        return False

    def SubstituteReply(self, reply, event):
        for key in self.reply_subst.keys():
            subst = self.reply_subst[key]
            if type(subst) != type(""):
                subst = str(subst(event))
            reply = reply.replace("["+key+"]", subst)
        return reply

module = {
    "class": UnknownCMD,
    "type": MOD_LISTENER,
}
<|endoftext|>


<commit_before>#!/usr/bin/python2.4

import httplib, urllib, sys, os

mergedsource = []

for dirpath, dirnames, filenames in os.walk('.'):
    for filename in filenames:
        if filename.endswith('.js'):
            path = os.path.join(dirpath, filename)
            source = open(path).read()
            mergedsource.append(source)

mergedsourcestring = ''.join(mergedsource)

params = urllib.urlencode([
    ('js_code', mergedsourcestring),
    ('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
    ('output_format', 'text'),
    ('output_info', 'compiled_code')
  ])

headers = { "Content-type": "application/x-www-form-urlencoded" }
conn = httplib.HTTPConnection('closure-compiler.appspot.com')
conn.request('POST', '/compile', params, headers)
response = conn.getresponse()
data = response.read()
conn.close

open('gecui-all.js', 'w').write(data)
<commit_msg>Changed to non specific version of python<commit_after>#!/usr/bin/python

import httplib, urllib, sys, os

mergedsource = []

for dirpath, dirnames, filenames in os.walk('.'):
    for filename in filenames:
        if filename.endswith('.js'):
            path = os.path.join(dirpath, filename)
            source = open(path).read()
            mergedsource.append(source)

mergedsourcestring = ''.join(mergedsource)

params = urllib.urlencode([
    ('js_code', mergedsourcestring),
    ('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
    ('output_format', 'text'),
    ('output_info', 'compiled_code')
  ])

headers = { "Content-type": "application/x-www-form-urlencoded" }
conn = httplib.HTTPConnection('closure-compiler.appspot.com')
conn.request('POST', '/compile', params, headers)
response = conn.getresponse()
data = response.read()
conn.close

open('gecui-all.js', 'w').write(data)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# pylint: disable=W0212

import sys

from agate.data_types import Text

def print_structure(self, output=sys.stdout, max_rows=None):
    """
    Print this table's column names and types as a plain-text table.

    :param output:
        The output to print to.
    """
    from agate.table import Table

    name_column = [n for n in self._column_names]
    type_column = [t.__class__.__name__ for t in self._column_types]
    rows = zip(name_column, type_column)
    column_names = ['column', 'data_type']
    text = Text()
    column_types = [text, text]

    table = Table(rows, column_names, column_types)

    return table.print_table(output=output, max_column_width=None, max_rows=None)
<commit_msg>Update print_structure.py<commit_after>#!/usr/bin/env python
# pylint: disable=W0212

import sys

from agate.data_types import Text

def print_structure(self, output=sys.stdout, max_rows=None):
    """
    Print this table's column names and types as a plain-text table.

    :param output:
        The output to print to.
    """
    from agate.table import Table

    name_column = [n for n in self._column_names]
    type_column = [t.__class__.__name__ for t in self._column_types]
    rows = zip(name_column, type_column)
    column_names = ['column', 'data_type']
    text = Text()
    column_types = [text, text]

    table = Table(rows, column_names, column_types)

    return table.print_table(output=output, max_column_width=None, max_rows=max_rows)
<|endoftext|>


<commit_before>import yaml
import base64
import json

def GenerateFirewall(context):
    name = 'opscenterfirewall-' + context.env['name']
    firewalls = [
        {
            'name': name,
            'type': 'compute.v1.firewall',
            'properties': {
                'sourceRanges': [
                    '0.0.0.0/0'
                ],
                'allowed': [{
                    'IPProtocol': 'tcp',
                    'ports': ['8888', '8443']
                }]
            }
        }
    ]

    return firewalls

def GenerateReferencesList(context):
    reference_list = []
    n_of_copies = context.properties['nodesPerZone']
    dep_name = context.env['deployment']
    for zone in context.properties['zones']:
        for idx in range(1, n_of_copies + 1):
            node_name = '$(ref.' + dep_name + '-' + zone + '-' + str(idx) + '-vm' + '.selfLink)'
            reference_list.append(node_name)
    return ' '.join(reference_list)

def GenerateConfig(context):
    config = {'resources': []}

    zonal_clusters = {
        'name': 'clusters-' + context.env['name'],
        'type': 'regional_multi_vm.py',
        'properties': {
            'sourceImage': 'https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1504-vivid-v20160114a',
            'zones': context.properties['zones'],
            'machineType': context.properties['machineType'],
            'network': 'default',
            'numberOfVMReplicas': context.properties['nodesPerZone'],
            'disks': [
                {
                    'deviceName': 'vm-test-data-disk',
                    'type': 'PERSISTENT',
                    'boot': 'false',
                    'autoDelete': 'true',
                    'initializeParams': {
                        'diskType': 'pd-ssd',
                        'diskSizeGb': context.properties['diskSize']
                    }
                }
            ],
            'bootDiskType': 'pd-standard',
            'metadata': {
                'items': [
                    {
                        'key': 'startup-script',
                        'value': '''
                          #!/bin/bash
                          mkdir /mnt
                          /usr/share/google/safe_format_and_mount -m "mkfs.ext4 -F" /dev/disk/by-id/google-${HOSTNAME}-test-data-disk /mnt
                          chmod 777 /mnt
                          apt-get update
                          apt-get install openjdk-7-jdk -yqq
                          '''
                    }
                ]
            }
        }
    }

    ops_center_script = '''
    #! /bin/bash
    ssh-keygen -b 2048 -t rsa -f /tmp/sshkey -q -N ""
    echo -n 'root:' | cat - /tmp/sshkey.pub > temp && mv temp /tmp/sshkey.pub
    gcloud compute project-info add-metadata --metadata-from-file sshKeys=/tmp/sshkey.pub
    apt-get update
    apt-get install openjdk-7-jdk -yqq

    echo "Installing OpsCenter"
    echo "deb http://debian.datastax.com/community stable main" | tee -a /etc/apt/sources.list.d/datastax.community.list
    curl -L http://debian.datastax.com/debian/repo_key | apt-key add -
    apt-get update
    apt-get -y install opscenter=5.2.2

    echo "Starting OpsCenter"
    sudo service opscenterd start

    echo "Waiting for OpsCenter to start..."
    sleep 15

    echo "Waiting for Java to install on the nodes..."
    sleep 120

    wget https://raw.githubusercontent.com/DSPN/google-cloud-platform-dse/master/provision/opsCenter.py

    echo "Generating a provision.json file"
    python opsCenter.py '''

    # parameters go here
    ops_center_script += context.env['deployment'] + ' '
    ops_center_script += base64.b64encode(json.dumps(context.properties['zones'])) + ' '
    ops_center_script += str(context.properties['nodesPerZone']) + ' '
    ops_center_script += str(context.properties['nodeType'])

    ops_center_script += '''
    echo "Provisioning a new cluster using provision.json"
    curl --insecure -H "Accept: application/json" -X POST http://127.0.0.1:8888/provision -d @provision.json
    '''

    ops_center_node = {
        'name': 'opscenter-' + context.env['name'],
        'type': 'vm_instance.py',
        'properties': {
            'sourceImage': 'https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1504-vivid-v20160114a',
            'zone': context.properties['opsCenterZone'],
            'machineType': context.properties['machineType'],
            'network': 'default',
            'bootDiskType': 'pd-standard',
            'serviceAccounts': [{
                'email': 'default',
                'scopes': ['https://www.googleapis.com/auth/compute']
            }],
            'metadata': {
                'items': [
                    {
                        'key': 'startup-script',
                        'value': ops_center_script
                    },
                    {
                        'key': 'bogus-references',
                        'value': GenerateReferencesList(context)
                    }
                ]
            }
        }
    }

    config['resources'].append(zonal_clusters)
    config['resources'].append(ops_center_node)
    config['resources'].extend(GenerateFirewall(context))

    return yaml.dump(config)
<commit_msg>update vm<commit_after>import yaml
import base64
import json

def GenerateFirewall(context):
    name = 'opscenterfirewall-' + context.env['name']
    firewalls = [
        {
            'name': name,
            'type': 'compute.v1.firewall',
            'properties': {
                'sourceRanges': [
                    '0.0.0.0/0'
                ],
                'allowed': [{
                    'IPProtocol': 'tcp',
                    'ports': ['8888', '8443']
                }]
            }
        }
    ]

    return firewalls

def GenerateReferencesList(context):
    reference_list = []
    n_of_copies = context.properties['nodesPerZone']
    dep_name = context.env['deployment']
    for zone in context.properties['zones']:
        for idx in range(1, n_of_copies + 1):
            node_name = '$(ref.' + dep_name + '-' + zone + '-' + str(idx) + '-vm' + '.selfLink)'
            reference_list.append(node_name)
    return ' '.join(reference_list)

def GenerateConfig(context):
    config = {'resources': []}

    zonal_clusters = {
        'name': 'clusters-' + context.env['name'],
        'type': 'regional_multi_vm.py',
        'properties': {
            'sourceImage': 'https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1510-wily-v20160226',
            'zones': context.properties['zones'],
            'machineType': context.properties['machineType'],
            'network': 'default',
            'numberOfVMReplicas': context.properties['nodesPerZone'],
            'disks': [
                {
                    'deviceName': 'vm-test-data-disk',
                    'type': 'PERSISTENT',
                    'boot': 'false',
                    'autoDelete': 'true',
                    'initializeParams': {
                        'diskType': 'pd-ssd',
                        'diskSizeGb': context.properties['diskSize']
                    }
                }
            ],
            'bootDiskType': 'pd-standard',
            'metadata': {
                'items': [
                    {
                        'key': 'startup-script',
                        'value': '''
                          #!/bin/bash
                          mkdir /mnt
                          /usr/share/google/safe_format_and_mount -m "mkfs.ext4 -F" /dev/disk/by-id/google-${HOSTNAME}-test-data-disk /mnt
                          chmod 777 /mnt
                          apt-get update
                          apt-get install openjdk-7-jdk -yqq
                          '''
                    }
                ]
            }
        }
    }

    ops_center_script = '''
    #! /bin/bash
    ssh-keygen -b 2048 -t rsa -f /tmp/sshkey -q -N ""
    echo -n 'root:' | cat - /tmp/sshkey.pub > temp && mv temp /tmp/sshkey.pub
    gcloud compute project-info add-metadata --metadata-from-file sshKeys=/tmp/sshkey.pub
    apt-get update
    apt-get install openjdk-7-jdk -yqq

    echo "Installing OpsCenter"
    echo "deb http://debian.datastax.com/community stable main" | tee -a /etc/apt/sources.list.d/datastax.community.list
    curl -L http://debian.datastax.com/debian/repo_key | apt-key add -
    apt-get update
    apt-get -y install opscenter=5.2.2

    echo "Starting OpsCenter"
    sudo service opscenterd start

    echo "Waiting for OpsCenter to start..."
    sleep 15

    echo "Waiting for Java to install on the nodes..."
    sleep 120

    wget https://raw.githubusercontent.com/DSPN/google-cloud-platform-dse/master/provision/opsCenter.py

    echo "Generating a provision.json file"
    python opsCenter.py '''

    # parameters go here
    ops_center_script += context.env['deployment'] + ' '
    ops_center_script += base64.b64encode(json.dumps(context.properties['zones'])) + ' '
    ops_center_script += str(context.properties['nodesPerZone']) + ' '
    ops_center_script += str(context.properties['nodeType'])

    ops_center_script += '''
    echo "Provisioning a new cluster using provision.json"
    curl --insecure -H "Accept: application/json" -X POST http://127.0.0.1:8888/provision -d @provision.json
    '''

    ops_center_node = {
        'name': 'opscenter-' + context.env['name'],
        'type': 'vm_instance.py',
        'properties': {
            'sourceImage': 'https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1510-wily-v20160226',
            'zone': context.properties['opsCenterZone'],
            'machineType': context.properties['machineType'],
            'network': 'default',
            'bootDiskType': 'pd-standard',
            'serviceAccounts': [{
                'email': 'default',
                'scopes': ['https://www.googleapis.com/auth/compute']
            }],
            'metadata': {
                'items': [
                    {
                        'key': 'startup-script',
                        'value': ops_center_script
                    },
                    {
                        'key': 'bogus-references',
                        'value': GenerateReferencesList(context)
                    }
                ]
            }
        }
    }

    config['resources'].append(zonal_clusters)
    config['resources'].append(ops_center_node)
    config['resources'].extend(GenerateFirewall(context))

    return yaml.dump(config)
<|endoftext|>