<commit_before>import urllib
import urlparse
import re
import time

import requests

from requests.packages import urllib3

from flask import g, jsonify, redirect as flask_redirect, request

from oauthclientbridge import __version__, app

class Error(Exception):
    def __init__(self, error, description=None, uri=None, retry_after=None):
        self.error = error
        self.description = description
        self.uri = uri
        self.retry_after = retry_after

def error_handler(e):
    """Create a well formed JSON response with status and auth headers."""
    result = {'error': e.error}
    if e.description is not None:
        result['error_description'] = e.description
    if e.uri is not None:
        result['error_uri'] = e.uri

    response = jsonify(result)
    if e.error == 'invalid_client':
        response.status_code = 401
        response.www_authenticate.set_basic()
    elif e.retry_after:
        response.headers['Retry-After'] = int(e.retry_after + 1)
        response.status_code = 429
    else:
        response.status_code = 400

    return response

def fallback_error_handler(e):
    result = {'error': 'server_error', 'error_description': 'Unhandled error.'}
    return jsonify(result), 500

def nocache(response):
    """Turns off caching in case there is sensitive content in responses."""
    response.headers['Cache-Control'] = 'no-store'
    response.headers['Pragma'] = 'no-cache'
    return response

def fetch(uri, username, password, **data):
    """Perform post given URI with auth and provided data."""
    req = requests.Request('POST', uri, auth=(username, password), data=data)
    prepared = req.prepare()

    timeout = time.time() + app.config['OAUTH_FETCH_TIMEOUT']
    retry = 0

    error_description = 'An unknown error occurred talking to provider.'
    result = {'error': 'server_error', 'error_description': error_description}

    for i in range(app.config['OAUTH_FETCH_TOTAL_RETRIES']):
        prefix = 'attempt #%d %s' % (i + 1, uri)

        backoff = (2**i - 1) * app.config['OAUTH_FETCH_BACKOFF_FACTOR']
        remaining_timeout = timeout - time.time()

        if (retry or backoff) > remaining_timeout:
            app.logger.debug('Abort %s no timeout remaining.', prefix)
            break
        elif (retry or backoff) > 0:
            app.logger.debug('Retry %s [sleep %.3f]', prefix, retry or backoff)
            time.sleep(retry or backoff)

        result, status, retry = _fetch(prepared, remaining_timeout)

        if status is None:
            pass  # We didn't even get a response, so try again.
        elif status not in app.config['OAUTH_FETCH_RETRY_STATUS_CODES']:
            break
        elif 'error' not in result:
            break  # No error reported so might as well return it.

        app.logger.debug('Result %s [status %s] [retry after %s]',
                         prefix, status, retry)

    return result

def _fetch(prepared, timeout):
    try:
        resp = _session().send(prepared, timeout=timeout)
    except IOError as e:
        # Don't give API users error messages we don't control the contents of.
        if isinstance(e, requests.exceptions.ConnectionError):
            description = 'An error occurred while connecting to the provider.'
        elif isinstance(e, requests.exceptions.Timeout):
            description = 'Request timed out while talking to provider.'
        else:
            description = 'An unknown error occurred talking to provider.'

        app.logger.warning('Fetching %r failed: %s', prepared.url, e)

        # Server error isn't allowed everywhere, but fixing this has been
        # brought up in https://www.rfc-editor.org/errata_search.php?eid=4745
        result = {'error': 'server_error', 'error_description': description}
        status_code = None
        retry_after = 0
    else:
        result = _decode(resp)
        status_code = resp.status_code
        retry_after = _parse_retry(resp.headers.get('retry-after'))

    return result, status_code, retry_after

def _decode(resp):
    # Per OAuth spec all responses should be JSON, but this isn't allways
    # the case. For instance 502 errors and a gateway that does not correctly
    # create a fake JSON error response.

    try:
        return resp.json()
    except ValueError as e:
        app.logger.warning(
            'Fetching %r (HTTP %s, %s) failed: %s', resp.url, resp.status_code,
             resp.headers.get('Content-Type', 'text/plain'), e)

    description = 'Invalid JSON response from provider (%s)' % resp.status_code
    return {'error': 'server_error', 'error_description': description}

def _parse_retry(value):
    if not value:
        seconds = 0
    elif re.match(r'^\s*[0-9]+\s*$', value):
        seconds = int(value)
    else:
        date_tuple = email.utils.parsedate(value)
        if date_tuple is None:
            seconds = 0
        else:
            seconds = time.mktime(date_tuple) - time.time()
    return max(0, seconds)

def redirect(uri, **params):
    return flask_redirect(_rewrite_uri(uri, params))

def _session():
    if getattr(g, '_oauth_session', None) is None:
        g._oauth_session = requests.Session()
        g._oauth_session.headers['user-agent'] = (
            'oauthclientbridge %s' % __version__)
    return g._oauth_session

def _rewrite_query(original, params):
    parts = []
    query = urlparse.parse_qs(original, keep_blank_values=True)
    for key, value in params.items():
        query[key] = [value]  # Override with new params.
    for key, values in query.items():
        for value in values:  # Turn query into list of tuples.
            if isinstance(value, unicode):
                value = value.encode('utf-8')
            parts.append((key, value))
    return urllib.urlencode(parts)

def _rewrite_uri(uri, params):
    scheme, netloc, path, query, fragment = urlparse.urlsplit(uri)
    query = _rewrite_query(query, params)
    return urlparse.urlunsplit((scheme, netloc, path, query, fragment))
<commit_msg>Fix import ordering<commit_after>import re
import time
import urllib
import urlparse

import requests

from requests.packages import urllib3

from flask import g, jsonify, redirect as flask_redirect, request

from oauthclientbridge import __version__, app

class Error(Exception):
    def __init__(self, error, description=None, uri=None, retry_after=None):
        self.error = error
        self.description = description
        self.uri = uri
        self.retry_after = retry_after

def error_handler(e):
    """Create a well formed JSON response with status and auth headers."""
    result = {'error': e.error}
    if e.description is not None:
        result['error_description'] = e.description
    if e.uri is not None:
        result['error_uri'] = e.uri

    response = jsonify(result)
    if e.error == 'invalid_client':
        response.status_code = 401
        response.www_authenticate.set_basic()
    elif e.retry_after:
        response.headers['Retry-After'] = int(e.retry_after + 1)
        response.status_code = 429
    else:
        response.status_code = 400

    return response

def fallback_error_handler(e):
    result = {'error': 'server_error', 'error_description': 'Unhandled error.'}
    return jsonify(result), 500

def nocache(response):
    """Turns off caching in case there is sensitive content in responses."""
    response.headers['Cache-Control'] = 'no-store'
    response.headers['Pragma'] = 'no-cache'
    return response

def fetch(uri, username, password, **data):
    """Perform post given URI with auth and provided data."""
    req = requests.Request('POST', uri, auth=(username, password), data=data)
    prepared = req.prepare()

    timeout = time.time() + app.config['OAUTH_FETCH_TIMEOUT']
    retry = 0

    error_description = 'An unknown error occurred talking to provider.'
    result = {'error': 'server_error', 'error_description': error_description}

    for i in range(app.config['OAUTH_FETCH_TOTAL_RETRIES']):
        prefix = 'attempt #%d %s' % (i + 1, uri)

        backoff = (2**i - 1) * app.config['OAUTH_FETCH_BACKOFF_FACTOR']
        remaining_timeout = timeout - time.time()

        if (retry or backoff) > remaining_timeout:
            app.logger.debug('Abort %s no timeout remaining.', prefix)
            break
        elif (retry or backoff) > 0:
            app.logger.debug('Retry %s [sleep %.3f]', prefix, retry or backoff)
            time.sleep(retry or backoff)

        result, status, retry = _fetch(prepared, remaining_timeout)

        if status is None:
            pass  # We didn't even get a response, so try again.
        elif status not in app.config['OAUTH_FETCH_RETRY_STATUS_CODES']:
            break
        elif 'error' not in result:
            break  # No error reported so might as well return it.

        app.logger.debug('Result %s [status %s] [retry after %s]',
                         prefix, status, retry)

    return result

def _fetch(prepared, timeout):
    try:
        resp = _session().send(prepared, timeout=timeout)
    except IOError as e:
        # Don't give API users error messages we don't control the contents of.
        if isinstance(e, requests.exceptions.ConnectionError):
            description = 'An error occurred while connecting to the provider.'
        elif isinstance(e, requests.exceptions.Timeout):
            description = 'Request timed out while talking to provider.'
        else:
            description = 'An unknown error occurred talking to provider.'

        app.logger.warning('Fetching %r failed: %s', prepared.url, e)

        # Server error isn't allowed everywhere, but fixing this has been
        # brought up in https://www.rfc-editor.org/errata_search.php?eid=4745
        result = {'error': 'server_error', 'error_description': description}
        status_code = None
        retry_after = 0
    else:
        result = _decode(resp)
        status_code = resp.status_code
        retry_after = _parse_retry(resp.headers.get('retry-after'))

    return result, status_code, retry_after

def _decode(resp):
    # Per OAuth spec all responses should be JSON, but this isn't allways
    # the case. For instance 502 errors and a gateway that does not correctly
    # create a fake JSON error response.

    try:
        return resp.json()
    except ValueError as e:
        app.logger.warning(
            'Fetching %r (HTTP %s, %s) failed: %s', resp.url, resp.status_code,
             resp.headers.get('Content-Type', 'text/plain'), e)

    description = 'Invalid JSON response from provider (%s)' % resp.status_code
    return {'error': 'server_error', 'error_description': description}

def _parse_retry(value):
    if not value:
        seconds = 0
    elif re.match(r'^\s*[0-9]+\s*$', value):
        seconds = int(value)
    else:
        date_tuple = email.utils.parsedate(value)
        if date_tuple is None:
            seconds = 0
        else:
            seconds = time.mktime(date_tuple) - time.time()
    return max(0, seconds)

def redirect(uri, **params):
    return flask_redirect(_rewrite_uri(uri, params))

def _session():
    if getattr(g, '_oauth_session', None) is None:
        g._oauth_session = requests.Session()
        g._oauth_session.headers['user-agent'] = (
            'oauthclientbridge %s' % __version__)
    return g._oauth_session

def _rewrite_query(original, params):
    parts = []
    query = urlparse.parse_qs(original, keep_blank_values=True)
    for key, value in params.items():
        query[key] = [value]  # Override with new params.
    for key, values in query.items():
        for value in values:  # Turn query into list of tuples.
            if isinstance(value, unicode):
                value = value.encode('utf-8')
            parts.append((key, value))
    return urllib.urlencode(parts)

def _rewrite_uri(uri, params):
    scheme, netloc, path, query, fragment = urlparse.urlsplit(uri)
    query = _rewrite_query(query, params)
    return urlparse.urlunsplit((scheme, netloc, path, query, fragment))
<|endoftext|>


<commit_before>from docopt import docopt
import numpy as np
import pandas as pd
from patsy import dmatrices
from sklearn.ensemble import GradientBoostingClassifier as GBC
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.model_selection import cross_val_predict
from sklearn.preprocessing import scale

class Ontoclassifier:

    def __init__(self):
        self.n_fold = 10
        self.cores = -1
        self.verbosity = 2

    def train_logreg(self, data_file, outfile):
        hdf = pd.read_csv(data_file, delimiter='\t', header=0)

        y, X = dmatrices('label ~ freq_word1 + freq_word2 + freq_diff + similarity_pmi + similarity_svd + context_subsumption \
                          + context_entropy_word1 + context_entropy_word2 + entropy_diff + normalized_entropy_w1 + \
                          normalized_entropy_w2 + normalized_entropy_diff', hdf, return_type='dataframe')

        y = np.ravel(y)
        X = scale(X)

        clf = LogisticRegression(class_weight='balanced', solver='liblinear')
        predicted = cross_val_predict(clf, X, y, cv=self.n_fold, method='predict_proba', n_jobs=self.cores, verbose=self.verbosity)
        positive_pred = predicted[:, 1]
        print('CV Gini: ' + str(2 * metrics.roc_auc_score(y, positive_pred) - 1))
        i = 0
        ids = np.ravel(hdf.get('id'))
        with open(outfile, 'w') as of:
            while i < len(positive_pred):
                of.write(ids[i] + '\t' + str(positive_pred[i]) + '\n')
                i += 1

    def train_random_forest(self, data_file, outfile):
        hdf = pd.read_csv(data_file, delimiter='\t', header=0)

        y, X = dmatrices('label ~ freq_word1 + freq_word2 + freq_diff + similarity_pmi + similarity_svd + context_subsumption \
                          + context_entropy_word1 + context_entropy_word2 + entropy_diff + normalized_entropy_w1 + \
                          normalized_entropy_w2 + normalized_entropy_diff', hdf, return_type='dataframe')
        y = np.ravel(y)

        clf = RFC(n_estimators=300, verbose=2, random_state=12, class_weight='balanced')

        predicted = cross_val_predict(clf, X, y, cv=self.n_fold, verbose=self.verbosity, method='predict_proba', n_jobs=self.cores)
        positive_pred = predicted[:, 1]
        print('CV Gini: ' + str(2 * metrics.roc_auc_score(y, positive_pred) - 1))
        i = 0
        ids = np.ravel(hdf.get('id'))
        with open(outfile, 'w') as of:
            while i < len(positive_pred):
                of.write(ids[i] + '\t' + str(positive_pred[i]) + '\n')
                i += 1

    def train_boosted_trees(self, data_file, outfile):
        hdf = pd.read_csv(data_file, delimiter='\t', header=0)

        y, X = dmatrices('label ~ freq_word1 + freq_word2 + freq_diff + similarity_pmi + similarity_svd + context_subsumption \
                          + context_entropy_word1 + context_entropy_word2 + entropy_diff + normalized_entropy_w1 + \
                          normalized_entropy_w2 + normalized_entropy_diff', hdf, return_type='dataframe')
        y = np.ravel(y)

        clf = GBC(random_state=12, verbose=self.verbosity)

        predicted = cross_val_predict(clf, X, y, cv=self.n_fold, verbose=2, method='predict_proba', n_jobs=self.cores)
        positive_pred = predicted[:, 1]
        print('CV Gini: ' + str(2 * metrics.roc_auc_score(y, positive_pred) - 1))
        i = 0
        ids = np.ravel(hdf.get('id'))
        with open(outfile, 'w') as of:
            while i < len(positive_pred):
                of.write(ids[i] + '\t' + str(positive_pred[i]) + '\n')
                i += 1

def main():
    args = docopt("""
    Usage:
        ontoclassifier.py <data_file> <classifier> <output>
    """)
    oc = Ontoclassifier()
    if args['<classifier>'] == 'logistic_regression':
        oc.train_logreg(args['<data_file>'], args['<output>'])
    if args['<classifier>'] == 'random_forest':
        oc.train_random_forest(args['<data_file>'], args['<output>'])
    if args['<classifier>'] == 'boosted_trees':
        oc.train_boosted_trees(args['<data_file>'], args['<output>'])

if __name__ == '__main__':
    main()
<commit_msg>make some classifier variables class members<commit_after>from docopt import docopt
import numpy as np
import pandas as pd
from patsy import dmatrices
from sklearn.ensemble import GradientBoostingClassifier as GBC
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.model_selection import cross_val_predict
from sklearn.preprocessing import scale

class Ontoclassifier:

    def __init__(self):
        self.n_fold = 3
        self.cores = 1
        self.verbosity = 2
        self.random_state = 12

    def train_logreg(self, data_file, outfile):
        hdf = pd.read_csv(data_file, delimiter='\t', header=0)

        y, X = dmatrices('label ~ freq_word1 + freq_word2 + freq_diff + similarity_pmi + similarity_svd + context_subsumption \
                          + context_entropy_word1 + context_entropy_word2 + entropy_diff + normalized_entropy_w1 + \
                          normalized_entropy_w2 + normalized_entropy_diff', hdf, return_type='dataframe')

        y = np.ravel(y)
        X = scale(X)

        clf = LogisticRegression(class_weight='balanced', solver='liblinear')
        predicted = cross_val_predict(clf, X, y, cv=self.n_fold, method='predict_proba', n_jobs=self.cores, verbose=self.verbosity)
        positive_pred = predicted[:, 1]
        print('CV Gini: ' + str(2 * metrics.roc_auc_score(y, positive_pred) - 1))
        i = 0
        ids = np.ravel(hdf.get('id'))
        with open(outfile, 'w') as of:
            while i < len(positive_pred):
                of.write(ids[i] + '\t' + str(positive_pred[i]) + '\n')
                i += 1

    def train_random_forest(self, data_file, outfile):
        hdf = pd.read_csv(data_file, delimiter='\t', header=0)

        y, X = dmatrices('label ~ freq_word1 + freq_word2 + freq_diff + similarity_pmi + similarity_svd + context_subsumption \
                          + context_entropy_word1 + context_entropy_word2 + entropy_diff + normalized_entropy_w1 + \
                          normalized_entropy_w2 + normalized_entropy_diff', hdf, return_type='dataframe')
        y = np.ravel(y)

        clf = RFC(n_estimators=300, verbose=2, random_state=self.random_state, class_weight='balanced')

        predicted = cross_val_predict(clf, X, y, cv=self.n_fold, verbose=self.verbosity, method='predict_proba', n_jobs=self.cores)
        positive_pred = predicted[:, 1]
        print('CV Gini: ' + str(2 * metrics.roc_auc_score(y, positive_pred) - 1))
        i = 0
        ids = np.ravel(hdf.get('id'))
        with open(outfile, 'w') as of:
            while i < len(positive_pred):
                of.write(ids[i] + '\t' + str(positive_pred[i]) + '\n')
                i += 1

    def train_boosted_trees(self, data_file, outfile):
        hdf = pd.read_csv(data_file, delimiter='\t', header=0)

        y, X = dmatrices('label ~ freq_word1 + freq_word2 + freq_diff + similarity_pmi + similarity_svd + context_subsumption \
                          + context_entropy_word1 + context_entropy_word2 + entropy_diff + normalized_entropy_w1 + \
                          normalized_entropy_w2 + normalized_entropy_diff', hdf, return_type='dataframe')
        y = np.ravel(y)

        clf = GBC(random_state=self.random_state, verbose=self.verbosity)

        predicted = cross_val_predict(clf, X, y, cv=self.n_fold, verbose=2, method='predict_proba', n_jobs=self.cores)
        positive_pred = predicted[:, 1]
        print('CV Gini: ' + str(2 * metrics.roc_auc_score(y, positive_pred) - 1))
        i = 0
        ids = np.ravel(hdf.get('id'))
        with open(outfile, 'w') as of:
            while i < len(positive_pred):
                of.write(ids[i] + '\t' + str(positive_pred[i]) + '\n')
                i += 1

def main():
    args = docopt("""
    Usage:
        ontoclassifier.py <data_file> <classifier> <output>
    """)
    oc = Ontoclassifier()
    if args['<classifier>'] == 'logistic_regression':
        oc.train_logreg(args['<data_file>'], args['<output>'])
    if args['<classifier>'] == 'random_forest':
        oc.train_random_forest(args['<data_file>'], args['<output>'])
    if args['<classifier>'] == 'boosted_trees':
        oc.train_boosted_trees(args['<data_file>'], args['<output>'])

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>#!/usr/bin/python
#
# EM MEDIA HANDLER
# Copyright (c) 2014 Erin Morelli
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
'''Main handler module'''

# ======== IMPORT MODULES ======== #

import logging
from re import search, I
from shutil import rmtree
from os import path, listdir, remove
import mediahandler as mh
import mediahandler.util.notify as Notify
from mediahandler.util.config import getconfig, makeconfig
from mediahandler.util.args import get_arguments

# ======== DEFINE HANDLER CLASS ======== #

class Handler:
    '''Media handler class'''
    # ======== INIT CLASS ======== #

    def __init__(self):
        '''Initialize handler class'''
        # Set global variables
        self.args = {}
        self.settings = {}
        self.push = ''

    # ======== MOVE VIDEO FILES ======== #

    def add_video(self, src):
        '''Process video files'''
        logging.info("Moving file(s)")
        # Set which handler to use
        if self.args['type'] in ["TV", "Television", "TV Shows"]:
            (new_name, dst) = self.add_episode(src)
        elif self.args['type'] == 'Movies':
            (new_name, dst) = self.add_movie(src)
        # Check for errors
        if new_name is None:
            return None
        logging.debug("Name: %s", new_name)
        logging.debug("DST: %s", dst)
        # Check that new file exists
        if not path.isfile(dst):
            self.push.failure("File failed to move: %s" % self.args['name'])
        # Return new file name
        return new_name

    # ======== ADD TV EPISODE ======== #

    def add_episode(self, raw):
        '''Get and process TV episode information'''
        logging.info("Getting TV episode information")
        # Check that TV is enabled
        if not self.settings['TV']['enabled']:
            self.push.failure("TV type is not enabled")
        # Import TV module
        import mediahandler.types.tv as TV
        # Send info to handler
        (ep_title, new_file) = TV.get_episode(raw, self.settings['TV'])
        if ep_title is None:
            self.push.failure("Unable to match episode: %s"
                              % self.args['name'])
        # return folder and file paths
        return ep_title, new_file

    # ======== ADD MOVIE ======== #

    def add_movie(self, raw):
        '''Get and process movie information'''
        logging.info("Getting movie information")
        # Check that Movies are enabled
        if not self.settings['Movies']['enabled']:
            self.push.failure("Movies type is not enabled")
        # Import movie module
        import mediahandler.types.movies as Movies
        # Send info to handler
        (mov_title, new_file) = Movies.get_movie(raw, self.settings['Movies'])
        if mov_title is None:
            self.push.failure("Unable to match movie: %s" % self.args['name'])
        # return folder and file paths
        return mov_title, new_file

    # ======== ADD MUSIC ======== #

    def add_music(self, raw, is_single=False):
        '''Get and process music information'''
        logging.info("Getting music information")
        # Check that Movies are enabled
        if not self.settings['Movies']['enabled']:
            self.push.failure("Movies type is not enabled")
        # Import music module
        import mediahandler.types.music as Music
        # Send info to handler
        music_return = Music.get_music(raw, self.settings['Music'], is_single)
        (has_skips, music_info) = music_return
        # Check for no info
        if music_info is None:
            self.push.failure(
                "Unable to match music: %s" % self.args['name'])
        # Don't remove files if has skips
        if has_skips:
            self.settings['General']['keep_files'] = True
        # return album info
        return music_info

    # ======== ADD AUDIOBOOK ======== #

    def add_book(self, raw):
        '''Get and process book information'''
        logging.info("Getting audiobook information")
        # Check that Movies are enabled
        if not self.settings['Audiobooks']['enabled']:
            self.push.failure("Audiobooks type is not enabled")
        # Import audiobooks module
        import mediahandler.types.audiobooks as Audiobooks
        # Send to handler
        bok = Audiobooks.Book(self.settings['Audiobooks'])
        book_info = bok.get_book(raw)
        if book_info is None:
            self.push.failure("Unable to match book: %s" % self.args['name'])
            return None
        # return book info
        return book_info

    # ======== EXTRACT FILES ======== #

    def extract_files(self, raw):
        '''Send files to be extracted'''
        logging.info("Extracting files from compressed file")
        self.settings['extracted'] = raw
        # Import extract module
        import mediahandler.util.extract as Extract
        # Send to handler
        extracted = Extract.get_files(raw)
        if extracted is None:
            self.push.failure("Unable to extract files: %s"
                              % self.args['name'])
            return None
        # Send files back to handler
        return extracted

    # ======== SINGLE FILE HANDLER ======== #

    def __single_file(self, files):
        '''Process single files'''
        # Single file, treat differently
        logging.debug("Processing as a single file")
        self.settings['is_single'] = True
        # Look for zipped file first
        if search(r".(zip|rar|7z)$", files, I):
            logging.debug("Zipped file type detected")
            # Send to extractor
            if self.settings['has_filebot']:
                get_files = self.extract_files(files)
                # Check for failure
                if get_files is None:
                    return None
                # Rescan files
                self.__file_handler(get_files)
        # otherwise treat like other files
        if self.args['type'] == "Music":
            return self.add_music(files, True)
        else:
            # Set single file as source
            src = files
            # Move file
            video_info = self.add_video(src)
            # Check for problems
            if video_info is None:
                return
            return video_info

    # ======== FOLDER HANDLER ======== #

    def __process_folder(self, files):
        '''Process as folder'''
        # Otherwise process as folder
        logging.debug("Processing as a folder")
        self.settings['is_single'] = False
        # Get a list of files
        file_list = listdir(files)
        # Look for zipped file first
        if search(r".(zip|rar|7z)\n", '\n'.join(file_list), I):
            logging.debug("Zipped file type detected")
            # Send to extractor
            if self.settings['has_filebot']:
                get_files = self.extract_files(files)
                # Check for failure
                if get_files is None:
                    return None
                # Rescan files
                self.__file_handler(get_files)
        # Check for music
        if self.args['type'] == "Music":
            return self.add_music(files)
        else:
            # Locate video file in folder
            for item in file_list:
                # Look for file types we want
                if search(r"\.(mkv|avi|m4v|mp4)$", item, I):
                    # Set info
                    file_name = item
                    src = files+'/'+file_name
                    # Move file
                    video_info = self.add_video(src)
                    # Check for problems
                    if video_info is None:
                        return
                    return video_info

    # ======== MAIN FILE HANDLER ======== #

    def __file_handler(self, files):
        '''Handle files by type'''
        logging.info("Starting files handler")
        added_files = []
        # Process books first
        if self.args['type'] == "Books" or self.args['type'] == "Audiobooks":
            added_file = self.add_book(files)
            added_files.append(added_file)
        # Then check for folders/files
        elif path.isfile(files):
            added_file = self.__single_file(files)
            added_files.append(added_file)
        else:
            added_file = self.__process_folder(files)
            added_files.append(added_file)
        # Make sure files were added
        if len(added_files) == 0:
            self.push.failure("No %s files found for: %s"
                              % (self.args['type'], self.args['name']))
        # Remove old files
        if not self.settings['General']['keep_files']:
            if path.exists(files):
                if 'extracted' in self.settings.keys():
                    logging.debug("Removing extracted files folder")
                    rmtree(self.settings['extracted'])
                elif self.settings['is_single']:
                    logging.debug("Removing extra single file")
                    remove(files)
                else:
                    logging.debug("Removing extra files folder")
                    rmtree(files)
        # Send success notification
        self.push.success(added_files)
        # Finish
        return added_files

    # ======== PARSE DIRECTORY ======== #

    def __parse_dir(self, rawpath):
        '''Parse input directory structure'''
        logging.info("Extracing info from path")
        # Extract info from path
        parse_path = search(r"^((.*)?\/(.*))?\/(.*)$",
                            rawpath, I)
        if parse_path:
            self.args['path'] = parse_path.group(1)
            self.args['name'] = parse_path.group(4)
            # Look for custom type
            if 'type' not in self.args.keys():
                self.args['type'] = parse_path.group(3)
                if self.args['type'] not in mh.__mediatypes__:
                    self.push.failure(
                        'Media type %s not recognized' %
                        self.args['type'])
            logging.debug("Type detected: %s", self.args['type'])
        else:
            logging.debug("No type detected")
            if self.settings['Deluge']['enabled']:
                # Remove torrent
                import mediahandler.util.torrent as Torrent
                Torrent.remove_torrent(
                    self.settings['Deluge'],
                    self.args['hash'])
            # Notify about failure
            self.push.failure(
                "No type or name specified for media: %s" %
                self.args['name'])
        return

    # ======== HANDLE MEDIA ======== #

    def __handle_media(self, use_deluge):
        '''Sort args based on input'''
        logging.debug("Inputs: %s", self.args)
        # Determing if using deluge or not
        media_dir = ''
        if use_deluge:
            logging.info("Processing from deluge")
            media_dir = self.args['path']
        else:
            logging.info("Processing from command line")
            media_dir = path.dirname(self.args['media'])
        # Check to see that files exist
        if not path.exists(media_dir):
            # There was a problem, no files found
            self.push.failure("No media files found: %s" % media_dir)
        # Parse directory structure
        self.__parse_dir(media_dir)
        # Check that file was downloaded
        file_path = self.args['path'] + "/" + self.args['name']
        if path.exists(file_path):
            if self.settings['Deluge']['enabled'] and use_deluge:
                # Remove torrent
                import mediahandler.util.torrent as Torrent
                Torrent.remove_torrent(
                    self.settings['Deluge'],
                    self.args['hash'])
            # Send to handler
            new_files = self.__file_handler(file_path)
            # Check that files were returned
            if new_files is None:
                self.push.failure("No media files found: %s" %
                                  self.args['name'])
        else:
            # There was a problem, no files found
            self.push.failure("No media files found: %s" % self.args['name'])
        return new_files

    # ======== MAIN ADD MEDIA FUNCTION ======== #

    def addmedia(self):
        '''Main function'''
        # Get arguments
        (use_deluge, self.args) = get_arguments()
        # User-provided path
        __new_path = None
        # Check for user-specified config
        if 'config' in self.args.keys():
            __new_path = self.args['config']
        # Set paths
        __config_path = makeconfig(__new_path)
        # Get settings from config
        self.settings = getconfig(__config_path)
        # Set up notify instance
        self.push = Notify.Push(self.settings['Pushover'], use_deluge)
        # Start main function
        new_files = self.__handle_media(use_deluge)
        # Exit
        return new_files
<commit_msg>Pylint fixes<commit_after>#!/usr/bin/python
#
# EM MEDIA HANDLER
# Copyright (c) 2014 Erin Morelli
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
'''Main handler module'''

# ======== IMPORT MODULES ======== #

import logging
from re import search, I
from shutil import rmtree
from os import path, listdir, remove
import mediahandler as mh
import mediahandler.util.notify as Notify
from mediahandler.util.config import getconfig, makeconfig
from mediahandler.util.args import get_arguments

# ======== DEFINE HANDLER CLASS ======== #

class Handler:
    '''Media handler class'''
    # ======== INIT CLASS ======== #

    def __init__(self):
        '''Initialize handler class'''
        # Set global variables
        self.args = {}
        self.settings = {}
        self.push = ''

    # ======== MOVE VIDEO FILES ======== #

    def add_video(self, src):
        '''Process video files'''
        logging.info("Moving file(s)")
        # Set which handler to use
        if self.args['type'] in ["TV", "Television", "TV Shows"]:
            (new_name, dst) = self.add_episode(src)
        elif self.args['type'] == 'Movies':
            (new_name, dst) = self.add_movie(src)
        # Check for errors
        if new_name is None:
            return None
        logging.debug("Name: %s", new_name)
        logging.debug("DST: %s", dst)
        # Check that new file exists
        if not path.isfile(dst):
            self.push.failure("File failed to move: %s" % self.args['name'])
        # Return new file name
        return new_name

    # ======== ADD TV EPISODE ======== #

    def add_episode(self, raw):
        '''Get and process TV episode information'''
        logging.info("Getting TV episode information")
        # Check that TV is enabled
        if not self.settings['TV']['enabled']:
            self.push.failure("TV type is not enabled")
        # Import TV module
        import mediahandler.types.tv as TV
        # Send info to handler
        (ep_title, new_file) = TV.get_episode(raw, self.settings['TV'])
        if ep_title is None:
            self.push.failure("Unable to match episode: %s"
                              % self.args['name'])
        # return folder and file paths
        return ep_title, new_file

    # ======== ADD MOVIE ======== #

    def add_movie(self, raw):
        '''Get and process movie information'''
        logging.info("Getting movie information")
        # Check that Movies are enabled
        if not self.settings['Movies']['enabled']:
            self.push.failure("Movies type is not enabled")
        # Import movie module
        import mediahandler.types.movies as Movies
        # Send info to handler
        (mov_title, new_file) = Movies.get_movie(raw, self.settings['Movies'])
        if mov_title is None:
            self.push.failure("Unable to match movie: %s" % self.args['name'])
        # return folder and file paths
        return mov_title, new_file

    # ======== ADD MUSIC ======== #

    def add_music(self, raw, is_single=False):
        '''Get and process music information'''
        logging.info("Getting music information")
        # Check that Movies are enabled
        if not self.settings['Movies']['enabled']:
            self.push.failure("Movies type is not enabled")
        # Import music module
        import mediahandler.types.music as Music
        # Send info to handler
        music_return = Music.get_music(raw, self.settings['Music'], is_single)
        (has_skips, music_info) = music_return
        # Check for no info
        if music_info is None:
            self.push.failure(
                "Unable to match music: %s" % self.args['name'])
        # Don't remove files if has skips
        if has_skips:
            self.settings['General']['keep_files'] = True
        # return album info
        return music_info

    # ======== ADD AUDIOBOOK ======== #

    def add_book(self, raw):
        '''Get and process book information'''
        logging.info("Getting audiobook information")
        # Check that Movies are enabled
        if not self.settings['Audiobooks']['enabled']:
            self.push.failure("Audiobooks type is not enabled")
        # Import audiobooks module
        import mediahandler.types.audiobooks as Audiobooks
        # Send to handler
        bok = Audiobooks.Book(self.settings['Audiobooks'])
        book_info = bok.get_book(raw)
        if book_info is None:
            self.push.failure("Unable to match book: %s" % self.args['name'])
            return None
        # return book info
        return book_info

    # ======== EXTRACT FILES ======== #

    def extract_files(self, raw):
        '''Send files to be extracted'''
        logging.info("Extracting files from compressed file")
        self.settings['extracted'] = raw
        # Import extract module
        import mediahandler.util.extract as Extract
        # Send to handler
        extracted = Extract.get_files(raw)
        if extracted is None:
            self.push.failure("Unable to extract files: %s"
                              % self.args['name'])
            return None
        # Send files back to handler
        return extracted

    # ======== SINGLE FILE HANDLER ======== #

    def __single_file(self, files):
        '''Process single files'''
        # Single file, treat differently
        logging.debug("Processing as a single file")
        self.settings['is_single'] = True
        # Look for zipped file first
        if search(r".(zip|rar|7z)$", files, I):
            logging.debug("Zipped file type detected")
            # Send to extractor
            if self.settings['has_filebot']:
                get_files = self.extract_files(files)
                # Check for failure
                if get_files is None:
                    return None
                # Rescan files
                self.__file_handler(get_files)
        # otherwise treat like other files
        if self.args['type'] == "Music":
            return self.add_music(files, True)
        else:
            # Set single file as source
            src = files
            # Move file
            video_info = self.add_video(src)
            # Check for problems
            if video_info is None:
                return
            return video_info

    # ======== FOLDER HANDLER ======== #

    def __process_folder(self, files):
        '''Process as folder'''
        # Otherwise process as folder
        logging.debug("Processing as a folder")
        self.settings['is_single'] = False
        # Get a list of files
        file_list = listdir(files)
        # Look for zipped file first
        if search(r".(zip|rar|7z)\n", '\n'.join(file_list), I):
            logging.debug("Zipped file type detected")
            # Send to extractor
            if self.settings['has_filebot']:
                get_files = self.extract_files(files)
                # Check for failure
                if get_files is None:
                    return None
                # Rescan files
                self.__file_handler(get_files)
        # Check for music
        if self.args['type'] == "Music":
            return self.add_music(files)
        else:
            # Locate video file in folder
            for item in file_list:
                # Look for file types we want
                if search(r"\.(mkv|avi|m4v|mp4)$", item, I):
                    # Set info
                    file_name = item
                    src = files+'/'+file_name
                    # Move file
                    video_info = self.add_video(src)
                    # Check for problems
                    if video_info is None:
                        return
                    return video_info

    # ======== MAIN FILE HANDLER ======== #

    def __file_handler(self, files):
        '''Handle files by type'''
        logging.info("Starting files handler")
        added_files = []
        # Process books first
        if self.args['type'] == "Books" or self.args['type'] == "Audiobooks":
            added_file = self.add_book(files)
            added_files.append(added_file)
        # Then check for folders/files
        elif path.isfile(files):
            added_file = self.__single_file(files)
            added_files.append(added_file)
        else:
            added_file = self.__process_folder(files)
            added_files.append(added_file)
        # Make sure files were added
        if len(added_files) == 0:
            self.push.failure("No %s files found for: %s"
                              % (self.args['type'], self.args['name']))
        # Remove old files
        if not self.settings['General']['keep_files']:
            if path.exists(files):
                if 'extracted' in self.settings.keys():
                    logging.debug("Removing extracted files folder")
                    rmtree(self.settings['extracted'])
                elif self.settings['is_single']:
                    logging.debug("Removing extra single file")
                    remove(files)
                else:
                    logging.debug("Removing extra files folder")
                    rmtree(files)
        # Send success notification
        self.push.success(added_files)
        # Finish
        return added_files

    # ======== PARSE DIRECTORY ======== #

    def __parse_dir(self, rawpath):
        '''Parse input directory structure'''
        logging.info("Extracing info from path")
        # Extract info from path
        parse_path = search(r"^((.*)?\/(.*))?\/(.*)$",
                            rawpath, I)
        if parse_path:
            self.args['path'] = parse_path.group(1)
            self.args['name'] = parse_path.group(4)
            # Look for custom type
            if 'type' not in self.args.keys():
                self.args['type'] = parse_path.group(3)
                if self.args['type'] not in mh.__mediatypes__:
                    self.push.failure(
                        'Media type %s not recognized' %
                        self.args['type'], True)
            logging.debug("Type detected: %s", self.args['type'])
        else:
            logging.debug("No type detected")
            if self.settings['Deluge']['enabled']:
                # Remove torrent
                import mediahandler.util.torrent as Torrent
                Torrent.remove_torrent(
                    self.settings['Deluge'],
                    self.args['hash'])
            # Notify about failure
            self.push.failure(
                "No type or name specified for media: %s" %
                self.args['name'], True)
        return

    # ======== HANDLE MEDIA ======== #

    def __handle_media(self, use_deluge):
        '''Sort args based on input'''
        logging.debug("Inputs: %s", self.args)
        # Determing if using deluge or not
        media_dir = ''
        if use_deluge:
            logging.info("Processing from deluge")
            media_dir = self.args['path']
        else:
            logging.info("Processing from command line")
            media_dir = path.dirname(self.args['media'])
        # Check to see that files exist
        if not path.exists(media_dir):
            # There was a problem, no files found
            self.push.failure("No media files found: %s" % media_dir, True)
        # Parse directory structure
        self.__parse_dir(media_dir)
        # Check that file was downloaded
        file_path = self.args['path'] + "/" + self.args['name']
        if path.exists(file_path):
            if self.settings['Deluge']['enabled'] and use_deluge:
                # Remove torrent
                import mediahandler.util.torrent as Torrent
                Torrent.remove_torrent(
                    self.settings['Deluge'],
                    self.args['hash'])
            # Send to handler
            new_files = self.__file_handler(file_path)
            # Check that files were returned
            if new_files is None:
                self.push.failure("No media files found: %s" %
                                  self.args['name'], True)
        else:
            # There was a problem, no files found
            self.push.failure("No media files found: %s" %
                              self.args['name'], True)
        return new_files

    # ======== MAIN ADD MEDIA FUNCTION ======== #

    def addmedia(self):
        '''Main function'''
        # Get arguments
        (use_deluge, self.args) = get_arguments()
        # User-provided path
        __new_path = None
        # Check for user-specified config
        if 'config' in self.args.keys():
            __new_path = self.args['config']
        # Set paths
        __config_path = makeconfig(__new_path)
        # Get settings from config
        self.settings = getconfig(__config_path)
        # Set up notify instance
        self.push = Notify.Push(self.settings['Pushover'], use_deluge)
        # Start main function
        new_files = self.__handle_media(use_deluge)
        # Exit
        return new_files
<|endoftext|>


<commit_before>"""oauthenticator version info"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

version_info = (
    14,
    2,
    0,
#    'dev',  # comment-out this line for a release
)
__version__ = '.'.join(map(str, version_info[:3]))

if len(version_info) > 3:
    __version__ = '%s%s' % (__version__, version_info[3])
<commit_msg>back to dev<commit_after>"""oauthenticator version info"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

version_info = (
    14,
    2,
    0,
    'dev',  # comment-out this line for a release
)
__version__ = '.'.join(map(str, version_info[:3]))

if len(version_info) > 3:
    __version__ = '%s%s' % (__version__, version_info[3])
<|endoftext|>


<commit_before>import csv

from pygraphc.abstraction.ClusterAbstraction import ClusterAbstraction
from pygraphc.clustering.ClusterUtility import ClusterUtility

class AnomalyScore(object):
    """A class to calculate anomaly score in a cluster.
    """
    def __init__(self, graph, clusters, filename):
        """The constructor of class AnomalyScore.

        Parameters
        ----------
        graph       : graph
        clusters    : dict[list]
        filename    : str
        """
        self.graph = graph
        self.clusters = clusters
        self.filename = filename
        self.property = {}
        self.abstraction = {}

    def write_property(self):
        """Write cluster property to a file.
        """
        # get cluster abstraction and its properties
        self.abstraction = ClusterAbstraction.dp_lcs(self.graph, self.clusters)
        self.property = ClusterUtility.get_cluster_property(self.graph, self.clusters)

        # write to csv
        f = open(self.filename + '_anomaly.csv', 'wt')
        writer = csv.writer(f)

        # set header
        header = ('cluster_id', 'cluster_abstraction') + tuple(self.property[0].keys())
        writer.writerow(header)

        # write data
        for cluster_id, abstract in self.abstraction.iteritems():
            row = (cluster_id, abstract) + tuple(self.property[cluster_id].values())
            writer.writerow(row)
<commit_msg>Add description of Parameters section in docstring<commit_after>import csv
from pygraphc.abstraction.ClusterAbstraction import ClusterAbstraction
from pygraphc.clustering.ClusterUtility import ClusterUtility

class AnomalyScore(object):
    """A class to calculate anomaly score in a cluster.
    """
    def __init__(self, graph, clusters, filename):
        """The constructor of class AnomalyScore.

        Parameters
        ----------
        graph       : graph
            A graph to be analyzed for its anomaly.
        clusters    : dict[list]
            Dictionary of list containing node identifier for each clusters.
        filename    : str
            Filename for anomaly detection result.
        """
        self.graph = graph
        self.clusters = clusters
        self.filename = filename
        self.property = {}
        self.abstraction = {}

    def write_property(self):
        """Write cluster property to a file.
        """
        # get cluster abstraction and its properties
        self.abstraction = ClusterAbstraction.dp_lcs(self.graph, self.clusters)
        self.property = ClusterUtility.get_cluster_property(self.graph, self.clusters)

        # write to csv
        f = open(self.filename + '_anomaly.csv', 'wt')
        writer = csv.writer(f)

        # set header
        header = ('cluster_id', 'cluster_abstraction') + tuple(self.property[0].keys())
        writer.writerow(header)

        # write data
        for cluster_id, abstract in self.abstraction.iteritems():
            row = (cluster_id, abstract) + tuple(self.property[cluster_id].values())
            writer.writerow(row)
<|endoftext|>


<commit_before>"""
Define a few commands
"""

from .meeseeksbox.utils import Session, fix_issue_body, fix_comment_body

from .meeseeksbox.scopes import admin, write

@write
def close(*, session, payload, arguments):
    session.ghrequest('PATCH', payload['issue']
                      ['url'], json={'state': 'closed'})

@write
def open(*, session, payload, arguments):
    session.ghrequest('PATCH', payload['issue']['url'], json={'state': 'open'})

@admin
def migrate_issue_request(*, session:Session, payload:dict, arguments:str):
    """Todo:

    - Works through pagination of comments
    - Works through pagination of labels

    Link to non-migrated labels.
    """
    if arguments.startswith('to '):
        arguments = arguments[3:]

    org_repo = arguments
    org, repo = arguments.split('/')

    target_session = yield org_repo
    if not target_session:
        session.post_comment(
            payload['issue']['comments_url'], body="I'm afraid I can't do that. Maybe I need to be installed on target repository ?\n"
            "Click [here](https://github.com/integrations/meeseeksdev/installations/new) to do that.".format(botname='meeseeksdev')

        )
        return

    issue_title = payload['issue']['title']
    issue_body = payload['issue']['body']
    original_org = payload['organization']['login']
    original_repo = payload['repository']['name']
    original_poster = payload['issue']['user']['login']
    original_number = payload['issue']['number']
    migration_requester = payload['comment']['user']['login']
    request_id = payload['comment']['id']
    original_labels = [l['name'] for l in payload['issue']['labels']]

    if original_labels:
        available_labels = target_session.ghrequest('GET',
                                                    'https://api.github.com/repos/{org}/{repo}/labels'.format(
                                                        org=org, repo=repo),
                                                    None).json()

        available_labels = [l['name'] for l in available_labels]

    migrate_labels = [l for l in original_labels if l in available_labels]
    not_set_labels = [l for l in original_labels if l not in available_labels]

    new_response = target_session.create_issue(org, repo, issue_title,
                                               fix_issue_body(
                                                   issue_body, original_poster, original_repo, original_org,
                                                   original_number, migration_requester),
                                               labels=migrate_labels
                                               )

    new_issue = new_response.json()
    new_comment_url = new_issue['comments_url']

    original_comments = session.ghrequest(
        'GET', payload['issue']['comments_url'], None).json()

    for comment in original_comments:
        if comment['id'] == request_id:
            continue
        body = comment['body']
        op = comment['user']['login']
        url = comment['html_url']
        target_session.post_comment(new_comment_url, body=fix_comment_body(
            body, op, url, original_org, original_repo))

    if not_set_labels:
        body = "I was not able to apply the following label(s): %s " % ','.join(
            not_set_labels)
        target_session.post_comment(new_comment_url, body=body)

    session.post_comment(payload['issue'][
                         'comments_url'], body='Done as {}/{}#{}.'.format(org, repo, new_issue['number']))
    session.ghrequest('PATCH', payload['issue'][
                      'url'], json={'state': 'closed'})

from .meeseeksbox.scopes import pr_author, write
from .meeseeksbox.commands import tag, untag

@pr_author
@write
def ready(*, session, payload, arguments):
    tag(session, payload, 'need review')
    untag(session, payload, 'waiting for author')

@write
def merge(*, session, payload, arguments, method='merge'):
    print('===== merging =====')
    prnumber = payload['issue']['number']
    org_name = payload['repository']['owner']['login']
    repo_name = payload['repository']['name']

    # collect extended payload on the PR
    print('== Collecting data on Pull-request...')
    r = session.ghrequest('GET',
                          'https://api.github.com/repos/{}/{}/pulls/{}'.format(
                              org_name, repo_name, prnumber),
                          json=None)
    pr_data = r.json()
    head_sha = pr_data['head']['sha']
    mergeable = pr_data['mergeable'] == 'true'
    merges_url = pr_data['merges_url']
    repo_name = pr_data['head']['repo']['name']
    if mergeable:
        resp = session.ghrequest('PUT', merges_url, json={'sha': head_sha})
    else:
        print('')

    print(resp.json())
    resp.raise_for_status()
<commit_msg>fix merge command<commit_after>"""
Define a few commands
"""

from .meeseeksbox.utils import Session, fix_issue_body, fix_comment_body

from .meeseeksbox.scopes import admin, write

@write
def close(*, session, payload, arguments):
    session.ghrequest('PATCH', payload['issue']
                      ['url'], json={'state': 'closed'})

@write
def open(*, session, payload, arguments):
    session.ghrequest('PATCH', payload['issue']['url'], json={'state': 'open'})

@admin
def migrate_issue_request(*, session:Session, payload:dict, arguments:str):
    """Todo:

    - Works through pagination of comments
    - Works through pagination of labels

    Link to non-migrated labels.
    """
    if arguments.startswith('to '):
        arguments = arguments[3:]

    org_repo = arguments
    org, repo = arguments.split('/')

    target_session = yield org_repo
    if not target_session:
        session.post_comment(
            payload['issue']['comments_url'], body="I'm afraid I can't do that. Maybe I need to be installed on target repository ?\n"
            "Click [here](https://github.com/integrations/meeseeksdev/installations/new) to do that.".format(botname='meeseeksdev')

        )
        return

    issue_title = payload['issue']['title']
    issue_body = payload['issue']['body']
    original_org = payload['organization']['login']
    original_repo = payload['repository']['name']
    original_poster = payload['issue']['user']['login']
    original_number = payload['issue']['number']
    migration_requester = payload['comment']['user']['login']
    request_id = payload['comment']['id']
    original_labels = [l['name'] for l in payload['issue']['labels']]

    if original_labels:
        available_labels = target_session.ghrequest('GET',
                                                    'https://api.github.com/repos/{org}/{repo}/labels'.format(
                                                        org=org, repo=repo),
                                                    None).json()

        available_labels = [l['name'] for l in available_labels]

    migrate_labels = [l for l in original_labels if l in available_labels]
    not_set_labels = [l for l in original_labels if l not in available_labels]

    new_response = target_session.create_issue(org, repo, issue_title,
                                               fix_issue_body(
                                                   issue_body, original_poster, original_repo, original_org,
                                                   original_number, migration_requester),
                                               labels=migrate_labels
                                               )

    new_issue = new_response.json()
    new_comment_url = new_issue['comments_url']

    original_comments = session.ghrequest(
        'GET', payload['issue']['comments_url'], None).json()

    for comment in original_comments:
        if comment['id'] == request_id:
            continue
        body = comment['body']
        op = comment['user']['login']
        url = comment['html_url']
        target_session.post_comment(new_comment_url, body=fix_comment_body(
            body, op, url, original_org, original_repo))

    if not_set_labels:
        body = "I was not able to apply the following label(s): %s " % ','.join(
            not_set_labels)
        target_session.post_comment(new_comment_url, body=body)

    session.post_comment(payload['issue'][
                         'comments_url'], body='Done as {}/{}#{}.'.format(org, repo, new_issue['number']))
    session.ghrequest('PATCH', payload['issue'][
                      'url'], json={'state': 'closed'})

from .meeseeksbox.scopes import pr_author, write
from .meeseeksbox.commands import tag, untag

@pr_author
@write
def ready(*, session, payload, arguments):
    tag(session, payload, 'need review')
    untag(session, payload, 'waiting for author')

@write
def merge(*, session, payload, arguments, method='merge'):
    print('===== merging =====')
    prnumber = payload['issue']['number']
    org_name = payload['repository']['owner']['login']
    repo_name = payload['repository']['name']

    # collect extended payload on the PR
    print('== Collecting data on Pull-request...')
    r = session.ghrequest('GET',
                          'https://api.github.com/repos/{}/{}/pulls/{}'.format(
                              org_name, repo_name, prnumber),
                          json=None)
    pr_data = r.json()
    head_sha = pr_data['head']['sha']
    mergeable = pr_data['mergeable'] == 'true'
    repo_name = pr_data['head']['repo']['name']
    if mergeable:
        resp = session.ghrequest('PUT', 'http://api.github.com/repos/{}/{}/merges'.format(org_name, repo_name), json={'sha': head_sha})
    else:
        print('Not mergeable')

    print(resp.json())
    resp.raise_for_status()
<|endoftext|>


<commit_before>import logging

from inspect import signature
from inspect import Parameter
from collections import OrderedDict

from pyjection.resolvers import ServiceResolver, NameResolver
from pyjection.service import Service
from pyjection.helper import get_service_subject_identifier

class DependencyInjector(object):
    """
    Main class of the package.

    This is the interface that should be used to get objects from the dependency injector.
    """

    def __init__(self, resolvers=None):
        self._logger = logging.getLogger(__name__)
        self._services = dict()
        self._singletons = dict()
        self._resolvers = resolvers
        if not resolvers:
            self._resolvers = [
                ServiceResolver(self),
                NameResolver(self)
            ]

    def register(self, service_subject, identifier=None):
        """
        Register a new service in the dependency injector

        This service can be :
            * A class that will be instantiated when called
            * An already instantiated instance that will be returned

        If no identifier is passed, it will be the class name in snake_case

        :param service_subject: The class or instance
        :type service_subject: mixed
        :param identifier: The identifier used to later retrieve a service instance
        :type identifier: string

        :return: Return the newly created service entry
        :rtype: Service
        """
        if identifier is None:
            identifier = get_service_subject_identifier(service_subject)
        service = Service(service_subject)
        self._services[identifier] = service
        self._logger.debug(
            "Class %s registered with identifier %s",
            str(service_subject),
            identifier
        )
        return service

    def register_singleton(self, service_subject, identifier=None):
        """
        Register a new singleton service in in the dependency injector

        This service can be :
            * A class that will be instantiated when called
            * An already instantiated instance that will be returned

        If no identifier is passed, it will be the class name in snake_case

        :param service_subject: The class or instance
        :type service_subject: mixed
        :param identifier: The identifier used to later retrieve a service singleton
        :type identifier: string

        :return: Return the newly created dependency entry
        :rtype: Service
        """
        if identifier is None:
            identifier = get_service_subject_identifier(service_subject)
        service = Service(service_subject)
        service.is_singleton = True
        self._services[identifier] = service
        self._logger.debug(
            "Class %s registered as singleton with identifier %s",
            str(service_subject),
            identifier
        )
        return service

    def get(self, identifier):
        """
        Instantiate and retrieve the service matching this identifier

        If the service has been self has a singleton the same service object
        will be return each time this service is asked

        :param identifier: The identifier or the class to retrieve
        :type identifier: mixed
        :return: The instantiated object
        :rtype: mixed
        """
        if isinstance(identifier, str) is False:
            identifier = get_service_subject_identifier(identifier)

        self._validate_service_name(identifier)

        service = self._services[identifier]
        instance = self._get_singleton(identifier, service)
        if instance is not None:
            self._logger.debug("Return singleton with ID %s", identifier)
            return instance

        instance = self._get_instance(service)
        self._set_singleton(identifier, instance, service)
        self._logger.debug("Return instance with ID %s", identifier)
        return instance

    def get_uninstantiated(self, identifier):
        if isinstance(identifier, str) is False:
            identifier = get_service_subject_identifier(identifier)

        self._validate_service_name(identifier)

        service = self._services[identifier]
        return service.subject

    def has_service(self, identifier):
        """
        Check if the service matching the given identifier
        has already been declared

        :param identifier: Name of the service or the class
        :type identifier: mixed
        :return: Whether or not the service exists
        :rtype: boolean
        """
        if isinstance(identifier, str) is False:
            identifier = get_service_subject_identifier(identifier)

        if identifier in self._services:
            return True
        return False

    def _validate_service_name(self, identifier):
        if not self.has_service(identifier):
            self._logger.error("No service has been declared with ID %s", identifier)
            raise Exception("No service has been declared with this ID")

    def _get_singleton(self, identifier, service):
        """
        Return the singleton if it has been setted and
        the service represents a singleton

        :param identifier: the singleton identifier
        :param service: The service we need the singleton for
        :type identifier: string
        :type service: Service

        :return: The singleton instance or None
        :rtype: mixed
        """
        if service.is_singleton is True and identifier in self._singletons:
            return self._singletons[identifier]
        return None

    def _set_singleton(self, identifier, instance, service):
        """
        Set the instance as a singleton in the dict
        if the service represents a singleton

        :param identifier: the singleton identifier
        :param service: The service we want to set a singleton for
        :param instance: The singleton instance
        :type identifier: string
        :type service: Service
        :type instance: mixed
        """
        if service.is_singleton is True:
            self._singletons[identifier] = instance

    def _get_instance(self, service):
        """
        Return the instantiated object for the given service

        :param service: The service we need an instance for
        :type service: Service
        :return: The instantiated object
        """
        if service.type == 'instance':
            return service.subject
        arguments = self._generate_arguments_dict(service)
        return service.subject(**arguments)

    def _generate_arguments_dict(self, service):
        """
        Generate a dict containing all the parameters values
        required to Instantiate the service.

        An exception is raised if a mandatory argument cannot be
        retrieved.

        :param service: The service that needs to be instantiated
        :type service: Service
        :return: The parameters values to use to instantiate the service
        :rtype: dict
        """
        arguments = dict()

        # We can't use signature on class object __init__
        if self._is_object_init(service.subject):
            return arguments

        sig = signature(service.subject.__init__)
        method_parameters = OrderedDict(sig.parameters)

        # Pop the first param since it's the self class instance
        method_parameters.popitem(False)

        for method_parameter in method_parameters.values():
            argument = self._get_argument(service, method_parameter)
            if argument is not None:
                arguments[method_parameter.name] = argument

        return arguments

    @staticmethod
    def _is_object_init(subject):
        """
        Check if the __init__ method for the object comes from
        the default object class or has been overridden

        :param subject: The subject we want to check the __init__ for
        :type subject: mixed

        :return: Whether the __init__ method is the default on or not
        :rtype: boolean
        """
        if '__objclass__' in dir(subject.__init__) and subject.__init__.__objclass__ == object:
            return True
        return False

    def _get_argument(self, service, method_parameter):
        """
        Retrieve the argument value for the given service

        :param service: The service we need an argument for
        :param method_parameter: The parameter we need the value for
        :type service: Service
        :type method_parameter: Parameter
        :return: The argument value
        :rtype: mixed
        """
        for resolver in self._resolvers:
            resolved = resolver.resolve(method_parameter, service)
            if resolved:
                return resolved

        # If the parameter is *args or **kwargs then we don't raise any exception
        if method_parameter.kind == Parameter.VAR_POSITIONAL or \
                method_parameter.kind == Parameter.VAR_KEYWORD:
            return None
        # If the parameter has a default value then we don't raise any exception
        if method_parameter.default is not Parameter.empty:
            return None

        self._logger.error("A required argument is not set: %s", method_parameter.name)
        raise Exception("A required argument is not set (%s)" % method_parameter.name)
<commit_msg>Simplify method<commit_after>import logging

from inspect import signature
from inspect import Parameter
from collections import OrderedDict

from pyjection.resolvers import ServiceResolver, NameResolver
from pyjection.service import Service
from pyjection.helper import get_service_subject_identifier

class DependencyInjector(object):
    """
    Main class of the package.

    This is the interface that should be used to get objects from the dependency injector.
    """

    def __init__(self, resolvers=None):
        self._logger = logging.getLogger(__name__)
        self._services = dict()
        self._singletons = dict()
        self._resolvers = resolvers
        if not resolvers:
            self._resolvers = [
                ServiceResolver(self),
                NameResolver(self)
            ]

    def register(self, service_subject, identifier=None):
        """
        Register a new service in the dependency injector

        This service can be :
            * A class that will be instantiated when called
            * An already instantiated instance that will be returned

        If no identifier is passed, it will be the class name in snake_case

        :param service_subject: The class or instance
        :type service_subject: mixed
        :param identifier: The identifier used to later retrieve a service instance
        :type identifier: string

        :return: Return the newly created service entry
        :rtype: Service
        """
        if identifier is None:
            identifier = get_service_subject_identifier(service_subject)
        service = Service(service_subject)
        self._services[identifier] = service
        self._logger.debug(
            "Class %s registered with identifier %s",
            str(service_subject),
            identifier
        )
        return service

    def register_singleton(self, service_subject, identifier=None):
        """
        Register a new singleton service in in the dependency injector

        This service can be :
            * A class that will be instantiated when called
            * An already instantiated instance that will be returned

        If no identifier is passed, it will be the class name in snake_case

        :param service_subject: The class or instance
        :type service_subject: mixed
        :param identifier: The identifier used to later retrieve a service singleton
        :type identifier: string

        :return: Return the newly created dependency entry
        :rtype: Service
        """
        if identifier is None:
            identifier = get_service_subject_identifier(service_subject)
        service = Service(service_subject)
        service.is_singleton = True
        self._services[identifier] = service
        self._logger.debug(
            "Class %s registered as singleton with identifier %s",
            str(service_subject),
            identifier
        )
        return service

    def get(self, identifier):
        """
        Instantiate and retrieve the service matching this identifier

        If the service has been self has a singleton the same service object
        will be return each time this service is asked

        :param identifier: The identifier or the class to retrieve
        :type identifier: mixed
        :return: The instantiated object
        :rtype: mixed
        """
        if isinstance(identifier, str) is False:
            identifier = get_service_subject_identifier(identifier)

        self._validate_service_name(identifier)

        service = self._services[identifier]
        instance = self._get_singleton(identifier, service)
        if instance is not None:
            self._logger.debug("Return singleton with ID %s", identifier)
            return instance

        instance = self._get_instance(service)
        self._set_singleton(identifier, instance, service)
        self._logger.debug("Return instance with ID %s", identifier)
        return instance

    def get_uninstantiated(self, identifier):
        if isinstance(identifier, str) is False:
            identifier = get_service_subject_identifier(identifier)

        self._validate_service_name(identifier)

        service = self._services[identifier]
        return service.subject

    def has_service(self, identifier):
        """
        Check if the service matching the given identifier
        has already been declared

        :param identifier: Name of the service or the class
        :type identifier: mixed
        :return: Whether or not the service exists
        :rtype: boolean
        """
        if isinstance(identifier, str) is False:
            identifier = get_service_subject_identifier(identifier)

        if identifier in self._services:
            return True
        return False

    def _validate_service_name(self, identifier):
        if not self.has_service(identifier):
            self._logger.error("No service has been declared with ID %s", identifier)
            raise Exception("No service has been declared with this ID")

    def _get_singleton(self, identifier, service):
        """
        Return the singleton if it has been setted and
        the service represents a singleton

        :param identifier: the singleton identifier
        :param service: The service we need the singleton for
        :type identifier: string
        :type service: Service

        :return: The singleton instance or None
        :rtype: mixed
        """
        if service.is_singleton is True and identifier in self._singletons:
            return self._singletons[identifier]
        return None

    def _set_singleton(self, identifier, instance, service):
        """
        Set the instance as a singleton in the dict
        if the service represents a singleton

        :param identifier: the singleton identifier
        :param service: The service we want to set a singleton for
        :param instance: The singleton instance
        :type identifier: string
        :type service: Service
        :type instance: mixed
        """
        if service.is_singleton is True:
            self._singletons[identifier] = instance

    def _get_instance(self, service):
        """
        Return the instantiated object for the given service

        :param service: The service we need an instance for
        :type service: Service
        :return: The instantiated object
        """
        if service.type == 'instance':
            return service.subject
        arguments = self._generate_arguments_dict(service)
        return service.subject(**arguments)

    def _generate_arguments_dict(self, service):
        """
        Generate a dict containing all the parameters values
        required to Instantiate the service.

        An exception is raised if a mandatory argument cannot be
        retrieved.

        :param service: The service that needs to be instantiated
        :type service: Service
        :return: The parameters values to use to instantiate the service
        :rtype: dict
        """
        arguments = dict()

        # We can't use signature on class object __init__
        if self._is_object_init(service.subject):
            return arguments

        sig = signature(service.subject.__init__)
        method_parameters = OrderedDict(sig.parameters)

        # Pop the first param since it's the self class instance
        method_parameters.popitem(False)

        for method_parameter in method_parameters.values():
            argument = self._get_argument(service, method_parameter)
            if argument is not None:
                arguments[method_parameter.name] = argument

        return arguments

    @staticmethod
    def _is_object_init(subject):
        """
        Check if the __init__ method for the object comes from
        the default object class or has been overridden

        :param subject: The subject we want to check the __init__ for
        :type subject: mixed

        :return: Whether the __init__ method is the default on or not
        :rtype: boolean
        """
        if '__objclass__' in dir(subject.__init__) and subject.__init__.__objclass__ == object:
            return True
        return False

    def _get_argument(self, service, method_parameter):
        """
        Retrieve the argument value for the given service

        :param service: The service we need an argument for
        :param method_parameter: The parameter we need the value for
        :type service: Service
        :type method_parameter: Parameter
        :return: The argument value
        :rtype: mixed
        """
        for resolver in self._resolvers:
            resolved = resolver.resolve(method_parameter, service)
            if resolved:
                return resolved

        # If the parameter is *args or **kwargs or if the parameter has a default value
        # then we don't raise any exception
        if (method_parameter.kind in [Parameter.VAR_POSITIONAL, Parameter.VAR_KEYWORD]
                or method_parameter.default is not Parameter.empty):
            return None

        self._logger.error("A required argument is not set: %s", method_parameter.name)
        raise Exception("A required argument is not set (%s)" % method_parameter.name)
<|endoftext|>


<commit_before>from multiprocessing import Pool, cpu_count

from models.common.structs import Profile
from scripts.analyze import giveInfos
from scripts.analyze import plotPortfolioWeights
from scripts.runprofile import runProfile

profile1 = Profile()
profile1.SAMPLESIZE = 50000
profile1.NAME = "Two assets, full"
profile1.RES = 50
profile1.RISKMEASURES = ["ES", "VaR"]
profile1.SET = "full"
profile1.BURNIN = 20
profile1.SCALES = {'first': [1, 1], 'second': [1.3, 0.8]}

profile2 = Profile()
profile2.SAMPLESIZE = 50000
profile2.NAME = "Two assets, mixed"
profile2.RES = 50
profile2.RISKMEASURES = ["ES", "VaR"]
profile2.SET = "mixed"
profile2.BURNIN = 20
profile2.SCALES = {'first': [1, 1], 'second': [1.3, 0.8]}

jobs = [profile1, profile2]
workers = min(cpu_count(), len(jobs))
p = Pool(workers)

giveInfos(jobs, workers)
res = p.map(runProfile, jobs)
plotPortfolioWeights(res)

print("Analysis complete.")
<commit_msg>Changed reference implementation<commit_after>from multiprocessing import Pool, cpu_count

from models.common.structs import Profile
from scripts.analyze import giveInfos
from scripts.analyze import plotPortfolioWeights
from scripts.runprofile import runProfile

profile1 = Profile()
profile1.SAMPLESIZE = 500000
profile1.NAME = "Two assets, full"
profile1.RES = 500
profile1.RISKMEASURES = ["ES", "VaR"]
profile1.SET = "full"
profile1.BURNIN = 20
profile1.SCALES = {'first': [1, 1], 'second': [1.3, 0.8]}

profile2 = Profile()
profile2.SAMPLESIZE = 500000
profile2.NAME = "Two assets, mixed"
profile2.RES = 500
profile2.RISKMEASURES = ["ES", "VaR"]
profile2.SET = "mixed"
profile2.BURNIN = 20
profile2.SCALES = {'first': [1, 1], 'second': [1.3, 0.8]}

jobs = [profile1, profile2]
workers = min(cpu_count(), len(jobs))
p = Pool(workers)

giveInfos(jobs, workers)
res = p.map(runProfile, jobs)
plotPortfolioWeights(res)

print("Analysis complete.")
<|endoftext|>


<commit_before>#!/usr/bin/env python
'''
unit testing for the model classes.

'''
__author__ = 'M@Campbell'

import unittest
from flask import url_for
from ooiservices.app import create_app, db
from ooiservices.app.models import Array, InstrumentDeployment, PlatformDeployment, Stream, \
StreamParameter, User, OperatorEvent, OperatorEventType, Organization

from unittest import skipIf
import os
'''
These tests are additional to the normal testing performed by coverage; each of
these tests are to validate model logic outside of db management.

'''
class ModelTestCase(unittest.TestCase):
    def setUp(self):
        self.app = create_app('TESTING_CONFIG')
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        self.client = self.app.test_client(use_cookies=False)
        Organization.insert_org()

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.app_context.pop()

    def test_array(self):
        #Test the json in the object
        array = Array()
        self.assertTrue(array.to_json() == {'id': None, 'array_code': None, \
        'array_name': None, 'description': None, 'display_name': None, \
        'geo_location': None})

    def test_platform_deployment(self):
        #Test the json in the object
        platform_deployment = PlatformDeployment()
        self.assertTrue(platform_deployment.to_json() == {'id': None, \
        'array_id': None, 'display_name': None, 'end_date': None, \
        'geo_location': None, 'reference_designator': None, 'start_date': None})

    def test_instrument_deployment(self):
        #Test the json in the object
        instrument_deployment = InstrumentDeployment()
        should_be = {
            'id' :None,
            'depth': None,
            'display_name' : None,
            'end_date' : None,
            'geo_location': None,
            'platform_deployment_id' : None,
            'reference_designator' : None,
            'start_date' : None
        }
        self.assertEquals(instrument_deployment.to_json() , should_be)

    @skipIf(os.getenv('TRAVIS'), 'Skip if testing from Travis CI.')
    def test_stream(self):
        #Test the json in the object
        stream = Stream()
        self.assertTrue(stream.to_json() == {'id': None, 'description': None, \
        'instrument_id': None, 'stream_name': None})

    def test_parameter(self):
        #Test the json in the object
        stream_param = StreamParameter()
        self.assertTrue(stream_param.to_json() == {'id': None, 'data_type': None, \
        'long_name': None, 'parameter_name': None, 'short_name': None, \
        'standard_name': None, 'units': None})

    def test_user(self):
        #Test the json in the object
        user = User()
        self.assertEquals(user.to_json(), {
            'email': None,
            'id': None,
            'user_id': None,
            'active':None,
            'first_name': None,
            'last_name' : None,
            'organization_id' : None,
            'phone_alternate' : None,
            'phone_primary' : None,
            'scopes' : [],
            'role' : None,
            'user_name': None,
            'email_opt_in': None,
            'other_organization' : None,
            'vocation' : None,
            'country' : None,
            'state' : None
            })

    def test_operator_event_type(self):
        #Test the json in the object
        operator_event_type = OperatorEventType()
        self.assertTrue(operator_event_type.to_json() == {'id': None, 'type_name': None, 'type_description': None})

    def test_geometry(self):
        platform_deployment = PlatformDeployment()
        platform_deployment.reference_designator = 'TEST0000'
        platform_deployment.geo_location = 'POINT(-70 40)'
        db.session.add(platform_deployment)
        db.session.commit()
        pd = PlatformDeployment.query.filter(PlatformDeployment.reference_designator=='TEST0000').first()
        self.assertEquals(pd.geojson, {'coordinates': [-70, 40], 'type': 'Point'})
<commit_msg>Fixes missed test cases.<commit_after>#!/usr/bin/env python
'''
unit testing for the model classes.

'''
__author__ = 'M@Campbell'

import unittest
from flask import url_for
from ooiservices.app import create_app, db
from ooiservices.app.models import Array, InstrumentDeployment, PlatformDeployment, Stream, \
StreamParameter, User, OperatorEvent, OperatorEventType, Organization

from unittest import skipIf
import os
'''
These tests are additional to the normal testing performed by coverage; each of
these tests are to validate model logic outside of db management.

'''
class ModelTestCase(unittest.TestCase):
    def setUp(self):
        self.app = create_app('TESTING_CONFIG')
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        self.client = self.app.test_client(use_cookies=False)
        Organization.insert_org()

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.app_context.pop()

    def test_array(self):
        #Test the json in the object
        array = Array()
        self.assertTrue(array.to_json() == {'id': None, 'array_code': None, \
        'array_name': None, 'description': None, 'display_name': None, \
        'geo_location': None})

    def test_platform_deployment(self):
        #Test the json in the object
        platform_deployment = PlatformDeployment()
        self.assertTrue(platform_deployment.to_json() == {'id': None, \
        'array_id': None, 'display_name': None, 'end_date': None, \
        'geo_location': None, 'reference_designator': None, 'start_date': None})

    def test_instrument_deployment(self):
        #Test the json in the object
        instrument_deployment = InstrumentDeployment()
        should_be = {
            'id' :None,
            'depth': None,
            'display_name' : None,
            'end_date' : None,
            'geo_location': None,
            'platform_deployment_id' : None,
            'reference_designator' : None,
            'start_date' : None
        }
        self.assertEquals(instrument_deployment.to_json() , should_be)

    @skipIf(os.getenv('TRAVIS'), 'Skip if testing from Travis CI.')
    def test_stream(self):
        #Test the json in the object
        stream = Stream()
        self.assertTrue(stream.to_json() == {'id': None, 'stream': None, \
        'delivery_method': None, 'data_type': None, 'content': None, \
        'stream_description': None, 'concatenated_name': None})

    def test_parameter(self):
        #Test the json in the object
        stream_param = StreamParameter()
        self.assertTrue(stream_param.to_json() == {'id': None, 'data_type': None, \
        'long_name': None, 'parameter_name': None, 'short_name': None, \
        'standard_name': None, 'units': None})

    def test_user(self):
        #Test the json in the object
        user = User()
        self.assertEquals(user.to_json(), {
            'email': None,
            'id': None,
            'user_id': None,
            'active':None,
            'first_name': None,
            'last_name' : None,
            'organization_id' : None,
            'phone_alternate' : None,
            'phone_primary' : None,
            'scopes' : [],
            'role' : None,
            'user_name': None,
            'email_opt_in': None,
            'other_organization' : None,
            'vocation' : None,
            'country' : None,
            'state' : None,
            'api_user_name' : None,
            'api_user_token' : None
            })

    def test_operator_event_type(self):
        #Test the json in the object
        operator_event_type = OperatorEventType()
        self.assertTrue(operator_event_type.to_json() == {'id': None, 'type_name': None, 'type_description': None})

    def test_geometry(self):
        platform_deployment = PlatformDeployment()
        platform_deployment.reference_designator = 'TEST0000'
        platform_deployment.geo_location = 'POINT(-70 40)'
        db.session.add(platform_deployment)
        db.session.commit()
        pd = PlatformDeployment.query.filter(PlatformDeployment.reference_designator=='TEST0000').first()
        self.assertEquals(pd.geojson, {'coordinates': [-70, 40], 'type': 'Point'})
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2007-2009 Parisson
# Copyright (c) 2007 Olivier Guilyardi <olivier@samalyse.com>
# Copyright (c) 2007-2009 Guillaume Pellerin <pellerin@parisson.com>
#
# This file is part of TimeSide.

# TimeSide is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# TimeSide is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with TimeSide.  If not, see <http://www.gnu.org/licenses/>.

from timeside.core import Interface, TimeSideError

class IEncoder(Interface):
    """Encoder driver interface"""

    # Remark: the method prototypes do not include any self or cls argument
    # because an interface is meant to show what methods a class must expose
    # from the caller's point of view. However, when implementing the class
    # you'll obviously want to include this extra argument.

    def format():
        """Return the encode/encoding format as a short string
        Example: "MP3", "OGG", "AVI", ...
        """

    def description():
        """Return a string describing what this encode format provides, is good
        for, etc... The description is meant to help the end user decide what
        format is good for him/her
        """

    def file_extension():
        """Return the filename extension corresponding to this encode format"""

    def mime_type():
        """Return the mime type corresponding to this encode format"""

    def set_cache_dir(path):
        """Set the directory where cached files should be stored. Does nothing
        if the encoder doesn't support caching.

        The driver shouldn't assume that this method will always get called. A
        temporary directory should be used if that's not the case.
        """

    def process(source, metadata, options=None):
        """Perform the encoding process and stream the result as a generator.

        source is a raw audio stream coming from a decoder.

        metadata is a tuple containing tuples for each descriptor return by
        the dc.Ressource of the item, in the model order :
        ((name1, value1),(name2, value2),(name1, value3), ...)

        The returned file path is not meant to be permanent in any way, it
        should be considered temporary/volatile by the caller.

        It is highly recommended that encode drivers implement some sort of
        cache instead of re-encoding each time process() is called.

        It should be possible to make subsequent calls to process() with
        different items, using the same driver instance.
        """

class EncodeProcessError(TimeSideError):

    def __init__(self, message, command, subprocess):
        self.message = message
        self.command = str(command)
        self.subprocess = subprocess

    def __str__(self):
        if self.subprocess.stderr != None:
            error = self.subprocess.stderr.read()
        else:
            error = ''
        return "%s ; command: %s; error: %s" % (self.message,
                                                self.command,
                                                error)
<commit_msg>api: remove IEncoder.set_cache_dir()<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2007-2009 Parisson
# Copyright (c) 2007 Olivier Guilyardi <olivier@samalyse.com>
# Copyright (c) 2007-2009 Guillaume Pellerin <pellerin@parisson.com>
#
# This file is part of TimeSide.

# TimeSide is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# TimeSide is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with TimeSide.  If not, see <http://www.gnu.org/licenses/>.

from timeside.core import Interface, TimeSideError

class IEncoder(Interface):
    """Encoder driver interface"""

    # Remark: the method prototypes do not include any self or cls argument
    # because an interface is meant to show what methods a class must expose
    # from the caller's point of view. However, when implementing the class
    # you'll obviously want to include this extra argument.

    def format():
        """Return the encode/encoding format as a short string
        Example: "MP3", "OGG", "AVI", ...
        """

    def description():
        """Return a string describing what this encode format provides, is good
        for, etc... The description is meant to help the end user decide what
        format is good for him/her
        """

    def file_extension():
        """Return the filename extension corresponding to this encode format"""

    def mime_type():
        """Return the mime type corresponding to this encode format"""

    def process(source, metadata, options=None):
        """Perform the encoding process and stream the result as a generator.

        source is a raw audio stream coming from a decoder.

        metadata is a tuple containing tuples for each descriptor return by
        the dc.Ressource of the item, in the model order :
        ((name1, value1),(name2, value2),(name1, value3), ...)

        The returned file path is not meant to be permanent in any way, it
        should be considered temporary/volatile by the caller.

        It is highly recommended that encode drivers implement some sort of
        cache instead of re-encoding each time process() is called.

        It should be possible to make subsequent calls to process() with
        different items, using the same driver instance.
        """

class EncodeProcessError(TimeSideError):

    def __init__(self, message, command, subprocess):
        self.message = message
        self.command = str(command)
        self.subprocess = subprocess

    def __str__(self):
        if self.subprocess.stderr != None:
            error = self.subprocess.stderr.read()
        else:
            error = ''
        return "%s ; command: %s; error: %s" % (self.message,
                                                self.command,
                                                error)
<|endoftext|>