<commit_before>from __future__ import unicode_literals

from pyramid.httpexceptions import (
    HTTPFound,
    HTTPNotFound,
    )
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.security import (
    authenticated_userid,
    remember,
    forget,
)

from velruse import login_url

import datetime
import forms

import data

@view_config(route_name='home', renderer='home.mak')
def home_view(request):
    return {
        'handle_search_form': forms.HandleSearchForm,
        'charsheet_form': forms.CharsheetForm,
        'github_login_url': login_url(request, 'github'),
    }

@view_config(context='velruse.AuthenticationComplete')
def service_login_complete(request):
    context = request.context
    username = context.profile['accounts'][0]['username']
    oauth_token = context.credentials['oauthAccessToken']

    headers = remember(request, username)

    request.session['token'] = request.context.credentials['oauthAccessToken']

    response = HTTPFound(location=request.route_url('charsheet',
                                                    username=username),
                                                    headers=headers)

    return response

@view_config(context='velruse.AuthenticationDenied', renderer='json')
def service_login_denied(request):
    return {
        'result': 'denied',
    }

@view_config(route_name='stats', renderer='stats.mak')
def global_stats(request):
    return {'stats': data.global_stats()}

@view_config(route_name='login', renderer='login.mak')
def login(request):
    login_url = request.resource_url(request.context, 'login')
    referrer = request.url
    if referrer == login_url:
        referrer = '/'  # never use the login form as came_from
    came_from = request.params.get('came_from', referrer)
    request.session['came_from'] = came_from
    return dict(
        openid_url=request.registry.settings['openid.provider'],
        url='http://' + request.registry.settings['charsheet.base_url'] \
                + '/dologin.html',
        came_from=came_from,
        )

@view_config(route_name='logout')
def logout(request):
    headers = forget(request)
    return HTTPFound(location=request.resource_url(request.context),
            headers=headers)

def openid_success(context, request, *args, **kwargs):
    """
    INCOMPLETE openID attempt! This function is not the one that is used.
    I am keeping it here for a little bit for archival purposes. Will
    delete when I am sure doing so will not break anything.
    """
    identity = request.params['openid.identity']
    email = request.params['openid.sreg.email']
    if not identity.startswith(request.registry.settings['openid.provider']):
        request.session.flash(
            'Error: Invalid OpenID provider. You can only use {0}.'.format(
                request.registry.settings['openid.provider']))
        return HTTPFound(location=request.application_url + '/login')
    #username = identity.split('/')[-1]  # Not sure what this is for
    headers = remember(request, email)
    came_from = request.session['came_from']
    del(request.session['came_from'])
    response = HTTPFound(location=came_from)
    response.headerlist.extend(headers)
    return response

@view_config(route_name='submit')
def fetch_data(request):
    try:
        usernames = {
            'github': request.params['charsheetform:github'],
            'ohloh': request.params['charsheetform:ohloh'],
            'coderwall': request.params['charsheetform:coderwall'],
        }

        username = '???'  # Set default username
        # Cycle through usernames available, use first one that exists
        for name in usernames:
            if usernames[name]:
                username = usernames[name]
                break
    except KeyError:
        return HTTPFound(location=request.route_url('home'))

    stats_dict = data.handle_all(request, usernames)

    if username != '???':
        data.inject_knowledge(username, stats_dict)
        return HTTPFound(location=request.route_url('charsheet',
                                                    username=username))
    return HTTPFound(location=request.route_url('home'))

@view_config(route_name='handle_search')
def handle_search(request):

    # Get handle from form and strip it of hanging whitespace
    handle = request.params['handlesearchform:handle'].strip()

    # If a username was entered, look for it
    if handle != '':
        return HTTPFound(location=request.route_url('charsheet',
                                                    username=handle))
    # No handle was entered, so go back to home view
    return HTTPFound(location=request.route_url('home'))

@view_config(route_name='charsheet', renderer='chartemplate.mak')
def charsheet_view(request):
    """Render stats for a user."""
    username = request.matchdict.get('username')
    user = data.get_user(username)
    if user:
        request.session.flash("Character sheet generated.")
        return dict(stats=user)
    else:
        return HTTPFound(location=request.route_url('home'))

conn_err_msg = """\
Pyramid is having a problem using your SQL database.  The problem
might be caused by one of the following things:

1.  You may need to run the "initialize_charsheet_db" script
    to initialize your database tables.  Check your virtual
    environment's "bin" directory for this script and try to run it.

2.  Your database server may not be running.  Check that the
    database server referred to by the "sqlalchemy.url" setting in
    your "development.ini" file is running.

After you fix the problem, please restart the Pyramid application to
try it again.
"""
<commit_msg>Simplify username detection.<commit_after>from __future__ import unicode_literals

from pyramid.httpexceptions import (
    HTTPFound,
    HTTPNotFound,
    )
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.security import (
    authenticated_userid,
    remember,
    forget,
)

from velruse import login_url

import datetime
import forms

import data

@view_config(route_name='home', renderer='home.mak')
def home_view(request):
    return {
        'handle_search_form': forms.HandleSearchForm,
        'charsheet_form': forms.CharsheetForm,
        'github_login_url': login_url(request, 'github'),
    }

@view_config(context='velruse.AuthenticationComplete')
def service_login_complete(request):
    context = request.context
    username = context.profile['accounts'][0]['username']
    oauth_token = context.credentials['oauthAccessToken']

    headers = remember(request, username)

    request.session['token'] = request.context.credentials['oauthAccessToken']

    response = HTTPFound(location=request.route_url('charsheet',
                                                    username=username),
                                                    headers=headers)

    return response

@view_config(context='velruse.AuthenticationDenied', renderer='json')
def service_login_denied(request):
    return {
        'result': 'denied',
    }

@view_config(route_name='stats', renderer='stats.mak')
def global_stats(request):
    return {'stats': data.global_stats()}

@view_config(route_name='login', renderer='login.mak')
def login(request):
    login_url = request.resource_url(request.context, 'login')
    referrer = request.url
    if referrer == login_url:
        referrer = '/'  # never use the login form as came_from
    came_from = request.params.get('came_from', referrer)
    request.session['came_from'] = came_from
    return dict(
        openid_url=request.registry.settings['openid.provider'],
        url='http://' + request.registry.settings['charsheet.base_url'] \
                + '/dologin.html',
        came_from=came_from,
        )

@view_config(route_name='logout')
def logout(request):
    headers = forget(request)
    return HTTPFound(location=request.resource_url(request.context),
            headers=headers)

def openid_success(context, request, *args, **kwargs):
    """
    INCOMPLETE openID attempt! This function is not the one that is used.
    I am keeping it here for a little bit for archival purposes. Will
    delete when I am sure doing so will not break anything.
    """
    identity = request.params['openid.identity']
    email = request.params['openid.sreg.email']
    if not identity.startswith(request.registry.settings['openid.provider']):
        request.session.flash(
            'Error: Invalid OpenID provider. You can only use {0}.'.format(
                request.registry.settings['openid.provider']))
        return HTTPFound(location=request.application_url + '/login')
    #username = identity.split('/')[-1]  # Not sure what this is for
    headers = remember(request, email)
    came_from = request.session['came_from']
    del(request.session['came_from'])
    response = HTTPFound(location=came_from)
    response.headerlist.extend(headers)
    return response

@view_config(route_name='submit')
def fetch_data(request):
    try:
        usernames = {
            'github': request.params['charsheetform:github'],
            'ohloh': request.params['charsheetform:ohloh'],
            'coderwall': request.params['charsheetform:coderwall'],
        }
    except KeyError:
        return HTTPFound(location=request.route_url('home'))

    stats_dict = data.handle_all(request, usernames)

    if any(usernames.values()):
        username = [name for name in usernames.values() if name][0]
        data.inject_knowledge(username, stats_dict)
        return HTTPFound(location=request.route_url('charsheet',
                                                    username=username))
    return HTTPFound(location=request.route_url('home'))

@view_config(route_name='handle_search')
def handle_search(request):

    # Get handle from form and strip it of hanging whitespace
    handle = request.params['handlesearchform:handle'].strip()

    # If a username was entered, look for it
    if handle != '':
        return HTTPFound(location=request.route_url('charsheet',
                                                    username=handle))
    # No handle was entered, so go back to home view
    return HTTPFound(location=request.route_url('home'))

@view_config(route_name='charsheet', renderer='chartemplate.mak')
def charsheet_view(request):
    """Render stats for a user."""
    username = request.matchdict.get('username')
    user = data.get_user(username)
    if user:
        request.session.flash("Character sheet generated.")
        return dict(stats=user)
    else:
        return HTTPFound(location=request.route_url('home'))

conn_err_msg = """\
Pyramid is having a problem using your SQL database.  The problem
might be caused by one of the following things:

1.  You may need to run the "initialize_charsheet_db" script
    to initialize your database tables.  Check your virtual
    environment's "bin" directory for this script and try to run it.

2.  Your database server may not be running.  Check that the
    database server referred to by the "sqlalchemy.url" setting in
    your "development.ini" file is running.

After you fix the problem, please restart the Pyramid application to
try it again.
"""
<|endoftext|>


<commit_before>import random
import requests
import time
from parsing import *
from datahandling import *
from bayesianfuncs import bayesian_learn_title
from globalvars import GlobalVars
import os
import re
from datetime import datetime
from utcdate import UtcDate

def post_message_in_room(room_id_str, msg, length_check=True):
    if room_id_str == GlobalVars.charcoal_room_id:
        GlobalVars.charcoal_hq.send_message(msg, length_check)
    elif room_id_str == GlobalVars.meta_tavern_room_id:
        GlobalVars.tavern_on_the_meta.send_message(msg, length_check)

def watcher(ev, wrap2):
    if ev.type_id != 1:
        return
    print(ev)
    ev_room = str(ev.data["room_id"])
    ev_user_id = str(ev.data["user_id"])
    message_parts = ev.message.content_source.split(" ")
    second_part_lower = "" if len(message_parts) < 2 else message_parts[1].lower()
    content_lower = ev.content.lower()
    if re.compile(":[0-9]+").search(message_parts[0]):
        if (second_part_lower.startswith("false") or second_part_lower.startswith("fp")) \
                and is_privileged(ev_room, ev_user_id):
            try:
                should_delete = True
                msg_id = int(message_parts[0][1:])
                msg_content = None
                msg_to_delete = wrap2.get_message(msg_id)
                if str(msg_to_delete.owner.id) == GlobalVars.smokeDetector_user_id[ev_room]:
                    msg_content = msg_to_delete.content_source
                if msg_content is not None:
                    site_post_id = fetch_post_id_and_site_from_msg_content(msg_content)
                    if site_post_id is None:
                        ev.message.reply("Could not register title as false positive.")
                        return
                    post_type = site_post_id[2]
                    add_false_positive((site_post_id[0], site_post_id[1]))
                    user_added = False
                    if message_parts[1].lower().startswith("falseu") or message_parts[1].lower().startswith("fpu"):
                        url_from_msg = fetch_owner_url_from_msg_content(msg_content)
                        user = get_user_from_url(url_from_msg)
                        add_whitelisted_user(user)
                        user_added = True
                    learned = False
                    if post_type == "question":
                        learned = bayesian_learn_title(fetch_title_from_msg_content(msg_content), "good")
                        if learned and user_added:
                            ev.message.reply("Registered question as false positive, whitelisted user and added title to Bayesian doctype 'good'.")
                        elif learned:
                            ev.message.reply("Registered question as false positive and added title to Bayesian doctype 'good'.")
                        else:
                            ev.message.reply("Registered question as false positive, but could not add title to Bayesian doctype 'good'.")
                    elif post_type == "answer":
                        if user_added:
                            ev.message.reply("Registered answer as false positive and whitelisted user.")
                        else:
                            ev.message.reply("Registered answer as false positive.")
                    if should_delete:
                        msg_to_delete.delete()
            except:
                pass  # couldn't delete message
        if (second_part_lower.startswith("true") or second_part_lower.startswith("tp")) \
                and is_privileged(ev_room, ev_user_id):
            try:
                msg_id = int(message_parts[0][1:])
                msg_content = None
                msg_true_positive = wrap2.get_message(msg_id)
                if str(msg_true_positive.owner.id) == GlobalVars.smokeDetector_user_id[ev_room]:
                    msg_content = msg_true_positive.content_source
                if msg_content is not None:
                    post_type = fetch_post_id_and_site_from_msg_content(msg_content)[2]
                    learned = False
                    user_added = False
                    if message_parts[1].lower().startswith("trueu") or message_parts[1].lower().startswith("tpu"):
                        url_from_msg = fetch_owner_url_from_msg_content(msg_content)
                        user = get_user_from_url(url_from_msg)
                        add_blacklisted_user(user)
                        user_added = True
                    if post_type == "question":
                        learned = bayesian_learn_title(fetch_title_from_msg_content(msg_content), "bad")
                        if learned and user_added:
                            ev.message.reply("Blacklisted user and registered question as true positive: added title to the Bayesian doctype 'bad'.")
                        elif learned:
                            ev.message.reply("Registered question as true positive: added title to the Bayesian doctype 'bad'.")
                        else:
                            ev.message.reply("Something went wrong when registering question as true positive.")
                    elif post_type == "answer":
                        if user_added:
                            ev.message.reply("Blacklisted user.")
                        else:
                            ev.message.reply("`true`/`tp` cannot be used for answers because their job is to add the title of the *question* to the Bayesian doctype 'bad'. If you want to blacklist the poster of the answer, use `trueu` or `tpu`.")
            except:
                raise
        if second_part_lower.startswith("ignore") and is_privileged(ev_room, ev_user_id):
            try:
                msg_id = int(message_parts[0][1:])
                msg_content = None
                msg_ignore = wrap2.get_message(msg_id)
                if(str(msg_ignore.owner.id) == GlobalVars.smokeDetector_user_id[ev_room]):
                    msg_content = msg_ignore.content_source
                if(msg_content is not None):
                    post_id_site = fetch_post_id_and_site_from_msg_content(msg_content)[0:2]
                    add_ignored_post(post_id_site)
                    ev.message.reply("Post ignored; alerts about it will no longer be posted.")
            except:
                pass
        if (second_part_lower == "delete" or second_part_lower == "remove" or second_part_lower == "gone") \
                and is_privileged(ev_room, ev_user_id):
            try:
                msg_id = int(message_parts[0][1:])
                msg_to_delete = wrap2.get_message(msg_id)
                if str(msg_to_delete.owner.id) == GlobalVars.smokeDetector_user_id[ev_room]:
                    msg_to_delete.delete()
            except:
                pass  # couldn't delete message
    if content_lower.startswith("!!/addblu") \
            and is_privileged(ev_room, ev_user_id):
        uid, val = get_user_from_list_command(content_lower)
        if uid > -1 and val != "":
            add_blacklisted_user((uid, val))
            ev.message.reply("User blacklisted (`%s` on `%s`)." % (uid, val))
        elif uid == -2:
            ev.message.reply("Error: %s" % val)
        else:
            ev.message.reply("Invalid format. Valid format: `!!/addblu profileurl` *or* `!!/addblu userid sitename`.")
    if content_lower.startswith("!!/rmblu") \
            and is_privileged(ev_room, ev_user_id):
        uid, val = get_user_from_list_command(content_lower)
        if uid > -1 and val != "":
            if remove_blacklisted_user((uid, val)):
                ev.message.reply("User removed from blacklist (`%s` on `%s`)." % (uid, val))
            else:
                ev.message.reply("User is not blacklisted.")
        elif uid == -2:
            ev.message.reply("Error: %s" % val)
        else:
            ev.message.reply("Invalid format. Valid format: `!!/rmblu profileurl` *or* `!!/rmblu userid sitename`.")
    if content_lower.startswith("!!/addwlu") \
            and is_privileged(ev_room, ev_user_id):
        uid, val = get_user_from_list_command(content_lower)
        if uid > -1 and val != "":
            add_whitelisted_user((uid, val))
            ev.message.reply("User whitelisted (`%s` on `%s`)." % (uid, val))
        elif uid == -2:
            ev.message.reply("Error: %s" % val)
        else:
            ev.message.reply("Invalid format. Valid format: `!!/addwlu profileurl` *or* `!!/addwlu userid sitename`.")
    if content_lower.startswith("!!/rmwlu") \
            and is_privileged(ev_room, ev_user_id):
        uid, val = get_user_from_list_command(content_lower)
        if uid != -1 and val != "":
            if remove_whitelisted_user((uid, val)):
                ev.message.reply("User removed from whitelist (`%s` on `%s`)." % (uid, val))
            else:
                ev.message.reply("User is not whitelisted.")
        elif uid == -2:
            ev.message.reply("Error: %s" % val)
        else:
            ev.message.reply("Invalid format. Valid format: `!!/rmwlu profileurl` *or* `!!/rmwlu userid sitename`.")
    if content_lower.startswith("!!/wut"):
        ev.message.reply("Whaddya mean, 'wut'? Humans...")
    if content_lower.startswith("!!/lick"):
        ev.message.reply("*licks ice cream cone*")
    if content_lower.startswith("!!/hats"):
        wb_end = datetime(2015, 1, 5, 0, 0, 0)
        now = datetime.utcnow()
        if wb_end > now:
            diff = wb_end - now
            hours, remainder = divmod(diff.seconds, 3600)
            minutes, seconds = divmod(remainder, 60)
            daystr = "days" if diff.days != 1 else "day"
            hourstr = "hours" if hours != 1 else "hour"
            minutestr = "minutes" if minutes != 1 else "minute"
            secondstr = "seconds" if seconds != 1 else "second"
            ev.message.reply("HATS ARE AWESOME. Winter Bash will end in %s %s, %s %s, %s %s and %s %s. :(" %
                             (diff.days, daystr, hours, hourstr, minutes, minutestr, seconds, secondstr))
        else:
            ev.message.reply("WINTERBASH IS OVER! :(")
    if content_lower.startswith("!!/alive"):
        if ev_room == GlobalVars.charcoal_room_id:
            ev.message.reply('Of course')
        elif ev_room == GlobalVars.meta_tavern_room_id:
            ev.message.reply(random.choice(['Yup', 'You doubt me?', 'Of course', '... did I miss something?',
                                            'plz send teh coffee',
                                            'Watching this endless list of new questions *never* gets boring',
                                            'Kinda sorta']))
    if content_lower.startswith("!!/rev"):
            ev.message.reply(
                '[' +
                GlobalVars.commit_with_author +
                '](https://github.com/Charcoal-SE/SmokeDetector/commit/' +
                GlobalVars.commit +
                ')'
            )
    if content_lower.startswith("!!/status"):
            now = datetime.utcnow()
            diff = now - UtcDate.startup_utc_date
            minutes, remainder = divmod(diff.seconds, 60)
            minutestr = "minutes" if minutes != 1 else "minute"
            ev.message.reply('Running since %s UTC (%s %s)' % (GlobalVars.startup_utc, minutes, minutestr))
    if content_lower.startswith("!!/reboot"):
        if is_privileged(ev_room, ev_user_id):
            post_message_in_room(ev_room, "Goodbye, cruel world")
            os._exit(5)
    if content_lower.startswith("!!/stappit"):
        if is_privileged(ev_room, ev_user_id):
            post_message_in_room(ev_room, "Goodbye, cruel world")
            os._exit(6)
    if content_lower.startswith("!!/master"):
        if is_privileged(ev_room, ev_user_id):
            ev.message.reply("Checking out to master and restarting...")
            os._exit(8)
    if content_lower.startswith("!!/clearbl"):
        if is_privileged(ev_room, ev_user_id):
            if os.path.isfile("blacklistedUsers.txt"):
                os.remove("blacklistedUsers.txt")
                GlobalVars.blacklisted_users = []
                ev.message.reply("Kaboom, blacklisted users cleared.")
            else:
                ev.message.reply("There are no blacklisted users at the moment.")
    if content_lower.startswith("!!/block"):
        if is_privileged(ev_room, ev_user_id):
            ev.message.reply("blocked")
            timeToBlock = ev.content[9:].strip()
            timeToBlock = int(timeToBlock) if timeToBlock else 0
            if 0 < timeToBlock < 14400:
                GlobalVars.blockedTime = time.time() + timeToBlock
            else:
                GlobalVars.blockedTime = time.time() + 900
    if content_lower.startswith("!!/unblock"):
        if is_privileged(ev_room, ev_user_id):
            GlobalVars.blockedTime = time.time()
            ev.message.reply("unblocked")
    if content_lower.startswith("!!/errorlogs"):
        if is_privileged(ev_room, ev_user_id):
            count = -1
            if len(message_parts) != 2:
                ev.message.reply("The !!/errorlogs command requires 1 argument.")
                return
            try:
                count = int(message_parts[1])
            except ValueError:
                pass
            if count == -1:
                ev.message.reply("Invalid argument.")
                return
            logs_part = fetch_lines_from_error_log(count)
            post_message_in_room(ev_room, logs_part, False)
    if content_lower.startswith("!!/pull"):
        if is_privileged(ev_room, ev_user_id):
            r = requests.get('https://api.github.com/repos/Charcoal-SE/SmokeDetector/git/refs/heads/master')
            latest_sha = r.json()["object"]["sha"]
            r = requests.get('https://api.github.com/repos/Charcoal-SE/SmokeDetector/commits/' + latest_sha + '/statuses')
            states = []
            for status in r.json():
                state = status["state"]
                states.append(state)
            if "success" in states:
                ev.message.reply("Pulling latest from master -- CI build passed.")
                os._exit(3)
            elif "error" in states or "failure" in states:
                ev.message.reply("CI build failed! :( Please check your commit.")
            elif "pending" in states or not states:
                ev.message.reply("CI build is still pending, wait until the build has finished and then pull again.")
    if content_lower.startswith("!!/help"):
        ev.message.reply("I'm [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector), a bot that detects spam and low-quality posts on the network and posts alerts to chat.")
    if content_lower.startswith("!!/apiquota"):
        ev.message.reply(GlobalVars.apiquota)
    if content_lower.startswith("!!/queuestatus"):
        ev.message.reply(GlobalVars.bodyfetcher.print_queue())
<commit_msg>Untest. --autopull<commit_after>import random
import requests
import time
from parsing import *
from datahandling import *
from bayesianfuncs import bayesian_learn_title
from globalvars import GlobalVars
import os
import re
from datetime import datetime
from utcdate import UtcDate

def post_message_in_room(room_id_str, msg, length_check=True):
    if room_id_str == GlobalVars.charcoal_room_id:
        GlobalVars.charcoal_hq.send_message(msg, length_check)
    elif room_id_str == GlobalVars.meta_tavern_room_id:
        GlobalVars.tavern_on_the_meta.send_message(msg, length_check)

def watcher(ev, wrap2):
    if ev.type_id != 1:
        return
    print(ev)
    ev_room = str(ev.data["room_id"])
    ev_user_id = str(ev.data["user_id"])
    message_parts = ev.message.content_source.split(" ")
    second_part_lower = "" if len(message_parts) < 2 else message_parts[1].lower()
    content_lower = ev.content.lower()
    if re.compile(":[0-9]+").search(message_parts[0]):
        if (second_part_lower.startswith("false") or second_part_lower.startswith("fp")) \
                and is_privileged(ev_room, ev_user_id):
            try:
                should_delete = True
                msg_id = int(message_parts[0][1:])
                msg_content = None
                msg_to_delete = wrap2.get_message(msg_id)
                if str(msg_to_delete.owner.id) == GlobalVars.smokeDetector_user_id[ev_room]:
                    msg_content = msg_to_delete.content_source
                if msg_content is not None:
                    site_post_id = fetch_post_id_and_site_from_msg_content(msg_content)
                    if site_post_id is None:
                        ev.message.reply("Could not register title as false positive.")
                        return
                    post_type = site_post_id[2]
                    add_false_positive((site_post_id[0], site_post_id[1]))
                    user_added = False
                    if message_parts[1].lower().startswith("falseu") or message_parts[1].lower().startswith("fpu"):
                        url_from_msg = fetch_owner_url_from_msg_content(msg_content)
                        user = get_user_from_url(url_from_msg)
                        add_whitelisted_user(user)
                        user_added = True
                    learned = False
                    if post_type == "question":
                        learned = bayesian_learn_title(fetch_title_from_msg_content(msg_content), "good")
                        if learned and user_added:
                            ev.message.reply("Registered question as false positive, whitelisted user and added title to Bayesian doctype 'good'.")
                        elif learned:
                            ev.message.reply("Registered question as false positive and added title to Bayesian doctype 'good'.")
                        else:
                            ev.message.reply("Registered question as false positive, but could not add title to Bayesian doctype 'good'.")
                    elif post_type == "answer":
                        if user_added:
                            ev.message.reply("Registered answer as false positive and whitelisted user.")
                        else:
                            ev.message.reply("Registered answer as false positive.")
                    if should_delete:
                        msg_to_delete.delete()
            except:
                pass  # couldn't delete message
        if (second_part_lower.startswith("true") or second_part_lower.startswith("tp")) \
                and is_privileged(ev_room, ev_user_id):
            try:
                msg_id = int(message_parts[0][1:])
                msg_content = None
                msg_true_positive = wrap2.get_message(msg_id)
                if str(msg_true_positive.owner.id) == GlobalVars.smokeDetector_user_id[ev_room]:
                    msg_content = msg_true_positive.content_source
                if msg_content is not None:
                    post_type = fetch_post_id_and_site_from_msg_content(msg_content)[2]
                    learned = False
                    user_added = False
                    if message_parts[1].lower().startswith("trueu") or message_parts[1].lower().startswith("tpu"):
                        url_from_msg = fetch_owner_url_from_msg_content(msg_content)
                        user = get_user_from_url(url_from_msg)
                        add_blacklisted_user(user)
                        user_added = True
                    if post_type == "question":
                        learned = bayesian_learn_title(fetch_title_from_msg_content(msg_content), "bad")
                        if learned and user_added:
                            ev.message.reply("Blacklisted user and registered question as true positive: added title to the Bayesian doctype 'bad'.")
                        elif learned:
                            ev.message.reply("Registered question as true positive: added title to the Bayesian doctype 'bad'.")
                        else:
                            ev.message.reply("Something went wrong when registering question as true positive.")
                    elif post_type == "answer":
                        if user_added:
                            ev.message.reply("Blacklisted user.")
                        else:
                            ev.message.reply("`true`/`tp` cannot be used for answers because their job is to add the title of the *question* to the Bayesian doctype 'bad'. If you want to blacklist the poster of the answer, use `trueu` or `tpu`.")
            except:
                pass
        if second_part_lower.startswith("ignore") and is_privileged(ev_room, ev_user_id):
            try:
                msg_id = int(message_parts[0][1:])
                msg_content = None
                msg_ignore = wrap2.get_message(msg_id)
                if(str(msg_ignore.owner.id) == GlobalVars.smokeDetector_user_id[ev_room]):
                    msg_content = msg_ignore.content_source
                if(msg_content is not None):
                    post_id_site = fetch_post_id_and_site_from_msg_content(msg_content)[0:2]
                    add_ignored_post(post_id_site)
                    ev.message.reply("Post ignored; alerts about it will no longer be posted.")
            except:
                pass
        if (second_part_lower == "delete" or second_part_lower == "remove" or second_part_lower == "gone") \
                and is_privileged(ev_room, ev_user_id):
            try:
                msg_id = int(message_parts[0][1:])
                msg_to_delete = wrap2.get_message(msg_id)
                if str(msg_to_delete.owner.id) == GlobalVars.smokeDetector_user_id[ev_room]:
                    msg_to_delete.delete()
            except:
                pass  # couldn't delete message
    if content_lower.startswith("!!/addblu") \
            and is_privileged(ev_room, ev_user_id):
        uid, val = get_user_from_list_command(content_lower)
        if uid > -1 and val != "":
            add_blacklisted_user((uid, val))
            ev.message.reply("User blacklisted (`%s` on `%s`)." % (uid, val))
        elif uid == -2:
            ev.message.reply("Error: %s" % val)
        else:
            ev.message.reply("Invalid format. Valid format: `!!/addblu profileurl` *or* `!!/addblu userid sitename`.")
    if content_lower.startswith("!!/rmblu") \
            and is_privileged(ev_room, ev_user_id):
        uid, val = get_user_from_list_command(content_lower)
        if uid > -1 and val != "":
            if remove_blacklisted_user((uid, val)):
                ev.message.reply("User removed from blacklist (`%s` on `%s`)." % (uid, val))
            else:
                ev.message.reply("User is not blacklisted.")
        elif uid == -2:
            ev.message.reply("Error: %s" % val)
        else:
            ev.message.reply("Invalid format. Valid format: `!!/rmblu profileurl` *or* `!!/rmblu userid sitename`.")
    if content_lower.startswith("!!/addwlu") \
            and is_privileged(ev_room, ev_user_id):
        uid, val = get_user_from_list_command(content_lower)
        if uid > -1 and val != "":
            add_whitelisted_user((uid, val))
            ev.message.reply("User whitelisted (`%s` on `%s`)." % (uid, val))
        elif uid == -2:
            ev.message.reply("Error: %s" % val)
        else:
            ev.message.reply("Invalid format. Valid format: `!!/addwlu profileurl` *or* `!!/addwlu userid sitename`.")
    if content_lower.startswith("!!/rmwlu") \
            and is_privileged(ev_room, ev_user_id):
        uid, val = get_user_from_list_command(content_lower)
        if uid != -1 and val != "":
            if remove_whitelisted_user((uid, val)):
                ev.message.reply("User removed from whitelist (`%s` on `%s`)." % (uid, val))
            else:
                ev.message.reply("User is not whitelisted.")
        elif uid == -2:
            ev.message.reply("Error: %s" % val)
        else:
            ev.message.reply("Invalid format. Valid format: `!!/rmwlu profileurl` *or* `!!/rmwlu userid sitename`.")
    if content_lower.startswith("!!/wut"):
        ev.message.reply("Whaddya mean, 'wut'? Humans...")
    if content_lower.startswith("!!/lick"):
        ev.message.reply("*licks ice cream cone*")
    if content_lower.startswith("!!/hats"):
        wb_end = datetime(2015, 1, 5, 0, 0, 0)
        now = datetime.utcnow()
        if wb_end > now:
            diff = wb_end - now
            hours, remainder = divmod(diff.seconds, 3600)
            minutes, seconds = divmod(remainder, 60)
            daystr = "days" if diff.days != 1 else "day"
            hourstr = "hours" if hours != 1 else "hour"
            minutestr = "minutes" if minutes != 1 else "minute"
            secondstr = "seconds" if seconds != 1 else "second"
            ev.message.reply("HATS ARE AWESOME. Winter Bash will end in %s %s, %s %s, %s %s and %s %s. :(" %
                             (diff.days, daystr, hours, hourstr, minutes, minutestr, seconds, secondstr))
        else:
            ev.message.reply("WINTERBASH IS OVER! :(")
    if content_lower.startswith("!!/alive"):
        if ev_room == GlobalVars.charcoal_room_id:
            ev.message.reply('Of course')
        elif ev_room == GlobalVars.meta_tavern_room_id:
            ev.message.reply(random.choice(['Yup', 'You doubt me?', 'Of course', '... did I miss something?',
                                            'plz send teh coffee',
                                            'Watching this endless list of new questions *never* gets boring',
                                            'Kinda sorta']))
    if content_lower.startswith("!!/rev"):
            ev.message.reply(
                '[' +
                GlobalVars.commit_with_author +
                '](https://github.com/Charcoal-SE/SmokeDetector/commit/' +
                GlobalVars.commit +
                ')'
            )
    if content_lower.startswith("!!/status"):
            now = datetime.utcnow()
            diff = now - UtcDate.startup_utc_date
            minutes, remainder = divmod(diff.seconds, 60)
            minutestr = "minutes" if minutes != 1 else "minute"
            ev.message.reply('Running since %s UTC (%s %s)' % (GlobalVars.startup_utc, minutes, minutestr))
    if content_lower.startswith("!!/reboot"):
        if is_privileged(ev_room, ev_user_id):
            post_message_in_room(ev_room, "Goodbye, cruel world")
            os._exit(5)
    if content_lower.startswith("!!/stappit"):
        if is_privileged(ev_room, ev_user_id):
            post_message_in_room(ev_room, "Goodbye, cruel world")
            os._exit(6)
    if content_lower.startswith("!!/master"):
        if is_privileged(ev_room, ev_user_id):
            ev.message.reply("Checking out to master and restarting...")
            os._exit(8)
    if content_lower.startswith("!!/clearbl"):
        if is_privileged(ev_room, ev_user_id):
            if os.path.isfile("blacklistedUsers.txt"):
                os.remove("blacklistedUsers.txt")
                GlobalVars.blacklisted_users = []
                ev.message.reply("Kaboom, blacklisted users cleared.")
            else:
                ev.message.reply("There are no blacklisted users at the moment.")
    if content_lower.startswith("!!/block"):
        if is_privileged(ev_room, ev_user_id):
            ev.message.reply("blocked")
            timeToBlock = ev.content[9:].strip()
            timeToBlock = int(timeToBlock) if timeToBlock else 0
            if 0 < timeToBlock < 14400:
                GlobalVars.blockedTime = time.time() + timeToBlock
            else:
                GlobalVars.blockedTime = time.time() + 900
    if content_lower.startswith("!!/unblock"):
        if is_privileged(ev_room, ev_user_id):
            GlobalVars.blockedTime = time.time()
            ev.message.reply("unblocked")
    if content_lower.startswith("!!/errorlogs"):
        if is_privileged(ev_room, ev_user_id):
            count = -1
            if len(message_parts) != 2:
                ev.message.reply("The !!/errorlogs command requires 1 argument.")
                return
            try:
                count = int(message_parts[1])
            except ValueError:
                pass
            if count == -1:
                ev.message.reply("Invalid argument.")
                return
            logs_part = fetch_lines_from_error_log(count)
            post_message_in_room(ev_room, logs_part, False)
    if content_lower.startswith("!!/pull"):
        if is_privileged(ev_room, ev_user_id):
            r = requests.get('https://api.github.com/repos/Charcoal-SE/SmokeDetector/git/refs/heads/master')
            latest_sha = r.json()["object"]["sha"]
            r = requests.get('https://api.github.com/repos/Charcoal-SE/SmokeDetector/commits/' + latest_sha + '/statuses')
            states = []
            for status in r.json():
                state = status["state"]
                states.append(state)
            if "success" in states:
                ev.message.reply("Pulling latest from master -- CI build passed.")
                os._exit(3)
            elif "error" in states or "failure" in states:
                ev.message.reply("CI build failed! :( Please check your commit.")
            elif "pending" in states or not states:
                ev.message.reply("CI build is still pending, wait until the build has finished and then pull again.")
    if content_lower.startswith("!!/help"):
        ev.message.reply("I'm [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector), a bot that detects spam and low-quality posts on the network and posts alerts to chat.")
    if content_lower.startswith("!!/apiquota"):
        ev.message.reply(GlobalVars.apiquota)
    if content_lower.startswith("!!/queuestatus"):
        ev.message.reply(GlobalVars.bodyfetcher.print_queue())
<|endoftext|>


<commit_before>'''
    census.py
    ---------

    This file collects ACS data.

    TO-DO: Establish the outputs needed from this data, and clean some of the
    raw data in this file.
'''
from temp_credentials import key
import requests
import pandas as pd

year = 2017 # Most recent year of 5 year estimates.
base = f'https://api.census.gov/data/{year}/acs/acs5'

fields = {
    'B01003_001E': 'total_population',
}

response = requests.get(base,
        params={
            'get': 'NAME,' + ','.join(fields.keys()),
            'for': 'tract:*',
            'in': 'state:11',
            'key': key,
            }
)
print(response)
data = response.json()
df = pd.DataFrame(data[1:], columns=data[0])
df = df.rename(columns=fields)
print(df.head())
<commit_msg>All fields accounted for census<commit_after>'''
    census.py
    ---------

    This file collects ACS data.

    TO-DO: Establish the outputs needed from this data, and clean some of the
    raw data in this file.
'''
from temp_credentials import key
import requests
import pandas as pd

year = 2017 # Most recent year of 5 year estimates.
base = f'https://api.census.gov/data/{year}/acs/acs5'

fields = {
    'B01003_001E': 'total_population',
    'B02001_003E': 'black_population',
    'B17020_002E': 'population_in_poverty',
    'B23025_002E': 'labor_force_population',
    'B16008_019E': 'foreign_born_population',
    'B09002_015E': 'single_mom_households',
    'B19025_001E': 'aggregate_household_income',
    'B25057_001E': 'lower_rent_quartile_in_dollars',
    'B25058_001E': 'median_rent_quartile_in_dollars',
    'B25059_001E': 'upper_rent_quartile_in_dollars',
}

response = requests.get(base,
        params={
            'get': 'NAME,' + ','.join(fields.keys()),
            'for': 'tract:*',
            'in': 'state:11',
            'key': key,
            }
)

if response.status_code == 200:
    data = response.json()
    df = pd.DataFrame(data[1:], columns=data[0])
    df = df.rename(columns=fields)
    print(df.head())
else:
    print("Could not query")
<|endoftext|>


<commit_before>import re

class MockCursor:
    def __init__(self, data):
        self.cursor_pos =0
        self.data = data

    def fetch(self, batch_size):
        batch = self.data[self.cursor_pos : self.cursor_pos + batch_size]
        self.cursor_pos += batch_size
        return batch

class MockPlPy:
    def __init__(self):
        self._reset()

    def _reset(self):
        self.infos = []
        self.notices = []
        self.debugs = []
        self.logs = []
        self.warnings = []
        self.errors = []
        self.fatals = []
        self.executes = []
        self.results = []
        self.prepares = []
        self.results = []

    def _define_result(self, query, result):
        pattern = re.compile(query, re.IGNORECASE | re.MULTILINE)
        self.results.append([pattern, result])

    def notice(self, msg):
        self.notices.append(msg)

    def info(self, msg):
        self.infos.append(msg)

    def cursor(self,query):
        data = self.execute(query)
        return MockCursor(data)

    def execute(self, query): # TODO: additional arguments
       for result in self.results:
          if result[0].match(query):
            return result[1]
       return []
<commit_msg>formatting<commit_after>import re

class MockCursor:
    def __init__(self, data):
        self.cursor_pos = 0
        self.data = data

    def fetch(self, batch_size):
        batch = self.data[self.cursor_pos : self.cursor_pos + batch_size]
        self.cursor_pos += batch_size
        return batch

class MockPlPy:
    def __init__(self):
        self._reset()

    def _reset(self):
        self.infos = []
        self.notices = []
        self.debugs = []
        self.logs = []
        self.warnings = []
        self.errors = []
        self.fatals = []
        self.executes = []
        self.results = []
        self.prepares = []
        self.results = []

    def _define_result(self, query, result):
        pattern = re.compile(query, re.IGNORECASE | re.MULTILINE)
        self.results.append([pattern, result])

    def notice(self, msg):
        self.notices.append(msg)

    def info(self, msg):
        self.infos.append(msg)

    def cursor(self, query):
        data = self.execute(query)
        return MockCursor(data)

    def execute(self, query): # TODO: additional arguments
       for result in self.results:
          if result[0].match(query):
            return result[1]
       return []
<|endoftext|>


<commit_before>#!/usr/bin/python
import couchdb, time
server = couchdb.Server()
db = server['node']
def publish_doc(name, doc_data):
    try:
        del db[name]
    except couchdb.http.ResourceNotFound as ex:
        print ex
    db[name] = doc_data
default_status = {'timestamp' :'',
                  'active':True,
                  'node_id': '',
                  'node_name':'',
                  'doc_count':'',
                  'install_time':time.asctime(),
                  'start_time':'',
                  'last_in_sync':'',
                  'in_sync_node':'',
                  'last_out_sync':'',
                  'out_sync_node':''  }
default_description = {'timestamp':        'string',
                       'active':        True,
                       'node_id':        'string',
                       'node_name':        'string',
                       'node_description':    'string',
                       'node_admin_url':    'string',
                       'network_id':        'string',
                       'network_name':     'string',
                       'network_description':    'string',
                       'network_admin_url':    'string',
                       'community_id':    'string' ,
                       'community_name':     'string',
                       'community_description':'string',
                       'community_admin_url':'string',
                       'policy_id':        'string',
                       'policy_version':    'string',
                       'gateway_node':    True,
                       'open_connect_source':True,
                       'open_connect_dest':    True,
                       'social_community':    True,}
default_services = {'timestamp':        'string',
                    'active':        True,
                    'node_id':        'string',
                    'node_name':        'string',
                    'services':[{'active':        True,
                                 'service_id':        'string',
                                 'service_type':    'string',
                                 'service_name':    'string',
                                 'service_description':    'string',
                                 'service_version':    'string',
                                 'service_endpoint':    'string',
                                 'service_auth':    'string',
                                 'service_data':    {}
                              }]}
default_policy = {'timestamp':        'string',
                  'active':        True,
                  'node_id':        'string',
                  'node_name':        'string',
                  'network_id':        'string',
                  'network_name':     'string',
                  'network_description':    'string',
                  'policy_id':        'string',
                  'policy_version':    'string',
                  'TTL':            0}
publish_doc('status',default_status)
publish_doc('policy',default_policy)
publish_doc('description',default_description)
publish_doc('services',default_services)

<commit_msg>set_initial creates DBs<commit_after>#!/usr/bin/python
import couchdb, time
server = couchdb.Server()
def create(name):
    try:
        server.create(name)
    except:
        print 'DB already exists'
create('node')
create('resource_data')
create('community')
create('network')
db = server['node']
def publish_doc(name, doc_data):
    try:
        del db[name]
    except couchdb.http.ResourceNotFound as ex:
        print ex
    db[name] = doc_data
default_status = {'timestamp' :'',
                  'active':True,
                  'node_id': '',
                  'node_name':'',
                  'doc_count':'',
                  'install_time':time.asctime(),
                  'start_time':'',
                  'last_in_sync':'',
                  'in_sync_node':'',
                  'last_out_sync':'',
                  'out_sync_node':''  }
default_description = {'timestamp':        'string',
                       'active':        True,
                       'node_id':        'string',
                       'node_name':        'string',
                       'node_description':    'string',
                       'node_admin_url':    'string',
                       'network_id':        'string',
                       'network_name':     'string',
                       'network_description':    'string',
                       'network_admin_url':    'string',
                       'community_id':    'string' ,
                       'community_name':     'string',
                       'community_description':'string',
                       'community_admin_url':'string',
                       'policy_id':        'string',
                       'policy_version':    'string',
                       'gateway_node':    True,
                       'open_connect_source':True,
                       'open_connect_dest':    True,
                       'social_community':    True,}
default_services = {'timestamp':        'string',
                    'active':        True,
                    'node_id':        'string',
                    'node_name':        'string',
                    'services':[{'active':        True,
                                 'service_id':        'string',
                                 'service_type':    'string',
                                 'service_name':    'string',
                                 'service_description':    'string',
                                 'service_version':    'string',
                                 'service_endpoint':    'string',
                                 'service_auth':    'string',
                                 'service_data':    {}
                              }]}
default_policy = {'timestamp':        'string',
                  'active':        True,
                  'node_id':        'string',
                  'node_name':        'string',
                  'network_id':        'string',
                  'network_name':     'string',
                  'network_description':    'string',
                  'policy_id':        'string',
                  'policy_version':    'string',
                  'TTL':            0}
publish_doc('status',default_status)
publish_doc('policy',default_policy)
publish_doc('description',default_description)
publish_doc('services',default_services)

<|endoftext|>


<commit_before>from collections import deque
from copy import copy
from time import time
from pycounters.base import THREAD_DISPATCHER, BaseListener
from pycounters.counters.base import BaseCounter, AutoDispatch, Timer, TimerMixin, TriggerMixin, AccumulativeCounterValue, AverageCounterValue

__author__ = 'boaz'

class EventCounter(TriggerMixin, BaseCounter):
    """ Counts the number of times an end event has fired.
    """

    def __init__(self, name, events=None):
        self.value = None
        super(EventCounter, self).__init__(name, events=events)

    def _get_value(self):
        return AccumulativeCounterValue(self.value)

    def _report_event_value(self, name, value):

        if self.value:
            self.value += value
        else:
            self.value = long(value)

    def _clear(self):
        self.value = 0L

class TotalCounter(AutoDispatch, BaseCounter):
    """ Counts the total of events' values.
    """

    def __init__(self, name, events=None):
        self.value = None
        super(TotalCounter, self).__init__(name, events=events)

    def _get_value(self):
        return AccumulativeCounterValue(self.value)

    def _report_event_value(self, name, value):

        if self.value:
            self.value += value
        else:
            self.value = long(value)

    def _clear(self):
        self.value = 0L

class AverageWindowCounter(AutoDispatch, BaseCounter):
    """ Calculates a running average of arbitrary values """

    def __init__(self, name, window_size=300.0, events=None):
        super(AverageWindowCounter, self).__init__(name, events=events)
        self.window_size = window_size
        self.values = deque()
        self.times = deque()

    def _clear(self):
        self.values.clear()
        self.times.clear()

    def _get_value(self):
        self._trim_window()
        if not self.values:
            v = None
        else:
            v = sum(self.values, 0.0) / len(self.values)

        return AverageCounterValue(v, len(self.values))

    def _trim_window(self):
        window_limit = self._get_current_time() - self.window_size
        # trim old data
        while self.times and self.times[0] < window_limit:
            self.times.popleft()
            self.values.popleft()

    def _report_event_value(self, param, value):
        self._trim_window()
        self.values.append(value)
        self.times.append(self._get_current_time())

    def _get_current_time(self):
        return time()

class FrequencyCounter(TriggerMixin, AverageWindowCounter):
    """ Counts the frequency of end events in the last five minutes
    """

    def _get_value(self):
        self._trim_window()
        if not self.values or len(self.values) < 1:
            return AccumulativeCounterValue(0.0)
        return AccumulativeCounterValue(sum(self.values, 0.0) / (self._get_current_time() - self.times[0]))

class AverageTimeCounter(TimerMixin, AverageWindowCounter):
    """ Counts the average time between start and end events
    """

    pass

class ValueAccumulator(AutoDispatch, BaseCounter):
    """ Captures all named values it gets and accumulates them. Also allows rethrowing them, prefixed with their name."""

    def __init__(self, name, events=None):
        self.accumulated_values = dict()
        # forces the object not to accumulate values. Used when the object itself is raising events
        self._ignore_values = False

        super(ValueAccumulator, self).__init__(name, events=events)

    def _report_event_value(self, name, value):
        if self._ignore_values:
            return
        cur_value = self.accumulated_values.get(name)
        if cur_value:
            cur_value += value
        else:
            cur_value = value
        self.accumulated_values[name] = cur_value

    def _get_value(self):
        return copy(self.accumulated_values)

    def _clear(self):
        self.accumulated_values.clear()

    def raise_value_events(self, clear=False):
        """ raises accumuated values as value events. """
        with self.lock:
            self._ignore_values = True
            try:
                for k, v in self.accumulated_values.iteritems():
                    THREAD_DISPATCHER.dispatch_event(self.name + "." + k, "value", v)
            finally:
                self._ignore_values = True

            if clear:
                self._clear()

class ThreadTimeCategorizer(BaseListener):
    """ A class to divide the time spent by thread across multiple categories. Categories are mutually exclusive. """

    def __init__(self, name, categories, timer_class=Timer):
        super(ThreadTimeCategorizer, self).__init__()
        self.name = name
        self.category_timers = dict()
        self.timer_stack = list()  # a list of strings of paused timers
        for cat in categories:
            self.category_timers[cat] = timer_class()

    def get_times(self):
        ret = []
        for k, v in self.category_timers.iteritems():
            ret.append((self.name + "." + k, v.get_accumulated_time()))
        return ret

    def report_event(self, name, property, param):
        if property == "start":
            cat_timer = self.category_timers.get(name)
            if not cat_timer:
                return

            if self.timer_stack:
                self.timer_stack[-1].pause()

            cat_timer.start()
            self.timer_stack.append(cat_timer)

        elif property == "end":
            cat_timer = self.category_timers.get(name)  # if all went well it is there...
            if not cat_timer:
                return
            cat_timer.pause()
            self.timer_stack.pop()
            if self.timer_stack:
                self.timer_stack[-1].start()  # continute last paused timer

    def raise_value_events(self, clear=False):
        """ raises category total time as value events. """
        for k, v in self.get_times():
            THREAD_DISPATCHER.dispatch_event(k, "value", v)

        if clear:
            self.category_timers.clear()
<commit_msg>window counter<commit_after>from collections import deque
from copy import copy
from time import time
from pycounters.base import THREAD_DISPATCHER, BaseListener
from pycounters.counters.base import BaseCounter, AutoDispatch, Timer, TimerMixin, TriggerMixin, AccumulativeCounterValue, AverageCounterValue

__author__ = 'boaz'

class EventCounter(TriggerMixin, BaseCounter):
    """ Counts the number of times an end event has fired.
    """

    def __init__(self, name, events=None):
        self.value = None
        super(EventCounter, self).__init__(name, events=events)

    def _get_value(self):
        return AccumulativeCounterValue(self.value)

    def _report_event_value(self, name, value):

        if self.value:
            self.value += value
        else:
            self.value = long(value)

    def _clear(self):
        self.value = 0L

class TotalCounter(AutoDispatch, BaseCounter):
    """ Counts the total of events' values.
    """

    def __init__(self, name, events=None):
        self.value = None
        super(TotalCounter, self).__init__(name, events=events)

    def _get_value(self):
        return AccumulativeCounterValue(self.value)

    def _report_event_value(self, name, value):

        if self.value:
            self.value += value
        else:
            self.value = long(value)

    def _clear(self):
        self.value = 0L

class AverageWindowCounter(AutoDispatch, BaseCounter):
    """ Calculates a running average of arbitrary values """

    def __init__(self, name, window_size=300.0, events=None):
        super(AverageWindowCounter, self).__init__(name, events=events)
        self.window_size = window_size
        self.values = deque()
        self.times = deque()

    def _clear(self):
        self.values.clear()
        self.times.clear()

    def _get_value(self):
        self._trim_window()
        if not self.values:
            v = None
        else:
            v = sum(self.values, 0.0) / len(self.values)

        return AverageCounterValue(v, len(self.values))

    def _trim_window(self):
        window_limit = self._get_current_time() - self.window_size
        # trim old data
        while self.times and self.times[0] < window_limit:
            self.times.popleft()
            self.values.popleft()

    def _report_event_value(self, param, value):
        self._trim_window()
        self.values.append(value)
        self.times.append(self._get_current_time())

    def _get_current_time(self):
        return time()

class FrequencyCounter(TriggerMixin, AverageWindowCounter):
    """ Counts the frequency of end events in the last five minutes
    """

    def _get_value(self):
        self._trim_window()
        if not self.values or len(self.values) < 1:
            return AccumulativeCounterValue(0.0)
        return AccumulativeCounterValue(sum(self.values, 0.0) / (self._get_current_time() - self.times[0]))

class WindowCounter(TriggerMixin, AverageWindowCounter):
    """ Counts number of events in window """
    def _get_value(self):
        self._trim_window()
        if not self.values or len(self.values) < 1:
            return AccumulativeCounterValue(0.0)
        return AccumulativeCounterValue(sum(self.values, 0.0))

    def get_oldest_time(self):
        with self.lock:
            if not self.times:
                return None
            return self.times[0]

class AverageTimeCounter(TimerMixin, AverageWindowCounter):
    """ Counts the average time between start and end events
    """
    pass

class ValueAccumulator(AutoDispatch, BaseCounter):
    """ Captures all named values it gets and accumulates them. Also allows rethrowing them, prefixed with their name."""

    def __init__(self, name, events=None):
        self.accumulated_values = dict()
        # forces the object not to accumulate values. Used when the object itself is raising events
        self._ignore_values = False

        super(ValueAccumulator, self).__init__(name, events=events)

    def _report_event_value(self, name, value):
        if self._ignore_values:
            return
        cur_value = self.accumulated_values.get(name)
        if cur_value:
            cur_value += value
        else:
            cur_value = value
        self.accumulated_values[name] = cur_value

    def _get_value(self):
        return copy(self.accumulated_values)

    def _clear(self):
        self.accumulated_values.clear()

    def raise_value_events(self, clear=False):
        """ raises accumuated values as value events. """
        with self.lock:
            self._ignore_values = True
            try:
                for k, v in self.accumulated_values.iteritems():
                    THREAD_DISPATCHER.dispatch_event(self.name + "." + k, "value", v)
            finally:
                self._ignore_values = True

            if clear:
                self._clear()

class ThreadTimeCategorizer(BaseListener):
    """ A class to divide the time spent by thread across multiple categories. Categories are mutually exclusive. """

    def __init__(self, name, categories, timer_class=Timer):
        super(ThreadTimeCategorizer, self).__init__()
        self.name = name
        self.category_timers = dict()
        self.timer_stack = list()  # a list of strings of paused timers
        for cat in categories:
            self.category_timers[cat] = timer_class()

    def get_times(self):
        ret = []
        for k, v in self.category_timers.iteritems():
            ret.append((self.name + "." + k, v.get_accumulated_time()))
        return ret

    def report_event(self, name, property, param):
        if property == "start":
            cat_timer = self.category_timers.get(name)
            if not cat_timer:
                return

            if self.timer_stack:
                self.timer_stack[-1].pause()

            cat_timer.start()
            self.timer_stack.append(cat_timer)

        elif property == "end":
            cat_timer = self.category_timers.get(name)  # if all went well it is there...
            if not cat_timer:
                return
            cat_timer.pause()
            self.timer_stack.pop()
            if self.timer_stack:
                self.timer_stack[-1].start()  # continute last paused timer

    def raise_value_events(self, clear=False):
        """ raises category total time as value events. """
        for k, v in self.get_times():
            THREAD_DISPATCHER.dispatch_event(k, "value", v)

        if clear:
            self.category_timers.clear()
<|endoftext|>


<commit_before>from itertools import chain

from collections import (
    defaultdict,
    Iterable,
)

from django.http import HttpResponseNotAllowed

__version__ = '0.1.0'

class Handler(object):
    """Container for views.

    :param decorators: (optional) list of decorators that will be applied
        to each endpoint.
    """

    def __init__(self, decorators=None):
        self._decorators = decorators or []
        self._views = defaultdict(dict)
        self._pre_hooks = defaultdict(list)
        self._post_hooks = defaultdict(list)
        self._invalid_endpoint_names = dir(self)

    def add_view(self, method, endpoint_name, view):
        """Adds a view to handler.

        :param method: HTTP method to be handled by the view
        :param endpoint_name: name of endpoint to associate the view with
        :param view: function to be used for requests handling
        """

        self._ensure_endpoint_exists(endpoint_name)
        self._views[endpoint_name][method.upper()] = view

    def _ensure_endpoint_exists(self, endpoint_name):
        self._validate_endpoint_name(endpoint_name)

        if endpoint_name not in self._views:
            self._add_endpoint(endpoint_name)

    def _validate_endpoint_name(self, endpoint_name):
        if endpoint_name in self._invalid_endpoint_names:
            raise ValueError('Invalid endpoint name {}'.format(endpoint_name))

    def _add_endpoint(self, endpoint_name):
        def endpoint(request, *args, **kwargs):
            for hook in self._get_pre_hooks(endpoint_name):
                hook(request, *args, **kwargs)

            try:
                view = self._views[endpoint_name][request.method]
            except KeyError:
                allowed_methods = self._views[endpoint_name].keys()
                response = HttpResponseNotAllowed(allowed_methods)
            else:
                response = view(request, *args, **kwargs)

            for hook in self._get_post_hooks(endpoint_name):
                hook(request, *args, **kwargs)

            return response

        for decorator in reversed(self._decorators):
            endpoint = decorator(endpoint)

        setattr(self, endpoint_name, endpoint)

    def _get_pre_hooks(self, endpoint_name):
        return chain(self._pre_hooks[None], self._pre_hooks[endpoint_name])

    def _get_post_hooks(self, endpoint_name):
        return chain(self._post_hooks[None], self._post_hooks[endpoint_name])

    def _register(self, method, endpoint_name):
        def decorator(view):
            self.add_view(method, endpoint_name, view)
            return view
        return decorator

    def get(self, endpoint_name):
        """Decorates a view to use it for handling of GET requests.

        :param endpoint_name: name of endpoint for given view.
        """
        return self._register('GET', endpoint_name)

    def head(self, endpoint_name):
        """Decorates a view to use it for handling of HEAD requests.

        :param endpoint_name: name of endpoint for given view.
        """

        return self._register('HEAD', endpoint_name)

    def options(self, endpoint_name):
        """Decorates a view to use it for handling of OPTIONS requests.

        :param endpoint_name: name of endpoint for given view.
        """

        return self._register('OPTIONS', endpoint_name)

    def post(self, endpoint_name):
        """Decorates a view to use it for handling of POST requests.

        :param endpoint_name: name of endpoint`.
        """

        return self._register('POST', endpoint_name)

    def put(self, endpoint_name):
        """Decorates a view to use it for handling of PUT requests.

        :param endpoint_name: name of endpoint.
        """

        return self._register('PUT', endpoint_name)

    def patch(self, endpoint_name):
        """Decorates a view to use it for handling of PATCH requests.

        :param endpoint_name: name of endpoint.
        """

        return self._register('PATCH', endpoint_name)

    def delete(self, endpoint_name):
        """Decorates a view to use it for handling of DELETE requests.

        :param endpoint_name: name of endpoint.
        """

        return self._register('DELETE', endpoint_name)

    def before(self, target):
        """Decorates a function to call it before views.

        :param target: (optional) name of endpoint. Without it the
            hook will be added for all endpoints.
        """
        if callable(target):
            endpoint_name = None
        else:
            endpoint_name = target

        def decorator(view):
            self.add_pre_hook(endpoint_name, view)
            return view

        if endpoint_name is None:
            return decorator(target)

        return decorator

    def add_pre_hook(self, endpoint_name, hook):
        """Adds a function to call it before endpoint's views.

        :param endpoint_name: name of handler endpoint
        :param hook: function that should be called after endpoint's views
        """
        self._pre_hooks[endpoint_name].append(hook)

    def after(self, target):
        """Decorates a function to call it after views.

        :param target: (optional) name of endpoint. Without it the
            hook will be added for all endpoints.
        """
        if callable(target):
            endpoint_name = None
        else:
            endpoint_name = target

        def decorator(view):
            self.add_post_hook(endpoint_name, view)
            return view

        if endpoint_name is None:
            return decorator(target)

        return decorator

    def add_post_hook(self, endpoint_name, hook):
        """Adds a function to call it after endpoint's views.

        :param endpoint_name: name of handler endpoint
        :param hook: function that should be called after endpoint's views
        """
        self._post_hooks[endpoint_name].append(hook)

    def decorate(self, endpoint_name, decorator):
        """Decorates an endpoint.

        :param endpoint_name: an endpoint to decorate.
        :param decorator: one decorator or iterable with decorators.
        """
        endpoint = getattr(self, endpoint_name)

        if isinstance(decorator, Iterable):
            for dec in reversed(decorator):
                endpoint = dec(endpoint)
        else:
            endpoint = decorator(endpoint)

        setattr(self, endpoint_name, endpoint)
<commit_msg>change version<commit_after>from itertools import chain

from collections import (
    defaultdict,
    Iterable,
)

from django.http import HttpResponseNotAllowed

__version__ = '0.1.1'

class Handler(object):
    """Container for views.

    :param decorators: (optional) list of decorators that will be applied
        to each endpoint.
    """

    def __init__(self, decorators=None):
        self._decorators = decorators or []
        self._views = defaultdict(dict)
        self._pre_hooks = defaultdict(list)
        self._post_hooks = defaultdict(list)
        self._invalid_endpoint_names = dir(self)

    def add_view(self, method, endpoint_name, view):
        """Adds a view to handler.

        :param method: HTTP method to be handled by the view
        :param endpoint_name: name of endpoint to associate the view with
        :param view: function to be used for requests handling
        """

        self._ensure_endpoint_exists(endpoint_name)
        self._views[endpoint_name][method.upper()] = view

    def _ensure_endpoint_exists(self, endpoint_name):
        self._validate_endpoint_name(endpoint_name)

        if endpoint_name not in self._views:
            self._add_endpoint(endpoint_name)

    def _validate_endpoint_name(self, endpoint_name):
        if endpoint_name in self._invalid_endpoint_names:
            raise ValueError('Invalid endpoint name {}'.format(endpoint_name))

    def _add_endpoint(self, endpoint_name):
        def endpoint(request, *args, **kwargs):
            for hook in self._get_pre_hooks(endpoint_name):
                hook(request, *args, **kwargs)

            try:
                view = self._views[endpoint_name][request.method]
            except KeyError:
                allowed_methods = self._views[endpoint_name].keys()
                response = HttpResponseNotAllowed(allowed_methods)
            else:
                response = view(request, *args, **kwargs)

            for hook in self._get_post_hooks(endpoint_name):
                hook(request, *args, **kwargs)

            return response

        for decorator in reversed(self._decorators):
            endpoint = decorator(endpoint)

        setattr(self, endpoint_name, endpoint)

    def _get_pre_hooks(self, endpoint_name):
        return chain(self._pre_hooks[None], self._pre_hooks[endpoint_name])

    def _get_post_hooks(self, endpoint_name):
        return chain(self._post_hooks[None], self._post_hooks[endpoint_name])

    def _register(self, method, endpoint_name):
        def decorator(view):
            self.add_view(method, endpoint_name, view)
            return view
        return decorator

    def get(self, endpoint_name):
        """Decorates a view to use it for handling of GET requests.

        :param endpoint_name: name of endpoint for given view.
        """
        return self._register('GET', endpoint_name)

    def head(self, endpoint_name):
        """Decorates a view to use it for handling of HEAD requests.

        :param endpoint_name: name of endpoint for given view.
        """

        return self._register('HEAD', endpoint_name)

    def options(self, endpoint_name):
        """Decorates a view to use it for handling of OPTIONS requests.

        :param endpoint_name: name of endpoint for given view.
        """

        return self._register('OPTIONS', endpoint_name)

    def post(self, endpoint_name):
        """Decorates a view to use it for handling of POST requests.

        :param endpoint_name: name of endpoint`.
        """

        return self._register('POST', endpoint_name)

    def put(self, endpoint_name):
        """Decorates a view to use it for handling of PUT requests.

        :param endpoint_name: name of endpoint.
        """

        return self._register('PUT', endpoint_name)

    def patch(self, endpoint_name):
        """Decorates a view to use it for handling of PATCH requests.

        :param endpoint_name: name of endpoint.
        """

        return self._register('PATCH', endpoint_name)

    def delete(self, endpoint_name):
        """Decorates a view to use it for handling of DELETE requests.

        :param endpoint_name: name of endpoint.
        """

        return self._register('DELETE', endpoint_name)

    def before(self, target):
        """Decorates a function to call it before views.

        :param target: (optional) name of endpoint. Without it the
            hook will be added for all endpoints.
        """
        if callable(target):
            endpoint_name = None
        else:
            endpoint_name = target

        def decorator(view):
            self.add_pre_hook(endpoint_name, view)
            return view

        if endpoint_name is None:
            return decorator(target)

        return decorator

    def add_pre_hook(self, endpoint_name, hook):
        """Adds a function to call it before endpoint's views.

        :param endpoint_name: name of handler endpoint
        :param hook: function that should be called after endpoint's views
        """
        self._pre_hooks[endpoint_name].append(hook)

    def after(self, target):
        """Decorates a function to call it after views.

        :param target: (optional) name of endpoint. Without it the
            hook will be added for all endpoints.
        """
        if callable(target):
            endpoint_name = None
        else:
            endpoint_name = target

        def decorator(view):
            self.add_post_hook(endpoint_name, view)
            return view

        if endpoint_name is None:
            return decorator(target)

        return decorator

    def add_post_hook(self, endpoint_name, hook):
        """Adds a function to call it after endpoint's views.

        :param endpoint_name: name of handler endpoint
        :param hook: function that should be called after endpoint's views
        """
        self._post_hooks[endpoint_name].append(hook)

    def decorate(self, endpoint_name, decorator):
        """Decorates an endpoint.

        :param endpoint_name: an endpoint to decorate.
        :param decorator: one decorator or iterable with decorators.
        """
        endpoint = getattr(self, endpoint_name)

        if isinstance(decorator, Iterable):
            for dec in reversed(decorator):
                endpoint = dec(endpoint)
        else:
            endpoint = decorator(endpoint)

        setattr(self, endpoint_name, endpoint)
<|endoftext|>


<commit_before>
class Tree(object):
    def __init__(self, value, parent=None):
        self.parent = parent
        self.value = value
        self.children = []

    def add(self, value):
        new = Tree(value, parent=self)
        self.children.append(new)
        return new

    def search(self):
        if self.parent is not None:
            parent_value = self.parent.value
        else:
            parent_value = None

        atoms = []
        for bond in self.value.bonds:
            for atom in bond.atoms:
                if atom != self.value and atom != parent_value:
                    node = self.add(atom)
                    atoms.append(node)
        return atoms

    def get_common_parent(self, node):
        if self == node:
            return self.parent

        visited = []
        nodes = [self, node]
        i = 0
        stop = False
        while nodes[0] != nodes[1]:
            if nodes[i] in visited:
                break
            if nodes[i].parent is not None:
                visited.append(nodes[i])
                nodes[i] = nodes[i].parent
            i = (i + 1) % 2

        parent = nodes[i]
        return parent

    def get_common_set(self, parent, node):
        nodes = [self]
        while nodes[-1].parent != parent:
            nodes.append(nodes[-1].parent)

        temp_nodes = [node]
        while temp_nodes[-1].parent != parent:
            temp_nodes.append(temp_nodes[-1].parent)

        return nodes + [parent] + temp_nodes[::-1]

def breadth_first_search(molecule):
    point = molecule.atoms[0]
    tree = Tree(point)
    visited = [tree.value]
    points = tree.search()

    links = []
    while points:
        point = points.pop(0)
        if point.value not in visited:
            visited.append(point.value)
            points.extend(point.search())
        else:
            link = sorted([point.value, point.parent.value])
            links.append((link, point))
    return links, tree

def depth_first_search(molecule):
    # does not work with the cycle detection
    point = molecule.atoms[0]
    tree = Tree(point)
    visited = [tree.value]
    points = tree.search()

    links = []
    while points:
        point = points.pop()
        if point.value not in visited:
            visited.append(point.value)
            points.extend(point.search())
        else:
            link = sorted([point.value, point.parent.value])
            links.append((link, point))
    return links, tree

def get_cycles(links, tree):
    sorted_links = sorted(links, key=lambda y: [x.id for x in y[0]])
    cycles = []
    link_nodes = []
    for i in xrange(len(sorted_links)/2):
        first = sorted_links[2*i][1].parent
        second = sorted_links[2*i+1][1].parent
        link_nodes.extend([first, second])

        parent = first.get_common_parent(second)
        common_set = first.get_common_set(parent, second)
        temp = [x for x in common_set]
        cycles.append(temp)
    return cycles, link_nodes

def prune_cycles(cycles, link_nodes):
    link_set = set(link_nodes)
    final = []
    for cycle in cycles:
        temp = [x in link_set for x in cycle]
        if sum(temp) <= 2:
            final.append(cycle)
            continue

        # [Link1, 0, Link2, 1, 2, Link2, Link1]
        # goes to [Link1, 0, Link2, Link2, Link1]
        start = temp.index(True, 1) + 1     # +1 include the second True value
        end = temp[::-1].index(True, 1) + 1
        final.append(cycle[:start] + cycle[-end:])
    return final

def get_fused_cycles(cycles):
    cycle_sets = [set([x.value.id for x in y]) for y in cycles]
    sets = []
    used = []
    notused = []
    for i, cycle1 in enumerate(cycle_sets):
        temp = [i]
        if i in used:
            continue
        full_cycle = cycle1
        for j, cycle2 in enumerate(cycle_sets):
            if i >= j:
                continue
            if full_cycle & cycle2:
                full_cycle |= cycle2
                temp.append(j)
        if len(temp) > 1:
            sets.append(temp)
            used.extend(temp)
        else:
            notused.append(i)

    temp1 = [[cycles[x] for x in y] for y in sets]
    temp2 = [[cycles[x]] for x in notused]
    return temp1 + temp2

def sort_fused_cycles(cycles):
    sorted_cycles = []
    for fused_cycle in cycles:
        means = []
        if len(fused_cycle) == 1:
            sorted_cycles.append(fused_cycle)
            continue

        # max of three rings, maybe generallize later
        sets = [set([x.value.id for x in ring]) for ring in fused_cycle]

        if len(sets) == 2:
            if len(sets[0]) >= len(sets[1]):
                ordering = [0, 1]
            else:
                ordering = [1, 0]
        else:
            if not (sets[0] & sets[1]):
                ordering = [0, 2, 1]
            else:
                if len(sets[0]) == len(sets[2]):
                    ordering = [0, 1, 2]
                else:
                    ordering  = [2, 0, 1]
        temp = [fused_cycle[i] for i in ordering]
        sorted_cycles.append(temp)
    return sorted_cycles

def identify_cycle_types(cycles):
    types = []
    for fused_cycle in cycles:
        lengths = [len(x) for x in fused_cycle]
        if len(lengths) == 3:
            if lengths == [5, 6, 5]:
                types.append("DCORE")
            elif lengths == [6, 5, 6]:
                types.append("7")
            else:
                print lengths
                types.append("10")
        elif len(lengths) == 2:
            if lengths == [6, 5]:
                types.append("SCORE")
            else:
                types.append("9")
        else:
            types.append("4568")
    return types
<commit_msg>Remove debug print statement<commit_after>
class Tree(object):
    def __init__(self, value, parent=None):
        self.parent = parent
        self.value = value
        self.children = []

    def add(self, value):
        new = Tree(value, parent=self)
        self.children.append(new)
        return new

    def search(self):
        if self.parent is not None:
            parent_value = self.parent.value
        else:
            parent_value = None

        atoms = []
        for bond in self.value.bonds:
            for atom in bond.atoms:
                if atom != self.value and atom != parent_value:
                    node = self.add(atom)
                    atoms.append(node)
        return atoms

    def get_common_parent(self, node):
        if self == node:
            return self.parent

        visited = []
        nodes = [self, node]
        i = 0
        stop = False
        while nodes[0] != nodes[1]:
            if nodes[i] in visited:
                break
            if nodes[i].parent is not None:
                visited.append(nodes[i])
                nodes[i] = nodes[i].parent
            i = (i + 1) % 2

        parent = nodes[i]
        return parent

    def get_common_set(self, parent, node):
        nodes = [self]
        while nodes[-1].parent != parent:
            nodes.append(nodes[-1].parent)

        temp_nodes = [node]
        while temp_nodes[-1].parent != parent:
            temp_nodes.append(temp_nodes[-1].parent)

        return nodes + [parent] + temp_nodes[::-1]

def breadth_first_search(molecule):
    point = molecule.atoms[0]
    tree = Tree(point)
    visited = [tree.value]
    points = tree.search()

    links = []
    while points:
        point = points.pop(0)
        if point.value not in visited:
            visited.append(point.value)
            points.extend(point.search())
        else:
            link = sorted([point.value, point.parent.value])
            links.append((link, point))
    return links, tree

def depth_first_search(molecule):
    # does not work with the cycle detection
    point = molecule.atoms[0]
    tree = Tree(point)
    visited = [tree.value]
    points = tree.search()

    links = []
    while points:
        point = points.pop()
        if point.value not in visited:
            visited.append(point.value)
            points.extend(point.search())
        else:
            link = sorted([point.value, point.parent.value])
            links.append((link, point))
    return links, tree

def get_cycles(links, tree):
    sorted_links = sorted(links, key=lambda y: [x.id for x in y[0]])
    cycles = []
    link_nodes = []
    for i in xrange(len(sorted_links)/2):
        first = sorted_links[2*i][1].parent
        second = sorted_links[2*i+1][1].parent
        link_nodes.extend([first, second])

        parent = first.get_common_parent(second)
        common_set = first.get_common_set(parent, second)
        temp = [x for x in common_set]
        cycles.append(temp)
    return cycles, link_nodes

def prune_cycles(cycles, link_nodes):
    link_set = set(link_nodes)
    final = []
    for cycle in cycles:
        temp = [x in link_set for x in cycle]
        if sum(temp) <= 2:
            final.append(cycle)
            continue

        # [Link1, 0, Link2, 1, 2, Link2, Link1]
        # goes to [Link1, 0, Link2, Link2, Link1]
        start = temp.index(True, 1) + 1     # +1 include the second True value
        end = temp[::-1].index(True, 1) + 1
        final.append(cycle[:start] + cycle[-end:])
    return final

def get_fused_cycles(cycles):
    cycle_sets = [set([x.value.id for x in y]) for y in cycles]
    sets = []
    used = []
    notused = []
    for i, cycle1 in enumerate(cycle_sets):
        temp = [i]
        if i in used:
            continue
        full_cycle = cycle1
        for j, cycle2 in enumerate(cycle_sets):
            if i >= j:
                continue
            if full_cycle & cycle2:
                full_cycle |= cycle2
                temp.append(j)
        if len(temp) > 1:
            sets.append(temp)
            used.extend(temp)
        else:
            notused.append(i)

    temp1 = [[cycles[x] for x in y] for y in sets]
    temp2 = [[cycles[x]] for x in notused]
    return temp1 + temp2

def sort_fused_cycles(cycles):
    sorted_cycles = []
    for fused_cycle in cycles:
        means = []
        if len(fused_cycle) == 1:
            sorted_cycles.append(fused_cycle)
            continue

        # max of three rings, maybe generallize later
        sets = [set([x.value.id for x in ring]) for ring in fused_cycle]

        if len(sets) == 2:
            if len(sets[0]) >= len(sets[1]):
                ordering = [0, 1]
            else:
                ordering = [1, 0]
        else:
            if not (sets[0] & sets[1]):
                ordering = [0, 2, 1]
            else:
                if len(sets[0]) == len(sets[2]):
                    ordering = [0, 1, 2]
                else:
                    ordering  = [2, 0, 1]
        temp = [fused_cycle[i] for i in ordering]
        sorted_cycles.append(temp)
    return sorted_cycles

def identify_cycle_types(cycles):
    types = []
    for fused_cycle in cycles:
        lengths = [len(x) for x in fused_cycle]
        if len(lengths) == 3:
            if lengths == [5, 6, 5]:
                types.append("DCORE")
            elif lengths == [6, 5, 6]:
                types.append("7")
            else:
                types.append("10")
        elif len(lengths) == 2:
            if lengths == [6, 5]:
                types.append("SCORE")
            else:
                types.append("9")
        else:
            types.append("4568")
    return types<|endoftext|>


<commit_before># Setup for machines that only need to queue jobs.
import os
from setuptools import setup

def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

setup(
    name='UCLDC Harvester',
    version='0.8.1',
    py_modules=['harvester.config', ],
    include_package_data=True,
    license='BSD License - see LICENSE file',
    description='Harvester installed for queuing jobs for the UCLDC project',
    long_description=read('README.md'),
    author='Mark Redar',
    author_email='mark.redar@ucop.edu',
    classifiers=[
        'Environment :: Web Environment',
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Operating System :: Linux',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.7',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
    install_requires=[
        'requests==2.11.1',
        'Logbook==0.6.0',
        'redis==2.10.1',
        'rq',
        ],
    test_suite='test',
    tests_require=['mock>=1.0.1', 'httpretty==0.8.3', ],
)
<commit_msg>Need collection_registry_client module installed<commit_after># Setup for machines that only need to queue jobs.
import os
from setuptools import setup

def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

setup(
    name='UCLDC Harvester',
    version='0.8.1',
    py_modules=['harvester.config', 'harvester.collection_registry_client'],
    include_package_data=True,
    license='BSD License - see LICENSE file',
    description='Harvester installed for queuing jobs for the UCLDC project',
    long_description=read('README.md'),
    author='Mark Redar',
    author_email='mark.redar@ucop.edu',
    classifiers=[
        'Environment :: Web Environment',
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Operating System :: Linux',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.7',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
    install_requires=[
        'requests==2.11.1',
        'Logbook==0.6.0',
        'redis==2.10.1',
        'rq',
        ],
    test_suite='test',
    tests_require=['mock>=1.0.1', 'httpretty==0.8.3', ],
)
<|endoftext|>