<commit_before>"""
A recursive fractal tree builder.
"""
import math
import random

import svgwrite

# Values to use for non-random tree
DELTA_LENGTH = 0.61
DELTA_ANGLE = 45

# Random ranges to use
MIN_ANGLE = -65
MAX_ANGLE = 65
MIN_LENGTH = 0.45
MAX_LENGTH = 0.825
BRANCHES = [2, 2, 2, 3, 3, 3, 4, 5]

def build_tree(start=(0, 0), branch_len=150, angle=270, use_random=True):
    """
    A recursive function to build a fractal tree.

    Input:
    start - (x, y) tuple giving the starting point of the tree
    branch_len - the length of this branch of the tree

    Output:
    tree - list of (x1, y1, x2, y2) defining the line segments of this tree
    """
    if branch_len <= 3:
        return []
    else:
        tree = []
        x_end = start[0] + (branch_len * math.cos(math.radians(angle)))
        y_end = start[1] + (branch_len * math.sin(math.radians(angle)))
        tree.append((start[0], start[1], x_end, y_end))

        if use_random:
            for _ in range(random.choice(BRANCHES)):
                tree += build_tree(
                    (x_end, y_end),
                    branch_len * random.uniform(MIN_LENGTH, MAX_LENGTH),
                    angle + random.randrange(MIN_ANGLE, MAX_ANGLE),
                    use_random=use_random
                )
        else:
            r_angle = angle - DELTA_ANGLE
            l_angle = angle + DELTA_ANGLE
            r_len = branch_len * DELTA_LENGTH
            l_len = branch_len * DELTA_LENGTH
            # build the branches
            tree += build_tree((x_end, y_end), r_len, r_angle, use_random=use_random)
            tree += build_tree((x_end, y_end), l_len, l_angle, use_random=use_random)

        return tree

def tree_normalize(tree):
    """
    Takes a list of line segments defining a tree and normalizes them by
    making all coordinates positive.
    """
    # Find the minimum x and y values
    x_vals = [(i[0], i[2]) for i in tree]
    x_vals = [item for sublist in x_vals for item in sublist]
    y_vals = [(i[1], i[3]) for i in tree]
    y_vals = [item for sublist in y_vals for item in sublist]

    x_shift = abs(min(x_vals))
    y_shift = abs(min(y_vals))

    # Add the shift values to each point
    new_tree = []
    for line in tree:
        new_tree.append((
            line[0] + x_shift,
            line[1] + y_shift,
            line[2] + x_shift,
            line[3] + y_shift
            ))
    return new_tree

def find_branch_length(branch):
    """
    Calculates the length of the given branch.
    """
    return math.sqrt((branch[2] - branch[0])**2 + (branch[3] - branch[1])**2)

def get_max_vals(tree):
    """
    Finds the max x and y values in the given tree and returns them.
    """
    x_vals = [(i[0], i[2]) for i in tree]
    x_vals = [item for sublist in x_vals for item in sublist]
    y_vals = [(i[1], i[3]) for i in tree]
    y_vals = [item for sublist in y_vals for item in sublist]

    return max(x_vals), max(y_vals)

def generate_svg(tree):
    """
    Takes a list of line segments defining a tree and builds the SVG for them.
    """
    tree = tree_normalize(tree)

    dwg = svgwrite.Drawing(size=get_max_vals(tree))

    # Add each branch to the drawing
    for branch in tree:
        width = math.floor(find_branch_length(branch) / 8)
        if width < 1:
            width = 1
        color = svgwrite.rgb(139, 69, 19)
        if find_branch_length(branch) < 6:
            color = svgwrite.rgb(34, 139, 34)
        dwg.add(dwg.line(
            start=branch[:2],
            end=branch[2:],
            stroke=color,
            stroke_width=width,
            stroke_linecap="round"
            ))
    # The linejoin parameter only works for shapes and polylines...
    #svgwrite.mixins.Presentation.stroke(dwg, linejoin="round")

    return dwg

def write_tree_file(tree, filename):
    """
    Takes a list of line segments defining a tree and writes them to an SVG file.
    """
    svg = generate_svg(tree)
    # Save the drawing
    svg.saveas(filename)

def get_tree_xml(tree):
    """
    Takes a list of line segments defining a tree and returns the XML
    for the SVG file as a string.
    """
    svg = generate_svg(tree)
    return svg.tostring()

if __name__ == "__main__":
    RANDOMIZED = False
    STARTLENGTH = 100
    TREE = build_tree((0, 0), STARTLENGTH, 270, use_random=RANDOMIZED)
    # write_tree(TREE, "test.svg")
    print(get_tree_xml(TREE))

<commit_msg>move parameters inside build_tree()<commit_after>"""
A recursive fractal tree builder.
"""
import math
import random

import svgwrite

def build_tree(start=(0, 0), branch_len=150, angle=270, use_random=True):
    """
    A recursive function to build a fractal tree.

    Input:
    start - (x, y) tuple giving the starting point of the tree
    branch_len - the length of this branch of the tree

    Output:
    tree - list of (x1, y1, x2, y2) defining the line segments of this tree
    """
    params = {
        "length": (0.45, 0.825),
        "angle": (-65, 65),
        "branches": [2, 2, 2, 3, 3, 3, 4, 5]
    }

    if branch_len <= 3:
        return []
    else:
        tree = []
        x_end = start[0] + (branch_len * math.cos(math.radians(angle)))
        y_end = start[1] + (branch_len * math.sin(math.radians(angle)))
        tree.append((start[0], start[1], x_end, y_end))

        if use_random:
            for _ in range(random.choice(params["branches"])):
                tree += build_tree(
                    (x_end, y_end),
                    branch_len * random.uniform(
                        params["length"][0],
                        params["length"][1]
                    ),
                    angle + random.randrange(params["angle"][0], params["angle"][1]),
                    use_random=use_random
                )
        else:
            tree += build_tree(
                (x_end, y_end),
                branch_len * 0.61,
                angle - 45,
                use_random=use_random
            )
            tree += build_tree(
                (x_end, y_end),
                branch_len * 0.61,
                angle + 45,
                use_random=use_random
            )

        return tree

def tree_normalize(tree):
    """
    Takes a list of line segments defining a tree and normalizes them by
    making all coordinates positive.
    """
    # Find the minimum x and y values
    x_vals = [(i[0], i[2]) for i in tree]
    x_vals = [item for sublist in x_vals for item in sublist]
    y_vals = [(i[1], i[3]) for i in tree]
    y_vals = [item for sublist in y_vals for item in sublist]

    x_shift = abs(min(x_vals))
    y_shift = abs(min(y_vals))

    # Add the shift values to each point
    new_tree = []
    for line in tree:
        new_tree.append((
            line[0] + x_shift,
            line[1] + y_shift,
            line[2] + x_shift,
            line[3] + y_shift
            ))
    return new_tree

def find_branch_length(branch):
    """
    Calculates the length of the given branch.
    """
    return math.sqrt((branch[2] - branch[0])**2 + (branch[3] - branch[1])**2)

def get_max_vals(tree):
    """
    Finds the max x and y values in the given tree and returns them.
    """
    x_vals = [(i[0], i[2]) for i in tree]
    x_vals = [item for sublist in x_vals for item in sublist]
    y_vals = [(i[1], i[3]) for i in tree]
    y_vals = [item for sublist in y_vals for item in sublist]

    return max(x_vals), max(y_vals)

def generate_svg(tree):
    """
    Takes a list of line segments defining a tree and builds the SVG for them.
    """
    tree = tree_normalize(tree)

    dwg = svgwrite.Drawing(size=get_max_vals(tree))

    # Add each branch to the drawing
    for branch in tree:
        width = math.floor(find_branch_length(branch) / 8)
        if width < 1:
            width = 1
        color = svgwrite.rgb(139, 69, 19)
        if find_branch_length(branch) < 6:
            color = svgwrite.rgb(34, 139, 34)
        dwg.add(dwg.line(
            start=branch[:2],
            end=branch[2:],
            stroke=color,
            stroke_width=width,
            stroke_linecap="round"
            ))
    # The linejoin parameter only works for shapes and polylines...
    #svgwrite.mixins.Presentation.stroke(dwg, linejoin="round")

    return dwg

def write_tree_file(tree, filename):
    """
    Takes a list of line segments defining a tree and writes them to an SVG file.
    """
    svg = generate_svg(tree)
    # Save the drawing
    svg.saveas(filename)

def get_tree_xml(tree):
    """
    Takes a list of line segments defining a tree and returns the XML
    for the SVG file as a string.
    """
    svg = generate_svg(tree)
    return svg.tostring()

if __name__ == "__main__":
    RANDOMIZED = False
    STARTLENGTH = 100
    TREE = build_tree((0, 0), STARTLENGTH, 270, use_random=RANDOMIZED)
    # write_tree(TREE, "test.svg")
    print(get_tree_xml(TREE))

<|endoftext|>


<commit_before>""" Karma Plugin (botwot plugins.karma) """

# Copyright 2015 Ray Schulz <https://rascul.io>
#
# 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 random
import re
import string
import time

from pyaib.plugins import keyword, observe, plugin_class
from pyaib.db import db_driver

@plugin_class
@plugin_class.requires('db')
class Factoids(object):
	def __init__(self, context, config):
		self.context = context
		self.db = context.db.get("karma")
		self.damage_types = [
			["blast", "blasts"],
			["cleave", "cleaves"],
			["crush", "crushes"],
			["hack", "hacks"],
			["hit", "hits"],
			["lance", "lances"],
			["pierce", "pierces"],
			["pound", "pounds"],
			["scythe", "scythes"],
			["shoot", "shoots"],
			["slash", "slashes"],
			["slice", "slices"],
			["smite", "smites"],
			["stab", "stabs"],
			["sting", "stings"],
			["strike", "strikes"],
			["whip", "whips"]]

		random.seed()

	def procs(self, s):
		""" strip punctuation and make lower case a string """
		return "".join(ch for ch in s if ch not in set(string.punctuation)).lower()

	def hit(self, attacker, defender):
		"""
		hit somebody
		return true if attacker hits defender, else false
		"""

		total = attacker["abs_karma"] + attacker["abs_karma"] / 2 + defender["abs_karma"] + defender["abs_karma"] / 2
		total = total + 10 if total < 20 else total

		res = random.randint(1, total)

		if res <= attacker["abs_karma"]:
			return True
		elif total - defender["abs_karma"] < res <= total:
			return False
		else:
			if random.choice([attacker, defender]) == attacker:
				return True
			else:
				return False

	def fight(self, msg, attacker, defender):
		status = 0

		if self.hit(attacker, defender):
			status += 1
			msg.reply("%s %s %s" % (attacker["name"], random.choice(self.damage_types)[1], defender["name"]))
		else:
			status -= 1
			msg.reply("%s fails to %s %s" % (attacker["name"], random.choice(self.damage_types[0]), defender["name"]))

		if self.hit(defender, attacker):
			status -= 1
			msg.reply("%s %s %s" % (defender["name"], random.choice(self.damage_types)[1], attacker["name"]))
		else:
			status += 1
			msg.reply("%s fails to %s %s" % (defender["name"], random.choice(self.damage_types[0]), attacker["name"]))

		return status

	@keyword('k')
	def keyword_kill(self, context, msg, trigger, args, kargs):
		if len(args) != 1:
			return

		attacker = {}
		defender = {}

		attacker["name"] = procs(msg.sender)
		defender["name"] = procs(args[0])

		for i in [attacker, defender]:
			i["karma"] = int(self.db.get("%s/karma" % i["name"]).value or 0)
			i["abs_karma"] = abs(i["karma"])

		status = self.fight(msg, attacker, defender)

	@keyword('karma')
	def keyword_karma(self, context, msg, trigger, args, kargs):
		""" tell you karmas """
		karma = self.db.get("%s/karma" % msg.sender).value or 0
		context.PRIVMSG(msg.sender, "You have %s karmas." % karma)

	@observe("IRC_MSG_PRIVMSG")
	def observe_privmsg_karma(self, context, msg):
		""" Look for karmas """
		m = re.match(r'(?P<name>\S+)(?P<op>\+\+|--)', msg.message)
		if m:
			name = self.procs(m.groupdict().get("name"))
			op = m.groupdict().get("op")

			if name != self.procs(msg.sender) and name != self.context.config.nick:
				last_karma = 0
				item = self.db.get("%s/last" % msg.sender)
				if item.value:
					last_karma = float(item.value)

				if last_karma + random.randint(600, 3600) < time.time():
					item.value = time.time()
					item.commit()

					karma = 0
					item = self.db.get("%s/karma" % name)
					if item.value:
						karma = int(item.value)
					if op == "++":
						item.value = karma + 1
					elif op == "--":
						item.value = karma - 1
					item.commit()

<commit_msg>finish the stuff<commit_after>""" Karma Plugin (botwot plugins.karma) """

# Copyright 2015 Ray Schulz <https://rascul.io>
#
# 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 random
import re
import string
import time

from pyaib.plugins import keyword, observe, plugin_class
from pyaib.db import db_driver

@plugin_class
@plugin_class.requires('db')
class Karma(object):
	def __init__(self, context, config):
		self.context = context
		self.db = context.db.get("karma")
		self.damage_types = [
			["blast", "blasts"],
			["cleave", "cleaves"],
			["crush", "crushes"],
			["hack", "hacks"],
			["hit", "hits"],
			["lance", "lances"],
			["pierce", "pierces"],
			["pound", "pounds"],
			["scythe", "scythes"],
			["shoot", "shoots"],
			["slash", "slashes"],
			["slice", "slices"],
			["smite", "smites"],
			["stab", "stabs"],
			["sting", "stings"],
			["strike", "strikes"],
			["whip", "whips"]]

		random.seed()

	def procs(self, s):
		""" strip punctuation and make lower case a string """
		return "".join(ch for ch in s if ch not in set(string.punctuation)).lower()

	def hit(self, attacker, defender):
		"""
		hit somebody
		return true if attacker hits defender, else false
		"""

		total = attacker["abs_karma"] + attacker["abs_karma"] / 2 + defender["abs_karma"] + defender["abs_karma"] / 2
		total = total + 10 if total < 20 else total

		res = random.randint(1, total)

		if res <= attacker["abs_karma"]:
			return True
		elif total - defender["abs_karma"] < res <= total:
			return False
		else:
			if random.choice([attacker, defender]) == attacker:
				return True
			else:
				return False

	def fight(self, msg, attacker, defender):
		status = 0

		if self.hit(attacker, defender):
			status += 1
			msg.reply("%s %s %s" % (attacker["name"], random.choice(self.damage_types)[1], defender["name"]))
		else:
			status -= 1
			msg.reply("%s fails to %s %s" % (attacker["name"], random.choice(self.damage_types)[0], defender["name"]))

		if self.hit(defender, attacker):
			status -= 1
			msg.reply("%s %s %s" % (defender["name"], random.choice(self.damage_types)[1], attacker["name"]))
		else:
			status += 1
			msg.reply("%s fails to %s %s" % (defender["name"], random.choice(self.damage_types)[0], attacker["name"]))

		return status

	def steal_karma(self, winner, loser):
		""" chance of winner stealing a karma from the loser """

		if random.random() <= .4:
			if winner["karma"] < 0:
				winner["karma"] -= 1
			else:
				winner["karma"] += 1

			if loser["karma"] < -1:
				loser["karma"] += 1
			elif loser["karma"] > 1:
				loser["karma"] -= 1

			for i in [winner, loser]:
				item = self.db.get("%s/karma" % i["name"])
				item.value = i["karma"]
				item.commit()

			return True
		else:
			return False

	@keyword('k')
	def keyword_kill(self, context, msg, trigger, args, kargs):
		""" <player> - kill player """

		if len(args) != 1:
			return

		if msg.target != context.config.plugin.karma.channel:
			return

		item = self.db.get("%s/next_fight" % msg.sender)
		if item.value and float(item.value) > time.time():
			msg.reply("%s: You are too exhausted." % msg.sender)
			return

		item.value = random.randint(600, 3600) + time.time()
		item.commit()

		attacker = {}
		defender = {}

		attacker["name"] = self.procs(msg.sender)
		defender["name"] = self.procs(args[0])

		for i in [attacker, defender]:
			i["karma"] = int(self.db.get("%s/karma" % i["name"]).value or 0)
			i["abs_karma"] = abs(i["karma"])

		status = self.fight(msg, attacker, defender)
		if status > 0:
			if self.steal_karma(attacker, defender):
				msg.reply("%s steals a karma!" % attacker["name"])
		elif status < 0:
			if self.steal_karma(defender, attacker):
				msg.reply("%s steals a karma!" % defender["name"])

	@keyword('karma')
	def keyword_karma(self, context, msg, trigger, args, kargs):
		""" tell you karmas """

		karma = self.db.get("%s/karma" % msg.sender).value or 0
		context.PRIVMSG(msg.sender, "You have %s karmas." % karma)

	@observe("IRC_MSG_PRIVMSG")
	def observe_privmsg_karma(self, context, msg):
		""" Look for karmas """

		m = re.match(r'(?P<name>\S+)(?P<op>\+\+|--)', msg.message)
		if m:
			name = self.procs(m.groupdict().get("name"))
			op = m.groupdict().get("op")

			if name != self.procs(msg.sender) and name != context.botnick:
				item = self.db.get("%s/next_karma" % msg.sender)

				if (not item.value) or (float(item.value) < time.time()):
					item.value = random.randint(600, 3600) + time.time()
					item.commit()

					karma = 0
					item = self.db.get("%s/karma" % name)
					if item.value:
						karma = int(item.value)
					if op == "++":
						item.value = karma + 1
					elif op == "--":
						item.value = karma - 1
					item.commit()
<|endoftext|>


<commit_before>from pysis import SIS
import os
import json
import pdb

class APITokenException(Exception):
    pass

class BaseWorker(object):

    def __init__(self, workerID, environment):
        self.env = environment
        self.uuid = workerID
        self.api = SIS()
        self.configuration_id = None

        # load configuration
        self.config = self.loadConfiguration()

    def loadConfiguration(self):
        self.worker = self.api.workers.get(uuid=self.uuid)
        print (self.worker.label)
        configValues = self.worker.getConfigurationValues(environment=self.env)
        config = {}

        for value in configValues:

            configValue = {}

            # store type
            configValue['type'] = value.type

            # store value
            if value.type == "integer":
                configValue['value'] = int(value.value)
            elif value.type == "json":
                configValue['value'] = json.loads(value.value)
            else:
                configValue['value'] = str(value.value)

            # store id
            configValue['id'] = value.id

            # store config dict
            config[value.key] = configValue

            # save configuration_id
            # should be the same each time
            # dumb, but whatever
            self.configuration_id = value.configuration_id

        return config

    def updateConfigurationValue(self, key, value):

        # update local value
        configValue = self.config[key]

        configValue['value'] = value

        # send along type so update completes properly
        value_type = configValue['type']

        value_id = configValue['id']

        self.worker.updateConfigurationValue(self.configuration_id, value_id, value, value_type)

    def getConfigurationValue(self, key):

        return self.config[key]['value']

    def createConfigurationValue(self, key, value, value_type):

        res = self.worker.createConfigurationValue(self.configuration_id, key, value, value_type)

        # load local values
        configValue = {'value': value, 'id': res.id, 'type': value_type}

        self.config[key] = configValue

    def deleteConfigurationValue(self, value):

        self.worker.deleteConfigurationValue(self.configuration_id, value['id'])

<commit_msg>Delete config from local dictionary as well<commit_after>from pysis import SIS
import os
import json
import pdb

class APITokenException(Exception):
    pass

class BaseWorker(object):

    def __init__(self, workerID, environment):
        self.env = environment
        self.uuid = workerID
        self.api = SIS()
        self.configuration_id = None

        # load configuration
        self.config = self.loadConfiguration()

    def loadConfiguration(self):
        self.worker = self.api.workers.get(uuid=self.uuid)
        print (self.worker.label)
        configValues = self.worker.getConfigurationValues(environment=self.env)
        config = {}

        for value in configValues:

            configValue = {}

            # store type
            configValue['type'] = value.type

            # store value
            if value.type == "integer":
                configValue['value'] = int(value.value)
            elif value.type == "json":
                configValue['value'] = json.loads(value.value)
            else:
                configValue['value'] = str(value.value)

            # store id
            configValue['id'] = value.id

            # store config dict
            config[value.key] = configValue

            # save configuration_id
            # should be the same each time
            # dumb, but whatever
            self.configuration_id = value.configuration_id

        return config

    def updateConfigurationValue(self, key, value):

        # update local value
        configValue = self.config[key]

        configValue['value'] = value

        # send along type so update completes properly
        value_type = configValue['type']

        value_id = configValue['id']

        self.worker.updateConfigurationValue(self.configuration_id, value_id, value, value_type)

    def getConfigurationValue(self, key):

        return self.config[key]['value']

    def createConfigurationValue(self, key, value, value_type):

        res = self.worker.createConfigurationValue(self.configuration_id, key, value, value_type)

        # load local values
        configValue = {'value': value, 'id': res.id, 'type': value_type}

        self.config[key] = configValue

    def deleteConfigurationValue(self, key, value):

        self.worker.deleteConfigurationValue(self.configuration_id, value['id'])

        # TODO: API provides no way of checking if deletion was successful
        del self.config[key]

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from nose import SkipTest
from nose.tools import eq_, ok_, assert_raises, assert_in

from parsimonious import Grammar, NodeVisitor, VisitationError, rule
from parsimonious.expressions import Literal
from parsimonious.nodes import Node

class HtmlFormatter(NodeVisitor):
    """Visitor that turns a parse tree into HTML fragments"""

    grammar = Grammar("""bold_open  = '(('""")  # just partial

    def visit_bold_open(self, node, visited_children):
        return ''

    def visit_bold_close(self, node, visited_children):
        return ''

    def visit_text(self, node, visited_children):
        """Return the text verbatim."""
        return node.text

    def visit_bold_text(self, node, visited_children):
        return ''.join(visited_children)

class ExplosiveFormatter(NodeVisitor):
    """Visitor which raises exceptions"""

    def visit_boom(self, node, visited_children):
        raise ValueError

def test_visitor():
    """Assert a tree gets visited correctly.

    We start with a tree from applying this grammar... ::

    ...to this text::

        ((o hai))

    """
    grammar = Grammar(r'''
        bold_text  = bold_open text bold_close
        text       = ~'[a-zA-Z 0-9]*'
        bold_open  = '(('
        bold_close = '))'
    ''')
    text = '((o hai))'
    tree = Node(grammar['bold_text'], text, 0, 9,
                [Node(grammar['bold_open'], text, 0, 2),
                 Node(grammar['text'], text, 2, 7),
                 Node(grammar['bold_close'], text, 7, 9)])
    eq_(grammar.parse(text), tree)
    result = HtmlFormatter().visit(tree)
    eq_(result, 'o hai')

def test_visitation_exception():
    assert_raises(VisitationError,
                  ExplosiveFormatter().visit,
                  Node(Literal(''), '', 0, 0))

def test_str():
    """Test str and unicode of ``Node``."""
    n = Node(Literal('something', name='text'), 'o hai', 0, 5)
    good = '<Node called "text" matching "o hai">'
    eq_(str(n), good)

def test_repr():
    """Test repr of ``Node``."""
    s = u'hai ö'
    boogie = u'böogie'
    n = Node(Literal(boogie), s, 0, 3, children=[
            Node(Literal(' '), s, 3, 4), Node(Literal(u'ö'), s, 4, 5)])
    eq_(repr(n),
        """s = {hai_o}\nNode({boogie}, s, 0, 3, children=[Node({space}, s, 3, 4), Node({o}, s, 4, 5)])""".format(
            hai_o=repr(s),
            boogie=repr(Literal(boogie)),
            space=repr(Literal(" ")),
            o=repr(Literal("ö")),
        )
    )

def test_parse_shortcut():
    """Exercise the simple case in which the visitor takes care of parsing."""
    eq_(HtmlFormatter().parse('(('), '')

def test_match_shortcut():
    """Exercise the simple case in which the visitor takes care of matching."""
    eq_(HtmlFormatter().match('((other things'), '')

class CoupledFormatter(NodeVisitor):
    @rule('bold_open text bold_close')
    def visit_bold_text(self, node, visited_children):
        return ''.join(visited_children)

    @rule('"(("')
    def visit_bold_open(self, node, visited_children):
        return ''

    @rule('"))"')
    def visit_bold_close(self, node, visited_children):
        return ''

    @rule('~"[a-zA-Z 0-9]*"')
    def visit_text(self, node, visited_children):
        """Return the text verbatim."""
        return node.text

def test_rule_decorator():
    """Make sure the @rule decorator works."""
    eq_(CoupledFormatter().parse('((hi))'), 'hi')

def test_rule_decorator_subclassing():
    """Make sure we can subclass and override visitor methods without blowing
    away the rules attached to them."""
    class OverridingFormatter(CoupledFormatter):
        def visit_text(self, node, visited_children):
            """Return the text capitalized."""
            return node.text.upper()

        @rule('"not used"')
        def visit_useless(self, node, visited_children):
            """Get in the way. Tempt the metaclass to pave over the
            superclass's grammar with a new one."""

    raise SkipTest("I haven't got around to making this work yet.")
    eq_(OverridingFormatter().parse('((hi))'), 'HI')

class PrimalScream(Exception):
    pass

def test_unwrapped_exceptions():
    class Screamer(NodeVisitor):
        grammar = Grammar("""greeting = 'howdy'""")
        unwrapped_exceptions = (PrimalScream,)

        def visit_greeting(self, thing, visited_children):
            raise PrimalScream('This should percolate up!')

    assert_raises(PrimalScream, Screamer().parse, 'howdy')

def test_node_inequality():
    node = Node(Literal('12345'), 'o hai', 0, 5)
    ok_(node != 5)
    ok_(node != None)
    ok_(node != Node(Literal('23456'), 'o hai', 0, 5))
    ok_(not (node != Node(Literal('12345'), 'o hai', 0, 5)))

def test_generic_visit_NotImplementedError_unnamed_node():
    class MyVisitor(NodeVisitor):
        grammar = Grammar(r'''
            bar = "b" "a" "r"
        ''')
        unwrapped_exceptions = (NotImplementedError, )

    with assert_raises(NotImplementedError) as e:
        MyVisitor().parse('bar')
    assert_in('No visitor method was defined for "b"', str(e.exception))

def test_generic_visit_NotImplementedError_named_node():
    class MyVisitor(NodeVisitor):
        grammar = Grammar(r'''
            bar = myrule myrule myrule
            myrule = ~"[bar]"
        ''')
        unwrapped_exceptions = (NotImplementedError, )

    with assert_raises(NotImplementedError) as e:
        MyVisitor().parse('bar')
    assert_in('No visitor method was defined for myrule = ~"[bar]"', str(e.exception))
<commit_msg>Fix failing repr test in python 2.<commit_after># -*- coding: utf-8 -*-
from nose import SkipTest
from nose.tools import eq_, ok_, assert_raises, assert_in

from parsimonious import Grammar, NodeVisitor, VisitationError, rule
from parsimonious.expressions import Literal
from parsimonious.nodes import Node

class HtmlFormatter(NodeVisitor):
    """Visitor that turns a parse tree into HTML fragments"""

    grammar = Grammar("""bold_open  = '(('""")  # just partial

    def visit_bold_open(self, node, visited_children):
        return ''

    def visit_bold_close(self, node, visited_children):
        return ''

    def visit_text(self, node, visited_children):
        """Return the text verbatim."""
        return node.text

    def visit_bold_text(self, node, visited_children):
        return ''.join(visited_children)

class ExplosiveFormatter(NodeVisitor):
    """Visitor which raises exceptions"""

    def visit_boom(self, node, visited_children):
        raise ValueError

def test_visitor():
    """Assert a tree gets visited correctly.

    We start with a tree from applying this grammar... ::

    ...to this text::

        ((o hai))

    """
    grammar = Grammar(r'''
        bold_text  = bold_open text bold_close
        text       = ~'[a-zA-Z 0-9]*'
        bold_open  = '(('
        bold_close = '))'
    ''')
    text = '((o hai))'
    tree = Node(grammar['bold_text'], text, 0, 9,
                [Node(grammar['bold_open'], text, 0, 2),
                 Node(grammar['text'], text, 2, 7),
                 Node(grammar['bold_close'], text, 7, 9)])
    eq_(grammar.parse(text), tree)
    result = HtmlFormatter().visit(tree)
    eq_(result, 'o hai')

def test_visitation_exception():
    assert_raises(VisitationError,
                  ExplosiveFormatter().visit,
                  Node(Literal(''), '', 0, 0))

def test_str():
    """Test str and unicode of ``Node``."""
    n = Node(Literal('something', name='text'), 'o hai', 0, 5)
    good = '<Node called "text" matching "o hai">'
    eq_(str(n), good)

def test_repr():
    """Test repr of ``Node``."""
    s = u'hai ö'
    boogie = u'böogie'
    n = Node(Literal(boogie), s, 0, 3, children=[
            Node(Literal(' '), s, 3, 4), Node(Literal(u'ö'), s, 4, 5)])
    eq_(repr(n),
        str("""s = {hai_o}\nNode({boogie}, s, 0, 3, children=[Node({space}, s, 3, 4), Node({o}, s, 4, 5)])""").format(
            hai_o=repr(s),
            boogie=repr(Literal(boogie)),
            space=repr(Literal(" ")),
            o=repr(Literal(u"ö")),
        )
    )

def test_parse_shortcut():
    """Exercise the simple case in which the visitor takes care of parsing."""
    eq_(HtmlFormatter().parse('(('), '')

def test_match_shortcut():
    """Exercise the simple case in which the visitor takes care of matching."""
    eq_(HtmlFormatter().match('((other things'), '')

class CoupledFormatter(NodeVisitor):
    @rule('bold_open text bold_close')
    def visit_bold_text(self, node, visited_children):
        return ''.join(visited_children)

    @rule('"(("')
    def visit_bold_open(self, node, visited_children):
        return ''

    @rule('"))"')
    def visit_bold_close(self, node, visited_children):
        return ''

    @rule('~"[a-zA-Z 0-9]*"')
    def visit_text(self, node, visited_children):
        """Return the text verbatim."""
        return node.text

def test_rule_decorator():
    """Make sure the @rule decorator works."""
    eq_(CoupledFormatter().parse('((hi))'), 'hi')

def test_rule_decorator_subclassing():
    """Make sure we can subclass and override visitor methods without blowing
    away the rules attached to them."""
    class OverridingFormatter(CoupledFormatter):
        def visit_text(self, node, visited_children):
            """Return the text capitalized."""
            return node.text.upper()

        @rule('"not used"')
        def visit_useless(self, node, visited_children):
            """Get in the way. Tempt the metaclass to pave over the
            superclass's grammar with a new one."""

    raise SkipTest("I haven't got around to making this work yet.")
    eq_(OverridingFormatter().parse('((hi))'), 'HI')

class PrimalScream(Exception):
    pass

def test_unwrapped_exceptions():
    class Screamer(NodeVisitor):
        grammar = Grammar("""greeting = 'howdy'""")
        unwrapped_exceptions = (PrimalScream,)

        def visit_greeting(self, thing, visited_children):
            raise PrimalScream('This should percolate up!')

    assert_raises(PrimalScream, Screamer().parse, 'howdy')

def test_node_inequality():
    node = Node(Literal('12345'), 'o hai', 0, 5)
    ok_(node != 5)
    ok_(node != None)
    ok_(node != Node(Literal('23456'), 'o hai', 0, 5))
    ok_(not (node != Node(Literal('12345'), 'o hai', 0, 5)))

def test_generic_visit_NotImplementedError_unnamed_node():
    class MyVisitor(NodeVisitor):
        grammar = Grammar(r'''
            bar = "b" "a" "r"
        ''')
        unwrapped_exceptions = (NotImplementedError, )

    with assert_raises(NotImplementedError) as e:
        MyVisitor().parse('bar')
    assert_in('No visitor method was defined for "b"', str(e.exception))

def test_generic_visit_NotImplementedError_named_node():
    class MyVisitor(NodeVisitor):
        grammar = Grammar(r'''
            bar = myrule myrule myrule
            myrule = ~"[bar]"
        ''')
        unwrapped_exceptions = (NotImplementedError, )

    with assert_raises(NotImplementedError) as e:
        MyVisitor().parse('bar')
    assert_in('No visitor method was defined for myrule = ~"[bar]"', str(e.exception))
<|endoftext|>


<commit_before># from __future__ import print_function
# from pprint import pprint
from app import db
from flask import Blueprint, render_template, flash, redirect, url_for
from sqlalchemy.orm import aliased
from .forms import EventForm, EventTypeForm, PriceForm, CommodityForm
from .models import Event, EventType, Price, Commodity

hermes = Blueprint('hermes', __name__)

def _get_table_info(table):
	def get_event():
		form_fields = ['symbol', 'event_type_id', 'value', 'date']
		table_headers = ['Symbol', 'Name', 'Unit', 'Value', 'Date']
		query = db.session.query(Event, EventType).join(EventType) \
			.order_by(Event.date)
		data_fields = [(0, 'symbol'), (1, 'name'), (1, 'unit'), (0, 'value'),
			(0, 'date')]
		return form_fields, table_headers, query, data_fields

	def get_event_type():
		form_fields = ['name', 'unit']
		table_headers = ['Type Name', 'Unit']
		query = db.session.query(EventType).order_by(EventType.name)
		data_fields = form_fields
		return form_fields, table_headers, query, data_fields

	def get_price():
		form_fields = ['commodity_id', 'currency_id', 'date', 'close']
		table_headers = ['Stock', 'Currency', 'Price', 'Date']
		Currency = aliased(Commodity)
		query = db.session.query(Price, Commodity, Currency) \
			.join(Price.commodity).join(Currency, Price.currency) \
			.order_by(Price.date)
		data_fields = [(1, 'symbol'), (2, 'symbol'), (0, 'date'), (0, 'close')]
		return form_fields, table_headers, query, data_fields

	def get_commodity():
		form_fields = ['symbol', 'name']
		table_headers = ['Symbol', 'Name']
		query = db.session.query(Commodity).order_by(Commodity.name)
		data_fields = form_fields
		return form_fields, table_headers, query, data_fields

	switch = {'event': get_event(),
		'event_type': get_event_type(),
		'price': get_price(),
		'commodity': get_commodity()}

	return switch.get(table)

def _get_plural(word):
	if word[-1] == 'y':
		return word[:-1] + 'ies'
	else:
		return word + 's'

@hermes.route('/<table>/', methods=['GET', 'POST'])
def get(table):
	plural_table = _get_plural(table).replace('_', ' ')
	table_as_class = table.title().replace('_', '')
	table_title = table.title().replace('_', ' ')
	plural_table_title = plural_table.title()
	form_fields, table_headers, query, data_fields = _get_table_info(table)
	id = table
	post_location = 'hermes.add'
	post_table = table
	title = '%s' % plural_table_title
	table_caption = '%s List' % table_title
	form_caption = '%s Entry Form' % table_title
	heading = 'Add %s to the database' % plural_table
	text = 'On this page you can add %s to the database and see them ' \
		'instantly updated in the lists below.' % plural_table
	results = query.all()

	try:
		form = eval('%sForm.new()' % table_as_class)
	except AttributeError:
		form = eval('%sForm()' % table_as_class)

	kwargs = {'id': id, 'title': title, 'heading': heading, 'text': text,
		'rows': results, 'form': form, 'form_caption': form_caption,
		'table_caption': table_caption, 'table_headers': table_headers,
		'data_fields': data_fields, 'form_fields': form_fields,
		'post_location': post_location, 'post_table': post_table}

	return render_template('entry.html', **kwargs)

@hermes.route('/add/<table>/', methods=['GET', 'POST'])
def add(table):
	table_as_class = table.title().replace('_', '')
	try:
		form = eval('%sForm.new()' % table_as_class)
	except AttributeError:
		form = eval('%sForm()' % table_as_class)

	if form.validate_on_submit():
		entry = eval('%s()' % table_as_class)
		form.populate_obj(entry)
		db.session.add(entry)
		db.session.commit()
		flash('Success! A new %s was posted.' % table.replace('_', ' '),
			'alert alert-success')

	else:
		[flash('%s: %s' % (k.title(), v[0]), 'alert alert-error')
			for k, v in form.errors.iteritems()]

	return redirect(url_for('hermes.get', table=table))

@hermes.route('/worth/', methods=['GET', 'POST'])
def worth():
	pass

@hermes.route('/api/', methods=['GET', 'POST'])
def api():
	pass
<commit_msg>Fix typo<commit_after># from __future__ import print_function
# from pprint import pprint
from app import db
from flask import Blueprint, render_template, flash, redirect, url_for
from sqlalchemy.orm import aliased
from .forms import EventForm, EventTypeForm, PriceForm, CommodityForm
from .models import Event, EventType, Price, Commodity

hermes = Blueprint('hermes', __name__)

def _get_table_info(table):
	def get_event():
		form_fields = ['symbol', 'event_type_id', 'value', 'date']
		table_headers = ['Symbol', 'Name', 'Unit', 'Value', 'Date']
		query = db.session.query(Event, EventType).join(EventType) \
			.order_by(Event.date)
		data_fields = [(0, 'symbol'), (1, 'name'), (1, 'unit'), (0, 'value'),
			(0, 'date')]
		return form_fields, table_headers, query, data_fields

	def get_event_type():
		form_fields = ['name', 'unit']
		table_headers = ['Type Name', 'Unit']
		query = db.session.query(EventType).order_by(EventType.name)
		data_fields = form_fields
		return form_fields, table_headers, query, data_fields

	def get_price():
		form_fields = ['commodity_id', 'currency_id', 'date', 'close']
		table_headers = ['Stock', 'Currency', 'Date', 'Price']
		Currency = aliased(Commodity)
		query = db.session.query(Price, Commodity, Currency) \
			.join(Price.commodity).join(Currency, Price.currency) \
			.order_by(Price.date)
		data_fields = [(1, 'symbol'), (2, 'symbol'), (0, 'date'), (0, 'close')]
		return form_fields, table_headers, query, data_fields

	def get_commodity():
		form_fields = ['symbol', 'name']
		table_headers = ['Symbol', 'Name']
		query = db.session.query(Commodity).order_by(Commodity.name)
		data_fields = form_fields
		return form_fields, table_headers, query, data_fields

	switch = {'event': get_event(),
		'event_type': get_event_type(),
		'price': get_price(),
		'commodity': get_commodity()}

	return switch.get(table)

def _get_plural(word):
	if word[-1] == 'y':
		return word[:-1] + 'ies'
	else:
		return word + 's'

@hermes.route('/<table>/', methods=['GET', 'POST'])
def get(table):
	plural_table = _get_plural(table).replace('_', ' ')
	table_as_class = table.title().replace('_', '')
	table_title = table.title().replace('_', ' ')
	plural_table_title = plural_table.title()
	form_fields, table_headers, query, data_fields = _get_table_info(table)
	id = table
	post_location = 'hermes.add'
	post_table = table
	title = '%s' % plural_table_title
	table_caption = '%s List' % table_title
	form_caption = '%s Entry Form' % table_title
	heading = 'Add %s to the database' % plural_table
	text = 'On this page you can add %s to the database and see them ' \
		'instantly updated in the lists below.' % plural_table
	results = query.all()

	try:
		form = eval('%sForm.new()' % table_as_class)
	except AttributeError:
		form = eval('%sForm()' % table_as_class)

	kwargs = {'id': id, 'title': title, 'heading': heading, 'text': text,
		'rows': results, 'form': form, 'form_caption': form_caption,
		'table_caption': table_caption, 'table_headers': table_headers,
		'data_fields': data_fields, 'form_fields': form_fields,
		'post_location': post_location, 'post_table': post_table}

	return render_template('entry.html', **kwargs)

@hermes.route('/add/<table>/', methods=['GET', 'POST'])
def add(table):
	table_as_class = table.title().replace('_', '')
	try:
		form = eval('%sForm.new()' % table_as_class)
	except AttributeError:
		form = eval('%sForm()' % table_as_class)

	if form.validate_on_submit():
		entry = eval('%s()' % table_as_class)
		form.populate_obj(entry)
		db.session.add(entry)
		db.session.commit()
		flash('Success! A new %s was posted.' % table.replace('_', ' '),
			'alert alert-success')

	else:
		[flash('%s: %s' % (k.title(), v[0]), 'alert alert-error')
			for k, v in form.errors.iteritems()]

	return redirect(url_for('hermes.get', table=table))

@hermes.route('/worth/', methods=['GET', 'POST'])
def worth():
	pass

@hermes.route('/api/', methods=['GET', 'POST'])
def api():
	pass
<|endoftext|>


<commit_before>import time
try:
    from sys import version_info
except ImportError:
    version_info = ( 0, 0 )   # a really early version
from pysnmp.entity.rfc3413 import config
from pysnmp.proto.proxy import rfc2576
from pysnmp.proto.api import v2c
from pysnmp.smi import error

vacmID = 3

class NotificationOriginator:
    def __init__(self, snmpContext):
        self.__pendingReqs = {}
        self.__sendRequestHandleSource = 0L
        self.__context = snmpContext

    def processResponsePdu(
        self,
        snmpEngine,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        PDU,
        statusInformation,
        sendPduHandle,
        (cbFun, cbCtx)
        ):
        # 3.3.6d
        ( origTransportDomain,
          origTransportAddress,
          origMessageProcessingModel,
          origSecurityModel,
          origSecurityName,
          origSecurityLevel,
          origContextEngineId,
          origContextName,
          origPduVersion,
          origPdu,
          origTimeout,
          origRetryCount,
          origRetries,
          origSendRequestHandle
          ) = self.__pendingReqs[sendPduHandle]
        del self.__pendingReqs[sendPduHandle]

        snmpEngine.transportDispatcher.jobFinished(id(self))

        if statusInformation:
            if origRetries == origRetryCount:
                cbFun(origSendRequestHandle,
                      statusInformation['errorIndication'],
                      cbCtx)
                return

            # 3.3.6a
            sendPduHandle = snmpEngine.msgAndPduDsp.sendPdu(
                snmpEngine,
                origTransportDomain,
                origTransportAddress,
                origMessageProcessingModel,
                origSecurityModel,
                origSecurityName,
                origSecurityLevel,
                origContextEngineId,
                origContextName,
                origPduVersion,
                origPdu,
                (self.processResponsePdu, origTimeout/1000 + time.time(),
                 (cbFun, cbCtx))
                )

            snmpEngine.transportDispatcher.jobStarted(id(self))

            # 3.3.6b
            self.__pendingReqs[sendPduHandle] = (
                origTransportDomain,
                origTransportAddress,
                origMessageProcessingModel,
                origSecurityModel,
                origSecurityName,
                origSecurityLevel,
                origContextEngineId,
                origContextName,
                origPduVersion,
                origPdu,
                origTimeout,
                origRetryCount,
                origRetries + 1,
                sendPduHandle
                )
            return

        # 3.3.6c
        cbFun(origSendRequestHandle, None, cbCtx)

    def sendNotification(
        self,
        snmpEngine,
        notificationTarget,
        notificationName,
        additionalVarBinds=None,
        cbFun=None,
        cbCtx=None,
        contextName=''
        ):
        # 3.3
        ( notifyTag,
          notifyType ) = config.getNotificationInfo(
            snmpEngine, notificationTarget
            )
        contextMibInstrumCtl = self.__context.getMibInstrum(
            contextName
            )
        for targetAddrName in config.getTargetNames(snmpEngine, notifyTag):
            ( transportDomain,
              transportAddress,
              timeout,
              retryCount,
              params ) = config.getTargetAddr(snmpEngine, targetAddrName)
            ( messageProcessingModel,
              securityModel,
              securityName,
              securityLevel ) = config.getTargetParams(snmpEngine, params)

            # 3.3.1 XXX
# XXX filtering's yet to be implemented
#             filterProfileName = config.getNotifyFilterProfile(params)

#             ( filterSubtree,
#               filterMask,
#               filterType ) = config.getNotifyFilter(filterProfileName)

            varBinds = []

            # 3.3.2 & 3.3.3
            sysUpTime, = contextMibInstrumCtl.mibBuilder.importSymbols(
                '__SNMPv2-MIB', 'sysUpTime'
                )
            varBinds.append((sysUpTime.name + (0,), sysUpTime.syntax))

            snmpTrapOid, = contextMibInstrumCtl.mibBuilder.importSymbols(
                'SNMPv2-MIB', 'snmpTrapOID'
                )

            snmpTrapVal, = apply(
                contextMibInstrumCtl.mibBuilder.importSymbols,
                notificationName
                )
            varBinds.append(
                (snmpTrapOid.name + (0,), v2c.ObjectIdentifier(snmpTrapVal.name))
                )

            # Get notification objects names
            for notificationObject in snmpTrapVal.getObjects():
                mibNode, = contextMibInstrumCtl.mibBuilder.importSymbols(
                    notificationObject #, mibNode.moduleName # XXX
                    )
                varBinds.append((mibNode.name + (0,), mibNode.syntax))

            if additionalVarBinds:
                if version_info < (1, 6):
                    additionalVarBinds = list(additionalVarBinds)
                varBinds.extend(additionalVarBinds)

            for varName, varVal in varBinds:
                try:
                    snmpEngine.accessControlModel[vacmID].isAccessAllowed(
                        snmpEngine, securityModel, securityName,
                        securityLevel, 'notify', contextName, varName
                        )
                except error.SmiError:
                    return

            # 3.3.4
            if notifyType == 1:
                pdu = v2c.SNMPv2TrapPDU()
            elif notifyType == 2:
                pdu = v2c.InformRequestPDU()
            else:
                raise RuntimeError()
            v2c.apiPDU.setDefaults(pdu)
            v2c.apiPDU.setVarBinds(pdu, varBinds)

            # User-side API assumes SMIv2
            if messageProcessingModel == 0:
                pdu = rfc2576.v2ToV1(pdu)
                pduVersion = 0
            else:
                pduVersion = 1

            # 3.3.5
            if notifyType == 1:
                snmpEngine.msgAndPduDsp.sendPdu(
                    snmpEngine,
                    transportDomain,
                    transportAddress,
                    messageProcessingModel,
                    securityModel,
                    securityName,
                    securityLevel,
                    self.__context.contextEngineId,
                    contextName,
                    pduVersion,
                    pdu,
                    None
                    )
            else:
                # 3.3.6a
                sendPduHandle = snmpEngine.msgAndPduDsp.sendPdu(
                    snmpEngine,
                    transportDomain,
                    transportAddress,
                    messageProcessingModel,
                    securityModel,
                    securityName,
                    securityLevel,
                    self.__context.contextEngineId,
                    contextName,
                    pduVersion,
                    pdu,
                    (self.processResponsePdu, timeout/1000 + time.time(),
                     (cbFun, cbCtx))
                    )

                # 3.3.6b
                self.__pendingReqs[sendPduHandle] = (
                    transportDomain,
                    transportAddress,
                    messageProcessingModel,
                    securityModel,
                    securityName,
                    securityLevel,
                    self.__context.contextEngineId,
                    contextName,
                    pduVersion,
                    pdu,
                    timeout,
                    retryCount,
                    1,
                    self.__sendRequestHandleSource
                    )

                snmpEngine.transportDispatcher.jobStarted(id(self))

# XXX
# move/group/implement config setting/retrieval at a stand-alone module

<commit_msg>fix to sysUpTime OID<commit_after>import time
try:
    from sys import version_info
except ImportError:
    version_info = ( 0, 0 )   # a really early version
from pysnmp.entity.rfc3413 import config
from pysnmp.proto.proxy import rfc2576
from pysnmp.proto.api import v2c
from pysnmp.smi import error

vacmID = 3

class NotificationOriginator:
    def __init__(self, snmpContext):
        self.__pendingReqs = {}
        self.__sendRequestHandleSource = 0L
        self.__context = snmpContext

    def processResponsePdu(
        self,
        snmpEngine,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        PDU,
        statusInformation,
        sendPduHandle,
        (cbFun, cbCtx)
        ):
        # 3.3.6d
        ( origTransportDomain,
          origTransportAddress,
          origMessageProcessingModel,
          origSecurityModel,
          origSecurityName,
          origSecurityLevel,
          origContextEngineId,
          origContextName,
          origPduVersion,
          origPdu,
          origTimeout,
          origRetryCount,
          origRetries,
          origSendRequestHandle
          ) = self.__pendingReqs[sendPduHandle]
        del self.__pendingReqs[sendPduHandle]

        snmpEngine.transportDispatcher.jobFinished(id(self))

        if statusInformation:
            if origRetries == origRetryCount:
                cbFun(origSendRequestHandle,
                      statusInformation['errorIndication'],
                      cbCtx)
                return

            # 3.3.6a
            sendPduHandle = snmpEngine.msgAndPduDsp.sendPdu(
                snmpEngine,
                origTransportDomain,
                origTransportAddress,
                origMessageProcessingModel,
                origSecurityModel,
                origSecurityName,
                origSecurityLevel,
                origContextEngineId,
                origContextName,
                origPduVersion,
                origPdu,
                (self.processResponsePdu, origTimeout/1000 + time.time(),
                 (cbFun, cbCtx))
                )

            snmpEngine.transportDispatcher.jobStarted(id(self))

            # 3.3.6b
            self.__pendingReqs[sendPduHandle] = (
                origTransportDomain,
                origTransportAddress,
                origMessageProcessingModel,
                origSecurityModel,
                origSecurityName,
                origSecurityLevel,
                origContextEngineId,
                origContextName,
                origPduVersion,
                origPdu,
                origTimeout,
                origRetryCount,
                origRetries + 1,
                sendPduHandle
                )
            return

        # 3.3.6c
        cbFun(origSendRequestHandle, None, cbCtx)

    def sendNotification(
        self,
        snmpEngine,
        notificationTarget,
        notificationName,
        additionalVarBinds=None,
        cbFun=None,
        cbCtx=None,
        contextName=''
        ):
        # 3.3
        ( notifyTag,
          notifyType ) = config.getNotificationInfo(
            snmpEngine, notificationTarget
            )
        contextMibInstrumCtl = self.__context.getMibInstrum(
            contextName
            )
        for targetAddrName in config.getTargetNames(snmpEngine, notifyTag):
            ( transportDomain,
              transportAddress,
              timeout,
              retryCount,
              params ) = config.getTargetAddr(snmpEngine, targetAddrName)
            ( messageProcessingModel,
              securityModel,
              securityName,
              securityLevel ) = config.getTargetParams(snmpEngine, params)

            # 3.3.1 XXX
# XXX filtering's yet to be implemented
#             filterProfileName = config.getNotifyFilterProfile(params)

#             ( filterSubtree,
#               filterMask,
#               filterType ) = config.getNotifyFilter(filterProfileName)

            varBinds = []

            # 3.3.2 & 3.3.3
            sysUpTime, = contextMibInstrumCtl.mibBuilder.importSymbols(
                '__SNMPv2-MIB', 'sysUpTime'
                )
            varBinds.append((sysUpTime.name, sysUpTime.syntax))

            snmpTrapOid, = contextMibInstrumCtl.mibBuilder.importSymbols(
                'SNMPv2-MIB', 'snmpTrapOID'
                )

            snmpTrapVal, = apply(
                contextMibInstrumCtl.mibBuilder.importSymbols,
                notificationName
                )
            varBinds.append(
                (snmpTrapOid.name + (0,), v2c.ObjectIdentifier(snmpTrapVal.name))
                )

            # Get notification objects names
            for notificationObject in snmpTrapVal.getObjects():
                mibNode, = contextMibInstrumCtl.mibBuilder.importSymbols(
                    notificationObject #, mibNode.moduleName # XXX
                    )
                varBinds.append((mibNode.name + (0,), mibNode.syntax))

            if additionalVarBinds:
                if version_info < (1, 6):
                    additionalVarBinds = list(additionalVarBinds)
                varBinds.extend(additionalVarBinds)

            for varName, varVal in varBinds:
                try:
                    snmpEngine.accessControlModel[vacmID].isAccessAllowed(
                        snmpEngine, securityModel, securityName,
                        securityLevel, 'notify', contextName, varName
                        )
                except error.SmiError:
                    return

            # 3.3.4
            if notifyType == 1:
                pdu = v2c.SNMPv2TrapPDU()
            elif notifyType == 2:
                pdu = v2c.InformRequestPDU()
            else:
                raise RuntimeError()
            v2c.apiPDU.setDefaults(pdu)
            v2c.apiPDU.setVarBinds(pdu, varBinds)

            # User-side API assumes SMIv2
            if messageProcessingModel == 0:
                pdu = rfc2576.v2ToV1(pdu)
                pduVersion = 0
            else:
                pduVersion = 1

            # 3.3.5
            if notifyType == 1:
                snmpEngine.msgAndPduDsp.sendPdu(
                    snmpEngine,
                    transportDomain,
                    transportAddress,
                    messageProcessingModel,
                    securityModel,
                    securityName,
                    securityLevel,
                    self.__context.contextEngineId,
                    contextName,
                    pduVersion,
                    pdu,
                    None
                    )
            else:
                # 3.3.6a
                sendPduHandle = snmpEngine.msgAndPduDsp.sendPdu(
                    snmpEngine,
                    transportDomain,
                    transportAddress,
                    messageProcessingModel,
                    securityModel,
                    securityName,
                    securityLevel,
                    self.__context.contextEngineId,
                    contextName,
                    pduVersion,
                    pdu,
                    (self.processResponsePdu, timeout/1000 + time.time(),
                     (cbFun, cbCtx))
                    )

                # 3.3.6b
                self.__pendingReqs[sendPduHandle] = (
                    transportDomain,
                    transportAddress,
                    messageProcessingModel,
                    securityModel,
                    securityName,
                    securityLevel,
                    self.__context.contextEngineId,
                    contextName,
                    pduVersion,
                    pdu,
                    timeout,
                    retryCount,
                    1,
                    self.__sendRequestHandleSource
                    )

                snmpEngine.transportDispatcher.jobStarted(id(self))

# XXX
# move/group/implement config setting/retrieval at a stand-alone module

<|endoftext|>


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

import random

class Plugin:
    vk = None

    def __init__(self, vk):
        self.vk = vk
        print('Плагин музыки')

    def getkeys(self):
        keys = [u'музыка', u'музыку', 'music']
        ret = {}
        for key in keys:
            ret[key] = self
        return ret

    def call(self, msg):
        music = None

        errors = []
        errors.append(u'Открой аудио, мудак!')
        errors.append(u'Мудак, аудиозаписи открой!')
        errors.append(u'Я бы с радостью тебе дал музыки, но ты мудак')

        try:
            count = self.vk.api.method('audio.getRecommendations',
                {'user_id': msg['user_id'], 'count': 1})

            music = self.vk.api.method('audio.getRecommendations',
                {'user_id': msg['user_id'],
                'offset': random.randint(0, count['count'] - 5),
                'count': 5})
        except:
            self.vk.respond(msg, {'message': random.choice(errors)})

        musicatt = []

        if music is not None and music['items']:
         for attach in music['items']:
            user = attach['owner_id']
            ident = attach['id']
            musicatt.append('audio' + str(user) + '_' + str(ident))

        answers = []
        answers.append(u'Вот твоя музыка')
        answers.append(u'Музыку заказывали?')
        answers.append(u'Бесплатная музыка!')

        attstring = ''

        if musicatt is not None:
         for item in musicatt:
            attstring += item + ','

        if attstring == '':
            self.vk.respond(msg, {'message': random.choice(errors)})
        else:
            self.vk.respond(msg, {'message': random.choice(answers),
            'attachment': attstring})
<commit_msg>Обновление сообщений<commit_after># -*- coding: utf-8 -*-

import random

class Plugin:
    vk = None

    def __init__(self, vk):
        self.vk = vk
        print('Плагин музыки')

    def getkeys(self):
        keys = [u'музыка', u'музыку', 'music']
        ret = {}
        for key in keys:
            ret[key] = self
        return ret

    def call(self, msg):
        music = None

        errors = []
        errors.append(u'Открой аудио, мудак!')
        errors.append(u'Мудак, аудиозаписи открой!')
        errors.append(u'У тебя аудио закрыты, мудак!')
        errors.append(u'Я бы с радостью тебе дал музыки, но ты мудак.')

        try:
            count = self.vk.api.method('audio.getRecommendations',
                {'user_id': msg['user_id'], 'count': 1})

            music = self.vk.api.method('audio.getRecommendations',
                {'user_id': msg['user_id'],
                'offset': random.randint(0, count['count'] - 5),
                'count': 5})
        except:
            self.vk.respond(msg, {'message': random.choice(errors)})

        musicatt = []

        if music is not None and music['items']:
         for attach in music['items']:
            user = attach['owner_id']
            ident = attach['id']
            musicatt.append('audio' + str(user) + '_' + str(ident))

        answers = []
        answers.append(u'Вот твоя музыка:')
        answers.append(u'Вот, послушай.')
        answers.append(u'Мои рекомендации для тебя:')
        answers.append(u'Музыку заказывали?')
        answers.append(u'Бесплатная музыка!')

        attstring = ''

        if musicatt is not None:
         for item in musicatt:
            attstring += item + ','

        if attstring == '':
            self.vk.respond(msg, {'message': random.choice(errors)})
        else:
            self.vk.respond(msg, {'message': random.choice(answers),
            'attachment': attstring})
<|endoftext|>


<commit_before>'''
Extractors that operate primarily or exclusively on Vector stimuli.
'''
from pliers.stimuli.vector import VectorStim
from pliers.extractors.base import Extractor, ExtractorResult
from pliers.utils import listify
import scipy
import numpy as np
import pandas as pd
from collections.abc import Iterable
import logging

class VectorMetricExtractor(Extractor):
    ''' Extracts summary metrics from 1D-array using numpy, scipy or custom
        functions
    Args:
        functions (str, functions or list): function or list of numpy, scipy,
        or custom functions to be applied to 1-dimensional numpy arrays.
            Functions can be passed directly (e.g. passing scipy.stats.entropy) if
            the package/module they belong to has been imported, or as strings
            (e.g. 'np.mean'). Custom functions returning integers or iterables
            can also be passed, either directly, or as strings (evaluated via
            eval() method).
        var_names (list): optional list of custom alias names for each metric
        kwargs: named arguments for function call
    '''

    _input_type = VectorStim
    _log_attributes = ('functions',)

    def __init__(self, functions=['np.mean', 'np.std', 'np.max', 'np.min'],
                 var_names=None, **kwargs):
        functions = listify(functions)
        var_names = listify(var_names)
        if var_names is not None:
            if len(var_names) != len(functions):
                raise ValueError('Length or var_names must match number of '
                                 'functions')
        for idx, f in enumerate(functions):
            if isinstance(f, str):
                f = f.replace('numpy', 'np')
                try:
                    f_list = f.split('.')
                    if len(f_list) > 1 and 'lambda' not in f:
                        functions[idx] = getattr(eval('.'.join(f_list[:-1])),
                                                f_list[-1])
                    else:
                        functions[idx] = eval(f)
                except:
                    raise ValueError(f"{f} is not a valid function")
        if var_names is None:
            var_names = [f.__name__ for f in functions]
        self.var_names = var_names

        self.functions = functions
        self.kwargs = kwargs
        super(VectorMetricExtractor, self).__init__()

    def _extract(self, stim):
        outputs = []
        for f in self.functions:
            metrics = f(stim.array, **self.kwargs)
            if isinstance(metrics, Iterable):
                metrics = np.array(metrics)
            outputs.append(metrics)
        return ExtractorResult([outputs], stim, self, self.var_names)

<commit_msg>remove function defaults<commit_after>'''
Extractors that operate primarily or exclusively on Vector stimuli.
'''
from pliers.stimuli.vector import VectorStim
from pliers.extractors.base import Extractor, ExtractorResult
from pliers.utils import listify
import scipy
import numpy as np
import pandas as pd
from collections.abc import Iterable
import logging

class VectorMetricExtractor(Extractor):
    ''' Extracts summary metrics from 1D-array using numpy, scipy or custom
        functions
    Args:
        functions (str, functions or list): function or list of numpy, scipy,
        or custom functions to be applied to 1-dimensional numpy arrays.
            Functions can be passed directly (e.g. passing scipy.stats.entropy) if
            the package/module they belong to has been imported, or as strings
            (e.g. 'np.mean'). Custom functions returning integers or iterables
            can also be passed, either directly, or as strings (evaluated via
            eval() method).
        var_names (list): optional list of custom alias names for each metric
        kwargs: named arguments for function call
    '''

    _input_type = VectorStim
    _log_attributes = ('functions',)

    def __init__(self, functions=None, var_names=None, **kwargs):
        functions = listify(functions)
        var_names = listify(var_names)
        if var_names is not None:
            if len(var_names) != len(functions):
                raise ValueError('Length or var_names must match number of '
                                 'functions')
        for idx, f in enumerate(functions):
            if isinstance(f, str):
                f = f.replace('numpy', 'np')
                try:
                    f_list = f.split('.')
                    if len(f_list) > 1 and 'lambda' not in f:
                        functions[idx] = getattr(eval('.'.join(f_list[:-1])),
                                                f_list[-1])
                    else:
                        functions[idx] = eval(f)
                except:
                    raise ValueError(f"{f} is not a valid function")
        if var_names is None:
            var_names = [f.__name__ for f in functions]
        self.var_names = var_names

        self.functions = functions
        self.kwargs = kwargs
        super(VectorMetricExtractor, self).__init__()

    def _extract(self, stim):
        outputs = []
        for f in self.functions:
            metrics = f(stim.array, **self.kwargs)
            if isinstance(metrics, Iterable):
                metrics = np.array(metrics)
            outputs.append(metrics)
        return ExtractorResult([outputs], stim, self, self.var_names)

<|endoftext|>


<commit_before>import logging
import os
import time

from parsl.channels import LocalChannel
from parsl.launchers import AprunLauncher
from parsl.providers.provider_base import JobState, JobStatus
from parsl.providers.torque.template import template_string
from parsl.providers.cluster_provider import ClusterProvider
from parsl.utils import RepresentationMixin

logger = logging.getLogger(__name__)

# From the man pages for qstat for PBS/Torque systems
translate_table = {
    'B': JobState.RUNNING,  # This state is returned for running array jobs
    'R': JobState.RUNNING,
    'C': JobState.COMPLETED,  # Completed after having run
    'E': JobState.COMPLETED,  # Exiting after having run
    'H': JobState.HELD,  # Held
    'Q': JobState.PENDING,  # Queued, and eligible to run
    'W': JobState.PENDING,  # Job is waiting for it's execution time (-a option) to be reached
    'S': JobState.HELD  # Suspended
}

class TorqueProvider(ClusterProvider, RepresentationMixin):
    """Torque Execution Provider

    This provider uses sbatch to submit, squeue for status, and scancel to cancel
    jobs. The sbatch script to be used is created from a template file in this
    same module.

    Parameters
    ----------
    channel : Channel
        Channel for accessing this provider. Possible channels include
        :class:`~parsl.channels.LocalChannel` (the default),
        :class:`~parsl.channels.SSHChannel`, or
        :class:`~parsl.channels.SSHInteractiveLoginChannel`.
    account : str
        Account the job will be charged against.
    queue : str
        Torque queue to request blocks from.
    nodes_per_block : int
        Nodes to provision per block.
    init_blocks : int
        Number of blocks to provision at the start of the run. Default is 1.
    min_blocks : int
        Minimum number of blocks to maintain. Default is 0.
    max_blocks : int
        Maximum number of blocks to maintain.
    parallelism : float
        Ratio of provisioned task slots to active tasks. A parallelism value of 1 represents aggressive
        scaling where as many resources as possible are used; parallelism close to 0 represents
        the opposite situation in which as few resources as possible (i.e., min_blocks) are used.
    walltime : str
        Walltime requested per block in HH:MM:SS.
    scheduler_options : str
        String to prepend to the #PBS blocks in the submit script to the scheduler.
        WARNING: scheduler_options should only be given #PBS strings, and should not have trailing newlines.
    worker_init : str
        Command to be run before starting a worker, such as 'module load Anaconda; source activate env'.
    launcher : Launcher
        Launcher for this provider. Possible launchers include
        :class:`~parsl.launchers.AprunLauncher` (the default), or
        :class:`~parsl.launchers.SingleNodeLauncher`,

    """
    def __init__(self,
                 channel=LocalChannel(),
                 account=None,
                 queue=None,
                 scheduler_options='',
                 worker_init='',
                 nodes_per_block=1,
                 init_blocks=1,
                 min_blocks=0,
                 max_blocks=1,
                 parallelism=1,
                 launcher=AprunLauncher(),
                 walltime="00:20:00",
                 cmd_timeout=120):
        label = 'torque'
        super().__init__(label,
                         channel,
                         nodes_per_block,
                         init_blocks,
                         min_blocks,
                         max_blocks,
                         parallelism,
                         walltime,
                         launcher,
                         cmd_timeout=cmd_timeout)

        self.account = account
        self.queue = queue
        self.scheduler_options = scheduler_options
        self.worker_init = worker_init
        self.template_string = template_string

        # Dictionary that keeps track of jobs, keyed on job_id
        self.resources = {}

    def _status(self):
        ''' Internal: Do not call. Returns the status list for a list of job_ids

        Args:
              self

        Returns:
              [status...] : Status list of all jobs
        '''

        job_ids = list(self.resources.keys())
        job_id_list = ' '.join(self.resources.keys())

        jobs_missing = list(self.resources.keys())

        retcode, stdout, stderr = self.execute_wait("qstat {0}".format(job_id_list))
        for line in stdout.split('\n'):
            parts = line.split()
            if not parts or parts[0].upper().startswith('JOB') or parts[0].startswith('---'):
                continue
            job_id = parts[0]  # likely truncated
            for long_job_id in job_ids:
                if long_job_id.startswith(job_id):
                    logger.debug('coerced job_id %s -> %s', job_id, long_job_id)
                    job_id = long_job_id
                    break
            state = translate_table.get(parts[4], JobState.UNKNOWN)
            self.resources[job_id]['status'] = JobStatus(state)
            jobs_missing.remove(job_id)

        # squeue does not report on jobs that are not running. So we are filling in the
        # blanks for missing jobs, we might lose some information about why the jobs failed.
        for missing_job in jobs_missing:
            self.resources[missing_job]['status'] = JobStatus(JobState.COMPLETED)

    def submit(self, command, tasks_per_node, job_name="parsl.torque"):
        ''' Submits the command onto an Local Resource Manager job.
        Submit returns an ID that corresponds to the task that was just submitted.

        If tasks_per_node <  1 : ! This is illegal. tasks_per_node should be integer

        If tasks_per_node == 1:
             A single node is provisioned

        If tasks_per_node >  1 :
             tasks_per_node number of nodes are provisioned.

        Args:
             - command  :(String) Commandline invocation to be made on the remote side.
             - tasks_per_node (int) : command invocations to be launched per node

        Kwargs:
             - job_name (String): Name for job, must be unique

        Returns:
             - None: At capacity, cannot provision more
             - job_id: (string) Identifier for the job

        '''

        # Set job name
        job_name = "parsl.{0}.{1}".format(job_name, time.time())

        # Set script path
        script_path = "{0}/{1}.submit".format(self.script_dir, job_name)
        script_path = os.path.abspath(script_path)

        logger.debug("Requesting nodes_per_block:%s tasks_per_node:%s", self.nodes_per_block,
                     tasks_per_node)

        job_config = {}
        # TODO : script_path might need to change to accommodate script dir set via channels
        job_config["submit_script_dir"] = self.channel.script_dir
        job_config["nodes"] = self.nodes_per_block
        job_config["task_blocks"] = self.nodes_per_block * tasks_per_node
        job_config["nodes_per_block"] = self.nodes_per_block
        job_config["tasks_per_node"] = tasks_per_node
        job_config["walltime"] = self.walltime
        job_config["scheduler_options"] = self.scheduler_options
        job_config["worker_init"] = self.worker_init
        job_config["user_script"] = command

        # Wrap the command
        job_config["user_script"] = self.launcher(command,
                                                  tasks_per_node,
                                                  self.nodes_per_block)

        logger.debug("Writing submit script")
        self._write_submit_script(self.template_string, script_path, job_name, job_config)

        channel_script_path = self.channel.push_file(script_path, self.channel.script_dir)

        submit_options = ''
        if self.queue is not None:
            submit_options = '{0} -q {1}'.format(submit_options, self.queue)
        if self.account is not None:
            submit_options = '{0} -A {1}'.format(submit_options, self.account)

        launch_cmd = "qsub {0} {1}".format(submit_options, channel_script_path)
        retcode, stdout, stderr = self.execute_wait(launch_cmd)

        job_id = None
        if retcode == 0:
            for line in stdout.split('\n'):
                if line.strip():
                    job_id = line.strip()
                    self.resources[job_id] = {'job_id': job_id, 'status': JobStatus(JobState.PENDING)}
        else:
            message = "Command '{}' failed with return code {}".format(launch_cmd, retcode)
            if (stdout is not None) and (stderr is not None):
                message += "\nstderr:{}\nstdout{}".format(stderr.strip(), stdout.strip())
            logger.error(message)

        return job_id

    def cancel(self, job_ids):
        ''' Cancels the jobs specified by a list of job ids

        Args:
        job_ids : [<job_id> ...]

        Returns :
        [True/False...] : If the cancel operation fails the entire list will be False.
        '''

        job_id_list = ' '.join(job_ids)
        retcode, stdout, stderr = self.execute_wait("qdel {0}".format(job_id_list))
        rets = None
        if retcode == 0:
            for jid in job_ids:
                self.resources[jid]['status'] = JobStatus(JobState.COMPLETED)  # Setting state to exiting
            rets = [True for i in job_ids]
        else:
            rets = [False for i in job_ids]

        return rets

    @property
    def status_polling_interval(self):
        return 60

if __name__ == "__main__":

    print("None")
<commit_msg>Remove stub main from PBS provider (#2188)<commit_after>import logging
import os
import time

from parsl.channels import LocalChannel
from parsl.launchers import AprunLauncher
from parsl.providers.provider_base import JobState, JobStatus
from parsl.providers.torque.template import template_string
from parsl.providers.cluster_provider import ClusterProvider
from parsl.utils import RepresentationMixin

logger = logging.getLogger(__name__)

# From the man pages for qstat for PBS/Torque systems
translate_table = {
    'B': JobState.RUNNING,  # This state is returned for running array jobs
    'R': JobState.RUNNING,
    'C': JobState.COMPLETED,  # Completed after having run
    'E': JobState.COMPLETED,  # Exiting after having run
    'H': JobState.HELD,  # Held
    'Q': JobState.PENDING,  # Queued, and eligible to run
    'W': JobState.PENDING,  # Job is waiting for it's execution time (-a option) to be reached
    'S': JobState.HELD  # Suspended
}

class TorqueProvider(ClusterProvider, RepresentationMixin):
    """Torque Execution Provider

    This provider uses sbatch to submit, squeue for status, and scancel to cancel
    jobs. The sbatch script to be used is created from a template file in this
    same module.

    Parameters
    ----------
    channel : Channel
        Channel for accessing this provider. Possible channels include
        :class:`~parsl.channels.LocalChannel` (the default),
        :class:`~parsl.channels.SSHChannel`, or
        :class:`~parsl.channels.SSHInteractiveLoginChannel`.
    account : str
        Account the job will be charged against.
    queue : str
        Torque queue to request blocks from.
    nodes_per_block : int
        Nodes to provision per block.
    init_blocks : int
        Number of blocks to provision at the start of the run. Default is 1.
    min_blocks : int
        Minimum number of blocks to maintain. Default is 0.
    max_blocks : int
        Maximum number of blocks to maintain.
    parallelism : float
        Ratio of provisioned task slots to active tasks. A parallelism value of 1 represents aggressive
        scaling where as many resources as possible are used; parallelism close to 0 represents
        the opposite situation in which as few resources as possible (i.e., min_blocks) are used.
    walltime : str
        Walltime requested per block in HH:MM:SS.
    scheduler_options : str
        String to prepend to the #PBS blocks in the submit script to the scheduler.
        WARNING: scheduler_options should only be given #PBS strings, and should not have trailing newlines.
    worker_init : str
        Command to be run before starting a worker, such as 'module load Anaconda; source activate env'.
    launcher : Launcher
        Launcher for this provider. Possible launchers include
        :class:`~parsl.launchers.AprunLauncher` (the default), or
        :class:`~parsl.launchers.SingleNodeLauncher`,

    """
    def __init__(self,
                 channel=LocalChannel(),
                 account=None,
                 queue=None,
                 scheduler_options='',
                 worker_init='',
                 nodes_per_block=1,
                 init_blocks=1,
                 min_blocks=0,
                 max_blocks=1,
                 parallelism=1,
                 launcher=AprunLauncher(),
                 walltime="00:20:00",
                 cmd_timeout=120):
        label = 'torque'
        super().__init__(label,
                         channel,
                         nodes_per_block,
                         init_blocks,
                         min_blocks,
                         max_blocks,
                         parallelism,
                         walltime,
                         launcher,
                         cmd_timeout=cmd_timeout)

        self.account = account
        self.queue = queue
        self.scheduler_options = scheduler_options
        self.worker_init = worker_init
        self.template_string = template_string

        # Dictionary that keeps track of jobs, keyed on job_id
        self.resources = {}

    def _status(self):
        ''' Internal: Do not call. Returns the status list for a list of job_ids

        Args:
              self

        Returns:
              [status...] : Status list of all jobs
        '''

        job_ids = list(self.resources.keys())
        job_id_list = ' '.join(self.resources.keys())

        jobs_missing = list(self.resources.keys())

        retcode, stdout, stderr = self.execute_wait("qstat {0}".format(job_id_list))
        for line in stdout.split('\n'):
            parts = line.split()
            if not parts or parts[0].upper().startswith('JOB') or parts[0].startswith('---'):
                continue
            job_id = parts[0]  # likely truncated
            for long_job_id in job_ids:
                if long_job_id.startswith(job_id):
                    logger.debug('coerced job_id %s -> %s', job_id, long_job_id)
                    job_id = long_job_id
                    break
            state = translate_table.get(parts[4], JobState.UNKNOWN)
            self.resources[job_id]['status'] = JobStatus(state)
            jobs_missing.remove(job_id)

        # squeue does not report on jobs that are not running. So we are filling in the
        # blanks for missing jobs, we might lose some information about why the jobs failed.
        for missing_job in jobs_missing:
            self.resources[missing_job]['status'] = JobStatus(JobState.COMPLETED)

    def submit(self, command, tasks_per_node, job_name="parsl.torque"):
        ''' Submits the command onto an Local Resource Manager job.
        Submit returns an ID that corresponds to the task that was just submitted.

        If tasks_per_node <  1 : ! This is illegal. tasks_per_node should be integer

        If tasks_per_node == 1:
             A single node is provisioned

        If tasks_per_node >  1 :
             tasks_per_node number of nodes are provisioned.

        Args:
             - command  :(String) Commandline invocation to be made on the remote side.
             - tasks_per_node (int) : command invocations to be launched per node

        Kwargs:
             - job_name (String): Name for job, must be unique

        Returns:
             - None: At capacity, cannot provision more
             - job_id: (string) Identifier for the job

        '''

        # Set job name
        job_name = "parsl.{0}.{1}".format(job_name, time.time())

        # Set script path
        script_path = "{0}/{1}.submit".format(self.script_dir, job_name)
        script_path = os.path.abspath(script_path)

        logger.debug("Requesting nodes_per_block:%s tasks_per_node:%s", self.nodes_per_block,
                     tasks_per_node)

        job_config = {}
        # TODO : script_path might need to change to accommodate script dir set via channels
        job_config["submit_script_dir"] = self.channel.script_dir
        job_config["nodes"] = self.nodes_per_block
        job_config["task_blocks"] = self.nodes_per_block * tasks_per_node
        job_config["nodes_per_block"] = self.nodes_per_block
        job_config["tasks_per_node"] = tasks_per_node
        job_config["walltime"] = self.walltime
        job_config["scheduler_options"] = self.scheduler_options
        job_config["worker_init"] = self.worker_init
        job_config["user_script"] = command

        # Wrap the command
        job_config["user_script"] = self.launcher(command,
                                                  tasks_per_node,
                                                  self.nodes_per_block)

        logger.debug("Writing submit script")
        self._write_submit_script(self.template_string, script_path, job_name, job_config)

        channel_script_path = self.channel.push_file(script_path, self.channel.script_dir)

        submit_options = ''
        if self.queue is not None:
            submit_options = '{0} -q {1}'.format(submit_options, self.queue)
        if self.account is not None:
            submit_options = '{0} -A {1}'.format(submit_options, self.account)

        launch_cmd = "qsub {0} {1}".format(submit_options, channel_script_path)
        retcode, stdout, stderr = self.execute_wait(launch_cmd)

        job_id = None
        if retcode == 0:
            for line in stdout.split('\n'):
                if line.strip():
                    job_id = line.strip()
                    self.resources[job_id] = {'job_id': job_id, 'status': JobStatus(JobState.PENDING)}
        else:
            message = "Command '{}' failed with return code {}".format(launch_cmd, retcode)
            if (stdout is not None) and (stderr is not None):
                message += "\nstderr:{}\nstdout{}".format(stderr.strip(), stdout.strip())
            logger.error(message)

        return job_id

    def cancel(self, job_ids):
        ''' Cancels the jobs specified by a list of job ids

        Args:
        job_ids : [<job_id> ...]

        Returns :
        [True/False...] : If the cancel operation fails the entire list will be False.
        '''

        job_id_list = ' '.join(job_ids)
        retcode, stdout, stderr = self.execute_wait("qdel {0}".format(job_id_list))
        rets = None
        if retcode == 0:
            for jid in job_ids:
                self.resources[jid]['status'] = JobStatus(JobState.COMPLETED)  # Setting state to exiting
            rets = [True for i in job_ids]
        else:
            rets = [False for i in job_ids]

        return rets

    @property
    def status_polling_interval(self):
        return 60
<|endoftext|>


<commit_before># from __future__ import print_function
from pprint import pprint
from flask import Blueprint, render_template, flash, redirect, url_for
from sqlalchemy.exc import IntegrityError

from app import db, Add
from app.connection import Connection
from app.helper import get_kwargs, portify, init_form
from .forms import EventForm, EventTypeForm, PriceForm, CommodityForm
from .models import Event, EventType, Price, Commodity, CommodityType

hermes = Blueprint('hermes', __name__)

def _bookmark(table):
	global __TABLE__
	__TABLE__ = table

@hermes.route('/<table>/', methods=['GET', 'POST'])
def get(table):
	table_as_class = table.title().replace('_', '')
	form = init_form(eval('%sForm' % table_as_class))
	site = portify(url_for('api', _external=True))
	conn = Connection(site, display=True)
	kwargs = get_kwargs(str(table), 'hermes', conn, form)
	return render_template('entry.html', **kwargs)

class AddHermes(Add):
	def get_vars(self):
		table_as_class = table.title().replace('_', '')
		form = init_form(eval('%sForm' % table_as_class))
		entry = eval('%s()' % table_as_class)
		redir = 'hermes.get'
		return form, entry, redir

	def bookmark_table(self, table):
		_bookmark(table)

@hermes.errorhandler(409)
@hermes.errorhandler(IntegrityError)
def duplicate_values(e):
	flash('Error: %s' % e.orig[0], 'alert alert-error')
	return redirect(url_for('.get', table=__TABLE__))

hermes.add_url_rule(
	'/add/<table>/', view_func=AddHermes.as_view('about_page'),
	methods=['GET', 'POST'])
<commit_msg>Hermes house cleaning<commit_after># from __future__ import print_function
from pprint import pprint
from flask import Blueprint, render_template, flash, redirect, url_for
from sqlalchemy.exc import IntegrityError

from app import Add
from app.connection import Connection
from app.helper import get_kwargs, portify, init_form
from .forms import EventForm, EventTypeForm, PriceForm, CommodityForm
from .models import Event, EventType, Price, Commodity, CommodityType

hermes = Blueprint('hermes', __name__)

def _bookmark(table):
	global __TABLE__
	__TABLE__ = table

@hermes.route('/<table>/', methods=['GET', 'POST'])
def get(table):
	table_as_class = table.title().replace('_', '')
	form = init_form(eval('%sForm' % table_as_class))
	site = portify(url_for('api', _external=True))
	conn = Connection(site, display=True)
	kwargs = get_kwargs(str(table), 'hermes', conn, form)
	return render_template('entry.html', **kwargs)

class AddHermes(Add):
	def get_vars(self):
		table_as_class = table.title().replace('_', '')
		form = init_form(eval('%sForm' % table_as_class))
		entry = eval('%s()' % table_as_class)
		redir = '.get'
		return form, entry, redir

	def bookmark_table(self, table):
		_bookmark(table)

@hermes.errorhandler(409)
@hermes.errorhandler(IntegrityError)
def duplicate_values(e):
	flash('Error: %s' % e.orig[0], 'alert alert-error')
	return redirect(url_for('.get', table=__TABLE__))

hermes.add_url_rule(
	'/add/<table>/', view_func=AddHermes.as_view('add'),
	methods=['GET', 'POST'])
<|endoftext|>