<commit_before>import fetchinterpreter
import unittest
from parseractions import *
from BeautifulSoup import BeautifulSoup

TEST_URL = "http://stackoverflow.com/questions/8221296/how-can-i-download-and-read-a-url-with-universal-newlines"

class TestFunctions(unittest.TestCase):
    def setUp(self):
        print "Testing: ", self
        fetchinterpreter.VARS = {}

    def test_fetchaction_get(self):
        name = "test"
        action = FetchAction(name, "GET", TEST_URL)
        fetchinterpreter.handle_line(action)
        self.assertEquals("GET", fetchinterpreter.VARS[name].method)
        self.assertEquals(TEST_URL, fetchinterpreter.VARS[name].url, TEST_URL)

    def test_fetchaction_post(self):
        name = "test"
        action = FetchAction(name, "POST", TEST_URL)
        fetchinterpreter.handle_line(action)
        self.assertEquals("POST", fetchinterpreter.VARS[name].method)
        self.assertEquals(TEST_URL, fetchinterpreter.VARS[name].url, TEST_URL)

    def test_filter_starts(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("starts","hello"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(2, len(output))
        self.assertTrue("hello world" in output)
        self.assertTrue("hello hello" in output)

    def test_filter_ends(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("ends","hello"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(2, len(output))
        self.assertTrue("world hello" in output)
        self.assertTrue("hello hello" in output)

    def test_filter_contains(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("contains","d he"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(1, len(output))
        self.assertTrue("world hello" in output)

    def test_filter_matches(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "HELLO100B",
            "YO100A",
            "hello100b",
            "foobar"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("matches","[A-Z]+100[ABC]"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(2, len(output))
        self.assertTrue("HELLO100B" in output)
        self.assertTrue("YO100A" in output)

    def test_filter_length_gt(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("length",">5"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(3, len(output))
        self.assertTrue("hello world" in output)
        self.assertTrue("world hello" in output)
        self.assertTrue("hello hello" in output)

    def test_filter_length_lt(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("length","<6"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(1, len(output))
        self.assertTrue("world" in output)

    def test_filter_length_eq(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("length","=5"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(1, len(output))
        self.assertTrue("world" in output)

    # Fine filters below.

    def test_filter_after(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = FineFilterAction("x", BasicFilterExpression("after","hell"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(4, len(output))
        self.assertEquals(["o world", "o", "o hello", ""], output)

    def test_filter_before(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = FineFilterAction("x", BasicFilterExpression("before","orl"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(4, len(output))
        self.assertEquals(["hello w", "w", "hello hello", "w"], output)

    def test_filter_afterpos(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = FineFilterAction("x", BasicFilterExpression("afterpos","3"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(4, len(output))
        self.assertEquals(["lo world", "ld hello", "lo hello", "ld"], output)

    def test_filter_beforepos(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = FineFilterAction("x", BasicFilterExpression("beforepos","3"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(["hel", "wor", "hel", "wor"], output)

    def test_filter_exclude(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world",
            "world world"
        ])
        action = FineFilterAction("x", BasicFilterExpression("exclude","world"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(["hello ", " hello", "hello hello", "", " "], output)

    def test_filter_striptags_img(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello
foobar
 world",
            "hello  world",
            "hello  world",
            "hello
hello
world",
        ])
        action = FineFilterAction("x", BasicFilterExpression("striptags","img"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals([
            "hello
foobar
 world",
            "hello  world",
            "hello  world",
            "hello
hello
world"], output)

    def test_filter_striptags_p_and_img(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello
foobar
 world",
            "hello  world",
            "hello  world",
            "hello
hello
world",
        ])
        action = FineFilterAction("x", BasicFilterExpression("striptags","p,img"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals([
            "hello  world",
            "hello  world",
            "hello  world",
            "hello world"], output)

    def test_filter_html_children(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.HtmlWrapper(
            [BeautifulSoup("<title>Page title</title>")])
        action = CoarseFilterAction("x", BasicFilterExpression("children","html"), "y")
        fetchinterpreter.handle_line(action)
        action = FineFilterAction("x", BasicFilterExpression("rawtext",""), "x")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(
            ["<title>Page title</title>"], output)

    def test_filter_html_findall_tag(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.HtmlWrapper(
            [BeautifulSoup("
p1

p2

p3
")])
        action = CoarseFilterAction("x", BasicFilterExpression("findall","p"), "y")
        fetchinterpreter.handle_line(action)
        action = FineFilterAction("x", BasicFilterExpression("rawtext",""), "x")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(
            ['
p1
', '
p2
', '
p3
'], output)

    def test_neg_coarse_filter(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x",
                                    NegFilterExpression(BasicFilterExpression("starts","hello")),
                                    "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(["world hello", "world"], output)

    def test_combined_filter_or(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "goodbye world",
            "hello hello",
            "world hello",
            "world goodbye"
        ])
        action = CoarseFilterAction("x",
                                    CombinedFilterExpression(
                                        BasicFilterExpression("starts","hello"),
                                        BasicFilterExpression("starts","goodbye"),
                                        "|"
                                    ),
                                    "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals([
            "hello world",
            "goodbye world",
            "hello hello"
        ], output)

    def test_combined_filter_and(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "goodbye world",
            "hello hello",
            "world hello",
            "hello goodbye"
        ])
        action = CoarseFilterAction("x",
                                    CombinedFilterExpression(
                                        BasicFilterExpression("starts","hello"),
                                        BasicFilterExpression("ends","goodbye"),
                                        "&"
                                    ),
                                    "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(["hello goodbye"], output)

    def test_combined_filter_and_noresult(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "goodbye world",
            "hello hello",
            "world hello",
            "world goodbye"
        ])
        action = CoarseFilterAction("x",
                                    CombinedFilterExpression(
                                        BasicFilterExpression("starts","hello"),
                                        BasicFilterExpression("starts","goodbye"),
                                        "&"
                                    ),
                                    "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals([], output)

    def test_assignment(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper(["hello world"])
        action = OutputAssignment("x", "y")
        fetchinterpreter.handle_line(action)
        self.assertTrue("x" in fetchinterpreter.VARS)
        self.assertEquals(["hello world"], fetchinterpreter.VARS["x"].output())

    def test_assignment_plus(self):
        fetchinterpreter.VARS["y1"] = fetchinterpreter.TextWrapper(["hello world"])
        fetchinterpreter.VARS["y2"] = fetchinterpreter.TextWrapper(["goodbye world"])
        action = OutputAssignment("x", ListPlus("y1", "y2"))
        fetchinterpreter.handle_line(action)
        self.assertTrue("x" in fetchinterpreter.VARS)
        self.assertEquals(["hello world", "goodbye world"], fetchinterpreter.VARS["x"].output())

    def test_assignment_valueat(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper(["hello", "world", "goodbye"])
        action = OutputAssignment("x", ListAt("y", "1"))
        fetchinterpreter.handle_line(action)
        self.assertTrue("x" in fetchinterpreter.VARS)
        self.assertEquals(["world"], fetchinterpreter.VARS["x"].output())

    def test_assignment_valueat_notinrange(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper(["hello", "world", "goodbye"])
        action = OutputAssignment("x", ListAt("y", "10"))
        try:
            fetchinterpreter.handle_line(action)
            self.fail("Expected failure")
        except fetchinterpreter.InterpreterException:
            pass # Expected

    def test_assignment_dict(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper(["hello world"])
        action = OutputAssignment("x", {"z" : "y"})
        fetchinterpreter.handle_line(action)
        self.assertTrue("x" in fetchinterpreter.VARS)
        self.assertTrue("z" in fetchinterpreter.VARS["x"])
        self.assertEquals(["hello world"], fetchinterpreter.VARS["x"]["z"].output())

    def test_assignment_dict_lvalue(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper(["hello world"])
        fetchinterpreter.VARS["x"] = {}
        action = OutputAssignment(DictAt("x","z"), "y")
        fetchinterpreter.handle_line(action)
        self.assertEquals(["hello world"], fetchinterpreter.VARS["x"]["z"].output())

if __name__ == '__main__':
    unittest.main()<commit_msg>Test for attr filter<commit_after>import fetchinterpreter
import unittest
from parseractions import *
from BeautifulSoup import BeautifulSoup

TEST_URL = "http://stackoverflow.com/questions/8221296/how-can-i-download-and-read-a-url-with-universal-newlines"

class TestFunctions(unittest.TestCase):
    def setUp(self):
        print "Testing: ", self
        fetchinterpreter.VARS = {}

    def test_fetchaction_get(self):
        name = "test"
        action = FetchAction(name, "GET", TEST_URL)
        fetchinterpreter.handle_line(action)
        self.assertEquals("GET", fetchinterpreter.VARS[name].method)
        self.assertEquals(TEST_URL, fetchinterpreter.VARS[name].url, TEST_URL)

    def test_fetchaction_post(self):
        name = "test"
        action = FetchAction(name, "POST", TEST_URL)
        fetchinterpreter.handle_line(action)
        self.assertEquals("POST", fetchinterpreter.VARS[name].method)
        self.assertEquals(TEST_URL, fetchinterpreter.VARS[name].url, TEST_URL)

    def test_filter_starts(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("starts","hello"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(2, len(output))
        self.assertTrue("hello world" in output)
        self.assertTrue("hello hello" in output)

    def test_filter_ends(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("ends","hello"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(2, len(output))
        self.assertTrue("world hello" in output)
        self.assertTrue("hello hello" in output)

    def test_filter_contains(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("contains","d he"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(1, len(output))
        self.assertTrue("world hello" in output)

    def test_filter_matches(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "HELLO100B",
            "YO100A",
            "hello100b",
            "foobar"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("matches","[A-Z]+100[ABC]"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(2, len(output))
        self.assertTrue("HELLO100B" in output)
        self.assertTrue("YO100A" in output)

    def test_filter_length_gt(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("length",">5"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(3, len(output))
        self.assertTrue("hello world" in output)
        self.assertTrue("world hello" in output)
        self.assertTrue("hello hello" in output)

    def test_filter_length_lt(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("length","<6"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(1, len(output))
        self.assertTrue("world" in output)

    def test_filter_length_eq(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x", BasicFilterExpression("length","=5"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(1, len(output))
        self.assertTrue("world" in output)

    # Fine filters below.

    def test_filter_after(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = FineFilterAction("x", BasicFilterExpression("after","hell"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(4, len(output))
        self.assertEquals(["o world", "o", "o hello", ""], output)

    def test_filter_before(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = FineFilterAction("x", BasicFilterExpression("before","orl"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(4, len(output))
        self.assertEquals(["hello w", "w", "hello hello", "w"], output)

    def test_filter_afterpos(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = FineFilterAction("x", BasicFilterExpression("afterpos","3"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(4, len(output))
        self.assertEquals(["lo world", "ld hello", "lo hello", "ld"], output)

    def test_filter_beforepos(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = FineFilterAction("x", BasicFilterExpression("beforepos","3"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(["hel", "wor", "hel", "wor"], output)

    def test_filter_exclude(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world",
            "world world"
        ])
        action = FineFilterAction("x", BasicFilterExpression("exclude","world"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(["hello ", " hello", "hello hello", "", " "], output)

    def test_filter_striptags_img(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello
foobar
 world",
            "hello  world",
            "hello  world",
            "hello
hello
world",
        ])
        action = FineFilterAction("x", BasicFilterExpression("striptags","img"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals([
            "hello
foobar
 world",
            "hello  world",
            "hello  world",
            "hello
hello
world"], output)

    def test_filter_striptags_p_and_img(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello
foobar
 world",
            "hello  world",
            "hello  world",
            "hello
hello
world",
        ])
        action = FineFilterAction("x", BasicFilterExpression("striptags","p,img"), "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals([
            "hello  world",
            "hello  world",
            "hello  world",
            "hello world"], output)

    def test_filter_html_children(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.HtmlWrapper(
            [BeautifulSoup("<title>Page title</title>")])
        action = CoarseFilterAction("x", BasicFilterExpression("children","html"), "y")
        fetchinterpreter.handle_line(action)
        action = FineFilterAction("x", BasicFilterExpression("rawtext",""), "x")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(
            ["<title>Page title</title>"], output)

    def test_filter_html_findall_tag(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.HtmlWrapper(
            [BeautifulSoup("
p1

p2

p3
")])
        action = CoarseFilterAction("x", BasicFilterExpression("findall","p"), "y")
        fetchinterpreter.handle_line(action)
        action = FineFilterAction("x", BasicFilterExpression("rawtext",""), "x")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(
            ['
p1
', '
p2
', '
p3
'], output)

    def test_filter_html_attr(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.HtmlWrapper(
            [BeautifulSoup("")])
        action = CoarseFilterAction("x", BasicFilterExpression("findall","img"), "y")
        fetchinterpreter.handle_line(action)
        action = FineFilterAction("x", BasicFilterExpression("attr","src"), "x")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(
            ["things", ""], output)

    def test_neg_coarse_filter(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "world hello",
            "hello hello",
            "world"
        ])
        action = CoarseFilterAction("x",
                                    NegFilterExpression(BasicFilterExpression("starts","hello")),
                                    "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(["world hello", "world"], output)

    def test_combined_filter_or(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "goodbye world",
            "hello hello",
            "world hello",
            "world goodbye"
        ])
        action = CoarseFilterAction("x",
                                    CombinedFilterExpression(
                                        BasicFilterExpression("starts","hello"),
                                        BasicFilterExpression("starts","goodbye"),
                                        "|"
                                    ),
                                    "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals([
            "hello world",
            "goodbye world",
            "hello hello"
        ], output)

    def test_combined_filter_and(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "goodbye world",
            "hello hello",
            "world hello",
            "hello goodbye"
        ])
        action = CoarseFilterAction("x",
                                    CombinedFilterExpression(
                                        BasicFilterExpression("starts","hello"),
                                        BasicFilterExpression("ends","goodbye"),
                                        "&"
                                    ),
                                    "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals(["hello goodbye"], output)

    def test_combined_filter_and_noresult(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper([
            "hello world",
            "goodbye world",
            "hello hello",
            "world hello",
            "world goodbye"
        ])
        action = CoarseFilterAction("x",
                                    CombinedFilterExpression(
                                        BasicFilterExpression("starts","hello"),
                                        BasicFilterExpression("starts","goodbye"),
                                        "&"
                                    ),
                                    "y")
        fetchinterpreter.handle_line(action)
        output = fetchinterpreter.VARS["x"].output()
        self.assertEquals([], output)

    def test_assignment(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper(["hello world"])
        action = OutputAssignment("x", "y")
        fetchinterpreter.handle_line(action)
        self.assertTrue("x" in fetchinterpreter.VARS)
        self.assertEquals(["hello world"], fetchinterpreter.VARS["x"].output())

    def test_assignment_plus(self):
        fetchinterpreter.VARS["y1"] = fetchinterpreter.TextWrapper(["hello world"])
        fetchinterpreter.VARS["y2"] = fetchinterpreter.TextWrapper(["goodbye world"])
        action = OutputAssignment("x", ListPlus("y1", "y2"))
        fetchinterpreter.handle_line(action)
        self.assertTrue("x" in fetchinterpreter.VARS)
        self.assertEquals(["hello world", "goodbye world"], fetchinterpreter.VARS["x"].output())

    def test_assignment_valueat(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper(["hello", "world", "goodbye"])
        action = OutputAssignment("x", ListAt("y", "1"))
        fetchinterpreter.handle_line(action)
        self.assertTrue("x" in fetchinterpreter.VARS)
        self.assertEquals(["world"], fetchinterpreter.VARS["x"].output())

    def test_assignment_valueat_notinrange(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper(["hello", "world", "goodbye"])
        action = OutputAssignment("x", ListAt("y", "10"))
        try:
            fetchinterpreter.handle_line(action)
            self.fail("Expected failure")
        except fetchinterpreter.InterpreterException:
            pass # Expected

    def test_assignment_dict(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper(["hello world"])
        action = OutputAssignment("x", {"z" : "y"})
        fetchinterpreter.handle_line(action)
        self.assertTrue("x" in fetchinterpreter.VARS)
        self.assertTrue("z" in fetchinterpreter.VARS["x"])
        self.assertEquals(["hello world"], fetchinterpreter.VARS["x"]["z"].output())

    def test_assignment_dict_lvalue(self):
        fetchinterpreter.VARS["y"] = fetchinterpreter.TextWrapper(["hello world"])
        fetchinterpreter.VARS["x"] = {}
        action = OutputAssignment(DictAt("x","z"), "y")
        fetchinterpreter.handle_line(action)
        self.assertEquals(["hello world"], fetchinterpreter.VARS["x"]["z"].output())

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


<commit_before>from flask import Flask, request, make_response, redirect, abort

app = Flask(__name__)

@app.route('/')
def index():
    return redirect('http://www.baidu.com')

@app.route('/user/<name>')
def user(name):
    return '
Hello, %s!
' % name

if __name__ == '__main__':
    app.run(debug=True)<commit_msg>edited<commit_after>from flask import Flask, request, make_response, redirect, abort
from flask.ext.script import Manager

app = Flask(__name__)
manager = Manager(app)

@app.route('/')
def index():
    response = make_response('
This document carries a cookie!
')
    response.set_cookie('answer', '42')
    return response

@app.route('/user/<name>')
def user(name):
    return '
Hello, %s!
' % name

@app.route('/user/<id>')
def get_user(id):
    user = load_user(id)
    if not user:
        abort(404)
    return '
Hello, %s
' % user.name

if __name__ == '__main__':
    manager.run()<|endoftext|>


<commit_before># 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.

INCLUDES = """
#ifndef OPENSSL_NO_EC
#include <openssl/ec.h>
#endif

#include <openssl/obj_mac.h>
"""

TYPES = """
static const int Cryptography_HAS_EC;

typedef ... EC_KEY;
typedef struct {
    ...;
} EC_builtin_curve;

static const int NID_X9_62_c2pnb163v1;
static const int NID_X9_62_c2pnb163v2;
static const int NID_X9_62_c2pnb163v3;
static const int NID_X9_62_c2pnb176v1;
static const int NID_X9_62_c2tnb191v1;
static const int NID_X9_62_c2tnb191v2;
static const int NID_X9_62_c2tnb191v3;
static const int NID_X9_62_c2onb191v4;
static const int NID_X9_62_c2onb191v5;
static const int NID_X9_62_c2pnb208w1;
static const int NID_X9_62_c2tnb239v1;
static const int NID_X9_62_c2tnb239v2;
static const int NID_X9_62_c2tnb239v3;
static const int NID_X9_62_c2onb239v4;
static const int NID_X9_62_c2onb239v5;
static const int NID_X9_62_c2pnb272w1;
static const int NID_X9_62_c2pnb304w1;
static const int NID_X9_62_c2tnb359v1;
static const int NID_X9_62_c2pnb368w1;
static const int NID_X9_62_c2tnb431r1;
static const int NID_X9_62_prime192v1;
static const int NID_X9_62_prime192v2;
static const int NID_X9_62_prime192v3;
static const int NID_X9_62_prime239v1;
static const int NID_X9_62_prime239v2;
static const int NID_X9_62_prime239v3;
static const int NID_X9_62_prime256v1;
static const int NID_secp112r1;
static const int NID_secp112r2;
static const int NID_secp128r1;
static const int NID_secp128r2;
static const int NID_secp160k1;
static const int NID_secp160r1;
static const int NID_secp160r2;
static const int NID_sect163k1;
static const int NID_sect163r1;
static const int NID_sect163r2;
static const int NID_secp192k1;
static const int NID_secp224k1;
static const int NID_secp224r1;
static const int NID_secp256k1;
static const int NID_secp384r1;
static const int NID_secp521r1;
static const int NID_sect113r1;
static const int NID_sect113r2;
static const int NID_sect131r1;
static const int NID_sect131r2;
static const int NID_sect193r1;
static const int NID_sect193r2;
static const int NID_sect233k1;
static const int NID_sect233r1;
static const int NID_sect239k1;
static const int NID_sect283k1;
static const int NID_sect283r1;
static const int NID_sect409k1;
static const int NID_sect409r1;
static const int NID_sect571k1;
static const int NID_sect571r1;
static const int NID_wap_wsg_idm_ecid_wtls1;
static const int NID_wap_wsg_idm_ecid_wtls3;
static const int NID_wap_wsg_idm_ecid_wtls4;
static const int NID_wap_wsg_idm_ecid_wtls5;
static const int NID_wap_wsg_idm_ecid_wtls6;
static const int NID_wap_wsg_idm_ecid_wtls7;
static const int NID_wap_wsg_idm_ecid_wtls8;
static const int NID_wap_wsg_idm_ecid_wtls9;
static const int NID_wap_wsg_idm_ecid_wtls10;
static const int NID_wap_wsg_idm_ecid_wtls11;
static const int NID_wap_wsg_idm_ecid_wtls12;
static const int NID_ipsec3;
static const int NID_ipsec4;
static const char *const SN_X9_62_c2pnb163v1;
static const char *const SN_X9_62_c2pnb163v2;
static const char *const SN_X9_62_c2pnb163v3;
static const char *const SN_X9_62_c2pnb176v1;
static const char *const SN_X9_62_c2tnb191v1;
static const char *const SN_X9_62_c2tnb191v2;
static const char *const SN_X9_62_c2tnb191v3;
static const char *const SN_X9_62_c2onb191v4;
static const char *const SN_X9_62_c2onb191v5;
static const char *const SN_X9_62_c2pnb208w1;
static const char *const SN_X9_62_c2tnb239v1;
static const char *const SN_X9_62_c2tnb239v2;
static const char *const SN_X9_62_c2tnb239v3;
static const char *const SN_X9_62_c2onb239v4;
static const char *const SN_X9_62_c2onb239v5;
static const char *const SN_X9_62_c2pnb272w1;
static const char *const SN_X9_62_c2pnb304w1;
static const char *const SN_X9_62_c2tnb359v1;
static const char *const SN_X9_62_c2pnb368w1;
static const char *const SN_X9_62_c2tnb431r1;
static const char *const SN_X9_62_prime192v1;
static const char *const SN_X9_62_prime192v2;
static const char *const SN_X9_62_prime192v3;
static const char *const SN_X9_62_prime239v1;
static const char *const SN_X9_62_prime239v2;
static const char *const SN_X9_62_prime239v3;
static const char *const SN_X9_62_prime256v1;
static const char *const SN_secp112r1;
static const char *const SN_secp112r2;
static const char *const SN_secp128r1;
static const char *const SN_secp128r2;
static const char *const SN_secp160k1;
static const char *const SN_secp160r1;
static const char *const SN_secp160r2;
static const char *const SN_sect163k1;
static const char *const SN_sect163r1;
static const char *const SN_sect163r2;
static const char *const SN_secp192k1;
static const char *const SN_secp224k1;
static const char *const SN_secp224r1;
static const char *const SN_secp256k1;
static const char *const SN_secp384r1;
static const char *const SN_secp521r1;
static const char *const SN_sect113r1;
static const char *const SN_sect113r2;
static const char *const SN_sect131r1;
static const char *const SN_sect131r2;
static const char *const SN_sect193r1;
static const char *const SN_sect193r2;
static const char *const SN_sect233k1;
static const char *const SN_sect233r1;
static const char *const SN_sect239k1;
static const char *const SN_sect283k1;
static const char *const SN_sect283r1;
static const char *const SN_sect409k1;
static const char *const SN_sect409r1;
static const char *const SN_sect571k1;
static const char *const SN_sect571r1;
static const char *const SN_wap_wsg_idm_ecid_wtls1;
static const char *const SN_wap_wsg_idm_ecid_wtls3;
static const char *const SN_wap_wsg_idm_ecid_wtls4;
static const char *const SN_wap_wsg_idm_ecid_wtls5;
static const char *const SN_wap_wsg_idm_ecid_wtls6;
static const char *const SN_wap_wsg_idm_ecid_wtls7;
static const char *const SN_wap_wsg_idm_ecid_wtls8;
static const char *const SN_wap_wsg_idm_ecid_wtls9;
static const char *const SN_wap_wsg_idm_ecid_wtls10;
static const char *const SN_wap_wsg_idm_ecid_wtls11;
static const char *const SN_wap_wsg_idm_ecid_wtls12;
static const char *const SN_ipsec3;
static const char *const SN_ipsec4;

"""

FUNCTIONS = """
"""

MACROS = """
EC_KEY *EC_KEY_new_by_curve_name(int);
void EC_KEY_free(EC_KEY *);

size_t EC_get_builtin_curves(EC_builtin_curve *, size_t);

int Cryptography_EC_builtin_curve_get_nid(EC_builtin_curve *);
"""

CUSTOMIZATIONS = """
#ifdef OPENSSL_NO_EC
static const long Cryptography_HAS_EC = 0;
typedef void EC_KEY;
typedef void EC_builtin_curve;
EC_KEY* (*EC_KEY_new_by_curve_name)(int) = NULL;
void (*EC_KEY_free)(EC_KEY *) = NULL;
size_t (*EC_get_builtin_curves)(EC_builtin_curve *, size_t) = NULL;
int (*Cryptography_EC_builtin_curve_get_nid)(EC_builtin_curve *) = NULL;
#else
static const long Cryptography_HAS_EC = 1;

int Cryptography_EC_builtin_curve_get_nid(EC_builtin_curve *c) {
    return c->nid;
}
#endif
"""

CONDITIONAL_NAMES = {
    "Cryptography_HAS_EC": [
        "EC_KEY_new_by_curve_name",
        "EC_KEY_free",
        "EC_get_builtin_curves",

        "Cryptography_EC_builtin_curve_get_nid",
    ],
}
<commit_msg>Simplify<commit_after># 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.

INCLUDES = """
#ifndef OPENSSL_NO_EC
#include <openssl/ec.h>
#endif

#include <openssl/obj_mac.h>
"""

TYPES = """
static const int Cryptography_HAS_EC;

typedef ... EC_KEY;
typedef struct {
    int nid;
    const char *comment;
} EC_builtin_curve;

static const int NID_X9_62_c2pnb163v1;
static const int NID_X9_62_c2pnb163v2;
static const int NID_X9_62_c2pnb163v3;
static const int NID_X9_62_c2pnb176v1;
static const int NID_X9_62_c2tnb191v1;
static const int NID_X9_62_c2tnb191v2;
static const int NID_X9_62_c2tnb191v3;
static const int NID_X9_62_c2onb191v4;
static const int NID_X9_62_c2onb191v5;
static const int NID_X9_62_c2pnb208w1;
static const int NID_X9_62_c2tnb239v1;
static const int NID_X9_62_c2tnb239v2;
static const int NID_X9_62_c2tnb239v3;
static const int NID_X9_62_c2onb239v4;
static const int NID_X9_62_c2onb239v5;
static const int NID_X9_62_c2pnb272w1;
static const int NID_X9_62_c2pnb304w1;
static const int NID_X9_62_c2tnb359v1;
static const int NID_X9_62_c2pnb368w1;
static const int NID_X9_62_c2tnb431r1;
static const int NID_X9_62_prime192v1;
static const int NID_X9_62_prime192v2;
static const int NID_X9_62_prime192v3;
static const int NID_X9_62_prime239v1;
static const int NID_X9_62_prime239v2;
static const int NID_X9_62_prime239v3;
static const int NID_X9_62_prime256v1;
static const int NID_secp112r1;
static const int NID_secp112r2;
static const int NID_secp128r1;
static const int NID_secp128r2;
static const int NID_secp160k1;
static const int NID_secp160r1;
static const int NID_secp160r2;
static const int NID_sect163k1;
static const int NID_sect163r1;
static const int NID_sect163r2;
static const int NID_secp192k1;
static const int NID_secp224k1;
static const int NID_secp224r1;
static const int NID_secp256k1;
static const int NID_secp384r1;
static const int NID_secp521r1;
static const int NID_sect113r1;
static const int NID_sect113r2;
static const int NID_sect131r1;
static const int NID_sect131r2;
static const int NID_sect193r1;
static const int NID_sect193r2;
static const int NID_sect233k1;
static const int NID_sect233r1;
static const int NID_sect239k1;
static const int NID_sect283k1;
static const int NID_sect283r1;
static const int NID_sect409k1;
static const int NID_sect409r1;
static const int NID_sect571k1;
static const int NID_sect571r1;
static const int NID_wap_wsg_idm_ecid_wtls1;
static const int NID_wap_wsg_idm_ecid_wtls3;
static const int NID_wap_wsg_idm_ecid_wtls4;
static const int NID_wap_wsg_idm_ecid_wtls5;
static const int NID_wap_wsg_idm_ecid_wtls6;
static const int NID_wap_wsg_idm_ecid_wtls7;
static const int NID_wap_wsg_idm_ecid_wtls8;
static const int NID_wap_wsg_idm_ecid_wtls9;
static const int NID_wap_wsg_idm_ecid_wtls10;
static const int NID_wap_wsg_idm_ecid_wtls11;
static const int NID_wap_wsg_idm_ecid_wtls12;
static const int NID_ipsec3;
static const int NID_ipsec4;
static const char *const SN_X9_62_c2pnb163v1;
static const char *const SN_X9_62_c2pnb163v2;
static const char *const SN_X9_62_c2pnb163v3;
static const char *const SN_X9_62_c2pnb176v1;
static const char *const SN_X9_62_c2tnb191v1;
static const char *const SN_X9_62_c2tnb191v2;
static const char *const SN_X9_62_c2tnb191v3;
static const char *const SN_X9_62_c2onb191v4;
static const char *const SN_X9_62_c2onb191v5;
static const char *const SN_X9_62_c2pnb208w1;
static const char *const SN_X9_62_c2tnb239v1;
static const char *const SN_X9_62_c2tnb239v2;
static const char *const SN_X9_62_c2tnb239v3;
static const char *const SN_X9_62_c2onb239v4;
static const char *const SN_X9_62_c2onb239v5;
static const char *const SN_X9_62_c2pnb272w1;
static const char *const SN_X9_62_c2pnb304w1;
static const char *const SN_X9_62_c2tnb359v1;
static const char *const SN_X9_62_c2pnb368w1;
static const char *const SN_X9_62_c2tnb431r1;
static const char *const SN_X9_62_prime192v1;
static const char *const SN_X9_62_prime192v2;
static const char *const SN_X9_62_prime192v3;
static const char *const SN_X9_62_prime239v1;
static const char *const SN_X9_62_prime239v2;
static const char *const SN_X9_62_prime239v3;
static const char *const SN_X9_62_prime256v1;
static const char *const SN_secp112r1;
static const char *const SN_secp112r2;
static const char *const SN_secp128r1;
static const char *const SN_secp128r2;
static const char *const SN_secp160k1;
static const char *const SN_secp160r1;
static const char *const SN_secp160r2;
static const char *const SN_sect163k1;
static const char *const SN_sect163r1;
static const char *const SN_sect163r2;
static const char *const SN_secp192k1;
static const char *const SN_secp224k1;
static const char *const SN_secp224r1;
static const char *const SN_secp256k1;
static const char *const SN_secp384r1;
static const char *const SN_secp521r1;
static const char *const SN_sect113r1;
static const char *const SN_sect113r2;
static const char *const SN_sect131r1;
static const char *const SN_sect131r2;
static const char *const SN_sect193r1;
static const char *const SN_sect193r2;
static const char *const SN_sect233k1;
static const char *const SN_sect233r1;
static const char *const SN_sect239k1;
static const char *const SN_sect283k1;
static const char *const SN_sect283r1;
static const char *const SN_sect409k1;
static const char *const SN_sect409r1;
static const char *const SN_sect571k1;
static const char *const SN_sect571r1;
static const char *const SN_wap_wsg_idm_ecid_wtls1;
static const char *const SN_wap_wsg_idm_ecid_wtls3;
static const char *const SN_wap_wsg_idm_ecid_wtls4;
static const char *const SN_wap_wsg_idm_ecid_wtls5;
static const char *const SN_wap_wsg_idm_ecid_wtls6;
static const char *const SN_wap_wsg_idm_ecid_wtls7;
static const char *const SN_wap_wsg_idm_ecid_wtls8;
static const char *const SN_wap_wsg_idm_ecid_wtls9;
static const char *const SN_wap_wsg_idm_ecid_wtls10;
static const char *const SN_wap_wsg_idm_ecid_wtls11;
static const char *const SN_wap_wsg_idm_ecid_wtls12;
static const char *const SN_ipsec3;
static const char *const SN_ipsec4;

"""

FUNCTIONS = """
"""

MACROS = """
EC_KEY *EC_KEY_new_by_curve_name(int);
void EC_KEY_free(EC_KEY *);

size_t EC_get_builtin_curves(EC_builtin_curve *, size_t);

"""

CUSTOMIZATIONS = """
#ifdef OPENSSL_NO_EC
static const long Cryptography_HAS_EC = 0;
typedef void EC_KEY;
typedef struct {
    int nid;
    const char *comment;
} EC_builtin_curve;
EC_KEY* (*EC_KEY_new_by_curve_name)(int) = NULL;
void (*EC_KEY_free)(EC_KEY *) = NULL;
size_t (*EC_get_builtin_curves)(EC_builtin_curve *, size_t) = NULL;
#else
static const long Cryptography_HAS_EC = 1;
#endif
"""

CONDITIONAL_NAMES = {
    "Cryptography_HAS_EC": [
        "EC_KEY_new_by_curve_name",
        "EC_KEY_free",
        "EC_get_builtin_curves",
    ],
}
<|endoftext|>


<commit_before>"""
 general mediawiki-engine (Web)

 @website     websites built on mediawiki (https://www.mediawiki.org)
 @provide-api yes (http://www.mediawiki.org/wiki/API:Search)

 @using-api   yes
 @results     JSON
 @stable      yes
 @parse       url, title

 @todo        content
"""

from json import loads
from string import Formatter
from searx.url_utils import urlencode, quote

# engine dependent config
categories = ['general']
language_support = True
paging = True
number_of_results = 1
search_type = 'nearmatch' # possible values: title, text, nearmatch

# search-url
base_url = 'https://{language}.wikipedia.org/'
search_postfix = 'w/api.php?action=query'\
    '&list=search'\
    '&{query}'\
    '&format=json'\
    '&sroffset={offset}'\
    '&srlimit={limit}'\
    '&srwhat={searchtype}'

# do search-request
def request(query, params):
    offset = (params['pageno'] - 1) * number_of_results

    string_args = dict(query=urlencode({'srsearch': query}),
                       offset=offset,
                       limit=number_of_results,
                       searchtype=search_type)

    format_strings = list(Formatter().parse(base_url))

    if params['language'] == 'all':
        language = 'en'
    else:
        language = params['language'].split('-')[0]

    # format_string [('https://', 'language', '', None), ('.wikipedia.org/', None, None, None)]
    if any(x[1] == 'language' for x in format_strings):
        string_args['language'] = language

    # write search-language back to params, required in response
    params['language'] = language

    search_url = base_url + search_postfix

    params['url'] = search_url.format(**string_args)

    return params

# get response from search-request
def response(resp):
    results = []

    search_results = loads(resp.text)

    # return empty array if there are no results
    if not search_results.get('query', {}).get('search'):
        return []

    # parse results
    for result in search_results['query']['search']:
        if result.get('snippet', '').startswith('#REDIRECT'):
            continue
        url = base_url.format(language=resp.search_params['language']) +\
            'wiki/' + quote(result['title'].replace(' ', '_').encode('utf-8'))

        # append result
        results.append({'url': url,
                        'title': result['title'],
                        'content': ''})

    # return results
    return results
<commit_msg>[fix] pep8<commit_after>"""
 general mediawiki-engine (Web)

 @website     websites built on mediawiki (https://www.mediawiki.org)
 @provide-api yes (http://www.mediawiki.org/wiki/API:Search)

 @using-api   yes
 @results     JSON
 @stable      yes
 @parse       url, title

 @todo        content
"""

from json import loads
from string import Formatter
from searx.url_utils import urlencode, quote

# engine dependent config
categories = ['general']
language_support = True
paging = True
number_of_results = 1
search_type = 'nearmatch'  # possible values: title, text, nearmatch

# search-url
base_url = 'https://{language}.wikipedia.org/'
search_postfix = 'w/api.php?action=query'\
    '&list=search'\
    '&{query}'\
    '&format=json'\
    '&sroffset={offset}'\
    '&srlimit={limit}'\
    '&srwhat={searchtype}'

# do search-request
def request(query, params):
    offset = (params['pageno'] - 1) * number_of_results

    string_args = dict(query=urlencode({'srsearch': query}),
                       offset=offset,
                       limit=number_of_results,
                       searchtype=search_type)

    format_strings = list(Formatter().parse(base_url))

    if params['language'] == 'all':
        language = 'en'
    else:
        language = params['language'].split('-')[0]

    # format_string [('https://', 'language', '', None), ('.wikipedia.org/', None, None, None)]
    if any(x[1] == 'language' for x in format_strings):
        string_args['language'] = language

    # write search-language back to params, required in response
    params['language'] = language

    search_url = base_url + search_postfix

    params['url'] = search_url.format(**string_args)

    return params

# get response from search-request
def response(resp):
    results = []

    search_results = loads(resp.text)

    # return empty array if there are no results
    if not search_results.get('query', {}).get('search'):
        return []

    # parse results
    for result in search_results['query']['search']:
        if result.get('snippet', '').startswith('#REDIRECT'):
            continue
        url = base_url.format(language=resp.search_params['language']) +\
            'wiki/' + quote(result['title'].replace(' ', '_').encode('utf-8'))

        # append result
        results.append({'url': url,
                        'title': result['title'],
                        'content': ''})

    # return results
    return results
<|endoftext|>


<commit_before>
"""
kombu.transport.mongodb
=======================

MongoDB transport.

:copyright: (c) 2010 - 2011 by Flavio Percoco Premoli.
:license: BSD, see LICENSE for more details.

"""
from Queue import Empty

from anyjson import serialize, deserialize
from pymongo import errors
from pymongo.connection import Connection

from kombu.transport import virtual

DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 27017

__author__ = "Flavio [FlaPer87] Percoco Premoli <flaper87@flaper87.org>"

class Channel(virtual.Channel):
    _client = None

    def _new_queue(self, queue, **kwargs):
        pass

    def _get(self, queue):
        try:
            msg = self.client.database.command("findandmodify",
                        "messages", query={"queue": queue}, remove=True)
        except errors.OperationFailure:
            raise Empty()
        return deserialize(msg["value"]["payload"])

    def _size(self, queue):
        return self.client.count()

    def _put(self, queue, message, **kwargs):
        self.client.insert({"payload": serialize(message), "queue": queue})

    def _purge(self, queue):
        size = self._size(queue)
        self.client.remove({"queue": queue})
        return size

    def close(self):
        super(Channel, self).close()
        self.client.database.connection.end_request()

    def _open(self):
        conninfo = self.connection.client
        mongoconn = Connection(host=conninfo.hostname, port=conninfo.port)
        dbname = conninfo.virtual_host
        if not dbname or dbname == "/":
            dbname = "kombu_default"
        database = getattr(mongoconn, dbname)
        col = database.messages
        col.ensure_index([("queue", 1)])
        return col

    @property
    def client(self):
        if self._client is None:
            self._client = self._open()
        return self._client

class Transport(virtual.Transport):
    Channel = Channel

    interval = 1
    default_port = DEFAULT_PORT
    connection_errors = (errors.ConnectionFailure, )
    channel_errors = (errors.ConnectionFailure,
                      errors.OperationFailure, )
<commit_msg>MongoDB transport requires MongoDB 1.3.0+<commit_after>
"""
kombu.transport.mongodb
=======================

MongoDB transport.

:copyright: (c) 2010 - 2011 by Flavio Percoco Premoli.
:license: BSD, see LICENSE for more details.

"""
from Queue import Empty

from anyjson import serialize, deserialize
from pymongo import errors
from pymongo.connection import Connection

from kombu.transport import virtual

DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 27017

__author__ = "Flavio [FlaPer87] Percoco Premoli <flaper87@flaper87.org>"

class Channel(virtual.Channel):
    _client = None

    def _new_queue(self, queue, **kwargs):
        pass

    def _get(self, queue):
        try:
            msg = self.client.database.command("findandmodify",
                        "messages", query={"queue": queue}, remove=True)
        except errors.OperationFailure, exc:
            if "No matching object found" in exc.args[0]:
                raise Empty()
            raise
        return deserialize(msg["value"]["payload"])

    def _size(self, queue):
        return self.client.count()

    def _put(self, queue, message, **kwargs):
        self.client.insert({"payload": serialize(message), "queue": queue})

    def _purge(self, queue):
        size = self._size(queue)
        self.client.remove({"queue": queue})
        return size

    def close(self):
        super(Channel, self).close()
        self.client.database.connection.end_request()

    def _open(self):
        conninfo = self.connection.client
        mongoconn = Connection(host=conninfo.hostname, port=conninfo.port)
        dbname = conninfo.virtual_host
        version = mongoconn.server_info()["version"]
        if tuple(map(int, version.split("."))) < (1, 3):
            raise NotImplementedError(
                "Kombu requires MongoDB version 1.3+, but connected to %s" % (
                    version, ))
        if not dbname or dbname == "/":
            dbname = "kombu_default"
        database = getattr(mongoconn, dbname)
        col = database.messages
        col.ensure_index([("queue", 1)])
        return col

    @property
    def client(self):
        if self._client is None:
            self._client = self._open()
        return self._client

class Transport(virtual.Transport):
    Channel = Channel

    interval = 1
    default_port = DEFAULT_PORT
    connection_errors = (errors.ConnectionFailure, )
    channel_errors = (errors.ConnectionFailure,
                      errors.OperationFailure, )
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Awesome riddles
"""

riddles = [
    ("Tu connais l'histoire de Toto au toilettes?", "Ben moi non plus"),
    ("Où est charlie?", "Sur ta mère"),
    ("Qu'est-ce qui est vert, qui vit sous terre, et qui mange des pierres ?", "Le mange cailloux"),
    ("Pourquoi les Américains ne voient-ils pas le bout du tunnel ?", "Parce que George Bush."),
    ("La petite fille tombe de la balançoire. Pourquoi ?", "Parce qu'elle n'a pas de bras."),
    ("Pourquoi les Spices Girls mouillent ?" , "Parce que les boys band!"),

]

if __name__ == "__main__":
    from bot import InnocentHand
    joker = InnocentHand(riddles)
    print joker()
    print joker(1)
<commit_msg>Riddles++<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Awesome riddles
"""

riddles = [
    ("Tu connais l'histoire de Toto au toilettes?", "Ben moi non plus"),
    ("Où est charlie?", "Sur ta mère"),
    ("Qu'est-ce qui est vert, qui vit sous terre, et qui mange des pierres ?", "Le mange cailloux"),
    ("Pourquoi les Américains ne voient-ils pas le bout du tunnel ?", "Parce que George Bush."),
    ("La petite fille tombe de la balançoire. Pourquoi ?", "Parce qu'elle n'a pas de bras."),
    ("Pourquoi les Spices Girls mouillent ?" , "Parce que les boys band!"),
    ("Pourquoi les blondes n'aiment pas les vibromasseurs ?", "Parceque ça fait mal aux dents")
]

if __name__ == "__main__":
    from bot import InnocentHand
    joker = InnocentHand(riddles)
    print joker()
    print joker(1)
<|endoftext|>


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

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

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

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

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '(&7)ub7ecukla=wzzb1h-u*x3l(93-=r*edcod@%)zn=v7u+@f'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'behave_django',
    'test_app',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'test_project.urls'

WSGI_APPLICATION = 'test_project.wsgi.application'

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

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

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

STATIC_URL = '/static/'

# TODO: Remove this
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'behave_django', 'templates'),
)

BEHAVE_FEATURES = (
    os.path.join(BASE_DIR, 'features'),
)
<commit_msg>Removed uneccessary TEMPLATE_DIRs modification for tests to pass<commit_after>"""
Django settings for test_project project.

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

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

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

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '(&7)ub7ecukla=wzzb1h-u*x3l(93-=r*edcod@%)zn=v7u+@f'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'behave_django',
    'test_app',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'test_project.urls'

WSGI_APPLICATION = 'test_project.wsgi.application'

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

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

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

STATIC_URL = '/static/'

# behave_django
BEHAVE_FEATURES = (
    os.path.join(BASE_DIR, 'features'),
)
<|endoftext|>


<commit_before>from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()
<commit_msg>renamed<commit_after><|endoftext|>


<commit_before>#!/usr/env/python -tt
# -*- encoding=utf8 -*-
#
#   Copyright 2009-2012 Michal Sadowski (sq6jnx at hamradio dot pl)
#
#   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 urllib
import pygame
import datetime
import re
from config import ibles  as config

lang=None

def downloadFile(url):
    webFile = urllib.urlopen(url)
    return webFile.read()

def my_import(name):
    mod = __import__(name)
    components = name.split('.')
    for comp in components[1:]:
        mod = getattr(mod, comp)
    return mod

strefy = [
        (  0,  0),
        ( 80,120), ( 40,190), (100,180),    # strefy:  1,  2,  3,
        ( 70,240), (140, 90), (200, 80),    #       :  4,  5,  6,
        (260, 50), (400, 90), (380,150),    #       :  7,  8,  9,
        (410,190), (510, 90), (550, 90),    #       : 10, 11, 12,
        (500,170), (550,200), (170,170),    #       : 13, 14, 15,
        (140,220), (200,200), (250,140),    #       : 16, 17, 18,
        (260,200), (320,170), (100,280),    #       : 19, 20, 21,
        ( 80,310), (170,260), (180,310),    #       : 22, 23, 24,
        (260,270), (250,350), (360,260),    #       : 25, 26, 27,
        (310,330), (360,360), (480,220),    #       : 28, 29, 30,
        (460,280), (440,340), (430,390),    #       : 31, 32, 33,
        (420,450), (560,310), (560,400),    #       : 34, 35, 36,
        (120,370), (180,390), (250,420),    #       : 37, 38, 39,
        (330,440), (390,490), (490,490),    #       : 40, 41, 42
        ]

poziomy = {
        #  R   G   B   A
        (255,  0,  0,255):  3, # zagrożenie duże
        (255,255,  0,255):  2, # zagrożenie średnie
        (  0,255,  0,255):  1, # zagrożenie małe
        (  0,  0,255,255):  0, # brak zagrożenia

        (250,250,212,255): -1, # rejon nieobjęty prognozowaniem
        (160,160,160,255): -2, # brak danych
        }

poziomy_nazwy = ['', 'male', 'srednie', 'duze', ]

def get_forecast_url():
    """Podaje dokładnie taki URL, na który powołuje się strona
    ``http://bazapozarow.ibles.pl/zagrozenie/``, niekiedy bowiem poranne raporty
    o zagrożeniu pożarowym są wydawane na godziny południowe. W przypadku,
    gdy bieżąca data i czas na komputerze jest z zakresu 1 października -- 31
    marca zwraca None, gdyż w tym okresie zagrożenie pożarowe lasów nie jest
    prognozowane."""

    _url=re.compile('mapa\.php\?rok=(\d{4})&mies=(\d{1,2})&'+\
            'dzien=(\d{1,2})&godz=(\d)&mapa=(\d)&ver=(\d)')

    url = "http://bazapozarow.ibles.pl/zagrozenie/mapa.php?"\
            +"rok=%s&mies=%s&dzien=%s&godz=%s&mapa=0&ver=0"

    # debug trick: odkomentuj poniższą linię aby funkcja zwróciła adres do mapki
    # na której "sporo się dzieje ;)
    #return url%('2011','6','7','1')
    # koniec tricka

    now = datetime.datetime.now()
    year, month, day, hour = now.year, now.month, now.day, now.hour

    if month not in range(4,10):
        return None
    else:
        webfile = downloadFile('http://bazapozarow.ibles.pl/zagrozenie/')
        #print _url.findall(webfile)[0]
        return url%( _url.findall(webfile)[0][0:-2] ) # dlaczego [0:-2]?!?

def getData(l):
    global lang
    lang = my_import(l+"."+l)
    data = {"data":"", "needCTCSS":False, "debug":None,
            "source":"ibles", "allOK":True}

    url = get_forecast_url()
    if url is None:
        return data

    mapa=downloadFile(url)
    f=open('ibles.png','wb')
    f.write(mapa)
    f.close()
    s= pygame.image.load('ibles.png')

    zagrozenie = 0

    for strefa in config.strefy:
        polozenie = strefy[strefa]
        kolor = tuple(s.get_at(polozenie))
        zagrozenie=max(zagrozenie, poziomy[kolor])

    if zagrozenie>0:
        data['data']=' '.join( ('w_lasach_wystepuje ',
                poziomy_nazwy[poziomy[kolor]],
                'zagrozenie_pozarowe',))

    return data

if __name__=='__main__':
    getData('pl_google')
<commit_msg>minor, but critical, bugfix<commit_after>#!/usr/env/python -tt
# -*- encoding=utf8 -*-
#
#   Copyright 2009-2012 Michal Sadowski (sq6jnx at hamradio dot pl)
#
#   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 urllib
import pygame
import datetime
import re
from config import ibles  as config

lang=None

def downloadFile(url):
    webFile = urllib.urlopen(url)
    return webFile.read()

def my_import(name):
    mod = __import__(name)
    components = name.split('.')
    for comp in components[1:]:
        mod = getattr(mod, comp)
    return mod

strefy = [
        (  0,  0),
        ( 80,120), ( 40,190), (100,180),    # strefy:  1,  2,  3,
        ( 70,240), (140, 90), (200, 80),    #       :  4,  5,  6,
        (260, 50), (400, 90), (380,150),    #       :  7,  8,  9,
        (410,190), (510, 90), (550, 90),    #       : 10, 11, 12,
        (500,170), (550,200), (170,170),    #       : 13, 14, 15,
        (140,220), (200,200), (250,140),    #       : 16, 17, 18,
        (260,200), (320,170), (100,280),    #       : 19, 20, 21,
        ( 80,310), (170,260), (180,310),    #       : 22, 23, 24,
        (260,270), (250,350), (360,260),    #       : 25, 26, 27,
        (310,330), (360,360), (480,220),    #       : 28, 29, 30,
        (460,280), (440,340), (430,390),    #       : 31, 32, 33,
        (420,450), (560,310), (560,400),    #       : 34, 35, 36,
        (120,370), (180,390), (250,420),    #       : 37, 38, 39,
        (330,440), (390,490), (490,490),    #       : 40, 41, 42
        ]

poziomy = {
        #  R   G   B   A
        (255,  0,  0,255):  3, # zagrożenie duże
        (255,255,  0,255):  2, # zagrożenie średnie
        (  0,255,  0,255):  1, # zagrożenie małe
        (  0,  0,255,255):  0, # brak zagrożenia

        (250,250,212,255): -1, # rejon nieobjęty prognozowaniem
        (160,160,160,255): -2, # brak danych
        }

poziomy_nazwy = ['', 'male', 'srednie', 'duze', ]

def get_forecast_url():
    """Podaje dokładnie taki URL, na który powołuje się strona
    ``http://bazapozarow.ibles.pl/zagrozenie/``, niekiedy bowiem poranne raporty
    o zagrożeniu pożarowym są wydawane na godziny południowe. W przypadku,
    gdy bieżąca data i czas na komputerze jest z zakresu 1 października -- 31
    marca zwraca None, gdyż w tym okresie zagrożenie pożarowe lasów nie jest
    prognozowane."""

    _url=re.compile('mapa\.php\?rok=(\d{4})&mies=(\d{1,2})&'+\
            'dzien=(\d{1,2})&godz=(\d)&mapa=(\d)&ver=(\d)')

    url = "http://bazapozarow.ibles.pl/zagrozenie/mapa.php?"\
            +"rok=%s&mies=%s&dzien=%s&godz=%s&mapa=0&ver=0"

    # debug trick: odkomentuj poniższą linię aby funkcja zwróciła adres do mapki
    # na której "sporo się dzieje ;)
    #return url%('2011','6','7','1')
    # koniec tricka

    now = datetime.datetime.now()
    year, month, day, hour = now.year, now.month, now.day, now.hour

    if month not in range(4,10):
        return None
    else:
        webfile = downloadFile('http://bazapozarow.ibles.pl/zagrozenie/')
        return url%( _url.findall(webfile)[0][0:-2] ) # dlaczego [0:-2]?!?

def getData(l):
    global lang
    lang = my_import(l+"."+l)
    data = {"data":"", "needCTCSS":False, "debug":None,
            "source":"ibles", "allOK":True}

    url = get_forecast_url()
    if url is None:
        return data

    mapa=downloadFile(url)
    f=open('ibles.png','wb')
    f.write(mapa)
    f.close()
    s= pygame.image.load('ibles.png')

    zagrozenie = 0

    for strefa in config.strefy:
        polozenie = strefy[strefa]
        kolor = tuple(s.get_at(polozenie))
        zagrozenie=max(zagrozenie, poziomy[kolor])

    if zagrozenie>0:
        print zagrozenie
        data['data']=' '.join( ('w_lasach_wystepuje ',
                poziomy_nazwy[zagrozenie],
                'zagrozenie_pozarowe',))

    return data

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


<commit_before>import os, datetime
from sets import Set
from werkzeug import secure_filename
from flask_login import current_user
from app import db, telomere
from app.model.manifest import Manifest
from app.model.sample import Sample
from openpyxl import load_workbook
import traceback

class ManifestService():

    def SaveAndReturn(self, manifestFile):
        filename = secure_filename(manifestFile.filename)

        manifest = Manifest(
            filename = filename,
            uploaded = datetime.datetime.now(),
            userId = current_user.id
            )

        db.session.add(manifest)
        db.session.flush()

        manifestFile.save(self.GetPath(manifest))

        return manifest

    def Process(self, manifest):
        wb = load_workbook(filename = self.GetPath(manifest), use_iterators = True)
        ws = wb.worksheets[0]

        try:
            chunks = [iter(ws.iter_rows(row_offset=1))] * 500

            for c in chunks:
#                for row in c:
#                    print row[2].value

#                db.engine.execute(
#                    Sample.__table__.insert(),
#                    [{  "plateName": row[0].value,
#                        "well": row[1].value,
#                        "sampleCode": row[2].value,
#                        "conditionDescription": row[3].value,
#                        "volume": row[4].value,
#                       "dnaTest": row[5].value,
#                        "picoTest": row[6].value,
#                        "manifestId": manifest.id}
#                        for row in c if len(row[2].value) > 0])

                db.session.bulk_insert_mappings(
                    Sample,
                    [{  "plateName": row[0].value,
                        "well": row[1].value,
                        "sampleCode": row[2].value,
                        "conditionDescription": row[3].value,
                        "volume": row[4].value,
                        "dnaTest": row[5].value,
                        "picoTest": row[6].value,
                        "manifestId": manifest.id}
                        for row in c]
                    )
        except:
            telomere.logger.error(traceback.format_exc())
            return False

        return True

    def ValidateFormat(self, manifest):

        wb = load_workbook(filename = self.GetPath(manifest), use_iterators = True)
        ws = wb.worksheets[0]

        return (
            ws['A1'].value == 'Plate Name' and
            ws['B1'].value == 'WELL' and
            ws['C1'].value == 'Sample_Name' and
            ws['D1'].value == 'Condition' and
            ws['E1'].value == 'Volume' and
            ws['F1'].value == 'DNA Test (ng/ul)' and
            ws['G1'].value == 'Pico Test (ng/ul)')

    def GetPath(self, manifest):
        return os.path.join(telomere.config['SPREADSHEET_UPLOAD_DIRECTORY'], self.GetFilename(manifest))

    def GetFilename(self, manifest):
        return "manifest_%d.xlsx" % manifest.id
<commit_msg>Less Chunky<commit_after>import os, datetime
from sets import Set
from werkzeug import secure_filename
from flask_login import current_user
from app import db, telomere
from app.model.manifest import Manifest
from app.model.sample import Sample
from openpyxl import load_workbook
import traceback

class ManifestService():

    def SaveAndReturn(self, manifestFile):
        filename = secure_filename(manifestFile.filename)

        manifest = Manifest(
            filename = filename,
            uploaded = datetime.datetime.now(),
            userId = current_user.id
            )

        db.session.add(manifest)
        db.session.flush()

        manifestFile.save(self.GetPath(manifest))

        return manifest

    def Process(self, manifest):
        wb = load_workbook(filename = self.GetPath(manifest), use_iterators = True)
        ws = wb.worksheets[0]

        try:
            chunks = [iter(ws.iter_rows(row_offset=1))] * 200

            for c in chunks:
#                for row in c:
#                    print row[2].value

#                db.engine.execute(
#                    Sample.__table__.insert(),
#                    [{  "plateName": row[0].value,
#                        "well": row[1].value,
#                        "sampleCode": row[2].value,
#                        "conditionDescription": row[3].value,
#                        "volume": row[4].value,
#                       "dnaTest": row[5].value,
#                        "picoTest": row[6].value,
#                        "manifestId": manifest.id}
#                        for row in c if len(row[2].value) > 0])

                db.session.bulk_insert_mappings(
                    Sample,
                    [{  "plateName": row[0].value,
                        "well": row[1].value,
                        "sampleCode": row[2].value,
                        "conditionDescription": row[3].value,
                        "volume": row[4].value,
                        "dnaTest": row[5].value,
                        "picoTest": row[6].value,
                        "manifestId": manifest.id}
                        for row in c]
                    )
        except:
            telomere.logger.error(traceback.format_exc())
            return False

        return True

    def ValidateFormat(self, manifest):

        wb = load_workbook(filename = self.GetPath(manifest), use_iterators = True)
        ws = wb.worksheets[0]

        return (
            ws['A1'].value == 'Plate Name' and
            ws['B1'].value == 'WELL' and
            ws['C1'].value == 'Sample_Name' and
            ws['D1'].value == 'Condition' and
            ws['E1'].value == 'Volume' and
            ws['F1'].value == 'DNA Test (ng/ul)' and
            ws['G1'].value == 'Pico Test (ng/ul)')

    def GetPath(self, manifest):
        return os.path.join(telomere.config['SPREADSHEET_UPLOAD_DIRECTORY'], self.GetFilename(manifest))

    def GetFilename(self, manifest):
        return "manifest_%d.xlsx" % manifest.id
<|endoftext|>


<commit_before>import os, json

def mk(directory):
	full = os.path.abspath(directory)
	path = full + '/' + os.path.basename(full) + '.sublime-project'
	with open(path, 'w') as f:
		f.write(
'''{{
	"folders":
	[
		{{
			"path": "{0}"
		}}
	]
}}'''.format(os.path.abspath(directory)))

	for i in [2, 3]:

		d = os.path.expanduser('~/.config/sublime-text-') + str(i)

		if os.path.isdir(d):

			with open(d + '/Settings/Session.sublime_session', 'r') as f:
				config = json.load(f, strict = False)

			config['workspaces']['recent_workspaces'].insert(0, path)

			with open(d + '/Settings/Session.sublime_session', 'w') as f:
				json.dump(config, f, indent = '\t')

def rm(directory):
	full = os.path.abspath(directory)
	path = full + '/' + os.path.basename(full) + '.sublime-project'
	os.remove(path)

	workspace_path = full + '/' + os.path.basename(full) + '.sublime-workspace'
	if os.path.isfile(workspace_path) : os.remove(workspace_path)

	for i in [2, 3]:

		d = os.path.expanduser('~/.config/sublime-text-') + str(i)

		if os.path.isdir(d):

			with open(d + '/Settings/Session.sublime_session', 'r') as f:
				config = json.load(f, strict = False)

			config['workspaces']['recent_workspaces'].remove(path)

			with open(d + '/Settings/Session.sublime_session', 'w') as f:
				json.dump(config, f, indent = '\t')<commit_msg>cleaner<commit_after>import os, json

def mk(directory):
	full = os.path.abspath(directory)
	path = full + '/' + os.path.basename(full) + '.sublime-project'
	with open(path, 'w') as f:
		project = {'folders' : [{ 'path' : os.path.abspath(directory) }]}
		json.dump(project, f, indent = '\t')

	for i in [2, 3]:

		d = os.path.expanduser('~/.config/sublime-text-') + str(i)

		if os.path.isdir(d):

			with open(d + '/Settings/Session.sublime_session', 'r') as f:
				config = json.load(f, strict = False)

			config['workspaces']['recent_workspaces'].insert(0, path)

			with open(d + '/Settings/Session.sublime_session', 'w') as f:
				json.dump(config, f, indent = '\t')

def rm(directory):
	full = os.path.abspath(directory)
	path = full + '/' + os.path.basename(full) + '.sublime-project'
	os.remove(path)

	workspace_path = full + '/' + os.path.basename(full) + '.sublime-workspace'
	if os.path.isfile(workspace_path) : os.remove(workspace_path)

	for i in [2, 3]:

		d = os.path.expanduser('~/.config/sublime-text-') + str(i)

		if os.path.isdir(d):

			with open(d + '/Settings/Session.sublime_session', 'r') as f:
				config = json.load(f, strict = False)

			config['workspaces']['recent_workspaces'].remove(path)

			with open(d + '/Settings/Session.sublime_session', 'w') as f:
				json.dump(config, f, indent = '\t')<|endoftext|>


<commit_before>"""
Auto Ripper

Ripping
    Uses MakeMKV to watch for movies inserted into DVD/BD Drives

    Automaticly checks for existing directory/movie and will NOT overwrite existing
    files or folders

    Checks minimum length of video to ensure movie is ripped not previews or other
    junk that happens to be on the DVD

    DVD goes in > MakeMKV gets a proper DVD name > MakeMKV Rips
    DVD does not get ejected, maybe it will get added to later versions

Compressing
    An optional additional used to rename and compress movies to an acceptable standard
    which still delivers quallity audio and video but reduces the file size
    dramatically.

    Using a nice value of 15 by default, it runs HandBrake as a background task
    that allows other critical tasks to complete first.

Released under the MIT license
Copyright (c) 2014, Jason Millward

@category   misc
@version    $Id: 1.6, 2014-03-11 16:52:00 CST $;
@author     Jason Millward <jason@jcode.me>
@license    http://opensource.org/licenses/MIT

Usage:
    autorip.py ( --rip | --compress | ( --rip --compress) ) [options]

Options:
    -h --help     Show this screen.
    --version     Show version.
    --debug       Output debug.
    --rip         Rip disc using makeMKV.
    --compress    Compress using handbrake.
    --test        Testing?

"""

import os
import sys
import yaml
from classes import *
from tendo import singleton

__version__="1.6"

me = singleton.SingleInstance()
DIR = os.path.dirname(os.path.realpath(__file__))
CONFIG_FILE = "%s/settings.cfg" % DIR

def eject(drive):
    """
        Ejects the DVD drive
        Not really worth its own class
    """
    log = logger.logger("Eject", True)

    log.debug("Ejecting drive: " + drive)
    log.debug("Attempting OS detection")

    try:
        if sys.platform == 'win32':
            log.debug("OS detected as Windows")
            import ctypes
            ctypes.windll.winmm.mciSendStringW("set cdaudio door open", None, drive, None)

        elif sys.platform == 'darwin':
            log.debug("OS detected as OSX")
            p = os.popen("drutil eject " + drive)

            while 1:
                line = p.readline()
                if not line: break
                log.debug(line.strip())

        else:
            log.debug("OS detected as Unix")
            p = os.popen("eject -vr " + drive)

            while 1:
                line = p.readline()
                if not line: break
                log.debug(line.strip())

    except Exception as ex:
        log.error("Could not detect OS or eject CD tray")
        log.ex("An exception of type %s occured." % type(ex).__name__)
        log.ex("Args: \r\n %s" % ex.args)

    finally:
        del log

def rip(config):
    """
        Main function for ripping
        Does everything
        Returns nothing
    """
    log = logger.logger("Rip", config['debug'])

    mkv_save_path = config['savePath']
    mkv_tmp_output = config['temp']

    mkv_api = makemkv.makeMKV(config)

    log.debug("Ripping started successfully")
    log.debug("Checking for DVDs")

    dvds = mkv_api.findDisc(mkv_tmp_output)

    log.debug("%d DVDs found" % len(dvds))

    if (len(dvds) > 0):
        # Best naming convention ever
        for dvd in dvds:
            mkv_api.setTitle(dvd["discTitle"])
            mkv_api.setIndex(dvd["discIndex"])

            movie_title = mkv_api.getTitle()

            if not os.path.exists('%s/%s' % (mkv_save_path, movie_title)):
                os.makedirs('%s/%s' % (mkv_save_path, movie_title))

                mkv_api.getDiscInfo()

                with stopwatch.stopwatch() as t:
                    status = mkv_api.ripDisc(mkv_save_path, mkv_tmp_output)

                if status:
                    if config['eject']:
                        eject(dvd['location'])

                    log.info("It took %s minute(s) to complete the ripping of %s" %
                         (t.minutes, movie_title)
                    )

                else:
                    log.info("MakeMKV did not did not complete successfully")
                    log.info("See log for more details")
                    log.debug("Movie title: %s" % movie_title)

            else:
                log.info("Movie folder %s already exists" % movie_title)

    else:
        log.info("Could not find any DVDs in drive list")

def compress(config):
    """
        Main function for compressing
        Does everything
        Returns nothing
    """
    log = logger.logger("Compress", config['debug'])

    hb = handbrake.handBrake(config['debug'])

    log.debug("Compressing started successfully")
    log.debug("Looking for movies to compress")

    if hb.loadMovie():
        log.info( "Compressing %s" % hb.getMovieTitle())

        with stopwatch.stopwatch() as t:
            convert = hb.convert(
                args=config['com'],
                nice=int(config['nice'])
            )

        if convert:
            log.info("Movie was compressed and encoded successfully")

            log.info( ("It took %s minutes to compress %s" %
                    (t.minutes, hb.getMovieTitle()))
            )
        else:
            log.info( "HandBrake did not complete successfully")

    else:
        log.info( "Queue does not exist or is empty")

if __name__ == '__main__':
    arguments = docopt.docopt(__doc__, version=__version__)
    config = yaml.safe_load(open(CONFIG_FILE))

    dbintegritycheck()

    if arguments['--rip']:
        config['makemkv']['debug'] = arguments['--debug']
        rip(config['makemkv'])

    if arguments['--compress']:
        config['handbrake']['debug'] = arguments['--debug']
        compress(config['handbrake'])
<commit_msg>Still testing structure<commit_after>"""
Auto Ripper

Ripping
    Uses MakeMKV to watch for movies inserted into DVD/BD Drives

    Automaticly checks for existing directory/movie and will NOT overwrite existing
    files or folders

    Checks minimum length of video to ensure movie is ripped not previews or other
    junk that happens to be on the DVD

    DVD goes in > MakeMKV gets a proper DVD name > MakeMKV Rips
    DVD does not get ejected, maybe it will get added to later versions

Compressing
    An optional additional used to rename and compress movies to an acceptable standard
    which still delivers quallity audio and video but reduces the file size
    dramatically.

    Using a nice value of 15 by default, it runs HandBrake as a background task
    that allows other critical tasks to complete first.

Released under the MIT license
Copyright (c) 2014, Jason Millward

@category   misc
@version    $Id: 1.6, 2014-03-11 16:52:00 CST $;
@author     Jason Millward <jason@jcode.me>
@license    http://opensource.org/licenses/MIT

Usage:
    autorip.py ( --rip | --compress | ( --rip --compress) ) [options]

Options:
    -h --help     Show this screen.
    --version     Show version.
    --debug       Output debug.
    --rip         Rip disc using makeMKV.
    --compress    Compress using handbrake.
    --test        Testing?

"""

import os
import sys
import yaml
from classes import *
from tendo import singleton

__version__="1.6"

me = singleton.SingleInstance()
DIR = os.path.dirname(os.path.realpath(__file__))
CONFIG_FILE = "%s/settings.cfg" % DIR

if __name__ == '__main__':
    arguments = docopt.docopt(__doc__, version=__version__)
    config = yaml.safe_load(open(CONFIG_FILE))

    dbintegritycheck()

    if arguments['--rip']:
        config['makemkv']['debug'] = arguments['--debug']
        rip(config['makemkv'])

    if arguments['--compress']:
        config['handbrake']['debug'] = arguments['--debug']
        compress(config['handbrake'])
<|endoftext|>


<commit_before>from flask import Flask, render_template, session, redirect, url_for, flash
from flask.ext.script import Manager
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask.ext.wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required

app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'

manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)

class NameForm(Form):
    name = StringField('What is your name?', validators=[Required()])
    submit = SubmitField('Submit')

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
    return render_template('500.html'), 500

@app.route('/', methods=['GET', 'POST'])
def index():
    form = NameForm()
    if form.validate_on_submit():
        old_name = session.get('name')
        if old_name is not None and old_name != form.name.data:
            flash('Looks like you have changed your name!')
        session['name'] = form.name.data
        return redirect(url_for('index'))
    return render_template('index.html', form=form, name=session.get('name'))

if __name__ == '__main__':
    manager.run()
<commit_msg>Chapter 5: Database models with Flask-SQLAlchemy (5a)<commit_after>import os
from flask import Flask, render_template, session, redirect, url_for, flash
from flask.ext.script import Manager
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask.ext.wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required
from flask.ext.sqlalchemy import SQLAlchemy

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['SQLALCHEMY_DATABASE_URI'] =\
    'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True

manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
db = SQLAlchemy(app)

class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.relationship('User', backref='role', lazy='dynamic')

    def __repr__(self):
        return '<Role %r>' % self.name

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))

    def __repr__(self):
        return '<User %r>' % self.username

class NameForm(Form):
    name = StringField('What is your name?', validators=[Required()])
    submit = SubmitField('Submit')

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
    return render_template('500.html'), 500

@app.route('/', methods=['GET', 'POST'])
def index():
    form = NameForm()
    if form.validate_on_submit():
        old_name = session.get('name')
        if old_name is not None and old_name != form.name.data:
            flash('Looks like you have changed your name!')
        session['name'] = form.name.data
        return redirect(url_for('index'))
    return render_template('index.html', form=form, name=session.get('name'))

if __name__ == '__main__':
    db.create_all()
    manager.run()
<|endoftext|>


<commit_before>import discord
from discord.ext import commands

class id:
    """id Cog"""
    def __init__(self, bot):
        self.bot = bot

    @commands.command(pass_context=True)
    async def id(self, ctx):
        """id the user"""
        id = ctx.message.author.id
        await self.bot.say("Your ID is: " + id)

def setup(bot):
    n = id(bot)
    bot.add_cog(n)<commit_msg>Update id.py<commit_after>import discord
from discord.ext import commands

class Id:
    """id Cog"""
    def __init__(self, bot):
        self.bot = bot

    @commands.command(pass_context=True)
    async def id(self, ctx):
        """Tells the invokers ID"""
        id = ctx.message.author.id
        await self.bot.say("Your ID is: " + id)

def setup(bot):
    n = Id(bot)
    bot.add_cog(n)
<|endoftext|>


<commit_before>#!/usr/bin/env python
import sys
import logging
import csv
from urllib2 import urlopen, Request
from BeautifulSoup import BeautifulSoup

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

SITE_URL = 'http://projects.fortworthgov.org'
MAX_NUM_PHASES = 10

def get_project_html(id):
    """Get the HTML from a particular project's details page

    This doesn't work so well because Ft Worth is using some kind of
    AJAX framework with weird javascript-generated POST parameters.
    """
    url = SITE_URL + '/FindByListProjectDetail.aspx?PID=%05d' % id
    logger.info("getting %s" % url)
    req = Request(url)
    response = urlopen(req)
    html = response.read()
    return html

PROJECT_DATA = {
    'name': 'lblProjName',
    'description': 'lblNotes',
    'number': 'lblProj_id',
    'district': 'lblDISTRICT',
    'manager': 'lbProjectManager',
    'manager_company': 'lblProjectManagerCompany',
    'manager_email': 'lblProjectManagerEmail',
    'manager_phone': 'lblProjectManagerPhone',
    'consultant': 'lblCONSULTANT',
    'contractor': 'lblCONTRACTOR',
    'status': 'lblStatus',
}

def parse_project(html):
    soup = BeautifulSoup(html)

    p = {}
    for key, label in PROJECT_DATA.items():
        p[key] = soup.find(name='span', attrs={
            'id': 'ctl00_PageContent_%s' % label
        }).contents[0]

    phases = []
    phase_table = soup.find(name='table', attrs={
        'id': 'ctl00_PageContent_grid_DXMainTable'})
    for row in phase_table.findAll('tr'):
        try:
            if 'dxgvDataRow' in row.attrs[1][1]:
                cols = row.findAll('td')
                phase = {'name': cols[0].contents[0],
                         'date': cols[1].contents[0]}
                phases.append(phase)
        except IndexError:
            pass

    p['phases'] = phases
    return p

def output_csv(projects):
    csv_writer = csv.writer(sys.stdout)
    project_headers = PROJECT_DATA.keys()
    phase_headers = []
    for n in range(MAX_NUM_PHASES):
        phase_headers.append('phase%d_name' % (n+1))
        phase_headers.append('phase%d_date' % (n+1))
    headers = project_headers + phase_headers
    csv_writer.writerow(headers)
    for p in projects:
        project_data = [p[k] for k in project_headers]
        phase_data = []
        for phase in p['phases'][:MAX_NUM_PHASES]:
            phase_data.append(phase['name'])
            phase_data.append(phase['date'])
        data = project_data + phase_data
        csv_writer.writerow(data)

def main():
    projects = []

    for pid in range(100):
        try:
            html = get_project_html(pid)
            project = parse_project(html)
            projects.append(project)
        except:
            pass

    #html = get_project_html(84)
    #project = parse_project(html)
    #projects.append(project)

    output_csv(projects)

if __name__ == '__main__':
    logging.basicConfig()
    main()
<commit_msg>removed erroneous docstring<commit_after>#!/usr/bin/env python
import sys
import logging
import csv
from urllib2 import urlopen, Request
from BeautifulSoup import BeautifulSoup

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

SITE_URL = 'http://projects.fortworthgov.org'
MAX_NUM_PHASES = 10

def get_project_html(id):
    """Get the HTML from a particular project's details page"""
    url = SITE_URL + '/FindByListProjectDetail.aspx?PID=%05d' % id
    logger.info("getting %s" % url)
    req = Request(url)
    response = urlopen(req)
    html = response.read()
    return html

PROJECT_DATA = {
    'name': 'lblProjName',
    'description': 'lblNotes',
    'number': 'lblProj_id',
    'district': 'lblDISTRICT',
    'manager': 'lbProjectManager',
    'manager_company': 'lblProjectManagerCompany',
    'manager_email': 'lblProjectManagerEmail',
    'manager_phone': 'lblProjectManagerPhone',
    'consultant': 'lblCONSULTANT',
    'contractor': 'lblCONTRACTOR',
    'status': 'lblStatus',
}

def parse_project(html):
    soup = BeautifulSoup(html)

    p = {}
    for key, label in PROJECT_DATA.items():
        p[key] = soup.find(name='span', attrs={
            'id': 'ctl00_PageContent_%s' % label
        }).contents[0]

    phases = []
    phase_table = soup.find(name='table', attrs={
        'id': 'ctl00_PageContent_grid_DXMainTable'})
    for row in phase_table.findAll('tr'):
        try:
            if 'dxgvDataRow' in row.attrs[1][1]:
                cols = row.findAll('td')
                phase = {'name': cols[0].contents[0],
                         'date': cols[1].contents[0]}
                phases.append(phase)
        except IndexError:
            pass

    p['phases'] = phases
    return p

def output_csv(projects):
    csv_writer = csv.writer(sys.stdout)
    project_headers = PROJECT_DATA.keys()
    phase_headers = []
    for n in range(MAX_NUM_PHASES):
        phase_headers.append('phase%d_name' % (n+1))
        phase_headers.append('phase%d_date' % (n+1))
    headers = project_headers + phase_headers
    csv_writer.writerow(headers)
    for p in projects:
        project_data = [p[k] for k in project_headers]
        phase_data = []
        for phase in p['phases'][:MAX_NUM_PHASES]:
            phase_data.append(phase['name'])
            phase_data.append(phase['date'])
        data = project_data + phase_data
        csv_writer.writerow(data)

def main():
    projects = []

    for pid in range(100):
        try:
            html = get_project_html(pid)
            project = parse_project(html)
            projects.append(project)
        except:
            pass

    #html = get_project_html(84)
    #project = parse_project(html)
    #projects.append(project)

    output_csv(projects)

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