<commit_before>from flask import render_template, redirect, url_for
from flask import Markup
from flask.ext.login import current_user

from application import application
from application.page.views import retrieve_page

@application.route('/')
def index():
	blocks = [ retrieve_page("index/" + str(i)) for i in range(1, 5) ]

	return render_template('index.htm', blocks=blocks, path="index")

@application.route('/via')
@application.route('/page/via')
def viavia():
	return redirect(url_for('.index'))

def test():
	return 'testing'

def view_navigation_bar():
	return Markup(render_template('navigation/view_bar.htm'))

<commit_msg>/pages to root<commit_after>from flask import render_template, redirect, url_for
from flask import Markup
from flask.ext.login import current_user

from application import application
from application.page.views import retrieve_page

@application.route('/')
def index():
	blocks = [ retrieve_page("index/" + str(i)) for i in range(1, 5) ]

	return render_template('index.htm', blocks=blocks, path="index")

@application.route('/via')
@application.route('/page/via')
@application.route('/page')
def viavia():
	return redirect(url_for('.index'))

def test():
	return 'testing'

def view_navigation_bar():
	return Markup(render_template('navigation/view_bar.htm'))

<|endoftext|>


<commit_before>from applitools.errors import EyesError
from applitools.geometry import Region

# Ignore regions related classes.

class IgnoreRegionByElement(object):
    def __init__(self, element):
        self.element = element

    def get_region(self, driver, eyes_screenshot):
        return eyes_screenshot.get_element_region_in_frame_viewport(self.element)

    def _str_(self):
        return "{0} Element: {}".format(self.__class__.__name__,  self.element)

class IgnoreRegionBySelector(object):
    def __init__(self, by, value):
        """
        :param by: (selenium.webdriver.common.by.By) The "by" part of a selenium selector for an element which
            represents the ignore region
        :param value: (str) The "value" part of a selenium selector for an element which represents the ignore region.
        """
        self.by = by
        self.value = value

    def get_region(self, driver, eyes_screenshot):
        element = driver.find_element(self.by, self.value)
        return eyes_screenshot.get_element_region_in_frame_viewport(element)

    def _str_(self):
        return "{0} {{by: {1}, value: {2}}}".format(self.__class__.__name__, self.by, self.value)

class _NopRegionWrapper(object):
    def __init__(self, region):
        self.region = region

    def get_region(self, driver, eyes_screenshot):
        return self.region

    def __str__(self):
        return str(self.region)

# Floating regions related classes.

class FloatingBounds(object):
    def __init__(self, max_left_offset=0, max_up_offset=0, max_right_offset=0, max_down_offset=0):
        self.max_left_offset = max_left_offset
        self.max_up_offset = max_up_offset
        self.max_right_offset = max_right_offset
        self.max_down_offset = max_down_offset

class FloatingRegion(object):
    def __init__(self, region, bounds):
        """
        :param region: (Region) The inner region (the floating part).
        :param bounds: (FloatingBounds) The outer rectangle bounding the inner region.
        """
        self.region = region
        self.bounds = bounds

    def get_region(self, driver, eyes_screenshot):
        """Used for compatibility when iterating over regions"""
        return self

    def __getstate__(self):
        return dict(top=self.region.top,
                    left=self.region.left,
                    width=self.region.width,
                    height=self.region.height,
                    maxLeftOffset=self.bounds.max_left_offset,
                    maxUpOffset=self.bounds.max_up_offset,
                    maxRightOffset=self.bounds.max_right_offset,
                    maxDownOffset=self.bounds.max_down_offset)

    # This is required in order for jsonpickle to work on this object.
    # noinspection PyMethodMayBeStatic
    def __setstate__(self, state):
        raise EyesError('Cannot create FloatingRegion instance from dict!')

    def _str_(self):
        return "{0} {{region: {1}, bounds: {2}}}".format(self.__class__.__name__, self.region, self.bounds)

class FloatingRegionByElement(object):
    def __init__(self, element, bounds):
        """
        :param element: (WebElement|EyesWebElement) The element which represents the inner region (the floating part).
        :param bounds: (FloatingBounds) The outer rectangle bounding the inner region.
        """
        self.element = element
        self.bounds = bounds

    def get_region(self, driver, eyes_screenshot):
        region = eyes_screenshot.get_element_region_in_frame_viewport(self.element)
        return FloatingRegion(region, self.bounds)

    def _str_(self):
        return "{0} {{element: {1}, bounds: {2}}}".format(self.__class__.__name__, self.element, self.bounds)

class FloatingRegionBySelector(object):
    def __init__(self, by, value, bounds):
        """
        :param by: (selenium.webdriver.common.by.By) The "by" part of a selenium selector for an element which
            represents the inner region
        :param value: (str) The "value" part of a selenium selector for an element which represents the inner region.
        :param bounds: (FloatingBounds) The outer rectangle bounding the inner region.
        """
        self.by = by
        self.value = value
        self.bounds = bounds

    def get_region(self, driver, eyes_screenshot):
        element = driver.find_element(self.by, self.value)
        region = eyes_screenshot.get_element_region_in_frame_viewport(element)
        return FloatingRegion(region, self.bounds)

    def _str_(self):
        return "{0} {{element: {{ by: {1}, value: {2}}}, bounds: {3} }}".format(self.__class__.__name__, self.by,
                                                                                self.value, self.bounds)

# Main class for the module
class Target(object):
    """
    Target for an eyes.check_window/region.
    """
    def __init__(self):
        self._ignore_caret = False
        self._ignore_regions = []
        self._floating_regions = []

    def ignore(self, *regions):
        """
        Add ignore regions to this target.
        :param regions: Ignore regions to add. Can be of several types:
            (Region) Region specified by coordinates
            (IgnoreRegionBySelector) Region specified by a selector of an element
            (IgnoreRegionByElement) Region specified by a WebElement instance.
        :return: (Target) self.
        """
        for region in regions:
            if region is None:
                continue
            if isinstance(region, Region):
                self._ignore_regions.append(_NopRegionWrapper(region))
            else:
                self._ignore_regions.append(region)
        return self

    def floating(self, *regions):
        """
        Add floating regions to this target.
        :param regions: Floating regions to add. Can be of several types:
            (Region) Region specified by coordinates
            (FloatingRegionByElement) Region specified by a WebElement instance.
            (FloatingRegionBySelector) Region specified by a selector of an element
        :return: (Target) self.
        """
        for region in regions:
            if region is None:
                continue
            self._floating_regions.append(region)
        return self

    def ignore_caret(self, ignore=True):
        """
        Whether we should ignore caret when matching screenshots.
        :param ignore: (boolean)
        :return: (Target) self
        """
        self._ignore_caret = ignore
        return self

    def get_ignore_caret(self):
        return self._ignore_caret

    @property
    def ignore_regions(self):
        """The ignore regions defined on the current target."""
        return self._ignore_regions

    @property
    def floating_regions(self):
        """The floating regions defined on the current target."""
        return self._floating_regions

<commit_msg>Set ignore_caret as True by default<commit_after>from applitools.errors import EyesError
from applitools.geometry import Region

# Ignore regions related classes.

class IgnoreRegionByElement(object):
    def __init__(self, element):
        self.element = element

    def get_region(self, driver, eyes_screenshot):
        return eyes_screenshot.get_element_region_in_frame_viewport(self.element)

    def _str_(self):
        return "{0} Element: {}".format(self.__class__.__name__,  self.element)

class IgnoreRegionBySelector(object):
    def __init__(self, by, value):
        """
        :param by: (selenium.webdriver.common.by.By) The "by" part of a selenium selector for an element which
            represents the ignore region
        :param value: (str) The "value" part of a selenium selector for an element which represents the ignore region.
        """
        self.by = by
        self.value = value

    def get_region(self, driver, eyes_screenshot):
        element = driver.find_element(self.by, self.value)
        return eyes_screenshot.get_element_region_in_frame_viewport(element)

    def _str_(self):
        return "{0} {{by: {1}, value: {2}}}".format(self.__class__.__name__, self.by, self.value)

class _NopRegionWrapper(object):
    def __init__(self, region):
        self.region = region

    def get_region(self, driver, eyes_screenshot):
        return self.region

    def __str__(self):
        return str(self.region)

# Floating regions related classes.

class FloatingBounds(object):
    def __init__(self, max_left_offset=0, max_up_offset=0, max_right_offset=0, max_down_offset=0):
        self.max_left_offset = max_left_offset
        self.max_up_offset = max_up_offset
        self.max_right_offset = max_right_offset
        self.max_down_offset = max_down_offset

class FloatingRegion(object):
    def __init__(self, region, bounds):
        """
        :param region: (Region) The inner region (the floating part).
        :param bounds: (FloatingBounds) The outer rectangle bounding the inner region.
        """
        self.region = region
        self.bounds = bounds

    def get_region(self, driver, eyes_screenshot):
        """Used for compatibility when iterating over regions"""
        return self

    def __getstate__(self):
        return dict(top=self.region.top,
                    left=self.region.left,
                    width=self.region.width,
                    height=self.region.height,
                    maxLeftOffset=self.bounds.max_left_offset,
                    maxUpOffset=self.bounds.max_up_offset,
                    maxRightOffset=self.bounds.max_right_offset,
                    maxDownOffset=self.bounds.max_down_offset)

    # This is required in order for jsonpickle to work on this object.
    # noinspection PyMethodMayBeStatic
    def __setstate__(self, state):
        raise EyesError('Cannot create FloatingRegion instance from dict!')

    def _str_(self):
        return "{0} {{region: {1}, bounds: {2}}}".format(self.__class__.__name__, self.region, self.bounds)

class FloatingRegionByElement(object):
    def __init__(self, element, bounds):
        """
        :param element: (WebElement|EyesWebElement) The element which represents the inner region (the floating part).
        :param bounds: (FloatingBounds) The outer rectangle bounding the inner region.
        """
        self.element = element
        self.bounds = bounds

    def get_region(self, driver, eyes_screenshot):
        region = eyes_screenshot.get_element_region_in_frame_viewport(self.element)
        return FloatingRegion(region, self.bounds)

    def _str_(self):
        return "{0} {{element: {1}, bounds: {2}}}".format(self.__class__.__name__, self.element, self.bounds)

class FloatingRegionBySelector(object):
    def __init__(self, by, value, bounds):
        """
        :param by: (selenium.webdriver.common.by.By) The "by" part of a selenium selector for an element which
            represents the inner region
        :param value: (str) The "value" part of a selenium selector for an element which represents the inner region.
        :param bounds: (FloatingBounds) The outer rectangle bounding the inner region.
        """
        self.by = by
        self.value = value
        self.bounds = bounds

    def get_region(self, driver, eyes_screenshot):
        element = driver.find_element(self.by, self.value)
        region = eyes_screenshot.get_element_region_in_frame_viewport(element)
        return FloatingRegion(region, self.bounds)

    def _str_(self):
        return "{0} {{element: {{ by: {1}, value: {2}}}, bounds: {3} }}".format(self.__class__.__name__, self.by,
                                                                                self.value, self.bounds)

# Main class for the module
class Target(object):
    """
    Target for an eyes.check_window/region.
    """
    def __init__(self):
        self._ignore_caret = True
        self._ignore_regions = []
        self._floating_regions = []

    def ignore(self, *regions):
        """
        Add ignore regions to this target.
        :param regions: Ignore regions to add. Can be of several types:
            (Region) Region specified by coordinates
            (IgnoreRegionBySelector) Region specified by a selector of an element
            (IgnoreRegionByElement) Region specified by a WebElement instance.
        :return: (Target) self.
        """
        for region in regions:
            if region is None:
                continue
            if isinstance(region, Region):
                self._ignore_regions.append(_NopRegionWrapper(region))
            else:
                self._ignore_regions.append(region)
        return self

    def floating(self, *regions):
        """
        Add floating regions to this target.
        :param regions: Floating regions to add. Can be of several types:
            (Region) Region specified by coordinates
            (FloatingRegionByElement) Region specified by a WebElement instance.
            (FloatingRegionBySelector) Region specified by a selector of an element
        :return: (Target) self.
        """
        for region in regions:
            if region is None:
                continue
            self._floating_regions.append(region)
        return self

    def ignore_caret(self, ignore=True):
        """
        Whether we should ignore caret when matching screenshots.
        :param ignore: (boolean)
        :return: (Target) self
        """
        self._ignore_caret = ignore
        return self

    def get_ignore_caret(self):
        return self._ignore_caret

    @property
    def ignore_regions(self):
        """The ignore regions defined on the current target."""
        return self._ignore_regions

    @property
    def floating_regions(self):
        """The floating regions defined on the current target."""
        return self._floating_regions

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""Command line execution listener module of ping-me"""
from __future__ import print_function
from dateutil import parser
import argparse
import calendar
import datetime
import getpass
import hashlib
import os
import sys
import time

import ping_me

home = os.path.expanduser("~")

def main():
    argparser = argparse.ArgumentParser(description='ping-me')

    argparser.add_argument("-d", "--date", action="store", dest="DATE",
                           default=None, nargs="+")
    argparser.add_argument("-t", "--time", action="store", dest="TIME",
                           default=None, nargs="+")
    argparser.add_argument("message", action="store", help="Message",
                           nargs="+")
    argparser.add_argument("-v", action="store_true", default=False)

    args = argparser.parse_args()

    if args.DATE != None and args.TIME != None:
        message = ' '.join(args.message).lstrip('to ')
        date_time = parser.parse(' '.join(args.DATE) +
                                 ' ' + ' '.join(args.TIME))

        ping_me.engine.engine(message, date_time.year, date_time.month,
                              date_time.day, date_time.hour, date_time.minute,
                              args.v)
    elif args.DATE == None:
        m_time = parser.parse(' '.join(args.TIME))
        c_time = datetime.datetime.now()
        if (m_time - c_time).days == -1:
            m_time += datetime.timedelta(1)
        message = ' '.join(args.message).lstrip('to ')
        ping_me.engine.engine(message, m_time.year, m_time.month,
                              m_time.day, m_time.hour, m_time.minute, args.v)
    elif args.TIME == None:
        c_time = `time.localtime().tm_hour` + ":" + `time.localtime().tm_min`
        m_date = parser.parse(' '.join(args.DATE) + ' ' + c_time)
        message = ' '.join(args.message).lstrip('to ')
        ping_me.engine.engine(message, m_date.year, m_date.month,
                              m_date.day, m_date.hour, m_date.minute, args.v)
    else:
        if len(message) == 1 and message == ['-e']:
            detailed_usage()
        elif len(message) == 1 and message == ['config']:
            ping_me.authenticate.newuser()
        elif len(message) == 1 and message == ['reconfig']:
            reconfig()
        else:
            # Process smartly
            pass

def detailed_usage():
    print("Welcome to the detailed documentation of ping-me !")
    # Inspired from 'import this'
    s = " "; l = "_ "; r = " _"; f = "/"; b = "\\"; p = "|"; d = "— "
    print(s*6 + l*5 + s + l*4 + r + s*12 + l + r*5 + s*2 + r + s*8 + l +
          s*7 + l*4)
    print(s*5 + f + s*8 + f + s*5 + f + s*4 + f + s + b + s*10 + f + s + f +
          s*12 + f + s + b + s*6 + f + s + p + s*6 + f + s*7)
    print(s*4 + f + s*8 + f + s*5 + f + s*4 + f + s*3 + b + s*8 + f + s + f +
          s*12 + f + s*3 + b + s*4 + f + s*2 + p + s*5 + f + s*7)
    print(s*3 + f + r*4 + f + s*5 + f + s*4 + f + s*5 + b + s*6 + f + s + f +
          s*2 + r*4 + s*2 + f + 5*s + b + s*2 + f + s*3 + p + s*4 + f + l*4)
    print(s*2 + f + s*14 + f + s*4 + f + s*7 + b + s*4 + f + s + f + s*9 + f +
          s*2 + f + s*7 + b + f + s*4 + p + s*3 + f + s*7)
    print(s + f + s*14 + f + s*4 + f + s*9 + b + s*2 + f + s + f + s*9 + f +
          s*2 + f + s*14 + p + s*2 + f + s*7)
    print(f + s*11 + d*4 + f + s*11 + b + f + s + f + (r*5)[1:] + f + s*2 + f
          + s*15 + p + s + f + (r*4)[1:])
    print("")
    print("ping-me works well with time and date flags already. "
          + "Use 'ping-me -h' for that option. "
          + "However, ping-me is smart enough to work without flags.\n")
    print("Examples : ")
    print("\t\t1. ping-me to call mom tonight")
    print("\t\t2. ping-me to buy milk early today")
    print("\t\t3. ping-me to go home seven days from now")
    print("\t\t4. ping-me to take a nap this afternoon")
    print("\t\t5. ping-me to go workout next month")
    print("")
    print("Report (and track process on fixing) bugs on "
          + "https://github.com/OrkoHunter/ping-me. Or simply write a mail "
          + "to Himanshu Mishra at himanshumishra[at]iitkgp[dot]ac[dot]in")

def reconfig():
    if not os.path.exists(home + "/.pingmeconfig"):
        ping_me.authenticate.newuser()
    else:
        old_pass = hashlib.md5(getpass.getpass("Old Password : " +
                                               "").rstrip()).hexdigest()
        if old_pass == ping_me.authenticate.extract_password():
            ping_me.authenticate.newuser()
        else:
            print("Authentication failed.")
            sys.exit(2)

if __name__ == "__main__":

    main()
<commit_msg>Fix bug in command line parse<commit_after># -*- coding: utf-8 -*-
"""Command line execution listener module of ping-me"""
from __future__ import print_function
from dateutil import parser
import argparse
import calendar
import datetime
import getpass
import hashlib
import os
import sys
import time

import ping_me

home = os.path.expanduser("~")

def main():
    argparser = argparse.ArgumentParser(description='ping-me')

    argparser.add_argument("-e", action="store_true", default=False)
    argparser.add_argument("-V", "--version", action="store_true",
                           default=False)
    argparser.add_argument("-d", "--date", action="store", dest="DATE",
                           default=None, nargs="+")
    argparser.add_argument("-t", "--time", action="store", dest="TIME",
                           default=None, nargs="+")
    argparser.add_argument("message", action="store", help="Message",
                           default=None, nargs="*")
    argparser.add_argument("-v", action="store_true", default=False)

    args = argparser.parse_args()

    if args.e:
        detailed_usage()
        sys.exit(2)

    if args.DATE != None and args.TIME != None:
        message = ' '.join(args.message).lstrip('to ')
        date_time = parser.parse(' '.join(args.DATE) +
                                 ' ' + ' '.join(args.TIME))

        ping_me.engine.engine(message, date_time.year, date_time.month,
                              date_time.day, date_time.hour, date_time.minute,
                              args.v)
    elif args.TIME != None:
        m_time = parser.parse(' '.join(args.TIME))
        c_time = datetime.datetime.now()
        if (m_time - c_time).days == -1:
            m_time += datetime.timedelta(1)
        message = ' '.join(args.message).lstrip('to ')
        ping_me.engine.engine(message, m_time.year, m_time.month,
                              m_time.day, m_time.hour, m_time.minute, args.v)
    elif args.DATE != None:
        c_time = `time.localtime().tm_hour` + ":" + `time.localtime().tm_min`
        m_date = parser.parse(' '.join(args.DATE) + ' ' + c_time)
        message = ' '.join(args.message).lstrip('to ')
        ping_me.engine.engine(message, m_date.year, m_date.month,
                              m_date.day, m_date.hour, m_date.minute, args.v)
    else:
        if len(args.message) == 1 and args.message == ['config']:
            ping_me.authenticate.newuser()
        elif len(args.message) == 1 and args.message == ['reconfig']:
            reconfig()
        else:
            # Process smartly
            pass

def detailed_usage():
    print("Welcome to the detailed documentation of ping-me !")
    # Inspired from 'import this'
    s = " "; l = "_ "; r = " _"; f = "/"; b = "\\"; p = "|"; d = "— "
    print(s*6 + l*5 + s + l*4 + r + s*12 + l + r*5 + s*2 + r + s*8 + l +
          s*7 + l*4)
    print(s*5 + f + s*8 + f + s*5 + f + s*4 + f + s + b + s*10 + f + s + f +
          s*12 + f + s + b + s*6 + f + s + p + s*6 + f + s*7)
    print(s*4 + f + s*8 + f + s*5 + f + s*4 + f + s*3 + b + s*8 + f + s + f +
          s*12 + f + s*3 + b + s*4 + f + s*2 + p + s*5 + f + s*7)
    print(s*3 + f + r*4 + f + s*5 + f + s*4 + f + s*5 + b + s*6 + f + s + f +
          s*2 + r*4 + s*2 + f + 5*s + b + s*2 + f + s*3 + p + s*4 + f + l*4)
    print(s*2 + f + s*14 + f + s*4 + f + s*7 + b + s*4 + f + s + f + s*9 + f +
          s*2 + f + s*7 + b + f + s*4 + p + s*3 + f + s*7)
    print(s + f + s*14 + f + s*4 + f + s*9 + b + s*2 + f + s + f + s*9 + f +
          s*2 + f + s*14 + p + s*2 + f + s*7)
    print(f + s*11 + d*4 + f + s*11 + b + f + s + f + (r*5)[1:] + f + s*2 + f
          + s*15 + p + s + f + (r*4)[1:])
    print("")
    print("ping-me works well with time and date flags already. "
          + "Use 'ping-me -h' for that option. "
          + "However, ping-me is smart enough to work without flags.\n")
    print("Examples : ")
    print("\t\t1. ping-me to call mom tonight")
    print("\t\t2. ping-me to buy milk early today")
    print("\t\t3. ping-me to go home seven days from now")
    print("\t\t4. ping-me to take a nap this afternoon")
    print("\t\t5. ping-me to go workout next month")
    print("")
    print("Report (and track process on fixing) bugs on "
          + "https://github.com/OrkoHunter/ping-me. Or simply write a mail "
          + "to Himanshu Mishra at himanshumishra[at]iitkgp[dot]ac[dot]in")

def reconfig():
    if not os.path.exists(home + "/.pingmeconfig"):
        ping_me.authenticate.newuser()
    else:
        old_pass = hashlib.md5(getpass.getpass("Old Password : " +
                                               "").rstrip()).hexdigest()
        if old_pass == ping_me.authenticate.extract_password():
            ping_me.authenticate.newuser()
        else:
            print("Authentication failed.")
            sys.exit(2)

if __name__ == "__main__":

    main()
<|endoftext|>


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

import io as StringIO
import sys
import unittest
import HTMLTestRunner

import tests
from tests.SampleTestPass import SampleTestPass
from tests.SampleTestFail import SampleTestFail
from tests.SampleTestBasic import SampleTestBasic

class TestHTMLTestRunner(unittest.TestCase):

    def setUp(self):

        self.suite = unittest.TestSuite()
        self.loader = unittest.TestLoader()

        self.suite.addTests(self.loader.loadTestsFromModule(tests.SampleTestPass))
        self.suite.addTests(self.loader.loadTestsFromModule(tests.SampleTestFail))
        self.suite.addTests(self.loader.loadTestsFromModule(tests.SampleTestBasic))

        self.results_output_buffer = StringIO.StringIO()
        HTMLTestRunner.HTMLTestRunner(stream=self.results_output_buffer).run(self.suite)
        self.byte_output = self.results_output_buffer.getvalue()

    def test_SampleTestPass(self):
        output1="".join(self.byte_output.split())
        output2="".join(SampleTestPass.EXPECTED_RESULT.split())
        self.assertGreater(output1.find(output2),0)

    @unittest.skip("Test Skipping")
    def test_SampleTestSkip(self):
        self.fail("This error should never be displayed")

    def test_SampleTestFail(self):
        output1="".join(self.byte_output.split())
        output2="".join(SampleTestFail.EXPECTED_RESULT.split())
        self.assertGreater(output1.find(output2),0)

    def test_SampleTestBasic(self):
        output1="".join(self.byte_output.split())
        output2="".join(SampleTestBasic.EXPECTED_RESULT.split())
        self.assertGreater(output1.find(output2),0)

def main():
    suite = unittest.TestLoader().loadTestsFromTestCase(TestHTMLTestRunner)
    unittest.TextTestRunner().run(suite)

if __name__ == "__main__":
    main()
<commit_msg>minor import cleanup and add skip testcase<commit_after># -*- coding: utf-8 -*-

from io import StringIO
from unittest import TestSuite, TestLoader, skip, TextTestRunner, TestCase
from HTMLTestRunner import HTMLTestRunner

import tests
from tests.SampleTestPass import SampleTestPass
from tests.SampleTestFail import SampleTestFail
from tests.SampleTestBasic import SampleTestBasic

class TestHTMLTestRunner(TestCase):

    def setUp(self):

        self.suite = TestSuite()
        self.loader = TestLoader()

        self.suite.addTests(self.loader.loadTestsFromModule(tests.SampleTestPass))
        self.suite.addTests(self.loader.loadTestsFromModule(tests.SampleTestFail))
        self.suite.addTests(self.loader.loadTestsFromModule(tests.SampleTestBasic))

        self.results_output_buffer = StringIO()
        HTMLTestRunner(stream=self.results_output_buffer).run(self.suite)
        self.byte_output = self.results_output_buffer.getvalue()

    def test_SampleTestPass(self):
        output1="".join(self.byte_output.split())
        output2="".join(SampleTestPass.EXPECTED_RESULT.split())
        self.assertGreater(output1.find(output2),0)

    @skip("Test Skipping")
    def test_SampleTestSkip(self):
        self.fail("This error should never be displayed")

    def test_SampleTestFail(self):
        output1="".join(self.byte_output.split())
        output2="".join(SampleTestFail.EXPECTED_RESULT.split())
        self.assertGreater(output1.find(output2),0)

    def test_SampleTestBasic(self):
        output1="".join(self.byte_output.split())
        output2="".join(SampleTestBasic.EXPECTED_RESULT.split())
        self.assertGreater(output1.find(output2),0)

def main():
    suite = TestLoader().loadTestsFromTestCase(TestHTMLTestRunner)
    TextTestRunner().run(suite)

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


<commit_before># This file is part of the GBI project.
# Copyright (C) 2012 Omniscale GmbH & Co. KG <http://omniscale.com>
#
# 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 threading

from contextlib import contextmanager

from geobox.model.tasks import Task
from geobox.utils import join_threads

import logging
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__)

class ProcessThread(threading.Thread):
    def __init__(self, app_state, task_class_mapping, task_process_mapping):
        threading.Thread.__init__(self)
        self.daemon = True
        self.app_state = app_state
        self.background_threads = {}
        self.task_process_mapping = task_process_mapping
        self.task_classes = task_class_mapping.values()
        self.concurrency = 2

    def run(self):
        self.cleanup_old_tasks()
        while not self.app_state.wait_for_app_shutdown(timeout=2):
            self.check_running_tasks()
            self.check_new_tasks()

        self.stop_running_tasks()

    def shutdown(self):
        pass

    def cleanup_old_tasks(self):
        session = self.app_state.user_db_session()
        query = session.query(Task).with_polymorphic(self.task_classes)
        query = query.filter(Task.is_running == True)
        for task in query:
            task.is_running = False
        session.commit()

    def check_new_tasks(self):
        free_task_slots = self.concurrency - len(self.background_threads)
        if free_task_slots <= 0:
            return

        session = self.app_state.user_db_session()
        query = session.query(Task).with_polymorphic(self.task_classes)
        query = query.filter(Task.is_active == True).filter(Task.is_running == False).filter(Task.is_paused == False)
        query = query.order_by(Task.time_created)
        query = query.limit(free_task_slots)
        for task in query:
            log.debug('starting %s', task)
            self.start_task_process(task)
            task.is_running = True
            session.commit()
        session.close()

    def start_task_process(self, task):
        log.debug('starting new process for %s', task)
        process_class = self.task_process_mapping[task.type]
        p = process_class(self.app_state, task)
        self.background_threads[task.id] = p
        p.start()

    def check_running_tasks(self):
        log.debug('checking tasks')
        session = self.app_state.user_db_session()
        for task_id, t in self.background_threads.items():
            if not t.is_alive():
                log.debug('process %s terminated', t)
                del self.background_threads[task_id]
                task = session.query(Task).with_polymorphic('*').get(task_id)
                task.is_running = False
                session.commit()

        for task_id, t in self.background_threads.items():
            task = session.query(Task).with_polymorphic('*').get(task_id)
            if task.is_paused:
                log.debug('task %s paused', t)
                t.terminate()

    def stop_running_tasks(self):
        log.debug('stopping task')
        for t in self.background_threads.itervalues():
            log.debug('stopping task %s', t)
            t.terminate()

        join_threads(self.background_threads.values(), max_wait_time=5)
        self.background_threads.clear()

class ProcessBase(threading.Thread):
    def __init__(self, app_state, task):
        threading.Thread.__init__(self)
        # store only task id, we don't want to keep task object
        # around in other thread
        self.task_id = task.id
        self.app_state = app_state

    @contextmanager
    def task(self):
        """
        Contextmanager for task object. Changes on object will
        be saved when no exception is raised.
        """
        session = self.app_state.user_db_session()
        query = session.query(Task).with_polymorphic('*')
        task = query.filter(Task.id == self.task_id).one()
        try:
            yield task
        except Exception:
            session.rollback()
            raise
        else:
            session.commit()

    def task_done(self):
        """
        Mark task as done.
        """
        with self.task() as task:
            task.refresh_time_updated()
            task.is_running = False
            task.is_active = False
            task.progress = 1.0
            log.debug('Task %d done' % self.task_id)

    def task_failed(self, e):
        """
        Mark task as failed
        """
        with self.task() as task:
            task.is_running = False
            task.is_active = True
            task.is_paused = True
            task.error = str(e)
            log.error('Task %d failed' % self.task_id)
            log.exception(e)

    def update_task_status(self):
        with self.task() as task:
            task.refresh_time_updated()

    def process(self):
        raise NotImplementedError()

    def run(self):
        self.process()

    def terminate(self):
        pass<commit_msg>Send logging infos to server when task process added<commit_after># This file is part of the GBI project.
# Copyright (C) 2012 Omniscale GmbH & Co. KG <http://omniscale.com>
#
# 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 threading

from contextlib import contextmanager

from geobox.model.tasks import Task
from geobox.utils import join_threads

from geobox.lib.server_logging import send_task_logging

import logging
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__)

class ProcessThread(threading.Thread):
    def __init__(self, app_state, task_class_mapping, task_process_mapping):
        threading.Thread.__init__(self)
        self.daemon = True
        self.app_state = app_state
        self.background_threads = {}
        self.task_process_mapping = task_process_mapping
        self.task_classes = task_class_mapping.values()
        self.concurrency = 2

    def run(self):
        self.cleanup_old_tasks()
        while not self.app_state.wait_for_app_shutdown(timeout=2):
            self.check_running_tasks()
            self.check_new_tasks()

        self.stop_running_tasks()

    def shutdown(self):
        pass

    def cleanup_old_tasks(self):
        session = self.app_state.user_db_session()
        query = session.query(Task).with_polymorphic(self.task_classes)
        query = query.filter(Task.is_running == True)
        for task in query:
            task.is_running = False
        session.commit()

    def check_new_tasks(self):
        free_task_slots = self.concurrency - len(self.background_threads)
        if free_task_slots <= 0:
            return

        session = self.app_state.user_db_session()
        query = session.query(Task).with_polymorphic(self.task_classes)
        query = query.filter(Task.is_active == True).filter(Task.is_running == False).filter(Task.is_paused == False)
        query = query.order_by(Task.time_created)
        query = query.limit(free_task_slots)
        for task in query:
            log.debug('starting %s', task)
            self.start_task_process(task)
            task.is_running = True
            session.commit()
        session.close()

    def start_task_process(self, task):
        log.debug('starting new process for %s', task)
        send_task_logging(self.app_state, task)
        process_class = self.task_process_mapping[task.type]
        p = process_class(self.app_state, task)
        self.background_threads[task.id] = p
        p.start()

    def check_running_tasks(self):
        log.debug('checking tasks')
        session = self.app_state.user_db_session()
        for task_id, t in self.background_threads.items():
            if not t.is_alive():
                log.debug('process %s terminated', t)
                del self.background_threads[task_id]
                task = session.query(Task).with_polymorphic('*').get(task_id)
                task.is_running = False
                session.commit()

        for task_id, t in self.background_threads.items():
            task = session.query(Task).with_polymorphic('*').get(task_id)
            if task.is_paused:
                log.debug('task %s paused', t)
                t.terminate()

    def stop_running_tasks(self):
        log.debug('stopping task')
        for t in self.background_threads.itervalues():
            log.debug('stopping task %s', t)
            t.terminate()

        join_threads(self.background_threads.values(), max_wait_time=5)
        self.background_threads.clear()

class ProcessBase(threading.Thread):
    def __init__(self, app_state, task):
        threading.Thread.__init__(self)
        # store only task id, we don't want to keep task object
        # around in other thread
        self.task_id = task.id
        self.app_state = app_state

    @contextmanager
    def task(self):
        """
        Contextmanager for task object. Changes on object will
        be saved when no exception is raised.
        """
        session = self.app_state.user_db_session()
        query = session.query(Task).with_polymorphic('*')
        task = query.filter(Task.id == self.task_id).one()
        try:
            yield task
        except Exception:
            session.rollback()
            raise
        else:
            session.commit()

    def task_done(self):
        """
        Mark task as done.
        """
        with self.task() as task:
            task.refresh_time_updated()
            task.is_running = False
            task.is_active = False
            task.progress = 1.0
            log.debug('Task %d done' % self.task_id)

    def task_failed(self, e):
        """
        Mark task as failed
        """
        with self.task() as task:
            task.is_running = False
            task.is_active = True
            task.is_paused = True
            task.error = str(e)
            log.error('Task %d failed' % self.task_id)
            log.exception(e)

    def update_task_status(self):
        with self.task() as task:
            task.refresh_time_updated()

    def process(self):
        raise NotImplementedError()

    def run(self):
        self.process()

    def terminate(self):
        pass<|endoftext|>


<commit_before>import sys
import time
import serial
from serial.tools import list_ports

class TCLab(object):

    def __init__(self, port=None, baud=9600):
        if (sys.platform == 'darwin') and not port:
            port = '/dev/tty.wchusbserial1410'
        else:
            port = self.findPort()
        print('Opening connection')
        self.sp = serial.Serial(port=port, baudrate=baud, timeout=2)
        self.sp.flushInput()
        self.sp.flushOutput()
        time.sleep(3)
        print('TCLab connected via Arduino on port ' + port)

    def findPort(self):
        found = False
        for port in list(list_ports.comports()):
            # Arduino Uno
            if port[2].startswith('USB VID:PID=16D0:0613'):
                port = port[0]
                found = True
            # HDuino
            if port[2].startswith('USB VID:PID=1A86:7523'):
                port = port[0]
                found = True
        if (not found):
            print('Arduino COM port not found')
            print('Please ensure that the USB cable is connected')
            print('--- Printing Serial Ports ---')
            for port in list(serial.tools.list_ports.comports()):
                print(port[0] + ' ' + port[1] + ' ' + port[2])
            print('For Windows:')
            print('  Open device manager, select "Ports (COM & LPT)"')
            print('  Look for COM port of Arduino such as COM4')
            print('For MacOS:')
            print('  Open terminal and type: ls /dev/*.')
            print('  Search for /dev/tty.usbmodem* or /dev/tty.usbserial*. The port number is *.')
            print('For Linux')
            print('  Open terminal and type: ls /dev/tty*')
            print('  Search for /dev/ttyUSB* or /dev/ttyACM*. The port number is *.')
            print('')
            port = input('Input port: ')
            # or hard-code it here
            #port = 'COM3'
        return port

    def stop(self):
        return self.read('X')

    def version(self):
        return self.read('VER')

    @property
    def T1(self):
        self._T1 = float(self.read('T1'))
        return self._T1

    @property
    def T2(self):
        self._T2 = float(self.read('T2'))
        return self._T2

    def LED(self,pwm):
        pwm = max(0.0,min(100.0,pwm))/2.0
        self.write('LED',pwm)
        return pwm

    def Q1(self,pwm):
        pwm = max(0.0,min(100.0,pwm))
        self.write('Q1',pwm)
        return pwm

    def Q2(self,pwm):
        pwm = max(0.0,min(100.0,pwm))
        self.write('Q2',pwm)
        return pwm

    def read(self,cmd):
        cmd_str = self.build_cmd_str(cmd,'')
        try:
            self.sp.write(cmd_str.encode())
            self.sp.flush()
        except Exception:
            return None
        return self.sp.readline().decode('UTF-8').replace("\r\n", "")

    def write(self,cmd,pwm):
        cmd_str = self.build_cmd_str(cmd,(pwm,))
        try:
            self.sp.write(cmd_str.encode())
            self.sp.flush()
        except:
            return None
        return self.sp.readline().decode('UTF-8').replace("\r\n", "")

    def build_cmd_str(self,cmd, args=None):
        """
        Build a command string that can be sent to the arduino.

        Input:
            cmd (str): the command to send to the arduino, must not
                contain a % character
            args (iterable): the arguments to send to the command
        """
        if args:
            args = ' '.join(map(str, args))
        else:
            args = ''
        return "{cmd} {args}\n".format(cmd=cmd, args=args)

    def close(self):
        try:
            self.sp.close()
            print('Arduino disconnected successfully')
        except:
            print('Problems disconnecting from Arduino.')
            print('Please unplug and reconnect Arduino.')
        return True

<commit_msg>Tem[erature Control Lab<commit_after>import sys
import time
import numpy as np
try:
    import serial
except:
    import pip
    pip.main(['install','pyserial'])
    import serial
from serial.tools import list_ports

class TCLab(object):

    def __init__(self, port=None, baud=9600):
        if (sys.platform == 'darwin') and not port:
            port = '/dev/tty.wchusbserial1410'
        else:
            port = self.findPort()
        print('Opening connection')
        self.sp = serial.Serial(port=port, baudrate=baud, timeout=2)
        self.sp.flushInput()
        self.sp.flushOutput()
        time.sleep(3)
        print('TCLab connected via Arduino on port ' + port)

    def findPort(self):
        found = False
        for port in list(list_ports.comports()):
            # Arduino Uno
            if port[2].startswith('USB VID:PID=16D0:0613'):
                port = port[0]
                found = True
            # Arduino HDuino
            if port[2].startswith('USB VID:PID=1A86:7523'):
                port = port[0]
                found = True
            # Arduino Leonardo
            if port[2].startswith('USB VID:PID=2341:8036'):
                port = port[0]
                found = True
        if (not found):
            print('Arduino COM port not found')
            print('Please ensure that the USB cable is connected')
            print('--- Printing Serial Ports ---')
            for port in list(serial.tools.list_ports.comports()):
                print(port[0] + ' ' + port[1] + ' ' + port[2])
            print('For Windows:')
            print('  Open device manager, select "Ports (COM & LPT)"')
            print('  Look for COM port of Arduino such as COM4')
            print('For MacOS:')
            print('  Open terminal and type: ls /dev/*.')
            print('  Search for /dev/tty.usbmodem* or /dev/tty.usbserial*. The port number is *.')
            print('For Linux')
            print('  Open terminal and type: ls /dev/tty*')
            print('  Search for /dev/ttyUSB* or /dev/ttyACM*. The port number is *.')
            print('')
            port = input('Input port: ')
            # or hard-code it here
            #port = 'COM3' # for Windows
            #port = '/dev/tty.wchusbserial1410' # for MacOS
        return port

    def stop(self):
        return self.read('X')

    def version(self):
        return self.read('VER')

    @property
    def T1(self):
        self._T1 = float(self.read('T1'))
        return self._T1

    @property
    def T2(self):
        self._T2 = float(self.read('T2'))
        return self._T2

    def LED(self,pwm):
        pwm = max(0.0,min(100.0,pwm))/2.0
        self.write('LED',pwm)
        return pwm

    def Q1(self,pwm):
        pwm = max(0.0,min(100.0,pwm))
        self.write('Q1',pwm)
        return pwm

    def Q2(self,pwm):
        pwm = max(0.0,min(100.0,pwm))
        self.write('Q2',pwm)
        return pwm

    # save txt file with data and set point
    # t = time
    # u1,u2 = heaters
    # y1,y2 = tempeatures
    # sp1,sp2 = setpoints
    def save_txt(self,t,u1,u2,y1,y2,sp1,sp2):
        data = np.vstack((t,u1,u2,y1,y2,sp1,sp2))  # vertical stack
        data = data.T                 # transpose data
        top = 'Time (sec), Heater 1 (%), Heater 2 (%), ' \
          + 'Temperature 1 (degC), Temperature 2 (degC), ' \
          + 'Set Point 1 (degC), Set Point 2 (degC)'
        np.savetxt('data.txt',data,delimiter=',',header=top,comments='')

    def read(self,cmd):
        cmd_str = self.build_cmd_str(cmd,'')
        try:
            self.sp.write(cmd_str.encode())
            self.sp.flush()
        except Exception:
            return None
        return self.sp.readline().decode('UTF-8').replace("\r\n", "")

    def write(self,cmd,pwm):
        cmd_str = self.build_cmd_str(cmd,(pwm,))
        try:
            self.sp.write(cmd_str.encode())
            self.sp.flush()
        except:
            return None
        return self.sp.readline().decode('UTF-8').replace("\r\n", "")

    def build_cmd_str(self,cmd, args=None):
        """
        Build a command string that can be sent to the arduino.

        Input:
            cmd (str): the command to send to the arduino, must not
                contain a % character
            args (iterable): the arguments to send to the command
        """
        if args:
            args = ' '.join(map(str, args))
        else:
            args = ''
        return "{cmd} {args}\n".format(cmd=cmd, args=args)

    def close(self):
        try:
            self.sp.close()
            print('Arduino disconnected successfully')
        except:
            print('Problems disconnecting from Arduino.')
            print('Please unplug and reconnect Arduino.')
        return True

<|endoftext|>


<commit_before>import sys
import os
import subprocess

# ===============================================================================
# Populate AKAZE-based programs
programs = ['AKAZE_features', 'AKAZE_match', 'AKAZE_compare']
for i in range(len(programs)):
  programs[i] = './bin/Release/' + programs[i]
if sys.platform == 'win32':
  for i in range(len(programs)):
    programs[i] = programs[i] + '.exe'

# ===============================================================================
# Helper functions
def extract_AKAZE_features(imagePath):
  command = " ".join([programs[0], ' -i ', imagePath])
  os.system(command)

def match_AKAZE_features(imagePath1, imagePath2, gdTruthHomography):
  command = " ".join(
    [programs[1], ' -L ', imagePath1, ' -R ', imagePath2, ' -H ', gdTruthHomography] )
  os.system(command)

def compare_AKAZE_BRISK_ORB(imagePath1, imagePath2, gdTruthHomography):
  command = " ".join(
    [programs[2], ' -L ', imagePath1, ' -R ', imagePath2, ' -H ', gdTruthHomography] )
  os.system(command)

# ===============================================================================
# Example datasets
imagePath1 = './datasets/iguazu/img1.pgm'
imagePath2 = './datasets/iguazu/img4.pgm'
gdTruthHomography = './datasets/iguazu/H1to4p'

# Go!
extract_AKAZE_features(imagePath1)
match_AKAZE_features(imagePath1, imagePath2, gdTruthHomography)
compare_AKAZE_BRISK_ORB(imagePath1, imagePath2, gdTruthHomography)
<commit_msg>Fix os path and case sensitive akaze binary filenames<commit_after>import sys
import os
import subprocess

# ===============================================================================
# Populate AKAZE-based programs
programs = ['akaze_features', 'akaze_match', 'akaze_compare']
for i in range(len(programs)):
  programs[i] = os.path.join('.', 'bin' , 'Release' , programs[i])

# ===============================================================================
# Helper functions
def extract_AKAZE_features(imagePath):
  command = " ".join([programs[0], ' ', imagePath])
  os.system(command)

def match_AKAZE_features(imagePath1, imagePath2, gdTruthHomography):
  command = " ".join(
    [programs[1], ' ', imagePath1, ' ', imagePath2, ' ', gdTruthHomography] )
  os.system(command)

def compare_AKAZE_BRISK_ORB(imagePath1, imagePath2, gdTruthHomography):
  command = " ".join(
    [programs[2], ' ', imagePath1, ' ', imagePath2, ' ', gdTruthHomography] )
  os.system(command)

# ===============================================================================
# Example datasets
imagePath1 = os.path.join('.','datasets','iguazu', 'img1.pgm')
imagePath2 = os.path.join('.','datasets','iguazu', 'img4.pgm')
gdTruthHomography = os.path.join('.','datasets','iguazu', 'H1to4p')

# Go!
extract_AKAZE_features(imagePath1)
match_AKAZE_features(imagePath1, imagePath2, gdTruthHomography)
compare_AKAZE_BRISK_ORB(imagePath1, imagePath2, gdTruthHomography)
<|endoftext|>


<commit_before>import sys
from itertools import ifilter
from requests_oauthlib import OAuth1Session
import json
import time

def isVideo(tweet):
  if 'extended_entities' in tweet:
    if 'media' in tweet['extended_entities']:
      for i in range(len(tweet['extended_entities']['media'])):
        if tweet['extended_entities']['media'][i]['type'] == 'video':
          return True
      return False
    else:
      return False
  else:
    return False

def filterTweet(tweet):
  f_tweet = {}
  if 'retweeted_status' in tweet:
    f_tweet['retweet'] = 1
    #??do i need to convert this to a dictionary? i don't think so
    t_object = tweet['retweeted_status']
    f_tweet['rt_id_str'] = tweet['id_str']
    f_tweet['rt_created_at'] = time.strftime('%Y-%m-%d %H:%M:%S',
      time.strptime(str(tweet['created_at']),'%a %b %d %H:%M:%S +0000 %Y'))
    f_tweet['rt_text'] = tweet['text']
  else:
    f_tweet['retweet'] = 0
    t_object = tweet
    f_tweet['rt_id_str'] = ""
    f_tweet['rt_created_at'] = ""
    f_tweet['rt_text'] = ""

  f_tweet['orig_id_str'] = t_object['id_str']
  f_tweet['orig_created_at'] = time.strftime('%Y-%m-%d %H:%M:%S',
    time.strptime(str(t_object['created_at']),'%a %b %d %H:%M:%S +0000 %Y'))
  f_tweet['orig_text'] = t_object['text']
  f_tweet['orig_retweet_count'] = t_object['retweet_count']
  f_tweet['orig_favorite_count'] = t_object['favorite_count']
  f_tweet['orig_user_id_str'] = t_object['user']['id_str']
  f_tweet['orig_user_screen_name'] = t_object['user']['screen_name']
  f_tweet['orig_user_followers_count'] = t_object['user']['followers_count']
  f_tweet['orig_user_friends_count'] = t_object['user']['friends_count']
  f_tweet['orig_user_statuses_count'] = t_object['user']['statuses_count']
  if 'entities' in t_object:
    if 'hashtags' in t_object['entities']:
      # hashtags are arrays
      f_tweet['orig_hashtags'] = t_object['entities']['hashtags']
    if 'user_mentions' in t_object['entities']:
      # user_mentions are arrays
      f_tweet['orig_user_mentions'] = t_object['entities']['user_mentions']
    if 'urls' in t_object['entities']:
      # urls are arrays
      f_tweet['orig_urls'] = t_object['entities']['urls']
    if 'media' in t_object['entities']:
      # media is an array
      for i in range(len(t_object['entities']['media'])):
        if 'source_status_id_str' in t_object['entities']['media'][i]:
          f_tweet['orig_source_status_id_str'] = t_object['entities']['media'][i]['source_status_id_str']
  for i in range(len(t_object['extended_entities']['media'])):
    if t_object['extended_entities']['media'][i]['type'] == 'video':
      f_tweet['orig_media_type'] = t_object['extended_entities']['media'][i]['type']
      f_tweet['orig_video_length_ms'] = t_object['extended_entities']['media'][i]['video_info']['duration_millis']
      f_tweet['orig_video_url'] = t_object['extended_entities']['media'][i]['expanded_url']
      # Could potentially store the thumbnail. I think you have to access it with <url>:thumb
      # or something like that.
      #f_tweet['orig_video_thumb'] = ""
  return f_tweet

class TwitterDataIngestSource:
  """Ingest data from Twitter"""

  def __init__(self, config):
    self.config = config

  def __iter__(self):
    if 'track' in self.config:
      self.track = self.config['track']
    else:
      self.track = 'ski,surf,board'

    auth = OAuth1Session(
      self.config['consumer_key'],
      client_secret = self.config['consumer_secret'],
      resource_owner_key = self.config['access_token'],
      resource_owner_secret = self.config['access_token_secret']
    )

    request = auth.post(
      'https://stream.twitter.com/1.1/statuses/filter.json',
      data = 'track=' + self.track,
      stream = True
    )

    # filter out empty lines sent to keep the stream alive
    self.source_iterator = ifilter(lambda x: x, request.iter_lines())

    return self

  def next(self):
    # This is a dictionary
    next_tweet = json.loads(self.source_iterator.next())
    while not isVideo(next_tweet):
      next_tweet = json.loads(self.source_iterator.next())
    filtered_tweet = filterTweet(next_tweet)

    return { 'tweet' : filtered_tweet }

<commit_msg>converted to rt_info structure<commit_after>import sys
from itertools import ifilter
from requests_oauthlib import OAuth1Session
import json
import time

def isVideo(tweet):
  if 'extended_entities' in tweet:
    if 'media' in tweet['extended_entities']:
      for i in range(len(tweet['extended_entities']['media'])):
        if tweet['extended_entities']['media'][i]['type'] == 'video':
          return True
      return False
    else:
      return False
  else:
    return False

def filterTweet(tweet):
  f_tweet = {}
  f_tweet['rt_info'] = []
  if 'retweeted_status' in tweet:
    f_tweet['retweet'] = 1
    t_object = tweet['retweeted_status']
    rt_info = {}
    rt_info['rt_id_str'] = tweet['id_str']
    rt_info['rt_created_at'] = time.strftime('%Y-%m-%d %H:%M:%S',
      time.strptime(str(tweet['created_at']),'%a %b %d %H:%M:%S +0000 %Y'))
    rt_info['rt_text'] = tweet['text']
    rt_info['orig_retweet_count'] = t_object['retweet_count']
    rt_info['orig_favorite_count'] = t_object['favorite_count']
    f_tweet['rt_info'].append(rt_info)
  else:
    f_tweet['retweet'] = 0
    t_object = tweet

  f_tweet['orig_id_str'] = t_object['id_str']
  f_tweet['orig_created_at'] = time.strftime('%Y-%m-%d %H:%M:%S',
    time.strptime(str(t_object['created_at']),'%a %b %d %H:%M:%S +0000 %Y'))
  f_tweet['orig_text'] = t_object['text']
  f_tweet['orig_retweet_count'] = t_object['retweet_count']
  f_tweet['orig_favorite_count'] = t_object['favorite_count']
  f_tweet['orig_user_id_str'] = t_object['user']['id_str']
  f_tweet['orig_user_screen_name'] = t_object['user']['screen_name']
  f_tweet['orig_user_followers_count'] = t_object['user']['followers_count']
  f_tweet['orig_user_friends_count'] = t_object['user']['friends_count']
  f_tweet['orig_user_statuses_count'] = t_object['user']['statuses_count']
  if 'entities' in t_object:
    if 'hashtags' in t_object['entities']:
      # hashtags are arrays
      f_tweet['orig_hashtags'] = t_object['entities']['hashtags']
    if 'user_mentions' in t_object['entities']:
      # user_mentions are arrays
      f_tweet['orig_user_mentions'] = t_object['entities']['user_mentions']
    if 'urls' in t_object['entities']:
      # urls are arrays
      f_tweet['orig_urls'] = t_object['entities']['urls']
    if 'media' in t_object['entities']:
      # media is an array
      for i in range(len(t_object['entities']['media'])):
        if 'source_status_id_str' in t_object['entities']['media'][i]:
          f_tweet['orig_source_status_id_str'] = t_object['entities']['media'][i]['source_status_id_str']
  for i in range(len(t_object['extended_entities']['media'])):
    if t_object['extended_entities']['media'][i]['type'] == 'video':
      f_tweet['orig_media_type'] = t_object['extended_entities']['media'][i]['type']
      f_tweet['orig_video_length_ms'] = t_object['extended_entities']['media'][i]['video_info']['duration_millis']
      f_tweet['orig_video_url'] = t_object['extended_entities']['media'][i]['expanded_url']
      # Could potentially store the thumbnail. I think you have to access it with <url>:thumb
      # or something like that.
      #f_tweet['orig_video_thumb'] = ""
  return f_tweet

class TwitterDataIngestSource:
  """Ingest data from Twitter"""

  def __init__(self, config):
    self.config = config

  def __iter__(self):
    if 'track' in self.config:
      self.track = self.config['track']
    else:
      self.track = 'ski,surf,board'

    auth = OAuth1Session(
      self.config['consumer_key'],
      client_secret = self.config['consumer_secret'],
      resource_owner_key = self.config['access_token'],
      resource_owner_secret = self.config['access_token_secret']
    )

    request = auth.post(
      'https://stream.twitter.com/1.1/statuses/filter.json',
      data = 'track=' + self.track,
      stream = True
    )

    # filter out empty lines sent to keep the stream alive
    self.source_iterator = ifilter(lambda x: x, request.iter_lines())

    return self

  def next(self):
    # This is a dictionary
    next_tweet = json.loads(self.source_iterator.next())
    while not isVideo(next_tweet):
      next_tweet = json.loads(self.source_iterator.next())
    filtered_tweet = filterTweet(next_tweet)

    return { 'tweet' : filtered_tweet }

<|endoftext|>


<commit_before>from django_countries.serializers import CountryFieldMixin
from rest_framework import serializers

from account.models import User
from agency.models import Agency, OtherAgency, AgencyOffice, AgencyMember

class AgencySerializer(serializers.ModelSerializer):

    class Meta:
        model = Agency
        fields = (
            'id',
            'name',
        )

class OtherAgencySerializer(serializers.ModelSerializer):

    class Meta:
        model = OtherAgency
        fields = "__all__"

class AgencyOfficeSerializer(CountryFieldMixin, serializers.ModelSerializer):

    agency = AgencySerializer()

    class Meta:
        model = AgencyOffice
        fields = (
            'id',
            'name',
            'country',
            'agency',
        )

class AgencyMemberSerializer(serializers.ModelSerializer):

    office = AgencyOfficeSerializer(read_only=True)
    role_display = serializers.CharField(source='get_role_display', read_only=True)

    class Meta:
        model = AgencyMember
        fields = (
            'id',
            'role',
            'role_display',
            'office',
            'office_id',
            'telephone',
        )

class AgencyMemberFullSerializer(AgencyMemberSerializer):

    permissions = serializers.SerializerMethodField()

    class Meta(AgencyMemberSerializer.Meta):
        fields = AgencyMemberSerializer.Meta.fields + (
            'permissions',
        )

    def get_permissions(self, agency_member):
        return [
            p.name for p in agency_member.user_permissions
        ]

class AgencyUserListSerializer(serializers.ModelSerializer):

    name = serializers.CharField(source='fullname', read_only=True)
    first_name = serializers.CharField(write_only=True)
    last_name = serializers.CharField(write_only=True)
    office_memberships = AgencyMemberSerializer(many=True, source='agency_members', allow_empty=False)

    class Meta:
        model = User

        fields = (
            'id',
            'is_active',
            'first_name',
            'last_name',
            'name',
            'email',
            'status',
            'office_memberships',
        )

class AgencyUserSerializer(AgencyUserListSerializer):

    office_memberships = AgencyMemberFullSerializer(many=True, source='agency_members', allow_empty=False)
<commit_msg>Add agency name for user list serializer<commit_after>from django_countries.serializers import CountryFieldMixin
from rest_framework import serializers

from account.models import User
from agency.models import Agency, OtherAgency, AgencyOffice, AgencyMember

class AgencySerializer(serializers.ModelSerializer):

    class Meta:
        model = Agency
        fields = (
            'id',
            'name',
        )

class OtherAgencySerializer(serializers.ModelSerializer):

    class Meta:
        model = OtherAgency
        fields = "__all__"

class AgencyOfficeSerializer(CountryFieldMixin, serializers.ModelSerializer):

    agency = AgencySerializer()

    class Meta:
        model = AgencyOffice
        fields = (
            'id',
            'name',
            'country',
            'agency',
        )

class AgencyMemberSerializer(serializers.ModelSerializer):

    office = AgencyOfficeSerializer(read_only=True)
    role_display = serializers.CharField(source='get_role_display', read_only=True)

    class Meta:
        model = AgencyMember
        fields = (
            'id',
            'role',
            'role_display',
            'office',
            'office_id',
            'telephone',
        )

class AgencyMemberFullSerializer(AgencyMemberSerializer):

    permissions = serializers.SerializerMethodField()

    class Meta(AgencyMemberSerializer.Meta):
        fields = AgencyMemberSerializer.Meta.fields + (
            'permissions',
        )

    def get_permissions(self, agency_member):
        return [
            p.name for p in agency_member.user_permissions
        ]

class AgencyUserListSerializer(serializers.ModelSerializer):

    name = serializers.CharField(source='fullname', read_only=True)
    first_name = serializers.CharField(write_only=True)
    last_name = serializers.CharField(write_only=True)
    agency_name = serializers.CharField(source='agency.name', read_only=True)
    office_memberships = AgencyMemberSerializer(many=True, source='agency_members', allow_empty=False)

    class Meta:
        model = User

        fields = (
            'id',
            'is_active',
            'first_name',
            'last_name',
            'name',
            'email',
            'status',
            'office_memberships',
            'agency_name',
        )

class AgencyUserSerializer(AgencyUserListSerializer):

    office_memberships = AgencyMemberFullSerializer(many=True, source='agency_members', allow_empty=False)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from flask import Flask
import settings
from flask_cors import CORS
from ifplus import Application
from werkzeug.contrib.fixers import ProxyFix

app = Flask(__name__)
app.config.from_object(settings)
cors = CORS(app=app)
server = Application(app=app)
app.wsgi_app = ProxyFix(app.wsgi_app)

@app.before_first_request
def start():
    app.tokens.init_mongodb()

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=True)
<commit_msg>Content Text Returns<commit_after># -*- coding: utf-8 -*-
from flask import Flask
import settings
from flask_cors import CORS
from ifplus import Application
from werkzeug.contrib.fixers import ProxyFix

app = Flask(__name__)
app.config.from_object(settings)
cors = CORS(app=app)
server = Application(app=app)
app.wsgi_app = ProxyFix(app.wsgi_app)

@app.before_first_request
def start():
    app.tokens.init_mongodb()
    app.vfs.init_mongodb()

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=True)
<|endoftext|>


<commit_before>from django.contrib import admin
from models import License, Submission, Revision, TagCreation

class LicenseAdmin(admin.ModelAdmin):
    list_display = ('name', 'description')

class SubmissionAdmin(admin.ModelAdmin):
    list_display = ('sub_type', 'slug', 'created_by', 'date_created',
                    'num_revisions')
    list_display_links = ('slug',)
    ordering = ['-date_created']

class RevisionAdmin(admin.ModelAdmin):
    list_display = ('pk', 'title', 'created_by', 'date_created',
                    'sub_license', 'item_url', 'rev_id', 'is_displayed')
    list_display_links = ('title',)
    ordering = ['-date_created']

class TagCreationAdmin(admin.ModelAdmin):
    list_display = ('date_created', 'tag', 'created_by', 'revision')

admin.site.register(License, LicenseAdmin)
admin.site.register(Submission, SubmissionAdmin)
admin.site.register(Revision, RevisionAdmin)
admin.site.register(TagCreation, TagCreationAdmin)
<commit_msg>ENH: add ZipFile admin<commit_after>from django.contrib import admin
from models import License, Submission, Revision, TagCreation, ZipFile

class LicenseAdmin(admin.ModelAdmin):
    list_display = ('name', 'description')

class SubmissionAdmin(admin.ModelAdmin):
    list_display = ('sub_type', 'slug', 'created_by', 'date_created',
                    'num_revisions')
    list_display_links = ('slug',)
    ordering = ['-date_created']

class RevisionAdmin(admin.ModelAdmin):
    list_display = ('pk', 'title', 'created_by', 'date_created',
                    'sub_license', 'item_url', 'rev_id', 'is_displayed')
    list_display_links = ('title',)
    ordering = ['-date_created']

class TagCreationAdmin(admin.ModelAdmin):
    list_display = ('date_created', 'tag', 'created_by', 'revision')

class ZipFileAdmin(admin.ModelAdmin):
    list_display = ('date_added', 'zip_hash', 'raw_zip_file')

admin.site.register(License, LicenseAdmin)
admin.site.register(Submission, SubmissionAdmin)
admin.site.register(Revision, RevisionAdmin)
admin.site.register(TagCreation, TagCreationAdmin)
admin.site.register(ZipFile, ZipFileAdmin)
<|endoftext|>


<commit_before>#!/usr/bin/python
from uiautomator import device as d
import time

# 开关屏幕
for i in range(1,50):
    d.screen.on()
    time.sleep(0.5)
    d.screen.off()
<commit_msg>屏幕压力测试<commit_after>#!/usr/bin/python
from uiautomator import device as d

import time

# 开关屏幕50次
for i in range(1,50):
    d.sleep()
    time.sleep(0.2)
    d.wakeup()
    time.sleep(0.2)
    # 新增wrapper，检查屏幕是否开启
    # 若没有开启则不正常
    if not d.screen.ison()
        print("第" + i + "次没有正常开启屏幕")
<|endoftext|>


<commit_before>import logging
logger = logging.getLogger(__name__)

import micawber

from django.db import models
from django.utils.translation import ugettext as _
from django.core.exceptions import ValidationError

from django_extensions.db.fields import (
    ModificationDateTimeField, CreationDateTimeField
)

from sorl.thumbnail import ImageField

class Album(models.Model):
    """ Album containing media objects. """

    title = models.CharField(max_length=255)
    # This is populated from the project slug in legacy and should have
    # the same length
    slug = models.SlugField(unique=True, max_length=100)
    description = models.TextField(blank=True)

    created = CreationDateTimeField()
    updated = ModificationDateTimeField()

    def __unicode__(self):
        return self.title or unicode(self.pk)

    @models.permalink
    def get_absolute_url(self):
        """ Get the URL for the current album. """

        return ('album_detail', (), {
            'slug': self.slug
        })

class MediaObjectBase(models.Model):
    """ Abstract base class for media objects contained in albums. """

    album = models.ForeignKey(Album)

    title = models.CharField(max_length=255, blank=True)
    description = models.TextField(blank=True)

    created = CreationDateTimeField()
    updated = ModificationDateTimeField()

    class Meta:
        abstract = True

    def __unicode__(self):
        return self.title or unicode(self.pk)

class OembedAbstractBase(models.Model):
    """ Abstract base class for classes populated through OEmbed. """

    # Title included in MediaObjectBase
    url = models.URLField()

    thumbnail_url = models.URLField(blank=True, verify_exists=True)
    thumbnail_width = models.SmallIntegerField(blank=True, null=True)
    thumbnail_height = models.SmallIntegerField(blank=True, null=True)

    provider_name = models.CharField(blank=True, max_length=255)
    provider_url = models.CharField(blank=True, max_length=255)

    author_name = models.CharField(blank=True, max_length=255)
    author_url = models.URLField(blank=True, verify_exists=False)

    class Meta:
        abstract = True

    def clean(self):
        """ Get OEmbed metadata on save. """

        # load up rules for some default providers, such as youtube and flickr
        providers = micawber.bootstrap_basic()

        # Get the data
        try:
            oembed_data = providers.request(self.url)
        except micawber.ProviderException:
            raise ValidationError(_('Could not find metadata for object.'))

        logger.debug(
            u'Found OEmbed data on %s for %s',
            self.url, self
        )

        for (key, value) in oembed_data.iteritems():
            try:
                if not getattr(self, key):
                    logger.debug(
                        u'Setting field %s on %s to %s', key, self, value
                    )
                    setattr(self, key, value)
            except AttributeError:
                pass

class LocalPicture(MediaObjectBase):
    """ Picture stored locally. """

    picture = ImageField(upload_to='pictures/')

    @models.permalink
    def get_absolute_url(self):
        """ Get the URL for the picture. """

        return ('localpicture_detail', (), {
            'album_slug': self.album.slug,
            'pk': str(self.pk)
        })

class EmbeddedVideo(MediaObjectBase, OembedAbstractBase):
    """ Embedded video, hosted remotely. """

    html = models.TextField(blank=True)
    width = models.SmallIntegerField(blank=True, null=True)
    height = models.SmallIntegerField(blank=True, null=True)

    duration = models.SmallIntegerField(blank=True, null=True)

    @models.permalink
    def get_absolute_url(self):
        """ Get the URL for the video. """

        return ('embeddedvideo_detail', (), {
            'album_slug': self.album.slug,
            'pk': str(self.pk)
        })

"""
Some more code I had lying around:

class Link(OembedAbstractBase):
    pass

class EmbeddedRich(OembedAbstractBase):
    html = models.TextField(blank=True)
    width = models.SmallIntegerField(blank=True, null=True)
    height = models.SmallIntegerField(blank=True, null=True)

class EmbeddedPhoto(OembedAbstractBase):
    width = models.SmallIntegerField(blank=True, null=True)
    height = models.SmallIntegerField(blank=True, null=True)
"""<commit_msg>Logging whenever a ProviderException pops up.<commit_after>import logging
logger = logging.getLogger(__name__)

import micawber

from django.db import models
from django.utils.translation import ugettext as _
from django.core.exceptions import ValidationError

from django_extensions.db.fields import (
    ModificationDateTimeField, CreationDateTimeField
)

from sorl.thumbnail import ImageField

class Album(models.Model):
    """ Album containing media objects. """

    title = models.CharField(max_length=255)
    # This is populated from the project slug in legacy and should have
    # the same length
    slug = models.SlugField(unique=True, max_length=100)
    description = models.TextField(blank=True)

    created = CreationDateTimeField()
    updated = ModificationDateTimeField()

    def __unicode__(self):
        return self.title or unicode(self.pk)

    @models.permalink
    def get_absolute_url(self):
        """ Get the URL for the current album. """

        return ('album_detail', (), {
            'slug': self.slug
        })

class MediaObjectBase(models.Model):
    """ Abstract base class for media objects contained in albums. """

    album = models.ForeignKey(Album)

    title = models.CharField(max_length=255, blank=True)
    description = models.TextField(blank=True)

    created = CreationDateTimeField()
    updated = ModificationDateTimeField()

    class Meta:
        abstract = True

    def __unicode__(self):
        return self.title or unicode(self.pk)

class OembedAbstractBase(models.Model):
    """ Abstract base class for classes populated through OEmbed. """

    # Title included in MediaObjectBase
    url = models.URLField()

    thumbnail_url = models.URLField(blank=True, verify_exists=True)
    thumbnail_width = models.SmallIntegerField(blank=True, null=True)
    thumbnail_height = models.SmallIntegerField(blank=True, null=True)

    provider_name = models.CharField(blank=True, max_length=255)
    provider_url = models.CharField(blank=True, max_length=255)

    author_name = models.CharField(blank=True, max_length=255)
    author_url = models.URLField(blank=True, verify_exists=False)

    class Meta:
        abstract = True

    def clean(self):
        """ Get OEmbed metadata on save. """

        # load up rules for some default providers, such as youtube and flickr
        providers = micawber.bootstrap_basic()

        # Get the data
        try:
            oembed_data = providers.request(self.url)
        except micawber.ProviderException:
            msg = _('Could not find metadata for object.')

            logger.exception(msg)
            raise ValidationError(msg)

        logger.debug(
            u'Found OEmbed data on %s for %s',
            self.url, self
        )

        for (key, value) in oembed_data.iteritems():
            try:
                if not getattr(self, key):
                    logger.debug(
                        u'Setting field %s on %s to %s', key, self, value
                    )
                    setattr(self, key, value)
            except AttributeError:
                pass

class LocalPicture(MediaObjectBase):
    """ Picture stored locally. """

    picture = ImageField(upload_to='pictures/')

    @models.permalink
    def get_absolute_url(self):
        """ Get the URL for the picture. """

        return ('localpicture_detail', (), {
            'album_slug': self.album.slug,
            'pk': str(self.pk)
        })

class EmbeddedVideo(MediaObjectBase, OembedAbstractBase):
    """ Embedded video, hosted remotely. """

    html = models.TextField(blank=True)
    width = models.SmallIntegerField(blank=True, null=True)
    height = models.SmallIntegerField(blank=True, null=True)

    duration = models.SmallIntegerField(blank=True, null=True)

    @models.permalink
    def get_absolute_url(self):
        """ Get the URL for the video. """

        return ('embeddedvideo_detail', (), {
            'album_slug': self.album.slug,
            'pk': str(self.pk)
        })

"""
Some more code I had lying around:

class Link(OembedAbstractBase):
    pass

class EmbeddedRich(OembedAbstractBase):
    html = models.TextField(blank=True)
    width = models.SmallIntegerField(blank=True, null=True)
    height = models.SmallIntegerField(blank=True, null=True)

class EmbeddedPhoto(OembedAbstractBase):
    width = models.SmallIntegerField(blank=True, null=True)
    height = models.SmallIntegerField(blank=True, null=True)
"""<|endoftext|>


<commit_before>import wpilib

class MyRobot(wpilib.SampleRobot):
    def robotInit(self):
        super().__init__()
        print("Team 1418 2015 Test Code")
        self.joystick1 = wpilib.Joystick(0)
        self.joystick2 = wpilib.Joystick(1)

        self.lmotor = wpilib.Talon(5)
        self.rmotor = wpilib.Talon(10)

        self.robot_drive = wpilib.RobotDrive(self.lmotor,self.rmotor )
        self.robot_drive.setSafetyEnabled(False)

    def operatorControl(self):
        print("Entering Teleop")
        while self.isOperatorControl() and self.isEnabled():
            self.robot_drive.tankDrive(self.joystick1, self.joystick2)
            wpilib.Timer.delay(.01)

    def disabled(self):
        '''Called when the robot is in disabled mode'''

        wpilib.Timer.delay(.01)

if __name__ == '__main__':

    wpilib.run(MyRobot)<commit_msg>Changed the pwm slot for the talons to be 0 and 1<commit_after>import wpilib

class MyRobot(wpilib.SampleRobot):
    def robotInit(self):
        super().__init__()
        print("Team 1418 2015 Test Code")
        self.joystick1 = wpilib.Joystick(0)
        self.joystick2 = wpilib.Joystick(1)

        self.lmotor = wpilib.Talon(0)
        self.rmotor = wpilib.Talon(1)

        self.robot_drive = wpilib.RobotDrive(self.lmotor,self.rmotor )
        self.robot_drive.setSafetyEnabled(False)

    def operatorControl(self):
        print("Entering Teleop")
        while self.isOperatorControl() and self.isEnabled():
            self.robot_drive.tankDrive(self.joystick1, self.joystick2)
            wpilib.Timer.delay(.01)

    def disabled(self):
        '''Called when the robot is in disabled mode'''

        wpilib.Timer.delay(.01)

if __name__ == '__main__':

    wpilib.run(MyRobot)<|endoftext|>


<commit_before>import datetime as dt
import os
import threading
import logging

from backlash.tbtools import get_current_traceback
from backlash.frtools import get_thread_stack, DumpThread
from backlash.utils import RequestContext
from .timer import Timer

class TraceSlowRequestsMiddleware(object):

    def __init__(self, app, reporters, context_injectors, interval=25,
                 exclude_paths=None):

        self.app = app
        self.reporters = reporters
        self.context_injectors = context_injectors
        self.interval = interval
        self.exclude_paths = exclude_paths or []

        self.timer = Timer()
        self.timer.setDaemon(True)
        self.timer.start()

    def _stream_response(self, environ, data):
        try:
            for chunk in data:
                yield chunk
        finally:
            if hasattr(data, 'close'):
                data.close()
            self._cancel_tracing(environ)

    def __call__(self, environ, start_response):
        try:
            self._start_tracing(environ)
            return self._stream_response(environ, self.app(environ, start_response))
        except Exception:
            self._cancel_tracing(environ)
            raise

    def peek(self, environ, thread_id, started):
        context = RequestContext({'environ': dict(environ)})
        for injector in self.context_injectors:
            context.update(injector(environ))

        context.update({
            'SLOW_REQUEST': {'ThreadID': thread_id,
                             'ProcessID': os.getpid(),
                             'Started': str(started)}
        })

        try:
            traceback = get_thread_stack(thread_id, environ.get('PATH_INFO', ''),
                                         context=context, error_type='SlowRequestError')
        except KeyError:
            logging.warn('\nUnable to retrieve SlowRequest Stack %s, '
                         'thread %s probably finished execution in mean time\n',
                         environ.get('PATH_INFO', ''), thread_id)
            return

        for r in self.reporters:
            try:
                r.report(traceback)
            except Exception:
                error = get_current_traceback(skip=1, show_hidden_frames=False)
                environ['wsgi.errors'].write('\nError while reporting slow request with %s\n' % r)
                environ['wsgi.errors'].write(error.plaintext)

    @classmethod
    def _get_thread_id(cls):
        return threading.current_thread().ident

    def _is_exempt(self, environ):
        """
        Returns True if this request's URL starts with one of the
        excluded paths.
        """
        exemptions = self.exclude_paths

        if exemptions:
            path = environ.get('PATH_INFO')
            for excluded_p in self.exclude_paths:
                if path.startswith(excluded_p):
                    return True

        return False

    def _start_tracing(self, environ):
        if not self._is_exempt(environ):
            job = self.timer.run_later(self.peek,
                                       self.interval,
                                       environ,
                                       self._get_thread_id(),
                                       dt.datetime.utcnow())
            environ['BACKLASH_SLOW_TRACING_JOB'] = job

    def _cancel_tracing(self, environ):
        try:
            tracing_job = environ.get('BACKLASH_SLOW_TRACING_JOB', None)
            if tracing_job is not None:
                self.timer.cancel(tracing_job)
        except Exception:
            error = get_current_traceback(skip=1, show_hidden_frames=False)
            environ['wsgi.errors'].write('Failed to cancel slow requests tracing timer\n')
            environ['wsgi.errors'].write(error.plaintext)
<commit_msg>Solve issues with StatusCodeRedirect and webob.Request.call_application()<commit_after>import datetime as dt
import os
import threading
import logging

from backlash.tbtools import get_current_traceback
from backlash.frtools import get_thread_stack, DumpThread
from backlash.utils import RequestContext
from .timer import Timer

class TraceSlowRequestsMiddleware(object):

    def __init__(self, app, reporters, context_injectors, interval=25,
                 exclude_paths=None):

        self.app = app
        self.reporters = reporters
        self.context_injectors = context_injectors
        self.interval = interval
        self.exclude_paths = exclude_paths or []

        self.timer = Timer()
        self.timer.setDaemon(True)
        self.timer.start()

    def _stream_response(self, environ, data):
        try:
            for chunk in data:
                yield chunk
        finally:
            if hasattr(data, 'close'):
                data.close()
            self._cancel_tracing(environ)

    def __call__(self, environ, start_response):
        try:
            self._start_tracing(environ)
            return self._stream_response(environ, self.app(environ, start_response))
        except Exception:
            self._cancel_tracing(environ)
            raise

    def peek(self, environ, thread_id, started):
        context = RequestContext({'environ': dict(environ)})
        for injector in self.context_injectors:
            context.update(injector(environ))

        context.update({
            'SLOW_REQUEST': {'ThreadID': thread_id,
                             'ProcessID': os.getpid(),
                             'Started': str(started)}
        })

        try:
            traceback = get_thread_stack(thread_id, environ.get('PATH_INFO', ''),
                                         context=context, error_type='SlowRequestError')
        except KeyError:
            logging.warn('\nUnable to retrieve SlowRequest Stack %s, '
                         'thread %s probably finished execution in mean time\n',
                         environ.get('PATH_INFO', ''), thread_id)
            return

        for r in self.reporters:
            try:
                r.report(traceback)
            except Exception:
                error = get_current_traceback(skip=1, show_hidden_frames=False)
                environ['wsgi.errors'].write('\nError while reporting slow request with %s\n' % r)
                environ['wsgi.errors'].write(error.plaintext)

    @classmethod
    def _get_thread_id(cls):
        return threading.current_thread().ident

    def _is_exempt(self, environ):
        """
        Returns True if this request's URL starts with one of the
        excluded paths.
        """
        exemptions = self.exclude_paths

        if exemptions:
            path = environ.get('PATH_INFO')
            for excluded_p in self.exclude_paths:
                if path.startswith(excluded_p):
                    return True

        return False

    def _start_tracing(self, environ):
        if not self._is_exempt(environ):
            job = self.timer.run_later(self.peek,
                                       self.interval,
                                       environ,
                                       self._get_thread_id(),
                                       dt.datetime.utcnow())
            # In some cases due to webob.Request.call_application() or
            # paste StatusCodeRedirect middleware multiple _start_tracing for the same
            # environ might happen without consuming the app_iter for the firsts
            # and so without canceling them, we register them and cancel them all together.
            environ.setdefault('BACKLASH_SLOW_TRACING_JOBS', []).append(job)

    def _cancel_tracing(self, environ):
        try:
            tracing_jobs = environ.get('BACKLASH_SLOW_TRACING_JOBS', [])
            for job in tracing_jobs:
                self.timer.cancel(job)
        except Exception:
            error = get_current_traceback(skip=1, show_hidden_frames=False)
            environ['wsgi.errors'].write('Failed to cancel slow requests tracing timer\n')
            environ['wsgi.errors'].write(error.plaintext)
<|endoftext|>


<commit_before>#!/usr/bin/python
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: op-test-framework/testcases/OpTestPCI.py $
#
# OpenPOWER Automated Test Project
#
# Contributors Listed Below - COPYRIGHT 2015,2017
# [+] International Business Machines Corp.
#
#
# 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.
#
# IBM_PROLOG_END_TAG

#  @package OpTestPCI.py
#   This testcase basically will test and gather PCI subsystem Info
#   Tools used are lspci and lsusb
#   any pci related tests will be added in this package

import time
import subprocess
import commands
import re
import sys
import os
import os.path

import unittest

import OpTestConfiguration
from common.OpTestSystem import OpSystemState

class OpTestPCI(unittest.TestCase):
    def setUp(self):
        conf = OpTestConfiguration.conf
        self.cv_HOST = conf.host()
        self.cv_IPMI = conf.ipmi()
        self.cv_SYSTEM = conf.system()
        self.pci_good_data_file = conf.lspci_file()

class OpTestPCISkiroot(OpTestPCI):
    # compare petitboot "lspci -mm -n" to known good data
    def runTest(self):
        self.cv_SYSTEM.goto_state(OpSystemState.PETITBOOT_SHELL)
        cmd = "lspci -mm -n"
        c = self.cv_SYSTEM.sys_get_ipmi_console()
        self.cv_IPMI.ipmi_host_set_unique_prompt()
        c.run_command("uname -a")
        c.run_command("cat /etc/os-release")

        res = '\n'.join(c.run_command(cmd))

        self.pci_data_petitboot = res
        diff_process = subprocess.Popen(['diff', "-u", self.pci_good_data_file , "-"], stdin=subprocess.PIPE)
        diff_stdout, diff_stderr = diff_process.communicate(self.pci_data_petitboot + '\n')
        r = diff_process.wait()
        if r == 0:
            print "All the pci devices are detected at petitboot"
        else:
            print diff_stdout
            raise OpTestError("There is a mismatch b/w known good output and tested petitboot lspci output")

class OpTestPCIHost(OpTestPCI):
    # Compare host "lspci -mm -n" output to known good
    def runTest(self):
        self.cv_SYSTEM.goto_state(OpSystemState.OS)

        self.cv_HOST.host_check_command("lspci", "lsusb")
        self.cv_HOST.host_list_pci_devices()
        self.cv_HOST.host_get_pci_verbose_info()
        self.cv_HOST.host_list_usb_devices()
        l_res = self.cv_HOST.host_run_command("lspci -mm -n")
        # FIXME: why do we get a blank line in l_res ?
        self.pci_data_hostos = l_res.replace("\r\n", "\n")
        diff_process = subprocess.Popen(['diff', "-u", self.pci_good_data_file , "-"], stdin=subprocess.PIPE)
        diff_stdout, diff_stderr = diff_process.communicate(self.pci_data_hostos)
        r = diff_process.wait()
        if r == 0:
            print "All the pci devices are detected in host OS"
        else:
            print diff_stdout
            raise OpTestError("There is a mismatch b/w known good output and tested host OS lspci output")

<commit_msg>OpTestPCI: Correct subprocess usage so we emit diff in test case error<commit_after>#!/usr/bin/python
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: op-test-framework/testcases/OpTestPCI.py $
#
# OpenPOWER Automated Test Project
#
# Contributors Listed Below - COPYRIGHT 2015,2017
# [+] International Business Machines Corp.
#
#
# 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.
#
# IBM_PROLOG_END_TAG

#  @package OpTestPCI.py
#   This testcase basically will test and gather PCI subsystem Info
#   Tools used are lspci and lsusb
#   any pci related tests will be added in this package

import time
import subprocess
import commands
import re
import sys
import os
import os.path

import unittest

import OpTestConfiguration
from common.OpTestSystem import OpSystemState

class OpTestPCI(unittest.TestCase):
    def setUp(self):
        conf = OpTestConfiguration.conf
        self.cv_HOST = conf.host()
        self.cv_IPMI = conf.ipmi()
        self.cv_SYSTEM = conf.system()
        self.pci_good_data_file = conf.lspci_file()

class OpTestPCISkiroot(OpTestPCI):
    # compare petitboot "lspci -mm -n" to known good data
    def runTest(self):
        self.cv_SYSTEM.goto_state(OpSystemState.PETITBOOT_SHELL)
        cmd = "lspci -mm -n"
        c = self.cv_SYSTEM.sys_get_ipmi_console()
        self.cv_IPMI.ipmi_host_set_unique_prompt()
        c.run_command("uname -a")
        c.run_command("cat /etc/os-release")

        res = '\n'.join(c.run_command(cmd))

        self.pci_data_petitboot = res
        diff_process = subprocess.Popen(['diff', "-u", self.pci_good_data_file , "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        diff_stdout, diff_stderr = diff_process.communicate(self.pci_data_petitboot + '\n')
        r = diff_process.wait()
        self.assertEqual(r, 0, "Stored and detected PCI devices differ:\n%s%s" % (diff_stdout, diff_stderr))

class OpTestPCIHost(OpTestPCI):
    # Compare host "lspci -mm -n" output to known good
    def runTest(self):
        self.cv_SYSTEM.goto_state(OpSystemState.OS)

        self.cv_HOST.host_check_command("lspci", "lsusb")
        self.cv_HOST.host_list_pci_devices()
        self.cv_HOST.host_get_pci_verbose_info()
        self.cv_HOST.host_list_usb_devices()
        l_res = self.cv_HOST.host_run_command("lspci -mm -n")
        # FIXME: why do we get a blank line in l_res ?
        self.pci_data_hostos = l_res.replace("\r\n", "\n")
        diff_process = subprocess.Popen(['diff', "-u", self.pci_good_data_file , "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        diff_stdout, diff_stderr = diff_process.communicate(self.pci_data_hostos)
        r = diff_process.wait()
        self.assertEqual(r, 0, "Stored and detected PCI devices differ:\n%s%s" % (diff_stdout, diff_stderr))

<|endoftext|>


<commit_before>"""
commands related to displaying stuff

FIXME: some of these commands duplicate stuff in ui
"""

import logging

from sen.docker_backend import DockerImage, DockerContainer
from sen.exceptions import NotifyError
from sen.tui.buffer import TreeBuffer, HelpBuffer, ImageInfoBuffer, ContainerInfoBuffer, \
    MainListBuffer
from sen.tui.commands.base import FrontendCommand, register_command

logger = logging.getLogger(__name__)

@register_command
class DisplayLayersTreeCommand(FrontendCommand):
    name = "display-layers-tree"

    def run(self):
        self.ui.add_and_display_buffer(TreeBuffer(self.docker_object, self.ui))

@register_command
class DisplayHelpCommand(FrontendCommand):
    name = "display-help"

    def run(self):
        self.ui.add_and_display_buffer(HelpBuffer(self.ui, self.buffer))

@register_command
class RefreshBufferCommand(FrontendCommand):
    name = "refresh-buffer"

    def run(self):
        self.buffer.refresh()
        self.ui.loop.refresh()

@register_command
class DisplayInfoBufferCommand(FrontendCommand):
    name = "display-info"

    def run(self):
        # TODO: needs better abstraction, backend object should be tied together with frontend
        # object via a new Class
        if isinstance(self.docker_object, DockerImage):
            buffer_class = ImageInfoBuffer
        elif isinstance(self.docker_object, DockerContainer):
            buffer_class = ContainerInfoBuffer
        else:
            self.ui.notify_message("Can't display info for '%s'" % self.docker_object,
                                   level="error")
            logger.error("unable to display info buffer for %r", self.docker_object)
            return
        try:
            # FIXME: this try/except block should be in upper frame
            self.ui.add_and_display_buffer(buffer_class(self.docker_object, self.ui))
        except NotifyError as ex:
            self.ui.notify_message(str(ex), level="error")
            logger.error(repr(ex))

@register_command
class DisplayListingCommand(FrontendCommand):
    name = "display-listing"

    def run(self):
        b = MainListBuffer(self.ui, self.docker_backend)
        b.refresh()
        self.ui.add_and_display_buffer(b, redraw=True)
<commit_msg>display listing command: don't refresh twice<commit_after>"""
commands related to displaying stuff

FIXME: some of these commands duplicate stuff in ui
"""

import logging

from sen.docker_backend import DockerImage, DockerContainer
from sen.exceptions import NotifyError
from sen.tui.buffer import TreeBuffer, HelpBuffer, ImageInfoBuffer, ContainerInfoBuffer, \
    MainListBuffer
from sen.tui.commands.base import FrontendCommand, register_command

logger = logging.getLogger(__name__)

@register_command
class DisplayLayersTreeCommand(FrontendCommand):
    name = "display-layers-tree"

    def run(self):
        self.ui.add_and_display_buffer(TreeBuffer(self.docker_object, self.ui))

@register_command
class DisplayHelpCommand(FrontendCommand):
    name = "display-help"

    def run(self):
        self.ui.add_and_display_buffer(HelpBuffer(self.ui, self.buffer))

@register_command
class RefreshBufferCommand(FrontendCommand):
    name = "refresh-buffer"

    def run(self):
        self.buffer.refresh()
        self.ui.loop.refresh()

@register_command
class DisplayInfoBufferCommand(FrontendCommand):
    name = "display-info"

    def run(self):
        # TODO: needs better abstraction, backend object should be tied together with frontend
        # object via a new Class
        if isinstance(self.docker_object, DockerImage):
            buffer_class = ImageInfoBuffer
        elif isinstance(self.docker_object, DockerContainer):
            buffer_class = ContainerInfoBuffer
        else:
            self.ui.notify_message("Can't display info for '%s'" % self.docker_object,
                                   level="error")
            logger.error("unable to display info buffer for %r", self.docker_object)
            return
        try:
            # FIXME: this try/except block should be in upper frame
            self.ui.add_and_display_buffer(buffer_class(self.docker_object, self.ui))
        except NotifyError as ex:
            self.ui.notify_message(str(ex), level="error")
            logger.error(repr(ex))

@register_command
class DisplayListingCommand(FrontendCommand):
    name = "display-listing"

    def run(self):
        b = MainListBuffer(self.ui, self.docker_backend)
        self.ui.add_and_display_buffer(b, redraw=True)
<|endoftext|>