<commit_before>import json

import requests

LEG_WITH_SOCKET = [
    132369, 132410, 137044, 132444, 132449, 132452, 132460, 133973, 133974, 137037, 137038, 137039, 137040,
    137041, 137042, 137043, 132378, 137045, 137046, 137047, 137048, 137049, 137050, 137051, 137052, 137054, 137055,
    137220, 137223, 137276, 137382, 138854
]

ENCHANTABLE_SLOTS = ["neck", "back", "finger1", "finger2"]

config = json.loads(open('config.json').read())  # Load Configs
API_KEY = config["blizzard_api_key"]
default_region = config["default_region"]

region_locale = {
    'us': ['us', 'en_US', 'en'],
#    'kr': ['kr', 'ko_KR', 'ko'],
#    'tw': ['tw', 'zh_TW', 'zh'],
    'eu': ['eu', 'en_GB', 'en']
}

def get_sockets(player_dictionary):
    """
    Return dict with total sockets and count of equipped gems and slots that are missing

    :param player_dictionary: Retrieved player dict from API
    :return: dict()
    """
    sockets = 0
    equipped_gems = 0

    for item in player_dictionary["items"]:
        if item in "averageItemLevel" or item in "averageItemLevelEquipped":
            continue

        if int(player_dictionary["items"][item]["id"]) in LEG_WITH_SOCKET:
            sockets += 1

        for bonus in player_dictionary["items"][item]["bonusLists"]:
            if bonus == 1808:  # 1808 is Legion prismatic socket bonus
                sockets += 1

        for ttip in player_dictionary["items"][item]["tooltipParams"]:
            if item in "mainHand" or item in "offHand":  # Ignore Relic
                continue
            if "gem" in ttip:  # Equipped gems are listed as gem0, gem1, etc...
                equipped_gems += 1

    return {"total_sockets": sockets,
            "equipped_gems": equipped_gems}

def get_enchants(player_dictionary):
    """
    Get count of enchants missing and slots that are missing
    :param player_dictionary:
    :return: dict()
    """
    missing_enchant_slots = []
    for slot in ENCHANTABLE_SLOTS:
        if "enchant" not in player_dictionary["items"][slot]["tooltipParams"]:
            missing_enchant_slots.append(slot)

    return {
        "enchantable_slots": len(ENCHANTABLE_SLOTS),
        "missing_slots": missing_enchant_slots,
        "total_missing": len(missing_enchant_slots)
    }

def get_raid_progression(player_dictionary, raid):
    r = [x for x in player_dictionary["progression"]
    ["raids"] if x["name"] in raid][0]
    normal = 0
    heroic = 0
    mythic = 0

    for boss in r["bosses"]:
        if boss["normalKills"] > 0:
            normal += 1
        if boss["heroicKills"] > 0:
            heroic += 1
        if boss["mythicKills"] > 0:
            mythic += 1

    return {"normal": normal,
            "heroic": heroic,
            "mythic": mythic,
            "total_bosses": len(r["bosses"])}

def get_mythic_progression(player_dictionary):
    achievements = player_dictionary["achievements"]
    plus_two = 0
    plus_five = 0
    plus_ten = 0

    if 33096 in achievements["criteria"]:
        index = achievements["criteria"].index(33096)
        plus_two = achievements["criteriaQuantity"][index]

    if 33097 in achievements["criteria"]:
        index = achievements["criteria"].index(33097)
        plus_five = achievements["criteriaQuantity"][index]

    if 33098 in achievements["criteria"]:
        index = achievements["criteria"].index(33098)
        plus_ten = achievements["criteriaQuantity"][index]

    return {
        "plus_two": plus_two,
        "plus_five": plus_five,
        "plus_ten": plus_ten
    }

def get_mythic_progression(player_dictionary):
    achievements = player_dictionary["achievements"]
    plus_two = 0
    plus_five = 0
    plus_ten = 0

    if 33096 in achievements["criteria"]:
        index = achievements["criteria"].index(33096)
        plus_two = achievements["criteriaQuantity"][index]

    if 33097 in achievements["criteria"]:
        index = achievements["criteria"].index(33097)
        plus_five = achievements["criteriaQuantity"][index]

    if 33098 in achievements["criteria"]:
        index = achievements["criteria"].index(33098)
        plus_ten = achievements["criteriaQuantity"][index]

    return {
        "plus_two": plus_two,
        "plus_five": plus_five,
        "plus_ten": plus_ten
    }

def get_char(name, server):
    r = requests.get(
<<<<<<< HEAD
        "https://%s.api.battle.net/wow/character/%s/%s?fields=items+progression+achievements&locale=%s&apikey=%s" % (
            region_locale[target_region][0], server, name, region_locale[target_region][1], API_KEY))
=======
        "https://us.api.battle.net/wow/character/%s/%s?fields=items+progression+achievements&locale=en_US&apikey=%s" % (
            server, name, API_KEY))
>>>>>>> 869337f486d04a9e069ac94a5353ce7d90474011
    if r.status_code != 200:
        raise Exception("Could Not Find Character (No 200 from API)")

    player_dict = json.loads(r.text)

    r = requests.get(
        "https://%s.api.battle.net/wow/data/character/classes?locale=%s&apikey=%s" % (
            region_locale[target_region][0], region_locale[target_region][1], API_KEY))
    if r.status_code != 200:
        raise Exception("Could Not Find Character Classes (No 200 From API)")
    class_dict = json.loads(r.text)
    class_dict = {c['id']: c['name'] for c in class_dict["classes"]}

    equipped_ivl = player_dict["items"]["averageItemLevelEquipped"]
    sockets = get_sockets(player_dict)
    enchants = get_enchants(player_dict)
    tov_progress = get_raid_progression(player_dict, "Trial of Valor")
    en_progress = get_raid_progression(player_dict, "The Emerald Nightmare")
    mythic_progress = get_mythic_progression(player_dict)

    armory_url = 'http://{}.battle.net/wow/{}/character/{}/{}/advanced'.format(
        region_locale[target_region][0], region_locale[target_region][2], server, name)

    return_string = ''
    return_string += "**%s** - **%s** - **%s %s**\n" % (
        name.title(), server.title(), player_dict['level'], class_dict[player_dict['class']])
    return_string += '<{}>\n'.format(armory_url)
    return_string += '```CSS\n'  # start Markdown

    # iLvL
    return_string += "Equipped Item Level: %s\n" % equipped_ivl

    # Mythic Progression
    return_string += "Mythics: +2: %s, +5: %s, +10: %s\n" % (mythic_progress["plus_two"],
                                                             mythic_progress["plus_five"],
                                                             mythic_progress["plus_ten"])

    # Raid Progression
    return_string += "EN: {1}/{0} (N), {2}/{0} (H), {3}/{0} (M)\n".format(en_progress["total_bosses"],
                                                                          en_progress["normal"],
                                                                          en_progress["heroic"],
                                                                          en_progress["mythic"])
    return_string += "TOV: {1}/{0} (N), {2}/{0} (H), {3}/{0} (M)\n".format(tov_progress["total_bosses"],
                                                                           tov_progress["normal"],
                                                                           tov_progress["heroic"],
                                                                           tov_progress["mythic"])

    # Gems
    return_string += "Gems Equipped: %s/%s\n" % (
        sockets["equipped_gems"], sockets["total_sockets"])

    # Enchants
    return_string += "Enchants: %s/%s\n" % (enchants["enchantable_slots"] - enchants["total_missing"],
                                            enchants["enchantable_slots"])
    if enchants["total_missing"] > 0:
        return_string += "Missing Enchants: {0}".format(
            ", ".join(enchants["missing_slots"]))

    return_string += '```'  # end Markdown
    return return_string

async def pug(client, message):
    target_region = default_region
    try:
        i = str(message.content).split(' ')
        name = i[1]
        server = i[2]
        if len(i) == 4 and i[3].lower() in region_locale.keys():
            target_region = i[3].lower()
        character_info = get_char(name, server, target_region)
        await client.send_message(message.channel, character_info)
    except Exception as e:
        await client.send_message(message.channel, "Error With Name or Server\n"
                                                   "Use: !pug <name> <server>\n"
                                                   "Hyphenate Two Word Servers (Ex: Twisting-Nether)")
<commit_msg>Fixed merge conflict with Blizzazrd API call<commit_after>import json

import requests

LEG_WITH_SOCKET = [
    132369, 132410, 137044, 132444, 132449, 132452, 132460, 133973, 133974, 137037, 137038, 137039, 137040,
    137041, 137042, 137043, 132378, 137045, 137046, 137047, 137048, 137049, 137050, 137051, 137052, 137054, 137055,
    137220, 137223, 137276, 137382, 138854
]

ENCHANTABLE_SLOTS = ["neck", "back", "finger1", "finger2"]

config = json.loads(open('config.json').read())  # Load Configs
API_KEY = config["blizzard_api_key"]
default_region = config["default_region"]

region_locale = {
    'us': ['us', 'en_US', 'en'],
#    'kr': ['kr', 'ko_KR', 'ko'],
#    'tw': ['tw', 'zh_TW', 'zh'],
    'eu': ['eu', 'en_GB', 'en']
}

def get_sockets(player_dictionary):
    """
    Return dict with total sockets and count of equipped gems and slots that are missing

    :param player_dictionary: Retrieved player dict from API
    :return: dict()
    """
    sockets = 0
    equipped_gems = 0

    for item in player_dictionary["items"]:
        if item in "averageItemLevel" or item in "averageItemLevelEquipped":
            continue

        if int(player_dictionary["items"][item]["id"]) in LEG_WITH_SOCKET:
            sockets += 1

        for bonus in player_dictionary["items"][item]["bonusLists"]:
            if bonus == 1808:  # 1808 is Legion prismatic socket bonus
                sockets += 1

        for ttip in player_dictionary["items"][item]["tooltipParams"]:
            if item in "mainHand" or item in "offHand":  # Ignore Relic
                continue
            if "gem" in ttip:  # Equipped gems are listed as gem0, gem1, etc...
                equipped_gems += 1

    return {"total_sockets": sockets,
            "equipped_gems": equipped_gems}

def get_enchants(player_dictionary):
    """
    Get count of enchants missing and slots that are missing
    :param player_dictionary:
    :return: dict()
    """
    missing_enchant_slots = []
    for slot in ENCHANTABLE_SLOTS:
        if "enchant" not in player_dictionary["items"][slot]["tooltipParams"]:
            missing_enchant_slots.append(slot)

    return {
        "enchantable_slots": len(ENCHANTABLE_SLOTS),
        "missing_slots": missing_enchant_slots,
        "total_missing": len(missing_enchant_slots)
    }

def get_raid_progression(player_dictionary, raid):
    r = [x for x in player_dictionary["progression"]
    ["raids"] if x["name"] in raid][0]
    normal = 0
    heroic = 0
    mythic = 0

    for boss in r["bosses"]:
        if boss["normalKills"] > 0:
            normal += 1
        if boss["heroicKills"] > 0:
            heroic += 1
        if boss["mythicKills"] > 0:
            mythic += 1

    return {"normal": normal,
            "heroic": heroic,
            "mythic": mythic,
            "total_bosses": len(r["bosses"])}

def get_mythic_progression(player_dictionary):
    achievements = player_dictionary["achievements"]
    plus_two = 0
    plus_five = 0
    plus_ten = 0

    if 33096 in achievements["criteria"]:
        index = achievements["criteria"].index(33096)
        plus_two = achievements["criteriaQuantity"][index]

    if 33097 in achievements["criteria"]:
        index = achievements["criteria"].index(33097)
        plus_five = achievements["criteriaQuantity"][index]

    if 33098 in achievements["criteria"]:
        index = achievements["criteria"].index(33098)
        plus_ten = achievements["criteriaQuantity"][index]

    return {
        "plus_two": plus_two,
        "plus_five": plus_five,
        "plus_ten": plus_ten
    }

def get_mythic_progression(player_dictionary):
    achievements = player_dictionary["achievements"]
    plus_two = 0
    plus_five = 0
    plus_ten = 0

    if 33096 in achievements["criteria"]:
        index = achievements["criteria"].index(33096)
        plus_two = achievements["criteriaQuantity"][index]

    if 33097 in achievements["criteria"]:
        index = achievements["criteria"].index(33097)
        plus_five = achievements["criteriaQuantity"][index]

    if 33098 in achievements["criteria"]:
        index = achievements["criteria"].index(33098)
        plus_ten = achievements["criteriaQuantity"][index]

    return {
        "plus_two": plus_two,
        "plus_five": plus_five,
        "plus_ten": plus_ten
    }

def get_char(name, server):
    r = requests.get("https://%s.api.battle.net/wow/character/%s/%s?fields=items+progression+achievements&locale=%s&apikey=%s" % (
            region_locale[target_region][0], server, name, region_locale[target_region][1], API_KEY))

    if r.status_code != 200:
        raise Exception("Could Not Find Character (No 200 from API)")

    player_dict = json.loads(r.text)

    r = requests.get(
        "https://%s.api.battle.net/wow/data/character/classes?locale=%s&apikey=%s" % (
            region_locale[target_region][0], region_locale[target_region][1], API_KEY))
    if r.status_code != 200:
        raise Exception("Could Not Find Character Classes (No 200 From API)")
    class_dict = json.loads(r.text)
    class_dict = {c['id']: c['name'] for c in class_dict["classes"]}

    equipped_ivl = player_dict["items"]["averageItemLevelEquipped"]
    sockets = get_sockets(player_dict)
    enchants = get_enchants(player_dict)
    tov_progress = get_raid_progression(player_dict, "Trial of Valor")
    en_progress = get_raid_progression(player_dict, "The Emerald Nightmare")
    mythic_progress = get_mythic_progression(player_dict)

    armory_url = 'http://{}.battle.net/wow/{}/character/{}/{}/advanced'.format(
        region_locale[target_region][0], region_locale[target_region][2], server, name)

    return_string = ''
    return_string += "**%s** - **%s** - **%s %s**\n" % (
        name.title(), server.title(), player_dict['level'], class_dict[player_dict['class']])
    return_string += '<{}>\n'.format(armory_url)
    return_string += '```CSS\n'  # start Markdown

    # iLvL
    return_string += "Equipped Item Level: %s\n" % equipped_ivl

    # Mythic Progression
    return_string += "Mythics: +2: %s, +5: %s, +10: %s\n" % (mythic_progress["plus_two"],
                                                             mythic_progress["plus_five"],
                                                             mythic_progress["plus_ten"])

    # Raid Progression
    return_string += "EN: {1}/{0} (N), {2}/{0} (H), {3}/{0} (M)\n".format(en_progress["total_bosses"],
                                                                          en_progress["normal"],
                                                                          en_progress["heroic"],
                                                                          en_progress["mythic"])
    return_string += "TOV: {1}/{0} (N), {2}/{0} (H), {3}/{0} (M)\n".format(tov_progress["total_bosses"],
                                                                           tov_progress["normal"],
                                                                           tov_progress["heroic"],
                                                                           tov_progress["mythic"])

    # Gems
    return_string += "Gems Equipped: %s/%s\n" % (
        sockets["equipped_gems"], sockets["total_sockets"])

    # Enchants
    return_string += "Enchants: %s/%s\n" % (enchants["enchantable_slots"] - enchants["total_missing"],
                                            enchants["enchantable_slots"])
    if enchants["total_missing"] > 0:
        return_string += "Missing Enchants: {0}".format(
            ", ".join(enchants["missing_slots"]))

    return_string += '```'  # end Markdown
    return return_string

async def pug(client, message):
    target_region = default_region
    try:
        i = str(message.content).split(' ')
        name = i[1]
        server = i[2]
        if len(i) == 4 and i[3].lower() in region_locale.keys():
            target_region = i[3].lower()
        character_info = get_char(name, server, target_region)
        await client.send_message(message.channel, character_info)
    except Exception as e:
        await client.send_message(message.channel, "Error With Name or Server\n"
                                                   "Use: !pug <name> <server> <region>\n"
                                                   "Hyphenate Two Word Servers (Ex: Twisting-Nether)")
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
'''
This module is a central location for all salt exceptions
'''

# Import python libs
import copy
import salt.exitcodes

class SaltException(Exception):
    '''
    Base exception class; all Salt-specific exceptions should subclass this
    '''

class SaltClientError(SaltException):
    '''
    Problem reading the master root key
    '''

class SaltMasterError(SaltException):
    '''
    Problem reading the master root key
    '''

class SaltSyndicMasterError(SaltException):
    '''
    Problem while proxying a request in the syndication master
    '''

class MasterExit(SystemExit):
    '''
    Rise when the master exits
    '''

class AuthenticationError(SaltException):
    '''
    If sha256 signature fails during decryption
    '''

class CommandNotFoundError(SaltException):
    '''
    Used in modules or grains when a required binary is not available
    '''

class CommandExecutionError(SaltException):
    '''
    Used when a module runs a command which returns an error and wants
    to show the user the output gracefully instead of dying
    '''

class LoaderError(SaltException):
    '''
    Problems loading the right renderer
    '''

class MinionError(SaltException):
    '''
    Minion problems reading uris such as salt:// or http://
    '''

class SaltInvocationError(SaltException, TypeError):
    '''
    Used when the wrong number of arguments are sent to modules or invalid
    arguments are specified on the command line
    '''

class PkgParseError(SaltException):
    '''
    Used when of the pkg modules cannot correctly parse the output from
    the CLI tool (pacman, yum, apt, aptitude, etc)
    '''

class SaltRenderError(SaltException):
    '''
    Used when a renderer needs to raise an explicit error. If a line number and
    buffer string are passed, get_context will be invoked to get the location
    of the error.
    '''
    def __init__(self,
                 error,
                 line_num=None,
                 buf='',
                 marker='    <======================',
                 trace=None):
        self.error = error
        exc_str = copy.deepcopy(error)
        self.line_num = line_num
        self.buffer = buf
        self.context = ''
        if trace:
            exc_str += '\n{0}\n'.format(trace)
        if self.line_num and self.buffer:

            import salt.utils
            self.context = salt.utils.get_context(
                self.buffer,
                self.line_num,
                marker=marker
            )
            exc_str += '; line {0}\n\n{1}'.format(
                self.line_num,
                self.context
            )
        SaltException.__init__(self, exc_str)

class SaltClientTimeout(SaltException):
    '''
    Thrown when a job sent through one of the *Client interfaces times out

    Takes the ``jid`` as a parameter
    '''
    def __init__(self, msg, jid=None, *args, **kwargs):
        super(SaltClientTimeout, self).__init__(msg, *args, **kwargs)
        self.jid = jid

class SaltReqTimeoutError(SaltException):
    '''
    Thrown when a salt master request call fails to return within the timeout
    '''

class TimedProcTimeoutError(SaltException):
    '''
    Thrown when a timed subprocess does not terminate within the timeout,
    or if the specified timeout is not an int or a float
    '''

class EauthAuthenticationError(SaltException):
    '''
    Thrown when eauth authentication fails
    '''

class TokenAuthenticationError(SaltException):
    '''
    Thrown when token authentication fails
    '''

class AuthorizationError(SaltException):
    '''
    Thrown when runner or wheel execution fails due to permissions
    '''

class SaltRunnerError(SaltException):
    '''
    Problem in runner
    '''

class SaltWheelError(SaltException):
    '''
    Problem in wheel
    '''

class SaltSystemExit(SystemExit):
    '''
    This exception is raised when an unsolvable problem is found. There's
    nothing else to do, salt should just exit.
    '''
    def __init__(self, code=0, msg=None):
        SystemExit.__init__(self, code)
        if msg:
            self.message = msg

class SaltCloudException(SaltException):
    '''
    Generic Salt Cloud Exception
    '''

class SaltCloudSystemExit(SaltCloudException):
    '''
    This exception is raised when the execution should be stopped.
    '''
    def __init__(self, message, exit_code=salt.exitcodes.EX_GENERIC):
        SaltCloudException.__init__(self, message)
        self.message = message
        self.exit_code = exit_code

class SaltCloudConfigError(SaltCloudException):
    '''
    Raised when a configuration setting is not found and should exist.
    '''

class SaltCloudNotFound(SaltCloudException):
    '''
    Raised when some cloud provider function cannot find what's being searched.
    '''

class SaltCloudExecutionTimeout(SaltCloudException):
    '''
    Raised when too much time has passed while querying/waiting for data.
    '''

class SaltCloudExecutionFailure(SaltCloudException):
    '''
    Raised when too much failures have occurred while querying/waiting for data.
    '''

class SaltCloudPasswordError(SaltCloudException):
    '''
    Raise when virtual terminal password input failed
    '''
<commit_msg>Fix sphinx build error<commit_after># -*- coding: utf-8 -*-
'''
This module is a central location for all salt exceptions
'''

# Import python libs
import copy
import salt.exitcodes

class SaltException(Exception):
    '''
    Base exception class; all Salt-specific exceptions should subclass this
    '''

class SaltClientError(SaltException):
    '''
    Problem reading the master root key
    '''

class SaltMasterError(SaltException):
    '''
    Problem reading the master root key
    '''

class SaltSyndicMasterError(SaltException):
    '''
    Problem while proxying a request in the syndication master
    '''

class MasterExit(SystemExit):
    '''
    Rise when the master exits
    '''

class AuthenticationError(SaltException):
    '''
    If sha256 signature fails during decryption
    '''

class CommandNotFoundError(SaltException):
    '''
    Used in modules or grains when a required binary is not available
    '''

class CommandExecutionError(SaltException):
    '''
    Used when a module runs a command which returns an error and wants
    to show the user the output gracefully instead of dying
    '''

class LoaderError(SaltException):
    '''
    Problems loading the right renderer
    '''

class MinionError(SaltException):
    '''
    Minion problems reading uris such as salt:// or http://
    '''

class SaltInvocationError(SaltException, TypeError):
    '''
    Used when the wrong number of arguments are sent to modules or invalid
    arguments are specified on the command line
    '''

class PkgParseError(SaltException):
    '''
    Used when of the pkg modules cannot correctly parse the output from
    the CLI tool (pacman, yum, apt, aptitude, etc)
    '''

class SaltRenderError(SaltException):
    '''
    Used when a renderer needs to raise an explicit error. If a line number and
    buffer string are passed, get_context will be invoked to get the location
    of the error.
    '''
    def __init__(self,
                 error,
                 line_num=None,
                 buf='',
                 marker='    <======================',
                 trace=None):
        self.error = error
        exc_str = copy.deepcopy(error)
        self.line_num = line_num
        self.buffer = buf
        self.context = ''
        if trace:
            exc_str += '\n{0}\n'.format(trace)
        if self.line_num and self.buffer:

            import salt.utils
            self.context = salt.utils.get_context(
                self.buffer,
                self.line_num,
                marker=marker
            )
            exc_str += '; line {0}\n\n{1}'.format(
                self.line_num,
                self.context
            )
        SaltException.__init__(self, exc_str)

class SaltClientTimeout(SaltException):
    '''
    Thrown when a job sent through one of the Client interfaces times out

    Takes the ``jid`` as a parameter
    '''
    def __init__(self, msg, jid=None, *args, **kwargs):
        super(SaltClientTimeout, self).__init__(msg, *args, **kwargs)
        self.jid = jid

class SaltReqTimeoutError(SaltException):
    '''
    Thrown when a salt master request call fails to return within the timeout
    '''

class TimedProcTimeoutError(SaltException):
    '''
    Thrown when a timed subprocess does not terminate within the timeout,
    or if the specified timeout is not an int or a float
    '''

class EauthAuthenticationError(SaltException):
    '''
    Thrown when eauth authentication fails
    '''

class TokenAuthenticationError(SaltException):
    '''
    Thrown when token authentication fails
    '''

class AuthorizationError(SaltException):
    '''
    Thrown when runner or wheel execution fails due to permissions
    '''

class SaltRunnerError(SaltException):
    '''
    Problem in runner
    '''

class SaltWheelError(SaltException):
    '''
    Problem in wheel
    '''

class SaltSystemExit(SystemExit):
    '''
    This exception is raised when an unsolvable problem is found. There's
    nothing else to do, salt should just exit.
    '''
    def __init__(self, code=0, msg=None):
        SystemExit.__init__(self, code)
        if msg:
            self.message = msg

class SaltCloudException(SaltException):
    '''
    Generic Salt Cloud Exception
    '''

class SaltCloudSystemExit(SaltCloudException):
    '''
    This exception is raised when the execution should be stopped.
    '''
    def __init__(self, message, exit_code=salt.exitcodes.EX_GENERIC):
        SaltCloudException.__init__(self, message)
        self.message = message
        self.exit_code = exit_code

class SaltCloudConfigError(SaltCloudException):
    '''
    Raised when a configuration setting is not found and should exist.
    '''

class SaltCloudNotFound(SaltCloudException):
    '''
    Raised when some cloud provider function cannot find what's being searched.
    '''

class SaltCloudExecutionTimeout(SaltCloudException):
    '''
    Raised when too much time has passed while querying/waiting for data.
    '''

class SaltCloudExecutionFailure(SaltCloudException):
    '''
    Raised when too much failures have occurred while querying/waiting for data.
    '''

class SaltCloudPasswordError(SaltCloudException):
    '''
    Raise when virtual terminal password input failed
    '''
<|endoftext|>


<commit_before>from pyld import jsonld
from jsonvalue import JsonValue, types
from jsonvalue import schemaorg
from datetime import datetime, date, time
import pytest

def test_expand_to_values_no_converters():
    d = {
        '@context': {
            'foo': {
                '@id': 'http://example.com/foo',
                '@type': 'http://example.com/date'
            }
        },
        'foo': '2010-01-01'
    }

    info = JsonValue()
    assert info.expand_to_values(d, d['@context']) == jsonld.expand(d)

def test_expand_to_values_converter():
    d = {
        '@context': {
            'foo': {
                '@id': 'http://example.com/foo',
                '@type': 'http://example.com/date'
            }
        },
        'foo': '2010-01-01'
    }

    info = JsonValue()

    info.type('http://example.com/date', schemaorg.Date)

    expanded = info.expand_to_values(d, d['@context'])

    assert expanded == [
        {
            u'http://example.com/foo':
            [{'@type': u'http://example.com/date',
              '@value': date(2010, 1, 1)}]
        }
    ]

    assert info.compact_from_values(expanded, d['@context']) == d

def test_to_values():
    d = {
        '@context': {
            'foo': {
                '@id': 'http://example.com/foo',
                '@type': 'http://example.com/date'
            }
        },
        'foo': '2010-01-01'
    }

    info = JsonValue()
    info.type('http://example.com/date', schemaorg.Date)

    values = info.to_values(d, d['@context'])
    assert values == {
        '@context': {
            'foo': {
                '@id': 'http://example.com/foo',
                '@type': 'http://example.com/date'
            }
        },
        'foo': date(2010, 1, 1)
    }

    assert info.from_values(values) == d

SCHEMA_ORG_DATA_TYPES_CONTEXT = types(dict(
    a=schemaorg.Boolean,
    b=schemaorg.Number,
    c=schemaorg.Float,
    d=schemaorg.Integer,
    e=schemaorg.Text,
    f=schemaorg.URL,
    g=schemaorg.Date,
    h=schemaorg.DateTime,
    i=schemaorg.Time
))

def test_schema_org_data_type_vocabulary_from_values():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    plain = jv.from_values(dict(
        a=True,
        b=1.2,
        c=1.4,
        d=2,
        e=u"Hello",
        f=u"http://www.example.com",
        g=date(2010, 10, 1),
        h=datetime(2011, 7, 21, 14, 32, 10),
        i=time(16, 20, 10),
    ), context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

    assert plain == dict(
        a=True,
        b=1.2,
        c=1.4,
        d=2,
        e=u"Hello",
        f=u"http://www.example.com",
        g='2010-10-01',
        h='2011-07-21T14:32:10',
        i='16:20:10',
    )

def test_schema_org_data_type_vocabulary_to_values():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    values = jv.to_values(dict(
        a=True,
        b=1.2,
        c=1.4,
        d=2,
        e=u"Hello",
        f=u"http://www.example.com",
        g='2010-10-01',
        h='2011-07-21T14:32:10',
        i='16:20:10',
    ), context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

    assert values == dict(
        a=True,
        b=1.2,
        c=1.4,
        d=2,
        e=u"Hello",
        f=u"http://www.example.com",
        g=date(2010, 10, 1),
        h=datetime(2011, 7, 21, 14, 32, 10),
        i=time(16, 20, 10),
    )

def test_schema_org_none_from_values():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    plain = jv.from_values(dict(
        a=None,
        b=None,
        c=None,
        d=None,
        e=None,
        f=None,
        g=None,
        h=None,
        i=None,
    ), context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

    # all Nones are removed by expansion
    assert plain == {}

def test_schema_org_none_to_values():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    values = jv.to_values(dict(
        a=None,
        b=None,
        c=None,
        d=None,
        e=None,
        f=None,
        g=None,
        h=None,
        i=None,
    ), context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

    # all Nones are removed by expansion
    assert values == {}

def test_schema_org_data_type_boolean_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(a='wrong'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_number_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(b='wrong'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_float_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(c='wrong'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_integer_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(d='wrong'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(d=1.1),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_text_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(e=1),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_url_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(f=1),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_date_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(g=datetime(2010, 1, 1)),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_datetime_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(h=date(2010, 1, 1)),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_time_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(i=date(2010, 1, 1)),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)
<commit_msg>A whole bunch of load error checks.<commit_after>from pyld import jsonld
from jsonvalue import JsonValue, types
from jsonvalue import schemaorg
from datetime import datetime, date, time
import pytest

def test_expand_to_values_no_converters():
    d = {
        '@context': {
            'foo': {
                '@id': 'http://example.com/foo',
                '@type': 'http://example.com/date'
            }
        },
        'foo': '2010-01-01'
    }

    info = JsonValue()
    assert info.expand_to_values(d, d['@context']) == jsonld.expand(d)

def test_expand_to_values_converter():
    d = {
        '@context': {
            'foo': {
                '@id': 'http://example.com/foo',
                '@type': 'http://example.com/date'
            }
        },
        'foo': '2010-01-01'
    }

    info = JsonValue()

    info.type('http://example.com/date', schemaorg.Date)

    expanded = info.expand_to_values(d, d['@context'])

    assert expanded == [
        {
            u'http://example.com/foo':
            [{'@type': u'http://example.com/date',
              '@value': date(2010, 1, 1)}]
        }
    ]

    assert info.compact_from_values(expanded, d['@context']) == d

def test_to_values():
    d = {
        '@context': {
            'foo': {
                '@id': 'http://example.com/foo',
                '@type': 'http://example.com/date'
            }
        },
        'foo': '2010-01-01'
    }

    info = JsonValue()
    info.type('http://example.com/date', schemaorg.Date)

    values = info.to_values(d, d['@context'])
    assert values == {
        '@context': {
            'foo': {
                '@id': 'http://example.com/foo',
                '@type': 'http://example.com/date'
            }
        },
        'foo': date(2010, 1, 1)
    }

    assert info.from_values(values) == d

SCHEMA_ORG_DATA_TYPES_CONTEXT = types(dict(
    a=schemaorg.Boolean,
    b=schemaorg.Number,
    c=schemaorg.Float,
    d=schemaorg.Integer,
    e=schemaorg.Text,
    f=schemaorg.URL,
    g=schemaorg.Date,
    h=schemaorg.DateTime,
    i=schemaorg.Time
))

def test_schema_org_data_type_vocabulary_from_values():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    plain = jv.from_values(dict(
        a=True,
        b=1.2,
        c=1.4,
        d=2,
        e=u"Hello",
        f=u"http://www.example.com",
        g=date(2010, 10, 1),
        h=datetime(2011, 7, 21, 14, 32, 10),
        i=time(16, 20, 10),
    ), context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

    assert plain == dict(
        a=True,
        b=1.2,
        c=1.4,
        d=2,
        e=u"Hello",
        f=u"http://www.example.com",
        g='2010-10-01',
        h='2011-07-21T14:32:10',
        i='16:20:10',
    )

def test_schema_org_data_type_vocabulary_to_values():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    values = jv.to_values(dict(
        a=True,
        b=1.2,
        c=1.4,
        d=2,
        e=u"Hello",
        f=u"http://www.example.com",
        g='2010-10-01',
        h='2011-07-21T14:32:10',
        i='16:20:10',
    ), context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

    assert values == dict(
        a=True,
        b=1.2,
        c=1.4,
        d=2,
        e=u"Hello",
        f=u"http://www.example.com",
        g=date(2010, 10, 1),
        h=datetime(2011, 7, 21, 14, 32, 10),
        i=time(16, 20, 10),
    )

def test_schema_org_none_from_values():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    plain = jv.from_values(dict(
        a=None,
        b=None,
        c=None,
        d=None,
        e=None,
        f=None,
        g=None,
        h=None,
        i=None,
    ), context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

    # all Nones are removed by expansion
    assert plain == {}

def test_schema_org_none_to_values():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    values = jv.to_values(dict(
        a=None,
        b=None,
        c=None,
        d=None,
        e=None,
        f=None,
        g=None,
        h=None,
        i=None,
    ), context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

    # all Nones are removed by expansion
    assert values == {}

def test_schema_org_data_type_dump_boolean_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(a='wrong'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_dump_number_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(b='wrong'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_dump_float_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(c='wrong'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_dump_integer_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(d='wrong'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(d=1.1),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_dump_text_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(e=1),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_dump_url_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(f=1),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_dump_date_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(g=datetime(2010, 1, 1)),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_dump_datetime_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(h=date(2010, 1, 1)),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_dump_time_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.from_values(
            dict(i=date(2010, 1, 1)),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_load_boolean_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.to_values(
            dict(a='wrong'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_load_number_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.to_values(
            dict(b='wrong'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_load_float_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.to_values(
            dict(c='wrong'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_load_integer_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.to_values(
            dict(d='wrong'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

    with pytest.raises(ValueError):
        jv.to_values(
            dict(d=1.1),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_load_text_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.to_values(
            dict(e=1),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_load_url_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.to_values(
            dict(f=1),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_load_date_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.to_values(
            dict(g='2011-14-01'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_load_datetime_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.to_values(
            dict(h='2011-12-01T00:64:17'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)

def test_schema_org_data_type_load_time_wrong():
    jv = JsonValue()

    jv.vocabulary(schemaorg.DATA_TYPE_VOCABULARY)

    with pytest.raises(ValueError):
        jv.to_values(
            dict(i='25:10:17'),
            context=SCHEMA_ORG_DATA_TYPES_CONTEXT)
<|endoftext|>


<commit_before>import inspect
import functools

from pikos._internal.util import is_context_manager

class Monitor(object):
    """ The monitor class decorator.

    This is the main entry point for all the monitors, inspectors, loggers and
    profilers that are supported by pikos. The MonitoAssistant is tring to be
    as versitile as possible in order to simplify setting up and invoking the
    actual monitoring/profiling class.

    The class can be instanciated by the user or used as a decorator -- throught
    the monitor alias -- for functions, methods and generators.

    Usage
    -----

    # as a decorator
    @monitor(FunctionLogger())
    def my_function():
        ...
        return

    # as an instance
    def my_function():
        ...
        return

    logfunctions = MonitorAssistant(FunctionLogger(), )
    logfunctions(my_function, *args, **kwrgs)
    ...

    """
    def __init__(self, obj):
        """ Class initialization.

        """
        self._function = None
        self._monitor_object = obj

    def __call__(self, function):
        self._function = function
        if is_context_manager(self._monitor_object):
            return self._use_context_manager()
        else:
            msg = "provided monitor object '{}' is not a context manager"
            ValueError(msg.format(self._monitor_object))

    def _use_context_manager(self):
        if inspect.isgeneratorfunction(self._function):
            self._is_generator = True
            fn = self.wrap_generator_with_context_manager()
        else:
            fn = self.wrap_function_with_context_manager()
        return fn

    def wrap_function_with_context_manager(self):
        @functools.wraps(self._function)
        def wrapper(*args, **kwds):
            with self._monitor_object:
                 return self._function(*args, **kwds)
        return wrapper

    def wrap_generator_with_context_manager(self):
        """ Wrap a generator to profile it.
        """
        def wrapper(*args, **kwds):
            with self._monitor_object:
                g = self._function(*args, **kwds)
                value = g.next()
            yield value
            while True:
                with self._monitor_object:
                    item = g.send(value)
                value = (yield item)
        return wrapper
<commit_msg>correct the docstring<commit_after>import inspect
import functools

from pikos._internal.util import is_context_manager

class Monitor(object):
    """ The monitor class decorator.

    This is the main entry point for all the monitors, inspectors, loggers and
    profilers that are supported by pikos. The MonitoAssistant is tring to be
    as versitile as possible in order to simplify setting up and invoking the
    actual monitoring/profiling class.

    The class can be instanciated by the user or used as a decorator -- throught
    the monitor alias -- for functions, methods and generators.

    Usage
    -----

    # as a decorator
    @monitor(FunctionMonitor())
    def my_function():
        ...
        return

    # as an instance
    def my_function():
        ...
        return

    logfunctions = MonitorAssistant(FunctionMonitor(), )
    logfunctions(my_function, *args, **kwrgs)
    ...

    """
    def __init__(self, obj):
        """ Class initialization.

        """
        self._function = None
        self._monitor_object = obj

    def __call__(self, function):
        self._function = function
        if is_context_manager(self._monitor_object):
            return self._use_context_manager()
        else:
            msg = "provided monitor object '{}' is not a context manager"
            ValueError(msg.format(self._monitor_object))

    def _use_context_manager(self):
        if inspect.isgeneratorfunction(self._function):
            self._is_generator = True
            fn = self.wrap_generator_with_context_manager()
        else:
            fn = self.wrap_function_with_context_manager()
        return fn

    def wrap_function_with_context_manager(self):
        @functools.wraps(self._function)
        def wrapper(*args, **kwds):
            with self._monitor_object:
                 return self._function(*args, **kwds)
        return wrapper

    def wrap_generator_with_context_manager(self):
        """ Wrap a generator to profile it.
        """
        def wrapper(*args, **kwds):
            with self._monitor_object:
                g = self._function(*args, **kwds)
                value = g.next()
            yield value
            while True:
                with self._monitor_object:
                    item = g.send(value)
                value = (yield item)
        return wrapper
<|endoftext|>


<commit_before>from __future__ import absolute_import

__version__ = '1.0b2'
__author__ = 'Simon Torres'
__date__ = '2016-06-28'
__email__ = "storres@ctio.noao.edu"
__status__ = "Development"<commit_msg>updated version number to 1.0b3<commit_after>from __future__ import absolute_import

__version__ = '1.0b3'
__author__ = 'Simon Torres'
__date__ = '2016-06-28'
__email__ = "storres@ctio.noao.edu"
__status__ = "Development"<|endoftext|>


<commit_before>"""
Monitor live network flow of one or more devices.
"""

import asyncio
import collections
from . import base

class Monitor(base.ECMCommand):
    """ Monitor live network flows. """

    name = 'monitor'
    use_pager = False

    def setup_args(self, parser):
        self.add_router_argument('idents', nargs='*')
        self.inject_table_factory()

    def run(self, args):
        filters = {
            "state": 'online',
            "product__series": 3
        }
        if args.idents:
            routers = [self.api.get_by_id_or_name('routers', x)
                       for x in args.idents]
            filters["id__in"] = ','.join(x['id'] for x in routers)
        fields = collections.OrderedDict((
            ("flow.start", self.flow_start_acc),
            ("flow.end", self.flow_end_acc),
            ("ip.daddr", 'orig.ip.daddr'),
            ("ip.protocol", 'orig.ip.protocol'),
            ("ip.saddr", 'orig.ip.saddr'),
            ("ip.dport", 'orig.ip.dport'),
            ("ip.sport", 'orig.ip.sport'),
            ("raw.pktcount", 'orig.raw.pktcount'),
            ("raw.pktlen", 'orig.raw.pktlen'),
        ))
        flows = collections.OrderedDict()

        res = self.api.put('remote', 'control/netflow/ulog/enable', True,
                           **filters)
        print('resd', res)
        print('resd', res)
        print('resd', res)
        while True:
            for x in self.api.remote('control.netflow.ulog.data', **filters):
                print('sx', x)

            with self.make_table(headers=fields.keys(),
                                 accessors=fields.values()) as t:
                t.print(flows)

    def flow_start_acc(self, record):
        return float('%s.%s' % (record['flow.start.sec'],
                                record['flow.start.usec']))

    def flow_end_acc(self, record):
        return float('%s.%s' % (record['flow.end.sec'],
                                record['flow.end.usec']))
j

class Netflow(base.ECMCommand):

    name = 'netflow'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.add_subcommand(Monitor, default=True)

command_classes = [Netflow]
<commit_msg>Remove "j" syntax typo from netflow.<commit_after>"""
Monitor live network flow of one or more devices.
"""

import asyncio
import collections
from . import base

class Monitor(base.ECMCommand):
    """ Monitor live network flows. """

    name = 'monitor'
    use_pager = False

    def setup_args(self, parser):
        self.add_router_argument('idents', nargs='*')
        self.inject_table_factory()

    def run(self, args):
        filters = {
            "state": 'online',
            "product__series": 3
        }
        if args.idents:
            routers = [self.api.get_by_id_or_name('routers', x)
                       for x in args.idents]
            filters["id__in"] = ','.join(x['id'] for x in routers)
        fields = collections.OrderedDict((
            ("flow.start", self.flow_start_acc),
            ("flow.end", self.flow_end_acc),
            ("ip.daddr", 'orig.ip.daddr'),
            ("ip.protocol", 'orig.ip.protocol'),
            ("ip.saddr", 'orig.ip.saddr'),
            ("ip.dport", 'orig.ip.dport'),
            ("ip.sport", 'orig.ip.sport'),
            ("raw.pktcount", 'orig.raw.pktcount'),
            ("raw.pktlen", 'orig.raw.pktlen'),
        ))
        flows = collections.OrderedDict()

        res = self.api.put('remote', 'control/netflow/ulog/enable', True,
                           **filters)
        print('resd', res)
        print('resd', res)
        print('resd', res)
        while True:
            for x in self.api.remote('control.netflow.ulog.data', **filters):
                print('sx', x)

            with self.make_table(headers=fields.keys(),
                                 accessors=fields.values()) as t:
                t.print(flows)

    def flow_start_acc(self, record):
        return float('%s.%s' % (record['flow.start.sec'],
                                record['flow.start.usec']))

    def flow_end_acc(self, record):
        return float('%s.%s' % (record['flow.end.sec'],
                                record['flow.end.usec']))

class Netflow(base.ECMCommand):

    name = 'netflow'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.add_subcommand(Monitor, default=True)

command_classes = [Netflow]
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# coding: UTF-8

import os
import datetime
import pprint
import subprocess
from optparse import OptionParser

__VERSION__ = '0.0.1'

def get_commits_log(commits):
    dead  = '\033[91m' + "D" + '\033[0m'
    alive = '\033[92m' + "A" + '\033[0m'
    dates = list(get_dates())
    projects = set()

    logs = dict() # dictionary from tuple of projname and date to dead or alive
    for path, commits in commits.items():
        project = get_str_projname(path)
        for date, commit_num in commits.items():
            logs[(project, date)] = alive if (commit_num > 0) else dead
            projects.add(project)

    splitted_logs = []
    proj_len = max(len(project) for project in projects) + 1

    date_log = ' ' * proj_len
    date_cnt = 0
    for date in dates:
        date_log += str(date_cnt) + ' '
        date_cnt += 1
    splitted_logs.append(date_log)

    proj_log = ''
    for proj in sorted(projects):
        proj_log = proj + ' ' * (proj_len - len(proj))
        for date in dates:
            proj_log += logs[(proj, date)] + ' '

        splitted_logs.append(proj_log)

    return '\n'.join(splitted_logs)

def get_commit_numbers(path):
    os.chdir(path)
    if not os.path.isdir(path + '/.git'):
        return None

    log = subprocess.check_output(
        ['git', 'log', '--oneline', '--date=short',
            '--pretty=format:\"%ad\"']).decode('utf-8')

    numbers = dict()

    for date in get_dates():
        year, month, day = date.year, date.month, date.day
        _format = "%04d-%02d-%02d" % (year, month, day)
        numbers[datetime.date(year, month, day)] = log.count(_format)
    return numbers

def get_children_dirs(path):
    yield os.path.abspath(path)
    for (_, dirs, _) in os.walk(path):
        for d in dirs:
            yield os.path.abspath(d)

def get_cvs_dirs(path):
    for p in get_children_dirs(path):
        if os.path.isdir(p + '/.git'):
            yield p

def get_dates():
    today = datetime.date.today()
    for days in [7 - days for days in range(7 + 1)]:
        date = today - datetime.timedelta(days=days)
        yield datetime.date(date.year, date.month, date.day)

def get_str_projname(project):
    return project.split('/')[-1]

def get_parser():
    parser = OptionParser(version=__VERSION__)
    return parser

def main():
    opts, args = get_parser().parse_args()
    base_path = '.'
    if len(args) >= 1:
        base_path = os.path.abspath(args[0])

    commits = dict()
    projects = list(get_cvs_dirs('.'))
    for path in projects:
        commits[path] = get_commit_numbers(path)

    commits_log = get_commits_log(commits)
    print(commits_log)

if __name__ == '__main__':
    main()
<commit_msg>day-num option<commit_after>#!/usr/bin/env python3
# coding: UTF-8

import os
import datetime
import pprint
import subprocess
from optparse import OptionParser

__VERSION__ = '0.0.1'

DEFAULT_NUMBER_OF_DAY = 7

def get_commits_log(commits, day_num):
    dead  = '\033[91m' + "D" + '\033[0m'
    alive = '\033[92m' + "A" + '\033[0m'
    dates = list(get_dates(day_num))
    projects = set()

    logs = dict() # dictionary from tuple of projname and date to dead or alive
    for path, commits in commits.items():
        project = get_str_projname(path)
        for date, commit_num in commits.items():
            logs[(project, date)] = alive if (commit_num > 0) else dead
            projects.add(project)

    splitted_logs = []
    proj_len = max(len(project) for project in projects) + 1

    date_log = ' ' * proj_len
    date_cnt = 0
    for date in dates:
        date_log += str(date_cnt) + ' '
        date_cnt += 1
    splitted_logs.append(date_log)

    proj_log = ''
    for proj in sorted(projects):
        proj_log = proj + ' ' * (proj_len - len(proj))
        for date in dates:
            proj_log += logs[(proj, date)] + ' '

        splitted_logs.append(proj_log)

    return '\n'.join(splitted_logs)

def get_commit_numbers(path, day_num):
    os.chdir(path)
    if not os.path.isdir(path + '/.git'):
        return None

    log = subprocess.check_output(
        ['git', 'log', '--oneline', '--date=short',
            '--pretty=format:\"%ad\"']).decode('utf-8')

    numbers = dict()

    for date in get_dates(day_num):
        year, month, day = date.year, date.month, date.day
        _format = "%04d-%02d-%02d" % (year, month, day)
        numbers[datetime.date(year, month, day)] = log.count(_format)
    return numbers

def get_children_dirs(path):
    yield os.path.abspath(path)
    for (_, dirs, _) in os.walk(path):
        for d in dirs:
            yield os.path.abspath(d)

def get_cvs_dirs(path):
    for p in get_children_dirs(path):
        if os.path.isdir(p + '/.git'):
            yield p

def get_dates(day_num):
    today = datetime.date.today()
    for days in [day_num - days for days in range(day_num + 1)]:
        date = today - datetime.timedelta(days=days)
        yield datetime.date(date.year, date.month, date.day)

def get_str_projname(project):
    return project.split('/')[-1]

def get_parser():
    parser = OptionParser(version=__VERSION__)
    parser.add_option(
        '--day-num', '-n',
        action='store',
        dest='day_num',
        help='number of considering day')
    return parser

def main():
    opts, args = get_parser().parse_args()
    if not opts.day_num:
        opts.day_num = DEFAULT_NUMBER_OF_DAY
    base_path = os.path.abspath('.' if len(args) == 0 else args[0])

    commits = dict()
    projects = list(get_cvs_dirs(base_path))
    for path in projects:
        commits[path] = get_commit_numbers(path, opts.day_num)

    commits_log = get_commits_log(commits, opts.day_num)
    print(commits_log)

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


<commit_before># coding: utf8
"""
    weasyprint.pdf
    --------------

    :copyright: Copyright 2011-2012 Simon Sapin and contributors, see AUTHORS.
    :license: BSD, see LICENSE for details.

"""

from __future__ import division, unicode_literals

class PDF(object):
    """PDF document post-processor adding links."""
    def __init__(self, bytesio, links, destinations):
        bytesio.seek(0)
        self.xref = []
        self.trailer = []
        self.objects = {}
        self.active = None
        self.numbers = []
        self.added_numbers = []
        self.pages = []

        lines = bytesio.readlines()
        self.outlines = lines[:2]

        for line in lines:
            if line.endswith(b' obj\n'):
                self.active = 'object'
                number = int(line.split()[0])
                self.numbers.append(number)
            elif line == b'xref\n':
                self.active = 'xref'
            elif line == b'trailer\n':
                self.active = 'trailer'

            if line.endswith(b'/Type /Page\n'):
                self.pages.append(number)

            if self.active == 'xref':
                self.xref.append(line)
            elif self.active == 'trailer':
                if line == b'startxref\n':
                    break
                self.trailer.append(line)
                if b'/Info' in line:
                    info = int(line.rsplit()[-3])
                    for i, info_line in enumerate(self.objects[info]):
                        if b'/Creator' in info_line:
                            pre = info_line.split(b'/Creator')[0]
                            new_line = b'%s/Creator (%s)\n' % (
                                pre, b'WeasyPrint')
                            self.objects[info][i] = new_line
                            self.replace_xref_size(
                                info, len(new_line) - len(info_line))
            elif self.active == 'object':
                if number not in self.objects:
                    self.objects[number] = []
                self.objects[number].append(line)

            if line == b'endobj\n':
                self.active = None

        for pdf_page_number, link_page in zip(self.pages, links):
            annot_numbers = []
            for link, x1, y1, x2, y2 in link_page:
                text = b''.join((
                    b'<< /Type /Annot /Subtype /Link',
                    b'/Rect [%f %f %f %f]\n' % (x1, y1, x2, y2)))
                if link:
                    if link.startswith('#'):
                        if link[1:] in destinations:
                            text += b''.join((
                                b'/A << /Type /Action /S /GoTo',
                                b'/D [%d /XYZ %d %d 1]\n' % (
                                    destinations[link[1:]])))
                    else:
                        text += b''.join((
                            b'/A << /Type /Action /S /URI',
                            b'/URI (%s)\n' % link))
                text += b'>>\n>>'
                annot_numbers.append(self.add_object(text))
            if annot_numbers:
                string = b'/Annots [%s]\n' % b' '.join(
                    b'%d 0 R' % number for number in annot_numbers)
                self.objects[pdf_page_number].insert(-2, string)
                self.replace_xref_size(pdf_page_number, len(string))

        for i, line in enumerate(self.trailer):
            if b'/Size' in line:
                pre = line.split(b'/Size')[0]
                self.trailer[i] = b'%s/Size %s\n' % (
                    pre, b'%s' % (len(self.numbers) + 1))

        for number in self.numbers:
            self.outlines.extend(self.objects[number])
        size = sum([len(line) for line in self.outlines])
        self.outlines.extend(self.xref + self.trailer)
        self.outlines.append(b'startxref\n%d\n%%EOF\n' % size)

    def add_object(self, text):
        """Add an object with ``text`` content at the end of the objects."""
        next_number = len(self.numbers) + 1
        text = b'%d 0 obj\n%s\nendobj' % (next_number, text)
        last_size = int(
            self.xref[self.numbers[-1] + 2].split()[0].lstrip(b'0'))
        last_object_size = len(''.join(self.objects[self.numbers[-1]]))
        self.xref.append(b'%010d 00000 n \n' % (last_size + last_object_size))
        self.numbers.append(next_number)
        self.added_numbers.append(next_number)
        self.objects[next_number] = [
            line + b'\n' for line in text.split(b'\n')]
        self.xref[1] = b'0 %d\n' % (next_number + 1)
        return next_number

    def replace_xref_size(self, number, offset_size):
        """Update xref adding ``offset_size`` bytes to ``object[number]``."""
        index = self.numbers.index(number)
        for next_number in self.numbers[index + 1:len(self.numbers)]:
            out = self.xref[next_number + 2]
            old_size, content = out.split(b' ', 1)
            old_size = int(old_size.lstrip(b'0')) + offset_size
            self.xref[next_number + 2] = b'%010d %s' % (old_size, content)

    def write(self, target):
        """Write the PDF content into the ``target`` stream."""
        for outline in self.outlines:
            target.write(outline)
<commit_msg>Remove a useless attribute<commit_after># coding: utf8
"""
    weasyprint.pdf
    --------------

    :copyright: Copyright 2011-2012 Simon Sapin and contributors, see AUTHORS.
    :license: BSD, see LICENSE for details.

"""

from __future__ import division, unicode_literals

class PDF(object):
    """PDF document post-processor adding links."""
    def __init__(self, bytesio, links, destinations):
        bytesio.seek(0)
        self.xref = []
        self.trailer = []
        self.objects = {}
        self.active = None
        self.numbers = []
        self.pages = []

        lines = bytesio.readlines()
        self.outlines = lines[:2]

        for line in lines:
            if line.endswith(b' obj\n'):
                self.active = 'object'
                number = int(line.split()[0])
                self.numbers.append(number)
            elif line == b'xref\n':
                self.active = 'xref'
            elif line == b'trailer\n':
                self.active = 'trailer'

            if line.endswith(b'/Type /Page\n'):
                self.pages.append(number)

            if self.active == 'xref':
                self.xref.append(line)
            elif self.active == 'trailer':
                if line == b'startxref\n':
                    break
                self.trailer.append(line)
                if b'/Info' in line:
                    info = int(line.rsplit()[-3])
                    for i, info_line in enumerate(self.objects[info]):
                        if b'/Creator' in info_line:
                            pre = info_line.split(b'/Creator')[0]
                            new_line = b'%s/Creator (%s)\n' % (
                                pre, b'WeasyPrint')
                            self.objects[info][i] = new_line
                            self.replace_xref_size(
                                info, len(new_line) - len(info_line))
            elif self.active == 'object':
                if number not in self.objects:
                    self.objects[number] = []
                self.objects[number].append(line)

            if line == b'endobj\n':
                self.active = None

        for pdf_page_number, link_page in zip(self.pages, links):
            annot_numbers = []
            for link, x1, y1, x2, y2 in link_page:
                text = b''.join((
                    b'<< /Type /Annot /Subtype /Link',
                    b'/Rect [%f %f %f %f]\n' % (x1, y1, x2, y2)))
                if link:
                    if link.startswith('#'):
                        if link[1:] in destinations:
                            text += b''.join((
                                b'/A << /Type /Action /S /GoTo',
                                b'/D [%d /XYZ %d %d 1]\n' % (
                                    destinations[link[1:]])))
                    else:
                        text += b''.join((
                            b'/A << /Type /Action /S /URI',
                            b'/URI (%s)\n' % link))
                text += b'>>\n>>'
                annot_numbers.append(self.add_object(text))
            if annot_numbers:
                string = b'/Annots [%s]\n' % b' '.join(
                    b'%d 0 R' % number for number in annot_numbers)
                self.objects[pdf_page_number].insert(-2, string)
                self.replace_xref_size(pdf_page_number, len(string))

        for i, line in enumerate(self.trailer):
            if b'/Size' in line:
                pre = line.split(b'/Size')[0]
                self.trailer[i] = b'%s/Size %s\n' % (
                    pre, b'%s' % (len(self.numbers) + 1))

        for number in self.numbers:
            self.outlines.extend(self.objects[number])
        size = sum([len(line) for line in self.outlines])
        self.outlines.extend(self.xref + self.trailer)
        self.outlines.append(b'startxref\n%d\n%%EOF\n' % size)

    def add_object(self, text):
        """Add an object with ``text`` content at the end of the objects."""
        next_number = len(self.numbers) + 1
        text = b'%d 0 obj\n%s\nendobj' % (next_number, text)
        last_size = int(
            self.xref[self.numbers[-1] + 2].split()[0].lstrip(b'0'))
        last_object_size = len(''.join(self.objects[self.numbers[-1]]))
        self.xref.append(b'%010d 00000 n \n' % (last_size + last_object_size))
        self.numbers.append(next_number)
        self.objects[next_number] = [
            line + b'\n' for line in text.split(b'\n')]
        self.xref[1] = b'0 %d\n' % (next_number + 1)
        return next_number

    def replace_xref_size(self, number, offset_size):
        """Update xref adding ``offset_size`` bytes to ``object[number]``."""
        index = self.numbers.index(number)
        for next_number in self.numbers[index + 1:len(self.numbers)]:
            out = self.xref[next_number + 2]
            old_size, content = out.split(b' ', 1)
            old_size = int(old_size.lstrip(b'0')) + offset_size
            self.xref[next_number + 2] = b'%010d %s' % (old_size, content)

    def write(self, target):
        """Write the PDF content into the ``target`` stream."""
        for outline in self.outlines:
            target.write(outline)
<|endoftext|>


<commit_before>"""
picture.py
Author: <your name here>
Credit: <list sources used, if any>

Assignment:

Use the ggame library to "paint" a graphical picture of something (e.g. a house, a face or landscape).

Use at least:
1. Three different Color objects.
2. Ten different Sprite objects.
3. One (or more) RectangleAsset objects.
4. One (or more) CircleAsset objects.
5. One (or more) EllipseAsset objects.
6. One (or more) PolygonAsset objects.

See:
https://github.com/HHS-IntroProgramming/Standards-and-Syllabus/wiki/Displaying-Graphics
for general information on how to use ggame.

See:
http://brythonserver.github.io/ggame/
for detailed information on ggame.

"""
from ggame import App, Color, LineStyle, Sprite, RectangleAsset, CircleAsset, EllipseAsset, PolygonAsset

# add your code here \/  \/  \/
from ggame import App, Color, LineStyle, Sprite
from ggame import RectangleAsset, CircleAsset, EllipseAsset, PolygonAsset

red = Color(0xff0000, 1.0)
green = Color(0x00ff00, 1.0)
blue = Color(0x0000ff, 1.0)
black = Color(0x000000, 1.0)
white = Color(0xf8f8ff, 1.0)
thinline = LineStyle(1, black)

circle = CircleAsset(50, thinline, red)
Sprite(circle, (50, 50))

base = RectangleAsset(200, 200, thinline, blue)
Sprite(base, (900, 400))

roof = PolygonAsset([(0,0), (200,0), (100, -120)], thinline, black)
Sprite(roof, (900, 400))

window1 = RectangleAsset(50, 80, thinline, white)
Sprite(window1, (945, 380))

# add your code here /\  /\  /\

myapp = App()
myapp.run()
<commit_msg>Updated from Brython Server: 1/27/2017 2:49:20 PM<commit_after>"""
picture.py
Author: <your name here>
Credit: <list sources used, if any>

Assignment:

Use the ggame library to "paint" a graphical picture of something (e.g. a house, a face or landscape).

Use at least:
1. Three different Color objects.
2. Ten different Sprite objects.
3. One (or more) RectangleAsset objects.
4. One (or more) CircleAsset objects.
5. One (or more) EllipseAsset objects.
6. One (or more) PolygonAsset objects.

See:
https://github.com/HHS-IntroProgramming/Standards-and-Syllabus/wiki/Displaying-Graphics
for general information on how to use ggame.

See:
http://brythonserver.github.io/ggame/
for detailed information on ggame.

"""
from ggame import App, Color, LineStyle, Sprite, RectangleAsset, CircleAsset, EllipseAsset, PolygonAsset

# add your code here \/  \/  \/
from ggame import App, Color, LineStyle, Sprite
from ggame import RectangleAsset, CircleAsset, EllipseAsset, PolygonAsset

red = Color(0xff0000, 1.0)
green = Color(0x00ff00, 1.0)
blue = Color(0x0000ff, 1.0)
black = Color(0x000000, 1.0)
white = Color(0xf8f8ff, 1.0)
thinline = LineStyle(1, black)

circle = CircleAsset(50, thinline, red)
Sprite(circle, (50, 50))

base = RectangleAsset(200, 200, thinline, blue)
Sprite(base, (900, 400))

roof = PolygonAsset([(0,0), (200,0), (100, -120)], thinline, black)
Sprite(roof, (900, 400))

window1 = RectangleAsset(50, 80, thinline, white)
Sprite(window1, (940, 430))

# add your code here /\  /\  /\

myapp = App()
myapp.run()
<|endoftext|>