<commit_before>from ummeli.api.models import (Certificate, Language, WorkExperience,
    Reference, CurriculumVitae)
from django.forms import (ModelForm, CheckboxInput,  Form, EmailField,
                                            RegexField,  CharField,  BooleanField,  IntegerField)
from django.forms.forms import BoundField
from django.utils.safestring import mark_safe
from django.utils.html import conditional_escape
from django.utils.encoding import force_unicode

def format_errors_as_pml(self):
    bf_errors = []
    for name,  field in self.fields.items():
        bf = BoundField(self, field, name)

        if(bf.errors):
            for error in bf.errors:
                bf_error = u'\n'.join([u'<TEXT position="ABOVE"><color value="red">%(label_name)s - %(error_message)s</color></TEXT>
'
                            % {
                            'label_name': conditional_escape(force_unicode(bf.label)),
                            'error_message': conditional_escape(force_unicode(error))
                            }])
                bf_errors.append(mark_safe(bf_error))
    return mark_safe(u'\n'.join(bf_errors))

def format_as_pml(self):
    output = []
    for name,  field in self.fields.items():
        bf = BoundField(self, field, name)

        text_field_type = 'Text'

        if(isinstance(field, IntegerField)):
            text_field_type = 'num'

        field_str = ('<TEXT position="ABOVE">%(label_name)s</TEXT><FIELD name="%(field_name)s" type="%(text_field_type)s" default="%(field_value)s"/>
'
                % {
                  'label_name' : conditional_escape(force_unicode(bf.label)),
                  'field_name' : bf.html_name,
                  'field_value' : bf.value()  if bf.value() != None  else '',
                  'text_field_type' : text_field_type
                  })
        if(isinstance(field, BooleanField)):
            default = bf.value()  if bf.value() != None  else ''

            field_str = ('''<CHOICE-GROUP type="radio" name="%(field_name)s">
            <TEXT>%(label_name)s</TEXT>
            <CHOICE value="True" checked="%(default_true)s">Yes</CHOICE>
            <CHOICE value="False" checked="%(default_false)s">No</CHOICE>
            </CHOICE-GROUP>''' %
            {
                'default_true':  'true' if default else 'false',
                'default_false':  'true' if not default else 'false',
                'label_name' : conditional_escape(force_unicode(bf.label)),
                'field_name' : bf.html_name,
                'field_value' : bf.value()  if bf.value() != None  else ''
            })

        output.append(field_str )
    return mark_safe(u'\n'.join(output))

class PMLModelForm(ModelForm):
    def as_pml(self):
        return format_as_pml(self)

    def errors_as_pml(self):
        return format_errors_as_pml(self)

class PMLForm(Form):
    def as_pml(self):
        return format_as_pml(self)

    def errors_as_pml(self):
        return format_errors_as_pml(self)

class PersonalDetailsForm(PMLModelForm):
    firstName = CharField(label = 'Firstname')
    dateOfBirth = CharField(label = 'Date of birth',  required=False)
    class Meta:
        model = CurriculumVitae
        fields = ('firstName', 'surname', 'dateOfBirth', 'gender')

class ContactDetailsForm(PMLModelForm):
    telephoneNumber= CharField(label = 'Phone number',  required=False)
    email = CharField(label = 'Email address',  required=False)
    houseNumber = CharField(label = 'House number',  required=False)
    streetName = CharField(label = 'Street name',  required=False)
    location = CharField(label = 'Location name',  required=False)
    class Meta:
        model = CurriculumVitae
        fields = ('telephoneNumber', 'email', 'houseNumber', 'streetName',
                'location')

class EducationDetailsForm(PMLModelForm):
    highestGrade = CharField(label = 'Highest grade passed',  required=False)
    highestGradeYear = IntegerField(label = 'Year passed',  required=False)
    school = CharField(label = 'Name of school', required=False)
    class Meta:
        model = CurriculumVitae
        fields = ('highestGrade', 'highestGradeYear', 'school')

class CertificateForm(PMLModelForm):
    name = CharField(label = 'Name of Certificate')
    institution = CharField(label = 'Name of institution', required=False)
    year = IntegerField(label = 'Year completed', required=False)
    class Meta:
        model = Certificate

class WorkExperienceForm(PMLModelForm):
    class Meta:
        model = WorkExperience

class LanguageForm(PMLModelForm):
    language = CharField(label = 'Name of language')
    readWrite = BooleanField(label = 'Can you Read and Write in this language')
    class Meta:
        model = Language
        widgets = {'readWrite': CheckboxInput(),}

class ReferenceForm(PMLModelForm):
    class Meta:
        model = Reference

class SendEmailForm(PMLForm):
    email = EmailField()

class SendFaxForm(PMLForm):
    fax = RegexField('[0-9+]',  error_message='Please enter a valid fax number.')
<commit_msg>changed default for BooleanField in pml<commit_after>from ummeli.api.models import (Certificate, Language, WorkExperience,
    Reference, CurriculumVitae)
from django.forms import (ModelForm, CheckboxInput,  Form, EmailField,
                                            RegexField,  CharField,  BooleanField,  IntegerField)
from django.forms.forms import BoundField
from django.utils.safestring import mark_safe
from django.utils.html import conditional_escape
from django.utils.encoding import force_unicode

def format_errors_as_pml(self):
    bf_errors = []
    for name,  field in self.fields.items():
        bf = BoundField(self, field, name)

        if(bf.errors):
            for error in bf.errors:
                bf_error = u'\n'.join([u'<TEXT position="ABOVE"><color value="red">%(label_name)s - %(error_message)s</color></TEXT>
'
                            % {
                            'label_name': conditional_escape(force_unicode(bf.label)),
                            'error_message': conditional_escape(force_unicode(error))
                            }])
                bf_errors.append(mark_safe(bf_error))
    return mark_safe(u'\n'.join(bf_errors))

def format_as_pml(self):
    output = []
    for name,  field in self.fields.items():
        bf = BoundField(self, field, name)

        text_field_type = 'Text'

        if(isinstance(field, IntegerField)):
            text_field_type = 'num'

        field_str = ('<TEXT position="ABOVE">%(label_name)s</TEXT><FIELD name="%(field_name)s" type="%(text_field_type)s" default="%(field_value)s"/>
'
                % {
                  'label_name' : conditional_escape(force_unicode(bf.label)),
                  'field_name' : bf.html_name,
                  'field_value' : bf.value()  if bf.value() != None  else '',
                  'text_field_type' : text_field_type
                  })
        if(isinstance(field, BooleanField)):
            default = bf.value()  if bf.value() != None  else ''

            field_str = ('''<CHOICE-GROUP type="radio" name="%(field_name)s">
            <TEXT>%(label_name)s</TEXT>
            <CHOICE value="True" %(default_true)s>Yes</CHOICE>
            <CHOICE value="False" %(default_false)s>No</CHOICE>
            </CHOICE-GROUP>''' %
            {
                'default_true':  'checked="true"' if default else '',
                'default_false':  'checked="true"' if not default else '',
                'label_name' : conditional_escape(force_unicode(bf.label)),
                'field_name' : bf.html_name,
                'field_value' : bf.value()  if bf.value() != None  else ''
            })

        output.append(field_str )
    return mark_safe(u'\n'.join(output))

class PMLModelForm(ModelForm):
    def as_pml(self):
        return format_as_pml(self)

    def errors_as_pml(self):
        return format_errors_as_pml(self)

class PMLForm(Form):
    def as_pml(self):
        return format_as_pml(self)

    def errors_as_pml(self):
        return format_errors_as_pml(self)

class PersonalDetailsForm(PMLModelForm):
    firstName = CharField(label = 'Firstname')
    dateOfBirth = CharField(label = 'Date of birth',  required=False)
    class Meta:
        model = CurriculumVitae
        fields = ('firstName', 'surname', 'dateOfBirth', 'gender')

class ContactDetailsForm(PMLModelForm):
    telephoneNumber= CharField(label = 'Phone number',  required=False)
    email = CharField(label = 'Email address',  required=False)
    houseNumber = CharField(label = 'House number',  required=False)
    streetName = CharField(label = 'Street name',  required=False)
    location = CharField(label = 'Location name',  required=False)
    class Meta:
        model = CurriculumVitae
        fields = ('telephoneNumber', 'email', 'houseNumber', 'streetName',
                'location')

class EducationDetailsForm(PMLModelForm):
    highestGrade = CharField(label = 'Highest grade passed',  required=False)
    highestGradeYear = IntegerField(label = 'Year passed',  required=False)
    school = CharField(label = 'Name of school', required=False)
    class Meta:
        model = CurriculumVitae
        fields = ('highestGrade', 'highestGradeYear', 'school')

class CertificateForm(PMLModelForm):
    name = CharField(label = 'Name of Certificate')
    institution = CharField(label = 'Name of institution', required=False)
    year = IntegerField(label = 'Year completed', required=False)
    class Meta:
        model = Certificate

class WorkExperienceForm(PMLModelForm):
    class Meta:
        model = WorkExperience

class LanguageForm(PMLModelForm):
    language = CharField(label = 'Name of language')
    readWrite = BooleanField(label = 'Can you Read and Write in this language')
    class Meta:
        model = Language
        widgets = {'readWrite': CheckboxInput(),}

class ReferenceForm(PMLModelForm):
    class Meta:
        model = Reference

class SendEmailForm(PMLForm):
    email = EmailField()

class SendFaxForm(PMLForm):
    fax = RegexField('[0-9+]',  error_message='Please enter a valid fax number.')
<|endoftext|>


<commit_before>#!/usr/bin/env python
# Copyright (c) 2013 Qumulo, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

# Import python libraries
import os
import sys
import smtplib
from email.mime.text import MIMEText

# Import Qumulo REST libraries
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import qumulo.lib.auth
import qumulo.lib.opts
import qumulo.lib.request
import qumulo.rest

# Size Definitions
KILOBYTE = 1024
MEGABYTE = 1024 * KILOBYTE
GIGABYTE = 1024 * MEGABYTE
TERABYTE = 1024 * GIGABYTE

###############CHANGE THESE SETTINGS AND CREATE THESE FILES FOR YOUR ENVIRONMENT #####################################
# Email settings
smtp_server = 'smtp.example.com'
sender = 'qumulo_cluster@example.com'
recipients = ['recipient1@example.com', 'recipient2@example.com']

# Location to write log file and header line for log file
logfile = './group_usage.log'
header = 'Group,SpaceUsed'
storagename = '[QUMULO CLUSTER]' # for email subject

# Import credentials
# credentials.txt formatted as single line composed of <host fqdn> <username> <password> <portnumber (default 8000)>
with open("credentials.txt") as file:
    line = file.readline()
    host, user, password, port = line.split()

# Import quota dictionary from quotas.txt file
# quotas.txt formatted as one line per quota formatted as <short name> <path on Qumulo storage> <nfs mount path> <quota size in TB>
quota_dict = {}
with open("quotas.txt") as file:
    for line in file:
        quotaname, storage_path, nfs_path, size = line.split()
        quota_dict[quotaname] = (storage_path, nfs_path, float(size))
######################################################################################################################

def login(host, user, passwd, port):
    '''Obtain credentials from the REST server'''
    conninfo = None
    creds = None

    try:
        # Create a connection to the REST server
        conninfo = qumulo.lib.request.Connection(host, int(port))

        # Provide username and password to retreive authentication tokens
        # used by the credentials object
        login_results, _ = qumulo.rest.auth.login(
                conninfo, None, user, passwd)

        # Create the credentials object which will be used for
        # authenticating rest calls
        creds = qumulo.lib.auth.Credentials.from_login_response(login_results)
    except Exception, excpt:
        print "Error connecting to the REST server: %s" % excpt
        print __doc__
        sys.exit(1)

    return (conninfo, creds)

def send_mail(smtp_server, sender, recipients, subject, body):
    mmsg = MIMEText(body, 'html')
    mmsg['Subject'] = subject
    mmsg['From'] = sender
    mmsg['To'] = ", ".join(recipients)

    session = smtplib.SMTP(smtp_server)
    session.sendmail(sender, recipients, mmsg.as_string())
    session.quit()

def build_mail(nfspath, quota, current_usage, smtp_server, sender, recipients):
    sane_current_usage = float(current_usage) / float(TERABYTE)
    subject = storagename + "Quota exceeded"
    body = ""
    body += "The usage on {} has exceeded the quota.
".format(nfspath)
    body += "Current usage: %0.2f TB
" %sane_current_usage
    body += "Quota: %0.2f TB
" %quota
    body += "
"
    send_mail(smtp_server, sender, recipients, subject, body)

# Build email and check against quota for notification, return current usage for tracking
def monitor_path(path, conninfo, creds):
    try:
        node = qumulo.rest.fs.read_dir_aggregates(conninfo, creds, path)
    except Exception, excpt:
        print 'Error retrieving path: %s' % excpt
    else:
        current_usage = int(node[0]['total_capacity'])
        return current_usage

def build_csv(quotaname, current_usage, logfile):
    with open(logfile, "a") as file:
        file.write(quotaname + ',' + str(current_usage) + '\n')

### Main subroutine
def main(argv):
    # Get credentials
    (conninfo, creds) = login(host, user, password, port)

    # Overwrite log file
    with open(logfile, "w") as file:
        file.write(header + '\n')

    # Get quotas and generate CSV
    for quotaname in quota_dict.keys():
        path, nfspath, quota = quota_dict[quotaname]
        current_usage = monitor_path(path, conninfo, creds)
        if current_usage is not None:
            quotaraw = int(quota) * TERABYTE
            if current_usage > quotaraw:
                build_mail(nfspath, quota, current_usage, smtp_server, sender, recipients)
            build_csv(quotaname, current_usage, logfile)

# Main
if __name__ == '__main__':
    main(sys.argv[1:])
<commit_msg>Fixed parameter file read paths to allow for cron to run the script.<commit_after>#!/usr/bin/env python
# Copyright (c) 2013 Qumulo, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

# Import python libraries
import os
import sys
import smtplib
from email.mime.text import MIMEText

# Import Qumulo REST libraries
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import qumulo.lib.auth
import qumulo.lib.opts
import qumulo.lib.request
import qumulo.rest

# Size Definitions
KILOBYTE = 1024
MEGABYTE = 1024 * KILOBYTE
GIGABYTE = 1024 * MEGABYTE
TERABYTE = 1024 * GIGABYTE

###############CHANGE THESE SETTINGS AND CREATE THESE FILES FOR YOUR ENVIRONMENT #####################################
# Email settings
smtp_server = 'smtp.example.com'
sender = 'qumulo_cluster@example.com'
recipients = ['recipient1@example.com', 'recipient2@example.com']

# Location to write log file and header line for log file
logfile = './group_usage.log'
header = 'Group,SpaceUsed'
storagename = '[QUMULO CLUSTER]' # for email subject

# Import credentials
# credentials.txt formatted as single line composed of <host fqdn> <username> <password> <portnumber (default 8000)>
with open(os.path.join(sys.path[0], "credentials.txt"),"r") as file:
    line = file.readline()
    host, user, password, port = line.split()

# Import quota dictionary from quotas.txt file
# quotas.txt formatted as one line per quota formatted as <short name> <path on Qumulo storage> <nfs mount path> <quota size in TB>
quota_dict = {}
with open(os.path.join(sys.path[0], "quotas.txt"),"r") as file:
    for line in file:
        quotaname, storage_path, nfs_path, size = line.split()
        quota_dict[quotaname] = (storage_path, nfs_path, float(size))
######################################################################################################################

def login(host, user, passwd, port):
    '''Obtain credentials from the REST server'''
    conninfo = None
    creds = None

    try:
        # Create a connection to the REST server
        conninfo = qumulo.lib.request.Connection(host, int(port))

        # Provide username and password to retreive authentication tokens
        # used by the credentials object
        login_results, _ = qumulo.rest.auth.login(
                conninfo, None, user, passwd)

        # Create the credentials object which will be used for
        # authenticating rest calls
        creds = qumulo.lib.auth.Credentials.from_login_response(login_results)
    except Exception, excpt:
        print "Error connecting to the REST server: %s" % excpt
        print __doc__
        sys.exit(1)

    return (conninfo, creds)

def send_mail(smtp_server, sender, recipients, subject, body):
    mmsg = MIMEText(body, 'html')
    mmsg['Subject'] = subject
    mmsg['From'] = sender
    mmsg['To'] = ", ".join(recipients)

    session = smtplib.SMTP(smtp_server)
    session.sendmail(sender, recipients, mmsg.as_string())
    session.quit()

def build_mail(nfspath, quota, current_usage, smtp_server, sender, recipients):
    sane_current_usage = float(current_usage) / float(TERABYTE)
    subject = storagename + "Quota exceeded"
    body = ""
    body += "The usage on {} has exceeded the quota.
".format(nfspath)
    body += "Current usage: %0.2f TB
" %sane_current_usage
    body += "Quota: %0.2f TB
" %quota
    body += "
"
    send_mail(smtp_server, sender, recipients, subject, body)

# Build email and check against quota for notification, return current usage for tracking
def monitor_path(path, conninfo, creds):
    try:
        node = qumulo.rest.fs.read_dir_aggregates(conninfo, creds, path)
    except Exception, excpt:
        print 'Error retrieving path: %s' % excpt
    else:
        current_usage = int(node[0]['total_capacity'])
        return current_usage

def build_csv(quotaname, current_usage, logfile):
    with open(logfile, "a") as file:
        file.write(quotaname + ',' + str(current_usage) + '\n')

### Main subroutine
def main(argv):
    # Get credentials
    (conninfo, creds) = login(host, user, password, port)

    # Overwrite log file
    with open(logfile, "w") as file:
        file.write(header + '\n')

    # Get quotas and generate CSV
    for quotaname in quota_dict.keys():
        path, nfspath, quota = quota_dict[quotaname]
        current_usage = monitor_path(path, conninfo, creds)
        if current_usage is not None:
            quotaraw = int(quota) * TERABYTE
            if current_usage > quotaraw:
                build_mail(nfspath, quota, current_usage, smtp_server, sender, recipients)
            build_csv(quotaname, current_usage, logfile)

# Main
if __name__ == '__main__':
    main(sys.argv[1:])
<|endoftext|>


<commit_before>import json
import logging
import re

logger = logging.getLogger(__name__)

class RtmEventHandler(object):
    def __init__(self, slack_clients, msg_writer):
        self.clients = slack_clients
        self.msg_writer = msg_writer

    def handle(self, event):

        if 'type' in event:
            self._handle_by_type(event['type'], event)

    def _handle_by_type(self, event_type, event):
        # See https://api.slack.com/rtm for a full list of events
        # if 'channel' in event:
        # logger.info(event)

        if event_type == 'error':
            # error
            self.msg_writer.write_error(event['channel'], json.dumps(event))
        elif event_type == 'message':
            # message was sent to channel
            self._handle_message(event)
        elif event_type == 'channel_joined':
            # you joined a channel
            self.msg_writer.write_help_message(event['channel'])
        elif event_type == 'group_joined':
            # you joined a private group
            self.msg_writer.write_help_message(event['channel'])
        else:
            pass

    def _handle_message(self, event):
        # Filter out messages from the bot itself, and from non-users (eg. webhooks)
        if ('user' in event) and (not self.clients.is_message_from_me(event['user'])):
            logger.info('reached cond')

            if self.clients.is_bot_mention(msg_txt) or self._is_direct_message(event['channel']):
                msg_txt = re.sub('(\<.+\> )', '', msg_txt) # remove bot name references
                logger.info('POST text: ' + msg_txt)
                # e.g. user typed: "@pybot tell me a joke!"
                if msg_txt[0] == '$' and msg_txt[-1] == '$':
                    self.msg_writer.write_latex(event['channel'], msg_txt)
                elif 'help' in msg_txt:
                    self.msg_writer.write_help_message(event['channel'])
                elif re.search('hi|hey|hello|howdy', msg_txt):
                    self.msg_writer.write_greeting(event['channel'], event['user'])
                elif 'joke' in msg_txt:
                    self.msg_writer.write_joke(event['channel'])
                elif 'attachment' in msg_txt:
                    self.msg_writer.demo_attachment(event['channel'])
                elif 'echo' in msg_txt:
                    self.msg_writer.send_message(event['channel'], msg_txt)
                else:
                    self.msg_writer.write_prompt(event['channel'])

    def _is_direct_message(self, channel):
        """Check if channel is a direct message channel

        Args:
            channel (str): Channel in which a message was received
        """
        return channel.startswith('D')
<commit_msg>fix bug<commit_after>import json
import logging
import re

logger = logging.getLogger(__name__)

class RtmEventHandler(object):
    def __init__(self, slack_clients, msg_writer):
        self.clients = slack_clients
        self.msg_writer = msg_writer

    def handle(self, event):

        if 'type' in event:
            self._handle_by_type(event['type'], event)

    def _handle_by_type(self, event_type, event):
        # See https://api.slack.com/rtm for a full list of events
        # if 'channel' in event:
        # logger.info(event)

        if event_type == 'error':
            # error
            self.msg_writer.write_error(event['channel'], json.dumps(event))
        elif event_type == 'message':
            # message was sent to channel
            self._handle_message(event)
        elif event_type == 'channel_joined':
            # you joined a channel
            self.msg_writer.write_help_message(event['channel'])
        elif event_type == 'group_joined':
            # you joined a private group
            self.msg_writer.write_help_message(event['channel'])
        else:
            pass

    def _handle_message(self, event):
        # Filter out messages from the bot itself, and from non-users (eg. webhooks)
        if ('user' in event) and (not self.clients.is_message_from_me(event['user'])):
            msg_txt = event['text']

            if self.clients.is_bot_mention(msg_txt) or self._is_direct_message(event['channel']):
                msg_txt = re.sub('(\<.+\> )', '', msg_txt) # remove bot name references
                logger.info('POST text: ' + msg_txt)
                # e.g. user typed: "@pybot tell me a joke!"
                if msg_txt[0] == '$' and msg_txt[-1] == '$':
                    self.msg_writer.write_latex(event['channel'], msg_txt)
                elif 'help' in msg_txt:
                    self.msg_writer.write_help_message(event['channel'])
                elif re.search('hi|hey|hello|howdy', msg_txt):
                    self.msg_writer.write_greeting(event['channel'], event['user'])
                elif 'joke' in msg_txt:
                    self.msg_writer.write_joke(event['channel'])
                elif 'attachment' in msg_txt:
                    self.msg_writer.demo_attachment(event['channel'])
                elif 'echo' in msg_txt:
                    self.msg_writer.send_message(event['channel'], msg_txt)
                else:
                    self.msg_writer.write_prompt(event['channel'])

    def _is_direct_message(self, channel):
        """Check if channel is a direct message channel

        Args:
            channel (str): Channel in which a message was received
        """
        return channel.startswith('D')
<|endoftext|>


<commit_before>__all__ = ('Subscription', 'SubscriptionItem')

class SubscriptionItem(object):
    """A pair of dealer_name and query linked to a Subscription."""

    __slots__ = ('dealer_name', 'query', 'subscription')

    def __init__(self, dealer_name, query, subscription=None):
        self.dealer_name = dealer_name
        self.query = query
        self.subscription = subscription

class Subscription(object):
    """
    An object which represents a contract that may be signed by a Client to be
    notified by one or more dealers, providing one or more queries for each
    Dealer.

    Each pair of Dealer and query is stored as a SubscriptionItem.

    A Subscription distinguishes three states:

    * Acquired: A Client has been associated with the Subscription (in other
      words, it has signed the contract). In order for a client to acquire a
      Subscription it needs to know its subscription_token.

    * WaitingClient: No client has acquired the Subscription yet. The
      Subscription object will store any deltas for the subscription items and
      forward them to the Client when it acquires the Subscription.

    * Stale: Time has passed since the Subscription was created but no Client
      has acquired it. Subscriptions in this state will be removed by the
      system in order to free memory.
    """

    __slots__ = ('token', 'client', '_awaited_client_buffer',
            '_awaited_client_timeout', 'items')

    def __init__(self, items):
        self.token = None
        self.client = None
        self._awaited_client_buffer = []
        self._awaited_client_timeout = None

        self.items = items
        for item in items:
            item.subscription = self

    def got_client(self, client):
        """Links this subscription with a client. If there were deltas received
        before, they will be sent now."""

        if self.client is not None:
            raise RuntimeError("Subscription already linked to a Client")

        self.client = client
        self.cancel_timeout()
        self.flush_awaited_client_buffer()

    def lost_client(self):
        """Unlinks this subscription from its client. New arrived deltas will
        no further be delivered to it."""

        self.client = None

    def deliver_delta(self, delta):
        """Sends a JSON encoded delta to the associated client.

        If there is currently no associated client the delta will be stored to
        be sent when a client arrives."""

        if self.client is not None:
            self.client.deliver_delta(delta)
        else:
            self._awaited_client_buffer.append(delta)

    def flush_awaited_client_buffer(self):
        """Sends deltas stored in the _awaited_client_buffer to the client and
        clears the buffer."""

        for delta in self._awaited_client_buffer:
            self.deliver_delta(delta)

        # Clear buffer
        self._awaited_client_buffer[:] = []

    def cancel_timeout(self, io_loop):
        io_loop.remove_timeout(self._awaited_client_timeout)
        self._awaited_client_timeout = None
<commit_msg>Cancel handles<commit_after>__all__ = ('Subscription', 'SubscriptionItem')

class SubscriptionItem(object):
    """A pair of dealer_name and query linked to a Subscription."""

    __slots__ = ('dealer_name', 'query', 'subscription')

    def __init__(self, dealer_name, query, subscription=None):
        self.dealer_name = dealer_name
        self.query = query
        self.subscription = subscription

class Subscription(object):
    """
    An object which represents a contract that may be signed by a Client to be
    notified by one or more dealers, providing one or more queries for each
    Dealer.

    Each pair of Dealer and query is stored as a SubscriptionItem.

    A Subscription distinguishes three states:

    * Acquired: A Client has been associated with the Subscription (in other
      words, it has signed the contract). In order for a client to acquire a
      Subscription it needs to know its subscription_token.

    * WaitingClient: No client has acquired the Subscription yet. The
      Subscription object will store any deltas for the subscription items and
      forward them to the Client when it acquires the Subscription.

    * Stale: Time has passed since the Subscription was created but no Client
      has acquired it. Subscriptions in this state will be removed by the
      system in order to free memory.
    """

    __slots__ = ('token', 'client', '_awaited_client_buffer',
            '_awaited_client_timeout', 'items')

    def __init__(self, items):
        self.token = None
        self.client = None
        self._awaited_client_buffer = []
        self._awaited_client_timeout = None

        self.items = items
        for item in items:
            item.subscription = self

    def got_client(self, client):
        """Links this subscription with a client. If there were deltas received
        before, they will be sent now."""

        if self.client is not None:
            raise RuntimeError("Subscription already linked to a Client")

        self.client = client
        self.cancel_timeout()
        self.flush_awaited_client_buffer()

    def lost_client(self):
        """Unlinks this subscription from its client. New arrived deltas will
        no further be delivered to it."""

        self.client = None

    def deliver_delta(self, delta):
        """Sends a JSON encoded delta to the associated client.

        If there is currently no associated client the delta will be stored to
        be sent when a client arrives."""

        if self.client is not None:
            self.client.deliver_delta(delta)
        else:
            self._awaited_client_buffer.append(delta)

    def flush_awaited_client_buffer(self):
        """Sends deltas stored in the _awaited_client_buffer to the client and
        clears the buffer."""

        for delta in self._awaited_client_buffer:
            self.deliver_delta(delta)

        # Clear buffer
        self._awaited_client_buffer[:] = []

    def cancel_timeout(self):
        if self._awaited_client_timeout:
            self._awaited_client_timeout.cancel()
            self._awaited_client_timeout = None
<|endoftext|>


<commit_before>#!/usr/bin/python
import unittest

from blivet.formats import device_formats
import blivet.formats.fs as fs

class InitializationTestCase(unittest.TestCase):
    """Test FS object initialization."""

    def testLabels(self):
        """Initialize some filesystems with valid and invalid labels."""

        # Ext2FS has a maximum length of 16
        self.assertRaisesRegexp(fs.FSError,
           "Filesystem label.*incorrectly formatted",
           fs.Ext2FS,
           device="/dev", label="root___filesystem")

        self.assertIsNotNone(fs.Ext2FS(label="root__filesystem"))

        # FATFS has a maximum length of 11
        self.assertRaisesRegexp(fs.FSError,
           "Filesystem label.*incorrectly formatted",
           fs.FATFS,
           device="/dev", label="rtfilesystem")

        self.assertIsNotNone(fs.FATFS(label="rfilesystem"))

        # JFS has a maximum length of 16
        self.assertRaisesRegexp(fs.FSError,
           "Filesystem label.*incorrectly formatted",
           fs.JFS,
           device="/dev", label="root___filesystem")

        self.assertIsNotNone(fs.JFS(label="root__filesystem"))

        # ReiserFS has a maximum length of 16
        self.assertRaisesRegexp(fs.FSError,
           "Filesystem label.*incorrectly formatted",
           fs.ReiserFS,
           device="/dev", label="root___filesystem")

        self.assertIsNotNone(fs.ReiserFS(label="root__filesystem"))

        #XFS has a maximum length 12 and does not allow spaces
        self.assertRaisesRegexp(fs.FSError,
           "Filesystem label.*incorrectly formatted",
           fs.XFS,
           device="/dev", label="root filesyst")
        self.assertRaisesRegexp(fs.FSError,
           "Filesystem label.*incorrectly formatted",
           fs.XFS,
           device="/dev", label="root file")

        self.assertIsNotNone(fs.XFS(label="root_filesys"))

        # all devices are permitted to have a label of None
        for k, v  in device_formats.items():
            self.assertIsNotNone(v(label=None))

class MethodsTestCase(unittest.TestCase):
    """Test some methods that do not require actual images."""

    def setUp(self):
        self.fs = {}
        for k, v  in device_formats.items():
            if issubclass(v, fs.FS) and not issubclass(v, fs.NFS):
                self.fs[k] = v(device="/dev", label="myfs")

    def testGetLabelArgs(self):
        self.longMessage = True

        # ReiserFS uses a -l flag
        reiserfs = self.fs["reiserfs"]
        self.assertEqual(reiserfs._labelfs.setLabelCommand(reiserfs),
           ["reiserfstune", "-l", "myfs", "/dev"], msg="reiserfs")

        # JFS, XFS use a -L flag
        lflag_classes = [fs.JFS, fs.XFS]
        for k, v in [(k, v) for k, v in self.fs.items() if any(isinstance(v, c) for c in lflag_classes)]:
            self.assertEqual(v._labelfs.setLabelCommand(v), [v._labelfs.name, "-L", "myfs", "/dev"], msg=k)

        # Ext2FS and descendants and FATFS do not use a flag
        noflag_classes = [fs.Ext2FS, fs.FATFS]
        for k, v in [(k, v) for k, v in self.fs.items() if any(isinstance(v, c) for c in noflag_classes)]:
            self.assertEqual(v._labelfs.setLabelCommand(v), [v._labelfs.name, "/dev", "myfs"], msg=k)

        # all of the remaining should have no labelfsProg
        omit_classes = [ fs.ReiserFS ] + lflag_classes + noflag_classes
        for k, v in [(k, v) for k, v in self.fs.items() if not any(isinstance(v, c) for c in omit_classes)]:
            self.assertIsNone(v.labelfsProg)

def suite():
    suite1 = unittest.TestLoader().loadTestsFromTestCase(InitializationTestCase)
    suite2 = unittest.TestLoader().loadTestsFromTestCase(MethodsTestCase)
    return unittest.TestSuite([suite1, suite2])

if __name__ == "__main__":
    unittest.main()
<commit_msg>More tests to check writing and reading labels (#1038590)<commit_after>#!/usr/bin/python
import os
import unittest

from devicelibs_test import baseclass
from blivet.formats import device_formats
import blivet.formats.fs as fs

class InitializationTestCase(unittest.TestCase):
    """Test FS object initialization."""

    def testLabels(self):
        """Initialize some filesystems with valid and invalid labels."""

        # Ext2FS has a maximum length of 16
        self.assertRaisesRegexp(fs.FSError,
           "Filesystem label.*incorrectly formatted",
           fs.Ext2FS,
           device="/dev", label="root___filesystem")

        self.assertIsNotNone(fs.Ext2FS(label="root__filesystem"))

        # FATFS has a maximum length of 11
        self.assertRaisesRegexp(fs.FSError,
           "Filesystem label.*incorrectly formatted",
           fs.FATFS,
           device="/dev", label="rtfilesystem")

        self.assertIsNotNone(fs.FATFS(label="rfilesystem"))

        # JFS has a maximum length of 16
        self.assertRaisesRegexp(fs.FSError,
           "Filesystem label.*incorrectly formatted",
           fs.JFS,
           device="/dev", label="root___filesystem")

        self.assertIsNotNone(fs.JFS(label="root__filesystem"))

        # ReiserFS has a maximum length of 16
        self.assertRaisesRegexp(fs.FSError,
           "Filesystem label.*incorrectly formatted",
           fs.ReiserFS,
           device="/dev", label="root___filesystem")

        self.assertIsNotNone(fs.ReiserFS(label="root__filesystem"))

        #XFS has a maximum length 12 and does not allow spaces
        self.assertRaisesRegexp(fs.FSError,
           "Filesystem label.*incorrectly formatted",
           fs.XFS,
           device="/dev", label="root filesyst")
        self.assertRaisesRegexp(fs.FSError,
           "Filesystem label.*incorrectly formatted",
           fs.XFS,
           device="/dev", label="root file")

        self.assertIsNotNone(fs.XFS(label="root_filesys"))

        # all devices are permitted to have a label of None
        for k, v  in device_formats.items():
            self.assertIsNotNone(v(label=None))

class MethodsTestCase(unittest.TestCase):
    """Test some methods that do not require actual images."""

    def setUp(self):
        self.fs = {}
        for k, v  in device_formats.items():
            if issubclass(v, fs.FS) and not issubclass(v, fs.NFS):
                self.fs[k] = v(device="/dev", label="myfs")

    def testGetLabelArgs(self):
        self.longMessage = True

        # ReiserFS uses a -l flag
        reiserfs = self.fs["reiserfs"]
        self.assertEqual(reiserfs._labelfs.setLabelCommand(reiserfs),
           ["reiserfstune", "-l", "myfs", "/dev"], msg="reiserfs")

        # JFS, XFS use a -L flag
        lflag_classes = [fs.JFS, fs.XFS]
        for k, v in [(k, v) for k, v in self.fs.items() if any(isinstance(v, c) for c in lflag_classes)]:
            self.assertEqual(v._labelfs.setLabelCommand(v), [v._labelfs.name, "-L", "myfs", "/dev"], msg=k)

        # Ext2FS and descendants and FATFS do not use a flag
        noflag_classes = [fs.Ext2FS, fs.FATFS]
        for k, v in [(k, v) for k, v in self.fs.items() if any(isinstance(v, c) for c in noflag_classes)]:
            self.assertEqual(v._labelfs.setLabelCommand(v), [v._labelfs.name, "/dev", "myfs"], msg=k)

        # all of the remaining should have no labelfsProg
        omit_classes = [ fs.ReiserFS ] + lflag_classes + noflag_classes
        for k, v in [(k, v) for k, v in self.fs.items() if not any(isinstance(v, c) for c in omit_classes)]:
            self.assertIsNone(v.labelfsProg)

class LabelingAsRootTestCase(baseclass.DevicelibsTestCase):
    """Tests for labeling a filesystem and reading its label.

       For some filesystems, there is an application for writing the label
       but the same application does not read the label. For those, it
       is only possible to check if the write operation completed succesfully.
    """

    @unittest.skipUnless(os.geteuid() == 0, "requires root privileges")
    def testLabelingXFS(self):
        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]

        an_fs = fs.XFS(device=_LOOP_DEV0)
        self.assertIsNone(an_fs.create())

        an_fs.label = "temeraire"
        self.assertIsNone(an_fs.writeLabel())
        self.assertEqual(an_fs.readLabel(), an_fs.label)

    @unittest.skipUnless(os.geteuid() == 0, "requires root privileges")
    def testLabelingFATFS(self):
        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]

        an_fs = fs.FATFS(device=_LOOP_DEV0)
        self.assertIsNone(an_fs.create())

        an_fs.label = "an fs"
        self.assertIsNone(an_fs.writeLabel())
        self.assertEqual(an_fs.readLabel(), an_fs.label)

    @unittest.skipUnless(os.geteuid() == 0, "requires root privileges")
    def testLabelingExt2FS(self):
        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]

        an_fs = fs.Ext2FS(device=_LOOP_DEV0)
        self.assertIsNone(an_fs.create())

        an_fs.label = "an fs"
        self.assertIsNone(an_fs.writeLabel())
        self.assertEqual(an_fs.readLabel(), an_fs.label)

    @unittest.skipUnless(os.geteuid() == 0, "requires root privileges")
    def testLabelingJFS(self):
        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]

        an_fs = fs.JFS(device=_LOOP_DEV0)
        self.assertIsNone(an_fs.create())

        an_fs.label = "an fs"
        self.assertIsNone(an_fs.writeLabel())

        self.assertRaisesRegexp(fs.FSError,
           "no application to read label",
           an_fs.readLabel)

    @unittest.skipUnless(os.geteuid() == 0, "requires root privileges")
    def testLabelingReiserFS(self):
        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]

        an_fs = fs.ReiserFS(device=_LOOP_DEV0)
        self.assertIsNone(an_fs.create())

        an_fs.label = "an fs"
        self.assertIsNone(an_fs.writeLabel())

        self.assertRaisesRegexp(fs.FSError,
           "no application to read label",
           an_fs.readLabel)

def suite():
    suite1 = unittest.TestLoader().loadTestsFromTestCase(InitializationTestCase)
    suite2 = unittest.TestLoader().loadTestsFromTestCase(MethodsTestCase)
    return unittest.TestSuite([suite1, suite2])

if __name__ == "__main__":
    unittest.main()
<|endoftext|>


<commit_before>from django.db import models
from django.utils import timezone

TYPICAL_SIZE = 256

class Meta(models.Model):
    departments_updated = models.DateTimeField()

    @classmethod
    def get(cls):
        if cls.objects.count():
            return cls.objects.get()
        else:
            return cls.objects.create(
                department_list_updated=timezone.now(),
            )

class Semester(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    value = models.CharField(max_length=6)
    year = models.IntegerField()
    section = models.IntegerField()
    name = models.CharField(max_length=TYPICAL_SIZE)
    ready = models.BooleanField(default=False)

    class Meta:
        ordering = ('-year', '-section', '-created')
        get_latest_by = 'created'

    def __str__(self):
        return self.value

class Department(models.Model):
    abbr = models.CharField(max_length=4)
    name_zh = models.CharField(max_length=TYPICAL_SIZE)
    name_en = models.CharField(max_length=TYPICAL_SIZE)

    class Meta:
        ordering = ('abbr',)

    def __str__(self):
        return self.abbr

class Course(models.Model):
    semester = models.ForeignKey(Semester)
    departments = models.ManyToManyField(Department)
    no = models.CharField(max_length=15)
    title_zh = models.CharField(max_length=TYPICAL_SIZE)
    title_en = models.CharField(max_length=TYPICAL_SIZE)
    ge_line = models.CharField(max_length=TYPICAL_SIZE)
    credit = models.IntegerField()
    time = models.CharField(max_length=TYPICAL_SIZE)  # TODO
    room = models.CharField(max_length=TYPICAL_SIZE)
    capacity = models.IntegerField(null=True)
    teacher = models.CharField(max_length=TYPICAL_SIZE)
    size_limit = models.IntegerField(null=True)
    freshmen_reserved = models.IntegerField(null=True)
    note = models.TextField()
    enrollment = models.IntegerField()
    object = models.CharField(max_length=TYPICAL_SIZE)
    prerequisite = models.CharField(max_length=TYPICAL_SIZE)  # TODO
    required_by = models.CharField(max_length=TYPICAL_SIZE)  # TODO
    syllabus_text = models.TextField()
    syllabus_attachment = None  # TODO

    class Meta:
        ordering = ('no', 'semester')

    def __str__(self):
        return self.no
<commit_msg>make querying fields db_index<commit_after>from django.db import models
from django.utils import timezone

TYPICAL_SIZE = 256

class Meta(models.Model):
    departments_updated = models.DateTimeField()

    @classmethod
    def get(cls):
        if cls.objects.count():
            return cls.objects.get()
        else:
            return cls.objects.create(
                department_list_updated=timezone.now(),
            )

class Semester(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    value = models.CharField(max_length=6, db_index=True)
    year = models.IntegerField()
    section = models.IntegerField()
    name = models.CharField(max_length=TYPICAL_SIZE)
    ready = models.BooleanField(default=False)

    class Meta:
        ordering = ('-year', '-section', '-created')
        get_latest_by = 'created'

    def __str__(self):
        return self.value

class Department(models.Model):
    abbr = models.CharField(max_length=4, db_index=True)
    name_zh = models.CharField(max_length=TYPICAL_SIZE)
    name_en = models.CharField(max_length=TYPICAL_SIZE)

    class Meta:
        ordering = ('abbr',)

    def __str__(self):
        return self.abbr

class Course(models.Model):
    semester = models.ForeignKey(Semester, db_index=True)
    departments = models.ManyToManyField(Department, db_index=True)
    no = models.CharField(max_length=15, db_index=True)
    title_zh = models.CharField(max_length=TYPICAL_SIZE)
    title_en = models.CharField(max_length=TYPICAL_SIZE)
    ge_line = models.CharField(max_length=TYPICAL_SIZE)
    credit = models.IntegerField()
    time = models.CharField(max_length=TYPICAL_SIZE)  # TODO
    room = models.CharField(max_length=TYPICAL_SIZE)
    capacity = models.IntegerField(null=True)
    teacher = models.CharField(max_length=TYPICAL_SIZE)
    size_limit = models.IntegerField(null=True)
    freshmen_reserved = models.IntegerField(null=True)
    note = models.TextField()
    enrollment = models.IntegerField()
    object = models.CharField(max_length=TYPICAL_SIZE)
    prerequisite = models.CharField(max_length=TYPICAL_SIZE)  # TODO
    required_by = models.CharField(max_length=TYPICAL_SIZE)  # TODO
    syllabus_text = models.TextField()
    syllabus_attachment = None  # TODO

    class Meta:
        ordering = ('no', 'semester')

    def __str__(self):
        return self.no
<|endoftext|>


<commit_before>#!/usr/bin/env python
# coding: utf-8

import logging

class SoftAssert():
    """Accumulate exceptions for later report

    Args:
        *exceptions: exception class (or iterable of exceptoin classes)
            to capture in further calls.

    Returns:
        callable (see __call__ for details). Also can be used as context.

    Attributes:
        message (str): aggregate report message prefix
        final_exception: exception to raise on final report (self.raise_errors)
    """
    message = "Errors collected"
    final_exception = AssertionError

    def __init__(self, *exceptions):
        self.errors = []
        self.track_exceptions = exceptions or AssertionError

    def __call__(self, func, *args, **kwargs):
        """Wrapper to run func inside a try block and collect exceptions

        Args:
            func (callable): callable to run
            *args, **kwargs: directly passed to func

        Returns:
            Same as func does
        """
        assert callable(func), "The first argument should be a callable"

        try:
            return func(*args, **kwargs)
        except self.track_exceptions as exc:
            logging.exception("Error happened, collecting")
            self.errors.append(exc)

    def __enter__(self):
        return self

    def __exit__(self):
        return self.raise_errors()

    def raise_errors(self, msg=None):
        """Raise exception if any errors were previously collected

        Args:
            msg (str): some alternative message to prefix error list output

        Raises:
            AssertionError by default, can be redefined in self.final_exception
        """
        if self.errors:
            raise self.final_exception(
                "{} (see logs above for tracebacks):\n{}".format(
                    self.message if msg is None else msg,
                    "\n".join(str(e) for e in self.errors)))
<commit_msg>Docstrings cleanup, context exit fix<commit_after>#!/usr/bin/env python
# coding: utf-8

import logging

class SoftAssert():
    """Accumulate exceptions for later report

    Args:
        *exceptions: exception class (or iterable of exceptoin classes)
            to capture in further calls.

    Returns:
        callable (see __call__ for details). Also can be used as context.

    Attributes:
        message (str): aggregate report message prefix
        final_exception: exception to raise on final report (self.raise_errors)
    """
    message = "Errors collected"
    final_exception = AssertionError

    def __init__(self, *exceptions):
        self.errors = []
        self.track_exceptions = exceptions or AssertionError

    def __call__(self, func, *args, **kwargs):
        """Wrapper to run func inside a try block and collect exceptions

        Args:
            func (callable): callable to run
            *args, **kwargs: directly passed to func

        Returns:
            Same as func does
        """
        assert callable(func), "The first argument should be a callable"

        try:
            return func(*args, **kwargs)
        except self.track_exceptions as exc:
            logging.exception("Exception happened, collecting")
            self.errors.append(exc)

    def __enter__(self):
        return self

    def __exit__(self, *args):
        return self.raise_errors()

    def raise_errors(self, msg=None):
        """Raise exception if any errors were previously collected

        Args:
            msg (str): some alternative message to prefix error list output

        Raises:
            AssertionError by default, can be redefined in self.final_exception
        """
        if self.errors:
            raise self.final_exception(
                "{} (see logs above for tracebacks):\n{}".format(
                    self.message if msg is None else msg,
                    "\n".join(str(e) for e in self.errors)))
<|endoftext|>


<commit_before>import json
import logging
import random
import Algorithmia

from textblob import TextBlob
from text_corpus import TextCorpus
from aylienapiclient import textapi

logger = logging.getLogger(__name__)

class RtmEventHandler(object):
    def __init__(self, slack_clients, msg_writer):
        self.clients = slack_clients
        self.msg_writer = msg_writer
        #self.trump_corpus = trump_corpus

    def handle(self, event):

        if 'type' in event:
            self._handle_by_type(event['type'], event)

    def _handle_by_type(self, event_type, event):
        # See https://api.slack.com/rtm for a full list of events
        # if event_type == 'error':
            # error
            # ignore self.msg_writer.write_error(event['channel'], json.dumps(event))
        if event_type == 'message':
            # message was sent to channel
            self._handle_message(event)
        elif event_type == 'channel_joined':
            # you joined a channel
            self.msg_writer.send_message(event['channel'], "Let's make America great again!")
        elif event_type == 'group_joined':
            # you joined a private group
            self.msg_writer.write_help_message(event['channel'])
        elif event_type == 'file_shared':
            self.msg_writer.send_message(event['channel'], "Got your file, thanks!")
        else:
            pass

    def _handle_message(self, event):
        # Filter out messages from the bot itself
        if not self.clients.is_message_from_me(event['user']):

            msg_txt = event['text']

            #if self.clients.is_bot_mention(msg_txt) or self._is_direct_message(event['channel']):
                #txt_b = TextBlob(msg_txt)

            #if self.clients.is_bot_mention(msg_txt) or self._is_direct_message(event['channel']):
                # e.g. user typed: "@pybot tell me a joke!"
            if 'sentiment' in msg_txt:
                client = textapi.Client("a19bb245", "2623b77754833e2711998a0b0bdad9db")
                sentiment = client.Sentiment({"text": msg_txt})
                str = sentiment['polarity']
                str2 = " - %3.3f" % sentiment['polarity_confidence']
                str += str2
                self.msg_writer.send_message(event['channel'], str)
            elif 'tag' in msg_txt:
                #self.msg_writer.write_analytics(event['channel'], msg_txt)
                #if self.clients.is_bot_mention(msg_txt) or self._is_direct_message(event['channel']):
                #txt_b = TextBlob(msg_txt)
#                   response = txt_b.tags
#                   self.msg_writer.send_message(event['channel'], response)
                algo = client.algo('nlp/AutoTag/1.0.0')
                tags = algo.pipe(text)
                str_final = ""
                #print entities.result
                for item in tags.result:
                    str_final += item
                    str_final += ", "
                self.msg_writer.send_message(event['channel'], str_final)
                #algo = client.algo('StanfordNLP/NamedEntityRecognition/0.2.0')
                #entities = algo.pipe(msg_txt)
            elif 'entity' in msg_txt:
                client = Algorithmia.client('sim3x6PzEv6m2icRR+23rqTTcOo1')
                algo = client.algo('StanfordNLP/NamedEntityRecognition/0.2.0')
                entities = algo.pipe(msg_txt)
                str_final = ""
                #print entities.result
                count = 0;
                for inner_l in entities.result:
                    for item in inner_l:
                        if count == 0:
                            pass
                        else:
                            str = item[0] + " - " + item[1] + ", "
                            str_final += str
                        count = count + 1
                self.msg_writer.send_message(event['channel'], str_final)
                #algo = client.algo('StanfordNLP/NamedEntityRecognition/0.2.0')
                #entities = algo.pipe(msg_txt)
            elif 'classify' in msg_txt:
                client = textapi.Client("a19bb245", "2623b77754833e2711998a0b0bdad9db")
                classifications = client.ClassifyByTaxonomy({"text": msg_txt, "taxonomy": "iab-qag"})
                sent_str = ""
                for category in classifications['categories']:
                    sent_str += category['label'] + ", "
                sent_str = sent_str[:-1]
                response = sent_str
                self.msg_writer.send_message(event['channel'], response)
            elif 'help' in msg_txt:
                self.msg_writer.write_help_message(event['channel'])
            elif 'joke' in msg_txt:
                self.msg_writer.write_joke(event['channel'])
            elif 'attachment' in msg_txt:
                self.msg_writer.demo_attachment(event['channel'])
            elif 'echo' in msg_txt:
                self.msg_writer.send_message(event['channel'], msg_txt)
            else:
                self.msg_writer.send_message(event['channel'], msg_txt)

            return

        def _is_direct_message(self, channel_id):
            client = Algorithmia.client('sim3x6PzEv6m2icRR+23rqTTcOo1')
            algo = client.algo('StanfordNLP/NamedEntityRecognition/0.2.0')
            entities = algo.pipe(msg_txt)
            str_final = ""
            count = 0;
            for inner_l in entities.result:
                for item in inner_l:
                    if count == 0:
                        pass
                    else:
                        str = item[0] + " - " + item[1] + ", "
                    str_final += str
                    count = count + 1
            self.msg_writer.send_message(event['channel'], str_final)
            return channel_id.startswith('D')
<commit_msg>Update event_handler.py<commit_after>import json
import logging
import random
import Algorithmia

from textblob import TextBlob
from text_corpus import TextCorpus
from aylienapiclient import textapi

logger = logging.getLogger(__name__)

class RtmEventHandler(object):
    def __init__(self, slack_clients, msg_writer):
        self.clients = slack_clients
        self.msg_writer = msg_writer
        #self.trump_corpus = trump_corpus

    def handle(self, event):

        if 'type' in event:
            self._handle_by_type(event['type'], event)

    def _handle_by_type(self, event_type, event):
        # See https://api.slack.com/rtm for a full list of events
        # if event_type == 'error':
            # error
            # ignore self.msg_writer.write_error(event['channel'], json.dumps(event))
        if event_type == 'message':
            # message was sent to channel
            self._handle_message(event)
        elif event_type == 'channel_joined':
            # you joined a channel
            self.msg_writer.send_message(event['channel'], "Let's make America great again!")
        elif event_type == 'group_joined':
            # you joined a private group
            self.msg_writer.write_help_message(event['channel'])
        elif event_type == 'file_shared':
            self.msg_writer.send_message(event['channel'], "Got your file, thanks!")
        else:
            pass

    def _handle_message(self, event):
        # Filter out messages from the bot itself
        if not self.clients.is_message_from_me(event['user']):

            msg_txt = event['text']

            #if self.clients.is_bot_mention(msg_txt) or self._is_direct_message(event['channel']):
                #txt_b = TextBlob(msg_txt)

            #if self.clients.is_bot_mention(msg_txt) or self._is_direct_message(event['channel']):
                # e.g. user typed: "@pybot tell me a joke!"
            if 'sentiment' in msg_txt:
                client = textapi.Client("a19bb245", "2623b77754833e2711998a0b0bdad9db")
                sentiment = client.Sentiment({"text": msg_txt})
                str = sentiment['polarity']
                str2 = " - %3.3f" % sentiment['polarity_confidence']
                str += str2
                self.msg_writer.send_message(event['channel'], str)
            elif 'tag' in msg_txt:
                #self.msg_writer.write_analytics(event['channel'], msg_txt)
                #if self.clients.is_bot_mention(msg_txt) or self._is_direct_message(event['channel']):
                #txt_b = TextBlob(msg_txt)
#                   response = txt_b.tags
#                   self.msg_writer.send_message(event['channel'], response)
                client = Algorithmia.client('sim3x6PzEv6m2icRR+23rqTTcOo1')
                algo = client.algo('nlp/AutoTag/1.0.0')
                tags = algo.pipe(text)
                str_final = ""
                #print entities.result
                for item in tags.result:
                    str_final += item
                    str_final += ", "
                self.msg_writer.send_message(event['channel'], str_final)
                #algo = client.algo('StanfordNLP/NamedEntityRecognition/0.2.0')
                #entities = algo.pipe(msg_txt)
            elif 'entity' in msg_txt:
                client = Algorithmia.client('sim3x6PzEv6m2icRR+23rqTTcOo1')
                algo = client.algo('StanfordNLP/NamedEntityRecognition/0.2.0')
                entities = algo.pipe(msg_txt)
                str_final = ""
                #print entities.result
                count = 0;
                for inner_l in entities.result:
                    for item in inner_l:
                        if count == 0:
                            pass
                        else:
                            str = item[0] + " - " + item[1] + ", "
                            str_final += str
                        count = count + 1
                self.msg_writer.send_message(event['channel'], str_final)
                #algo = client.algo('StanfordNLP/NamedEntityRecognition/0.2.0')
                #entities = algo.pipe(msg_txt)
            elif 'classify' in msg_txt:
                client = textapi.Client("a19bb245", "2623b77754833e2711998a0b0bdad9db")
                classifications = client.ClassifyByTaxonomy({"text": msg_txt, "taxonomy": "iab-qag"})
                sent_str = ""
                for category in classifications['categories']:
                    sent_str += category['label'] + ", "
                sent_str = sent_str[:-1]
                response = sent_str
                self.msg_writer.send_message(event['channel'], response)
            elif 'help' in msg_txt:
                self.msg_writer.write_help_message(event['channel'])
            elif 'joke' in msg_txt:
                self.msg_writer.write_joke(event['channel'])
            elif 'attachment' in msg_txt:
                self.msg_writer.demo_attachment(event['channel'])
            elif 'echo' in msg_txt:
                self.msg_writer.send_message(event['channel'], msg_txt)
            else:
                self.msg_writer.send_message(event['channel'], msg_txt)

            return

        def _is_direct_message(self, channel_id):
            client = Algorithmia.client('sim3x6PzEv6m2icRR+23rqTTcOo1')
            algo = client.algo('StanfordNLP/NamedEntityRecognition/0.2.0')
            entities = algo.pipe(msg_txt)
            str_final = ""
            count = 0;
            for inner_l in entities.result:
                for item in inner_l:
                    if count == 0:
                        pass
                    else:
                        str = item[0] + " - " + item[1] + ", "
                    str_final += str
                    count = count + 1
            self.msg_writer.send_message(event['channel'], str_final)
            return channel_id.startswith('D')
<|endoftext|>


<commit_before>'''
Module that will send emails from the redditgundeals@gmail.com account to users
that have requested e-mail alerts. Currently this will only send e-mails, it
doesn't allow for people to unsubscribe from alerts via e-mail. Need to figure
out how to read the inbox to do that.
'''

from helpers import color, inbox
from private import accountinfo
import smtplib

def __init__(self):
    self.temp = 0

def send_email(username, item, title, permalink, url):
    FROM = accountinfo.gmail_user
    TO = email
    SUBJECT = subject
    TEXT = inbox.compose_greeting(username) + \
    "We have found a match for your subscription to '" + item + "'! " + \
    "Below you will find the details:\n\t \n\t \n" + \
     "**Deal Title:**\t \n" + \
     title + "\t \n\t \n" + \
     "**Links:**\t \n" + \
     "[Reddit URL](" + permalink + ")" + "     |     " + \
     "[Sale URL](" + url + ")\n\n" + \
     "To unsubscribe from these alerts, message /u/GunDealsBot with the subject " + \
     item + "and with the message body 'Unsubscribe'.\n\n" + \
     inbox.compose_salutation()

    email = """From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    try:
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.login(gmail_user, gmail_pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        color.print_color('cyan', '\n\n' +
                            '----------- EMAIL SENT -----------\n' +
                            'destination: ' + destination + '\n' +
                            'subject:     ' + subject + '\n' +
                            'message:     ' + message + '\n\n\n')
    except:
        color.print_color('red', '\n\n' +
                            '----------- EMAIL FAILED -----------\n' +
                            'destination: ' + destination + '\n' +
                            'subject:     ' + subject + '\n' +
                            'message:     ' + message + '\n\n\n')
<commit_msg>gmail.py now pulls from private module.<commit_after>'''
Module that will send emails from the redditgundeals@gmail.com account to users
that have requested e-mail alerts. Currently this will only send e-mails, it
doesn't allow for people to unsubscribe from alerts via e-mail. Need to figure
out how to read the inbox to do that.
'''

from helpers import color, inbox
from private import accountinfo
import smtplib

def __init__(self):
    self.temp = 0

def send_email(username, item, title, permalink, url):
    FROM = accountinfo.gmail_user
    TO = email
    SUBJECT = subject
    TEXT = inbox.compose_greeting(username) + \
    "We have found a match for your subscription to '" + item + "'! " + \
    "Below you will find the details:\n\t \n\t \n" + \
     "**Deal Title:**\t \n" + \
     title + "\t \n\t \n" + \
     "**Links:**\t \n" + \
     "[Reddit URL](" + permalink + ")" + "     |     " + \
     "[Sale URL](" + url + ")\n\n" + \
     "To unsubscribe from these alerts, message /u/GunDealsBot with the subject " + \
     item + "and with the message body 'Unsubscribe'.\n\n" + \
     inbox.compose_salutation()

    email = """From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    try:
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.login(accountinfo.gmail_user, accountinfo.gmail_pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        color.print_color('cyan', '\n\n' +
                            '----------- EMAIL SENT -----------\n' +
                            'destination: ' + destination + '\n' +
                            'subject:     ' + subject + '\n' +
                            'message:     ' + message + '\n\n\n')
    except:
        color.print_color('red', '\n\n' +
                            '----------- EMAIL FAILED -----------\n' +
                            'destination: ' + destination + '\n' +
                            'subject:     ' + subject + '\n' +
                            'message:     ' + message + '\n\n\n')
<|endoftext|>