<commit_before># (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os

from ansible import constants as C
from ansible.compat.six import string_types
from ansible.errors import AnsibleParserError, AnsibleUndefinedVariable
from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject, AnsibleSequence

try:
    from __main__ import display
except ImportError:
    from ansible.utils.display import Display
    display = Display()

def load_list_of_blocks(ds, play, parent_block=None, role=None, task_include=None, use_handlers=False, variable_manager=None, loader=None):
    '''
    Given a list of mixed task/block data (parsed from YAML),
    return a list of Block() objects, where implicit blocks
    are created for each bare Task.
    '''

    # we import here to prevent a circular dependency with imports
    from ansible.playbook.block import Block

    assert isinstance(ds, (list, type(None)))

    block_list = []
    if ds:
        for block in ds:
            b = Block.load(
                block,
                play=play,
                parent_block=parent_block,
                role=role,
                task_include=task_include,
                use_handlers=use_handlers,
                variable_manager=variable_manager,
                loader=loader
            )
            # Implicit blocks are created by bare tasks listed in a play without
            # an explicit block statement. If we have two implicit blocks in a row,
            # squash them down to a single block to save processing time later.
            if b._implicit and len(block_list) > 0 and block_list[-1]._implicit:
                for t in b.block:
                    t._block = block_list[-1]
                block_list[-1].block.extend(b.block)
            else:
                block_list.append(b)

    return block_list

def load_list_of_tasks(ds, play, block=None, role=None, task_include=None, use_handlers=False, variable_manager=None, loader=None):
    '''
    Given a list of task datastructures (parsed from YAML),
    return a list of Task() or TaskInclude() objects.
    '''

    # we import here to prevent a circular dependency with imports
    from ansible.playbook.block import Block
    from ansible.playbook.handler import Handler
    from ansible.playbook.task import Task
    from ansible.playbook.task_include import TaskInclude
    from ansible.template import Templar

    assert isinstance(ds, list)

    task_list = []
    for task_ds in ds:
        assert isinstance(task_ds, dict)

        if 'block' in task_ds:
            t = Block.load(
                task_ds,
                play=play,
                parent_block=block,
                role=role,
                task_include=task_include,
                use_handlers=use_handlers,
                variable_manager=variable_manager,
                loader=loader,
            )
            task_list.append(t)
        else:
            if 'include' in task_ds:
                t = TaskInclude.load(task_ds, block=block, role=role, task_include=task_include, variable_manager=variable_manager, loader=loader)

                all_vars = variable_manager.get_vars(loader=loader, play=play, task=t)
                templar = Templar(loader=loader, variables=all_vars)

                # check to see if this include is static, which can be true if:
                # 1. the user set the 'static' option to true
                # 2. one of the appropriate config options was set
                # 3. the included file name contains no variables, and has no loop
                is_static = t.static or \
                            C.DEFAULT_TASK_INCLUDES_STATIC or \
                            (use_handlers and C.DEFAULT_HANDLER_INCLUDES_STATIC) or \
                            not templar._contains_vars(t.args.get('_raw_params')) and t.loop is None

                if is_static:
                    if t.loop is not None:
                        raise AnsibleParserError("You cannot use 'static' on an include with a loop", obj=task_ds)

                    # FIXME: all of this code is very similar (if not identical) to that in
                    #        plugins/strategy/__init__.py, and should be unified to avoid
                    #        patches only being applied to one or the other location
                    if task_include:
                        # handle relative includes by walking up the list of parent include
                        # tasks and checking the relative result to see if it exists
                        parent_include = task_include
                        cumulative_path = None
                        while parent_include is not None:
                            parent_include_dir = templar.template(os.path.dirname(parent_include.args.get('_raw_params')))
                            if cumulative_path is None:
                                cumulative_path = parent_include_dir
                            elif not os.path.isabs(cumulative_path):
                                cumulative_path = os.path.join(parent_include_dir, cumulative_path)
                            include_target = templar.template(t.args['_raw_params'])
                            if t._role:
                                new_basedir = os.path.join(t._role._role_path, 'tasks', cumulative_path)
                                include_file = loader.path_dwim_relative(new_basedir, 'tasks', include_target)
                            else:
                                include_file = loader.path_dwim_relative(loader.get_basedir(), cumulative_path, include_target)

                            if os.path.exists(include_file):
                                break
                            else:
                                parent_include = parent_include._task_include
                    else:
                        try:
                            include_target = templar.template(t.args['_raw_params'])
                        except AnsibleUndefinedVariable as e:
                            raise AnsibleParserError(
                                      "Error when evaluating variable in include name: %s.\n\n" \
                                      "When using static includes, ensure that any variables used in their names are defined in vars/vars_files\n" \
                                      "or extra-vars passed in from the command line. Static includes cannot use variables from inventory\n" \
                                      "sources like group or host vars." % t.args['_raw_params'],
                                      obj=task_ds,
                                      suppress_extended_error=True,
                                  )
                        if t._role:
                            if use_handlers:
                                include_file = loader.path_dwim_relative(t._role._role_path, 'handlers', include_target)
                            else:
                                include_file = loader.path_dwim_relative(t._role._role_path, 'tasks', include_target)
                        else:
                            include_file = loader.path_dwim(include_target)

                    data = loader.load_from_file(include_file)
                    if data is None:
                        return []
                    elif not isinstance(data, list):
                        raise AnsibleError("included task files must contain a list of tasks", obj=data)

                    included_blocks = load_list_of_blocks(
                        data,
                        play=play,
                        parent_block=block,
                        task_include=t,
                        role=role,
                        use_handlers=use_handlers,
                        loader=loader,
                        variable_manager=variable_manager,
                    )

                    # pop tags out of the include args, if they were specified there, and assign
                    # them to the include. If the include already had tags specified, we raise an
                    # error so that users know not to specify them both ways
                    tags = t.vars.pop('tags', [])
                    if isinstance(tags, string_types):
                        tags = tags.split(',')

                    if len(tags) > 0:
                        if len(t.tags) > 0:
                            raise AnsibleParserError(
                                "Include tasks should not specify tags in more than one way (both via args and directly on the task)." \
                                " Mixing tag specify styles is prohibited for whole import hierarchy, not only for single import statement",
                                obj=task_ds,
                                suppress_extended_error=True,
                            )
                        display.deprecated("You should not specify tags in the include parameters. All tags should be specified using the task-level option")
                    else:
                        tags = t.tags[:]

                    # now we extend the tags on each of the included blocks
                    for b in included_blocks:
                        b.tags = list(set(b.tags).union(tags))
                    # END FIXME

                    # FIXME: send callback here somehow...
                    # FIXME: handlers shouldn't need this special handling, but do
                    #        right now because they don't iterate blocks correctly
                    if use_handlers:
                        for b in included_blocks:
                            task_list.extend(b.block)
                    else:
                        task_list.extend(included_blocks)
                else:
                    task_list.append(t)
            elif use_handlers:
                t = Handler.load(task_ds, block=block, role=role, task_include=task_include, variable_manager=variable_manager, loader=loader)
                task_list.append(t)
            else:
                t = Task.load(task_ds, block=block, role=role, task_include=task_include, variable_manager=variable_manager, loader=loader)
                task_list.append(t)

    return task_list

def load_list_of_roles(ds, play, current_role_path=None, variable_manager=None, loader=None):
    '''
    Loads and returns a list of RoleInclude objects from the datastructure
    list of role definitions
    '''

    # we import here to prevent a circular dependency with imports
    from ansible.playbook.role.include import RoleInclude

    assert isinstance(ds, list)

    roles = []
    for role_def in ds:
        i = RoleInclude.load(role_def, play=play, current_role_path=current_role_path, variable_manager=variable_manager, loader=loader)
        roles.append(i)

    return roles

<commit_msg>Handle includes which may have been created in the flow of the playbook<commit_after># (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os

from ansible import constants as C
from ansible.compat.six import string_types
from ansible.errors import AnsibleParserError, AnsibleUndefinedVariable, AnsibleFileNotFound
from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject, AnsibleSequence

try:
    from __main__ import display
except ImportError:
    from ansible.utils.display import Display
    display = Display()

def load_list_of_blocks(ds, play, parent_block=None, role=None, task_include=None, use_handlers=False, variable_manager=None, loader=None):
    '''
    Given a list of mixed task/block data (parsed from YAML),
    return a list of Block() objects, where implicit blocks
    are created for each bare Task.
    '''

    # we import here to prevent a circular dependency with imports
    from ansible.playbook.block import Block

    assert isinstance(ds, (list, type(None)))

    block_list = []
    if ds:
        for block in ds:
            b = Block.load(
                block,
                play=play,
                parent_block=parent_block,
                role=role,
                task_include=task_include,
                use_handlers=use_handlers,
                variable_manager=variable_manager,
                loader=loader
            )
            # Implicit blocks are created by bare tasks listed in a play without
            # an explicit block statement. If we have two implicit blocks in a row,
            # squash them down to a single block to save processing time later.
            if b._implicit and len(block_list) > 0 and block_list[-1]._implicit:
                for t in b.block:
                    t._block = block_list[-1]
                block_list[-1].block.extend(b.block)
            else:
                block_list.append(b)

    return block_list

def load_list_of_tasks(ds, play, block=None, role=None, task_include=None, use_handlers=False, variable_manager=None, loader=None):
    '''
    Given a list of task datastructures (parsed from YAML),
    return a list of Task() or TaskInclude() objects.
    '''

    # we import here to prevent a circular dependency with imports
    from ansible.playbook.block import Block
    from ansible.playbook.handler import Handler
    from ansible.playbook.task import Task
    from ansible.playbook.task_include import TaskInclude
    from ansible.template import Templar

    assert isinstance(ds, list)

    task_list = []
    for task_ds in ds:
        assert isinstance(task_ds, dict)

        if 'block' in task_ds:
            t = Block.load(
                task_ds,
                play=play,
                parent_block=block,
                role=role,
                task_include=task_include,
                use_handlers=use_handlers,
                variable_manager=variable_manager,
                loader=loader,
            )
            task_list.append(t)
        else:
            if 'include' in task_ds:
                t = TaskInclude.load(task_ds, block=block, role=role, task_include=task_include, variable_manager=variable_manager, loader=loader)

                all_vars = variable_manager.get_vars(loader=loader, play=play, task=t)
                templar = Templar(loader=loader, variables=all_vars)

                # check to see if this include is static, which can be true if:
                # 1. the user set the 'static' option to true
                # 2. one of the appropriate config options was set
                # 3. the included file name contains no variables, and has no loop
                is_static = t.static or \
                            C.DEFAULT_TASK_INCLUDES_STATIC or \
                            (use_handlers and C.DEFAULT_HANDLER_INCLUDES_STATIC) or \
                            not templar._contains_vars(t.args.get('_raw_params')) and t.loop is None

                if is_static:
                    if t.loop is not None:
                        raise AnsibleParserError("You cannot use 'static' on an include with a loop", obj=task_ds)

                    # FIXME: all of this code is very similar (if not identical) to that in
                    #        plugins/strategy/__init__.py, and should be unified to avoid
                    #        patches only being applied to one or the other location
                    if task_include:
                        # handle relative includes by walking up the list of parent include
                        # tasks and checking the relative result to see if it exists
                        parent_include = task_include
                        cumulative_path = None
                        while parent_include is not None:
                            parent_include_dir = templar.template(os.path.dirname(parent_include.args.get('_raw_params')))
                            if cumulative_path is None:
                                cumulative_path = parent_include_dir
                            elif not os.path.isabs(cumulative_path):
                                cumulative_path = os.path.join(parent_include_dir, cumulative_path)
                            include_target = templar.template(t.args['_raw_params'])
                            if t._role:
                                new_basedir = os.path.join(t._role._role_path, 'tasks', cumulative_path)
                                include_file = loader.path_dwim_relative(new_basedir, 'tasks', include_target)
                            else:
                                include_file = loader.path_dwim_relative(loader.get_basedir(), cumulative_path, include_target)

                            if os.path.exists(include_file):
                                break
                            else:
                                parent_include = parent_include._task_include
                    else:
                        try:
                            include_target = templar.template(t.args['_raw_params'])
                        except AnsibleUndefinedVariable as e:
                            raise AnsibleParserError(
                                      "Error when evaluating variable in include name: %s.\n\n" \
                                      "When using static includes, ensure that any variables used in their names are defined in vars/vars_files\n" \
                                      "or extra-vars passed in from the command line. Static includes cannot use variables from inventory\n" \
                                      "sources like group or host vars." % t.args['_raw_params'],
                                      obj=task_ds,
                                      suppress_extended_error=True,
                                  )
                        if t._role:
                            if use_handlers:
                                include_file = loader.path_dwim_relative(t._role._role_path, 'handlers', include_target)
                            else:
                                include_file = loader.path_dwim_relative(t._role._role_path, 'tasks', include_target)
                        else:
                            include_file = loader.path_dwim(include_target)

                    try:
                        data = loader.load_from_file(include_file)
                        if data is None:
                            return []
                        elif not isinstance(data, list):
                            raise AnsibleError("included task files must contain a list of tasks", obj=data)
                    except AnsibleFileNotFound as e:
                        if t.static or \
                           C.DEFAULT_TASK_INCLUDES_STATIC or \
                           C.DEFAULT_HANDLER_INCLUDES_STATIC and use_handlers:
                            raise
                        display.deprecated(
                            "Included file '%s' not found, however since this include is not " \
                            "explicitly marked as 'static: yes', we will try and include it dynamically " \
                            "later. In the future, this will be an error unless 'static: no' is used " \
                            "on the include task. If you do not want missing includes to be considered " \
                            "dynamic, use 'static: yes' on the include or set the global ansible.cfg " \
                            "options to make all inclues static for tasks and/or handlers" % include_file,
                        )
                        task_list.append(t)
                        continue

                    included_blocks = load_list_of_blocks(
                        data,
                        play=play,
                        parent_block=block,
                        task_include=t,
                        role=role,
                        use_handlers=use_handlers,
                        loader=loader,
                        variable_manager=variable_manager,
                    )

                    # pop tags out of the include args, if they were specified there, and assign
                    # them to the include. If the include already had tags specified, we raise an
                    # error so that users know not to specify them both ways
                    tags = t.vars.pop('tags', [])
                    if isinstance(tags, string_types):
                        tags = tags.split(',')

                    if len(tags) > 0:
                        if len(t.tags) > 0:
                            raise AnsibleParserError(
                                "Include tasks should not specify tags in more than one way (both via args and directly on the task)." \
                                " Mixing tag specify styles is prohibited for whole import hierarchy, not only for single import statement",
                                obj=task_ds,
                                suppress_extended_error=True,
                            )
                        display.deprecated("You should not specify tags in the include parameters. All tags should be specified using the task-level option")
                    else:
                        tags = t.tags[:]

                    # now we extend the tags on each of the included blocks
                    for b in included_blocks:
                        b.tags = list(set(b.tags).union(tags))
                    # END FIXME

                    # FIXME: send callback here somehow...
                    # FIXME: handlers shouldn't need this special handling, but do
                    #        right now because they don't iterate blocks correctly
                    if use_handlers:
                        for b in included_blocks:
                            task_list.extend(b.block)
                    else:
                        task_list.extend(included_blocks)
                else:
                    task_list.append(t)
            elif use_handlers:
                t = Handler.load(task_ds, block=block, role=role, task_include=task_include, variable_manager=variable_manager, loader=loader)
                task_list.append(t)
            else:
                t = Task.load(task_ds, block=block, role=role, task_include=task_include, variable_manager=variable_manager, loader=loader)
                task_list.append(t)

    return task_list

def load_list_of_roles(ds, play, current_role_path=None, variable_manager=None, loader=None):
    '''
    Loads and returns a list of RoleInclude objects from the datastructure
    list of role definitions
    '''

    # we import here to prevent a circular dependency with imports
    from ansible.playbook.role.include import RoleInclude

    assert isinstance(ds, list)

    roles = []
    for role_def in ds:
        i = RoleInclude.load(role_def, play=play, current_role_path=current_role_path, variable_manager=variable_manager, loader=loader)
        roles.append(i)

    return roles

<|endoftext|>


<commit_before>"""
This module contains controller application to manage
a set of OpenFlow switches
"""
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER, DEAD_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_4 as ofp
from ryu.ofproto import ofproto_v1_4_parser as parser

from fabric.network import Network
import fabric.packet as pack
import fabric.flows as fl

IDLE_TIMEOUT = 10  #: Timeout for dynamic flows
DEF_PRI = 0
LOW_PRI = 1
HIGH_PRI = 2

class NetworkManager(app_manager.RyuApp):
    """
    Core class of this package, NetworkManager is a top-level Ryu application
    """

    def __init__(self, *args, **kwargs):
        super(NetworkManager, self).__init__(*args, **kwargs)
        self.net = Network()  #: Init the Network instance

	self.features_dict={}
	self.features_dict_intermediate={}

    @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
    def _handle_switch_features(self, ev):
        """
        Handle new switches connecting to the network, their ports
        and initiates a round of LLDP discovery.

        Feeds all port statuses to `self.net` and sends out LLDP discovery.

        TODO: Investigate if it is needed. Possibly better to handle
        switch state in `self._handle_state_change` and port state
        in `self._handle_port_status`.

        """
        msg = ev.msg
    	dict_key=msg.ports.keys()
	dict_key.remove(65534)
	print dict_key
	self.features_dict[msg.datapath_id]=""
	print self.features_dict
	for key in dict_key:

	    self.features_dict_intermediate[key]=[msg.ports[key].port_no,msg.ports[key].hw_addr,msg.ports[key].state]
	    print self.features_dict_intermediate

	self.features_dict[msg.datapath_id]=self.features_dict_intermediate
	print self.features_dict

    @set_ev_cls(ofp_event.EventOFPStateChange, [DEAD_DISPATCHER])
    def _handle_state_change(self, ev):
        """
        Handle switch going down and performs a topology update.

        Unlike OFPSwitchFeatures, this event doesn't carry ports state,
        but is fired also on switch going down.

        :param ev: datapath description and reason for state change
        :type ev: `ofp_event.EventOFPStateChange`,
                  where datapath can be None if negotiation didn't
                  end successfully.
        """
        pass

    @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
    def _handle_packet_in(self, ev):
        """
        Parses incoming packet and calls a method that corresponds to dl_type.

        :param ev: packet contents and match structure to describe its
                   headers.
        :type ev: `ofp_event.EventOFPPacketIn`
        """
        msg=ev.msg
        pkt=packet.Packet(msg.data)
        datapath=msg.datapath
        in_port=msg.in_port
        descr=pack.parse(msg.data,datapath.id,in_port)

        pkt_arp=pkt.get_protocol(arp.arp)
        if pkt_arp:
            descr=pack.parse_arp(descr,msg.data)
	    key=descr["nl_dst"]
	    if key in self.net.ip_to_mac:
	    	dl_dst=self.net.ip_to_mac[key]
		arp_dict={"nl_src":descr["nl_src"],"nl_dst":descr["nl_dst"],"dl_src":descr["dl_src"],"dl_dst":dl_dst}
		self.reply_to_arp(datapath.id,arp_dict)
		return

        pkt_lldp=pkt.get_protocol(lldp.lldp)
        if pkt_lldp:
           pack.parse_lldp(descr,msg.data)
           return

    @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
    def _handle_port_status(self, ev):
        """
        Depending on `ev.msg.reason` adds or deletes port entry
        in `self.net` and starts another round of LLDP discovery.

        :param ev: port description and reason for state change
        :type ev: `ofp_event.EventOFPPortStatus`
        """
        pass

    def reply_to_arp(dp, pkt):
        """
        Responds to incoming ARP request using `self.net.mac_of` dict

        :param dp: datapath object that corresponds to originating switch
        :type dp: `ryu.controller.controller.Datapath`

        :param pkt: parsed eth and ARP headers of the request
        :type pkt: dict
        """
        dl_dst, nl_dst, nl_src = pkt["dl_src"], pkt["nl_src"],pkt["nl_dst"]
        dl_src = self.net.mac_of[nl_src]
        out_port = dp.ofproto.OFPP_LOCAL

        pkt = create_arp( dl_src,dl_dst,nl_src,nl_dst)

        send_out_packet(dp,pkt,out_port)

    def send_lldp(dp):
        """
        Sends LLDP broadcast from a given switch.

        :param dp: datapath object that corresponds to originating switch
        :type dp: `ryu.controller.controller.Datapath`

        """

    	pass
<commit_msg>Update app.py<commit_after>"""
This module contains controller application to manage
a set of OpenFlow switches
"""
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER, DEAD_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_4 as ofp
from ryu.ofproto import ofproto_v1_4_parser as parser

from fabric.network import Network
import fabric.packet as pack
import fabric.flows as fl

IDLE_TIMEOUT = 10  #: Timeout for dynamic flows
DEF_PRI = 0
LOW_PRI = 1
HIGH_PRI = 2

class NetworkManager(app_manager.RyuApp):
    """
    Core class of this package, NetworkManager is a top-level Ryu application
    """

    def __init__(self, *args, **kwargs):
        super(NetworkManager, self).__init__(*args, **kwargs)
        self.net = Network()  #: Init the Network instance

	self.features_dict={}
	self.features_dict_intermediate={}

    @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
    def _handle_switch_features(self, ev):
        """
        Handle new switches connecting to the network, their ports
        and initiates a round of LLDP discovery.

        Feeds all port statuses to `self.net` and sends out LLDP discovery.

        TODO: Investigate if it is needed. Possibly better to handle
        switch state in `self._handle_state_change` and port state
        in `self._handle_port_status`.

        """
        msg = ev.msg
    	dict_key=msg.ports.keys()
	dict_key.remove(65534)
	print dict_key
	self.features_dict[msg.datapath_id]=""
	print self.features_dict
	for key in dict_key:

	    self.features_dict_intermediate[key]=[msg.ports[key].port_no,msg.ports[key].hw_addr,msg.ports[key].state]
	    print self.features_dict_intermediate

	self.features_dict[msg.datapath_id]=self.features_dict_intermediate
	print self.features_dict

    @set_ev_cls(ofp_event.EventOFPStateChange, [DEAD_DISPATCHER])
    def _handle_state_change(self, ev):
        """
        Handle switch going down and performs a topology update.

        Unlike OFPSwitchFeatures, this event doesn't carry ports state,
        but is fired also on switch going down.

        :param ev: datapath description and reason for state change
        :type ev: `ofp_event.EventOFPStateChange`,
                  where datapath can be None if negotiation didn't
                  end successfully.
        """
        pass

    @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
    def _handle_packet_in(self, ev):
        """
        Parses incoming packet and calls a method that corresponds to dl_type.

        :param ev: packet contents and match structure to describe its
                   headers.
        :type ev: `ofp_event.EventOFPPacketIn`
        """
        msg=ev.msg
        pkt=packet.Packet(msg.data)
        datapath=msg.datapath
        in_port=msg.in_port
        descr=pack.parse(msg.data,datapath.id,in_port)

        pkt_arp=pkt.get_protocol(arp.arp)
        if pkt_arp:
            descr=pack.parse_arp(descr,msg.data)
	    key=descr["nl_dst"]
	    if key in self.net.ip_to_mac:
	    	dl_dst=self.net.ip_to_mac[key]
		arp_dict={"nl_src":descr["nl_src"],"nl_dst":descr["nl_dst"],"dl_src":descr["dl_src"],"dl_dst":dl_dst}
		self.reply_to_arp(datapath.id,arp_dict)
		return

        pkt_lldp=pkt.get_protocol(lldp.lldp)
        if pkt_lldp:
           pack.parse_lldp(descr,msg.data)
           return

    @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
    def _handle_port_status(self, ev):
        """
        Depending on `ev.msg.reason` adds or deletes port entry
        in `self.net` and starts another round of LLDP discovery.

        :param ev: port description and reason for state change
        :type ev: `ofp_event.EventOFPPortStatus`
        """
        pass

    def reply_to_arp(dp, pkt):
        """
        Responds to incoming ARP request using `self.net.mac_of` dict

        :param dp: datapath object that corresponds to originating switch
        :type dp: `ryu.controller.controller.Datapath`

        :param pkt: parsed eth and ARP headers of the request
        :type pkt: dict
        """
        dl_dst, nl_dst, nl_src = pkt["dl_src"], pkt["nl_src"],pkt["nl_dst"]
        dl_src = self.net.mac_of[nl_src]
        out_port = dp.ofproto.OFPP_LOCAL

        pkt = create_arp( dl_src,dl_dst,nl_src,nl_dst)

        send_out_packet(dp,pkt,out_port)

    def send_lldp(datapath):
        """
        Sends LLDP broadcast from a given switch.

        :param dp: datapath object that corresponds to originating switch
        :type dp: `ryu.controller.controller.Datapath`

        """

    	pkt_lldp=pack.create_lldp(datapath.id)
    	msg=fl.send_out_packet(datapath,pkt_lldp,ofp.OFPP_FLOOD)
    	datapath.send_msg(msg)
<|endoftext|>


<commit_before># Adapted from https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10.py

from datetime import datetime
import math
import time
import numpy as np
import dataset
import tensorflow.python.platform
import tensorflow as tf

batch_size = 8

def conv_op(input_op, name, kw, kh, n_in, n_out, dw, dh):
    with tf.name_scope(name) as scope:
        kernel_init_val = tf.truncated_normal([kh, kw, n_in, n_out], dtype=tf.float32, stddev=1e-1)
        kernel = tf.Variable(kernel_init_val, trainable=True, name='w')
        conv = tf.nn.conv2d(input_op, kernel, (1, dh, dw, 1), padding='SAME')
        bias_init_val = tf.constant(0.0, shape=[n_out], dtype=tf.float32)
        biases = tf.Variable(bias_init_val, trainable=True, name='b')
        z = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())
        activation = tf.nn.relu(z, name=scope)
        return activation

def affine_op(input_op, name, n_in, n_out):
    with tf.name_scope(name) as scope:
        kernel = tf.Variable(tf.truncated_normal([n_in, n_out], dtype=tf.float32, stddev=1e-1), trainable=True, name='weights')
        biases = tf.Variable(tf.constant(0.0, shape=[n_out], dtype=tf.float32), trainable=True, name='biases')
        affine1 = tf.nn.relu_layer(input_op, kernel, biases, name=name)
        return affine1

def mpool_op(input_op, name, kh, kw, dh, dw):
    return tf.nn.max_pool(input_op,
                          ksize=[1, kh, kw, 1],
                          strides=[1, dh, dw, 1],
                          padding='VALID',
                          name=name)

def loss(logits, labels):
    labels = tf.expand_dims(labels, 1)
    indices = tf.expand_dims(tf.range(0, batch_size, 1), 1)
    concated = tf.concat(1, [indices, labels])
    onehot_labels = tf.sparse_to_dense(concated, tf.pack([batch_size, 10]), 1.0, 0.0)
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, onehot_labels, name='xentropy')
    loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')
    return loss

def inference(input_op):

    # assume input_op shape is 224x224x3

    # block 1 -- outputs 112x112x64
    conv1_1 = conv_op(input_op, name="conv1_1", kh=3, kw=3, n_in=3, n_out=64, dh=1, dw=1)
    conv1_2 = conv_op(conv1_1,  name="conv1_2", kh=3, kw=3, n_in=3, n_out=64, dh=1, dw=1)
    pool1 = mpool_op(conv1_2,   name="pool1",   kh=2, kw=2, dw=2, dh=2)

    # block 2 -- outputs 56x56x128
    conv2_1 = conv_op(pool1,    name="conv2_1", kh=3, kw=3, n_in=64,  n_out=128, dh=1, dw=1)
    conv2_2 = conv_op(conv2_1,  name="conv2_2", kh=3, kw=3, n_in=128, n_out=128, dh=1, dw=1)
    pool2 = mpool_op(conv2_2,   name="pool2",   kh=2, kw=2, dh=2, dw=2)

    # block 3 -- outputs 28x28x256
    conv3_1 = conv_op(pool2,    name="conv3_1", kh=3, kw=3, n_in=128, n_out=256, dh=1, dw=1)
    conv3_2 = conv_op(conv3_1,  name="conv3_2", kh=3, kw=3, n_in=256, n_out=256, dh=1, dw=1)
    pool3 = mpool_op(conv3_2,   name="pool3",   kh=2, kw=2, dh=2, dw=2)

    # block 4 -- outputs 14x14x512
    conv4_1 = conv_op(pool3,    name="conv4_1", kh=3, kw=3, n_in=256, n_out=512, dh=1, dw=1)
    conv4_2 = conv_op(conv4_1,  name="conv4_2", kh=3, kw=3, n_in=512, n_out=512, dh=1, dw=1)
    conv4_3 = conv_op(conv4_2,  name="conv4_2", kh=3, kw=3, n_in=512, n_out=512, dh=1, dw=1)
    pool4 = mpool_op(conv4_3,   name="pool4",   kh=2, kw=2, dh=2, dw=2)

    # block 5 -- outputs 7x7x512
    conv5_1 = conv_op(pool4,    name="conv5_1", kh=3, kw=3, n_in=512, n_out=512, dh=1, dw=1)
    conv5_2 = conv_op(conv5_1,  name="conv5_2", kh=3, kw=3, n_in=512, n_out=512, dh=1, dw=1)
    conv5_3 = conv_op(conv5_2,  name="conv5_3", kh=3, kw=3, n_in=512, n_out=512, dh=1, dw=1)
    pool5 = mpool_op(conv8,     name="pool5",   kh=2, kw=2, dw=2, dh=2)

    # flatten
    resh1 = tf.reshape(pool8, [-1, 512*7*7], name="resh1")

    # fully connected
    fc6 = affine_op(resh1,      name="fc6", n_in=512*7*7, n_out=4096)
    fc7 = affine_op(fc7,        name="fc7", n_in=4096, n_out=10)
    fc8 = affine_op(fc8,        name="fc8", n_in=10, n_out=10)
    return fc8

def random_test_input():
    """
    this generates random test input, useful for debugging
    """
    sz = 224
    channels = 3
    init_val = tf.random_normal(
        (batch_size, sz, sz, channels),
        dtype=tf.float32,
        stddev=1
    )
    images = tf.Variable(init_val)
    labels = tf.Variable(tf.ones([batch_size], dtype=tf.int32))
    return images, labels

def train(lr=0.00001, max_step=1000):
    """
    train model

    :param lr:  This is the learning rate
    """
    with tf.Graph().as_default():

        in_images = tf.placeholder("float", [batch_size, 32, 32, 3])
        images = tf.image.resize_images(in_images, 224, 224)
        labels = tf.placeholder("int32", [batch_size])

        # Build a Graph that computes the logits predictions from the
        # inference model.
        last_layer = inference(images)

        # Add a simple objective so we can calculate the backward pass.
        objective = loss(last_layer, labels)
        optimizer = tf.train.AdagradOptimizer(lr)
        global_step = tf.Variable(0, name="global_step", trainable=False)
        train_step = optimizer.minimize(objective)

        # grab variables we want to log
        tf.scalar_summary("loss", objective)
        tf.image_summary("images", images)

        summaries = tf.merge_all_summaries()

        # Build an initialization operation.
        initializer = tf.initialize_all_variables()

        # Start running operations on the Graph.
        with tf.Session() as sess:
            sess.run(initializer)
            writer = tf.train.SummaryWriter("train_logs", graph_def=sess.graph_def)
            for i in range(max_step):
                trn, tst = dataset.get_cifar10(batch_size)
                for batch in trn:
                    X = np.vstack(batch[0]).reshape(-1, 3, 32, 32).transpose(0, 2, 3, 1)
                    Y = np.array(batch[1])
                    result = sess.run(
                        [train_step, summaries, objective],
                        feed_dict = {
                            in_images: X,
                            labels: Y
                        }
                    )
                    writer.add_summary(result[1], i)
                    print i, result[2]
                    if result[2] is np.NaN:
                        return

if __name__ == '__main__':
    train()
<commit_msg>added batch iterator<commit_after># Adapted from https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10.py

from datetime import datetime
import math
import time
import numpy as np
import dataset
import tensorflow.python.platform
import tensorflow as tf

batch_size = 2

def conv_op(input_op, name, kw, kh, n_in, n_out, dw, dh):
    with tf.name_scope(name) as scope:
        kernel_init_val = tf.truncated_normal([kh, kw, n_in, n_out], dtype=tf.float32, stddev=1e-1)
        kernel = tf.Variable(kernel_init_val, trainable=True, name='w')
        conv = tf.nn.conv2d(input_op, kernel, (1, dh, dw, 1), padding='SAME')
        bias_init_val = tf.constant(0.0, shape=[n_out], dtype=tf.float32)
        biases = tf.Variable(bias_init_val, trainable=True, name='b')
        z = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())
        activation = tf.nn.relu(z, name=scope)
        return activation

def fc_op(input_op, name, n_in, n_out):
    with tf.name_scope(name) as scope:
        kernel = tf.Variable(tf.truncated_normal([n_in, n_out], dtype=tf.float32, stddev=1e-1), trainable=True, name='weights')
        biases = tf.Variable(tf.constant(0.0, shape=[n_out], dtype=tf.float32), trainable=True, name='biases')
        activation = tf.nn.relu_layer(input_op, kernel, biases, name=name)
        return activation

def mpool_op(input_op, name, kh, kw, dh, dw):
    return tf.nn.max_pool(input_op,
                          ksize=[1, kh, kw, 1],
                          strides=[1, dh, dw, 1],
                          padding='VALID',
                          name=name)

def loss(logits, labels):
    labels = tf.expand_dims(labels, 1)
    indices = tf.expand_dims(tf.range(0, batch_size, 1), 1)
    concated = tf.concat(1, [indices, labels])
    onehot_labels = tf.sparse_to_dense(concated, tf.pack([batch_size, 10]), 1.0, 0.0)
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, onehot_labels, name='xentropy')
    loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')
    return loss

def inference(input_op, dropout_keep_prob):

    # assume input_op shape is 224x224x3

    # block 1 -- outputs 112x112x64
    conv1_1 = conv_op(input_op, name="conv1_1", kh=3, kw=3, n_in=3,  n_out=64, dh=1, dw=1)
    conv1_2 = conv_op(conv1_1,  name="conv1_2", kh=3, kw=3, n_in=64, n_out=64, dh=1, dw=1)
    pool1 = mpool_op(conv1_2,   name="pool1",   kh=2, kw=2, dw=2, dh=2)

    # block 2 -- outputs 56x56x128
    conv2_1 = conv_op(pool1,    name="conv2_1", kh=3, kw=3, n_in=64,  n_out=128, dh=1, dw=1)
    conv2_2 = conv_op(conv2_1,  name="conv2_2", kh=3, kw=3, n_in=128, n_out=128, dh=1, dw=1)
    pool2 = mpool_op(conv2_2,   name="pool2",   kh=2, kw=2, dh=2, dw=2)

    # block 3 -- outputs 28x28x256
    conv3_1 = conv_op(pool2,    name="conv3_1", kh=3, kw=3, n_in=128, n_out=256, dh=1, dw=1)
    conv3_2 = conv_op(conv3_1,  name="conv3_2", kh=3, kw=3, n_in=256, n_out=256, dh=1, dw=1)
    pool3 = mpool_op(conv3_2,   name="pool3",   kh=2, kw=2, dh=2, dw=2)

    # block 4 -- outputs 14x14x512
    conv4_1 = conv_op(pool3,    name="conv4_1", kh=3, kw=3, n_in=256, n_out=512, dh=1, dw=1)
    conv4_2 = conv_op(conv4_1,  name="conv4_2", kh=3, kw=3, n_in=512, n_out=512, dh=1, dw=1)
    conv4_3 = conv_op(conv4_2,  name="conv4_2", kh=3, kw=3, n_in=512, n_out=512, dh=1, dw=1)
    pool4 = mpool_op(conv4_3,   name="pool4",   kh=2, kw=2, dh=2, dw=2)

    # block 5 -- outputs 7x7x512
    conv5_1 = conv_op(pool4,    name="conv5_1", kh=3, kw=3, n_in=512, n_out=512, dh=1, dw=1)
    conv5_2 = conv_op(conv5_1,  name="conv5_2", kh=3, kw=3, n_in=512, n_out=512, dh=1, dw=1)
    conv5_3 = conv_op(conv5_2,  name="conv5_3", kh=3, kw=3, n_in=512, n_out=512, dh=1, dw=1)
    pool5 = mpool_op(conv5_3,   name="pool5",   kh=2, kw=2, dw=2, dh=2)

    # flatten
    resh1 = tf.reshape(pool5, [-1, 512*7*7], name="resh1")

    # fully connected

    fc6 = fc_op(resh1, name="fc6", n_in=512*7*7, n_out=4096)
    fc6_drop = tf.nn.dropout(fc6, dropout_keep_prob, name="fc6_drop")

    fc7 = fc_op(fc6_drop, name="fc7", n_in=4096, n_out=10)
    fc7_drop = tf.nn.dropout(fc7, dropout_keep_prob, name="fc6_drop")

    fc8 = fc_op(fc7_drop, name="fc8", n_in=10, n_out=10)
    return fc8

def random_test_input():
    """
    this generates random test input, useful for debugging
    """
    sz = 224
    channels = 3
    init_val = tf.random_normal(
        (batch_size, sz, sz, channels),
        dtype=tf.float32,
        stddev=1
    )
    images = tf.Variable(init_val)
    labels = tf.Variable(tf.ones([batch_size], dtype=tf.int32))
    return images, labels

def train(lr=0.00001, max_step=1000):
    """
    train model

    :param lr:  This is the learning rate
    """
    with tf.Graph().as_default():

        in_images = tf.placeholder("float", [batch_size, 32, 32, 3])
        images = tf.image.resize_images(in_images, 224, 224)
        labels = tf.placeholder("int32", [batch_size])
        dropout_keep_prob = tf.placeholder("float")

        # Build a Graph that computes the logits predictions from the
        # inference model.
        last_layer = inference(images, dropout_keep_prob)

        # Add a simple objective so we can calculate the backward pass.
        objective = loss(last_layer, labels)
        optimizer = tf.train.AdagradOptimizer(lr)
        global_step = tf.Variable(0, name="global_step", trainable=False)
        train_step = optimizer.minimize(objective)

        # grab variables we want to log
        tf.scalar_summary("loss", objective)
        tf.image_summary("images", images)

        summaries = tf.merge_all_summaries()

        # Build an initialization operation.
        initializer = tf.initialize_all_variables()

        # Start running operations on the Graph.
        with tf.Session() as sess:
            sess.run(initializer)
            writer = tf.train.SummaryWriter("train_logs", graph_def=sess.graph_def)
            for i in range(max_step):
                trn, tst = dataset.get_cifar10(batch_size)
                for batch in trn:
                    X = np.vstack(batch[0]).reshape(-1, 3, 32, 32).transpose(0, 2, 3, 1)
                    Y = np.array(batch[1])
                    result = sess.run(
                        [train_step, summaries, objective],
                        feed_dict = {
                            in_images: X,
                            labels: Y,
                            dropout_keep_prob: 0.5
                        }
                    )
                    writer.add_summary(result[1], i)
                    print i, result[2]
                    if result[2] is np.NaN:
                        return

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


<commit_before>from __future__ import print_function
import sys
import os
import json
import webbrowser
import urllib
import requests

from yapsy import IPlugin
from awsume import awsumepy

# Python 3 compatibility (python 3 has urlencode in parse sub-module)
URLENCODE = getattr(urllib, 'parse', urllib).urlencode

class AwsumeConsole(IPlugin.IPlugin):
    """The AWS Management Console plugin. Opens an assumed-role to the AWS management console."""
    TARGET_VERSION = '3.0.0'

    def add_arguments(self, argument_parser):
        """Add the console flag."""
        argument_parser.add_argument('-c', '--console',
                                     action='store_true',
                                     default=False,
                                     dest='open_console',
                                     help='Open the AWS console to the AWSume\'d credentials')
        return argument_parser

    def pre_awsume(self, app, args):
        """If no profile_name is given to AWSume, check the environment for credentials."""
        #use the environment variables to open
        if args.open_console is True and args.profile_name is None:
            credentials, region = self.get_environment_credentials()
            response = self.make_aws_federation_request(credentials)
            signin_token = self.get_signin_token(response)
            console_url = self.get_console_url(signin_token, region)
            self.open_browser_to_url(console_url)
            exit(0)

    def post_awsume(self,
                    app,
                    args,
                    profiles,
                    user_session,
                    role_session):
        """Open the console using the currently AWSume'd credentials."""
        if args.open_console is True:
            if not role_session:
                awsumepy.safe_print('Cannot use these credentials to open the AWS Console.')
                return
            credentials, region = self.get_session_temp_credentials(role_session)
            response = self.make_aws_federation_request(credentials)
            signin_token = self.get_signin_token(response)
            console_url = self.get_console_url(signin_token, region)
            self.open_browser_to_url(console_url)

    def get_environment_credentials(self):
        """Get session credentials from the environment."""
        aws_region = 'us-east-1'
        if 'AWS_PROFILE' in os.environ:
            credentials_profiles = awsumepy.read_ini_file(awsumepy.AWS_CREDENTIALS_FILE)
            auto_profile = credentials_profiles[os.environ['AWS_PROFILE']]
            temp_credentials = {
                'sessionId': auto_profile['aws_access_key_id'],
                'sessionKey': auto_profile['aws_secret_access_key'],
                'sessionToken': auto_profile['aws_session_token']
            }
            if auto_profile.get('region'):
                aws_region = auto_profile.get('region')
        elif os.environ.get('AWS_ACCESS_KEY_ID') and os.environ.get('AWS_SECRET_ACCESS_KEY') and os.environ.get('AWS_SESSION_TOKEN'):
            temp_credentials = {
                'sessionId': os.environ['AWS_ACCESS_KEY_ID'],
                'sessionKey': os.environ['AWS_SECRET_ACCESS_KEY'],
                'sessionToken': os.environ['AWS_SESSION_TOKEN']
            }
            if os.environ.get('AWS_REGION'):
                aws_region = os.environ['AWS_REGION']
        else:
            awsumepy.safe_print('Cannot use these credentials to open the AWS Console.')
            exit(0)
        json_temp_credentials = json.dumps(temp_credentials)
        return json_temp_credentials, aws_region

    def get_session_temp_credentials(self, session):
        """Create a properly formatted json string of the given session. Return the session and the region to use."""
        if session.get('AccessKeyId') and session.get('SecretAccessKey') and session.get('SessionToken'):
            aws_region = 'us-east-1'
            temp_credentials = {
                'sessionId': session['AccessKeyId'],
                'sessionKey': session['SecretAccessKey'],
            }
            if 'SessionToken' in session:
                temp_credentials['sessionToken'] = session['SessionToken']
            if session.get('region'):
                aws_region = session['region']

            #format the credentials into a json formatted string
            json_temp_credentials = json.dumps(temp_credentials)
            return json_temp_credentials, aws_region
        awsumepy.safe_print('Cannot use these credentials to open the AWS Console.')
        exit(0)

    def make_aws_federation_request(self, temp_credentials):
        """Make the AWS federation request to get the signin token."""
        params = {
            'Action': 'getSigninToken',
            'Session': temp_credentials,
        }
        request_url = 'https://signin.aws.amazon.com/federation'
        response = requests.get(request_url, params=params)
        return response

    def get_signin_token(self, aws_response):
        """Get the signin token from the aws federation response."""
        return json.loads(aws_response.text)['SigninToken']

    def get_console_url(self, aws_signin_token, aws_region):
        """Get the url to open the browser to."""
        params = {
            'Action': 'login',
            'Issuer': '',
            'Destination': 'https://console.aws.amazon.com/console/home?region=' + aws_region,
            'SigninToken': aws_signin_token
        }
        url = 'https://signin.aws.amazon.com/federation?'
        url += URLENCODE(params)
        return url

    def open_browser_to_url(self, url):
        """Open the default browser to the given url. If that fails, display the url."""
        try:
            webbrowser.open(url)
        except Exception:
            awsumepy.safe_print('Cannot open browser, here is the link:')
            awsumepy.safe_print(url)
<commit_msg>Fixes reading region from auto-refresh- profiles<commit_after>from __future__ import print_function
import sys
import os
import json
import webbrowser
import urllib
import requests

from yapsy import IPlugin
from awsume import awsumepy

# Python 3 compatibility (python 3 has urlencode in parse sub-module)
URLENCODE = getattr(urllib, 'parse', urllib).urlencode

class AwsumeConsole(IPlugin.IPlugin):
    """The AWS Management Console plugin. Opens an assumed-role to the AWS management console."""
    TARGET_VERSION = '3.0.0'

    def add_arguments(self, argument_parser):
        """Add the console flag."""
        argument_parser.add_argument('-c', '--console',
                                     action='store_true',
                                     default=False,
                                     dest='open_console',
                                     help='Open the AWS console to the AWSume\'d credentials')
        return argument_parser

    def pre_awsume(self, app, args):
        """If no profile_name is given to AWSume, check the environment for credentials."""
        #use the environment variables to open
        if args.open_console is True and args.profile_name is None:
            credentials, region = self.get_environment_credentials()
            response = self.make_aws_federation_request(credentials)
            signin_token = self.get_signin_token(response)
            console_url = self.get_console_url(signin_token, region)
            self.open_browser_to_url(console_url)
            exit(0)

    def post_awsume(self,
                    app,
                    args,
                    profiles,
                    user_session,
                    role_session):
        """Open the console using the currently AWSume'd credentials."""
        if args.open_console is True:
            if not role_session:
                awsumepy.safe_print('Cannot use these credentials to open the AWS Console.')
                return
            credentials, region = self.get_session_temp_credentials(role_session)
            response = self.make_aws_federation_request(credentials)
            signin_token = self.get_signin_token(response)
            console_url = self.get_console_url(signin_token, region)
            self.open_browser_to_url(console_url)

    def get_environment_credentials(self):
        """Get session credentials from the environment."""
        aws_region = 'us-east-1'
        if 'AWS_PROFILE' in os.environ:
            credentials_profiles = awsumepy.read_ini_file(awsumepy.AWS_CREDENTIALS_FILE)
            auto_profile = credentials_profiles[os.environ['AWS_PROFILE']]
            temp_credentials = {
                'sessionId': auto_profile['aws_access_key_id'],
                'sessionKey': auto_profile['aws_secret_access_key'],
                'sessionToken': auto_profile['aws_session_token']
            }
            if auto_profile.get('aws_region'):
                aws_region = auto_profile.get('aws_region')
        elif os.environ.get('AWS_ACCESS_KEY_ID') and os.environ.get('AWS_SECRET_ACCESS_KEY') and os.environ.get('AWS_SESSION_TOKEN'):
            temp_credentials = {
                'sessionId': os.environ['AWS_ACCESS_KEY_ID'],
                'sessionKey': os.environ['AWS_SECRET_ACCESS_KEY'],
                'sessionToken': os.environ['AWS_SESSION_TOKEN']
            }
            if os.environ.get('AWS_REGION'):
                aws_region = os.environ['AWS_REGION']
        else:
            awsumepy.safe_print('Cannot use these credentials to open the AWS Console.')
            exit(0)
        json_temp_credentials = json.dumps(temp_credentials)
        return json_temp_credentials, aws_region

    def get_session_temp_credentials(self, session):
        """Create a properly formatted json string of the given session. Return the session and the region to use."""
        if session.get('AccessKeyId') and session.get('SecretAccessKey') and session.get('SessionToken'):
            aws_region = 'us-east-1'
            temp_credentials = {
                'sessionId': session['AccessKeyId'],
                'sessionKey': session['SecretAccessKey'],
            }
            if 'SessionToken' in session:
                temp_credentials['sessionToken'] = session['SessionToken']
            if session.get('region'):
                aws_region = session['region']

            #format the credentials into a json formatted string
            json_temp_credentials = json.dumps(temp_credentials)
            return json_temp_credentials, aws_region
        awsumepy.safe_print('Cannot use these credentials to open the AWS Console.')
        exit(0)

    def make_aws_federation_request(self, temp_credentials):
        """Make the AWS federation request to get the signin token."""
        params = {
            'Action': 'getSigninToken',
            'Session': temp_credentials,
        }
        request_url = 'https://signin.aws.amazon.com/federation'
        response = requests.get(request_url, params=params)
        return response

    def get_signin_token(self, aws_response):
        """Get the signin token from the aws federation response."""
        return json.loads(aws_response.text)['SigninToken']

    def get_console_url(self, aws_signin_token, aws_region):
        """Get the url to open the browser to."""
        params = {
            'Action': 'login',
            'Issuer': '',
            'Destination': 'https://console.aws.amazon.com/console/home?region=' + aws_region,
            'SigninToken': aws_signin_token
        }
        url = 'https://signin.aws.amazon.com/federation?'
        url += URLENCODE(params)
        return url

    def open_browser_to_url(self, url):
        """Open the default browser to the given url. If that fails, display the url."""
        try:
            webbrowser.open(url)
        except Exception:
            awsumepy.safe_print('Cannot open browser, here is the link:')
            awsumepy.safe_print(url)
<|endoftext|>


<commit_before>#BEGIN_HEADER
#END_HEADER

class WipeDev03:
    '''
    Module Name:
    WipeDev03

    Module Description:
    Destroy the dev03 workspace, handle, and shock databases.
    '''

    ######## WARNING FOR GEVENT USERS #######
    # Since asynchronous IO can lead to methods - even the same method -
    # interrupting each other, you must be *very* careful when using global
    # state. A method could easily clobber the state set by another while
    # the latter method is running.
    #########################################
    #BEGIN_CLASS_HEADER
    #END_CLASS_HEADER

    # config contains contents of config file in a hash or None if it couldn't
    # be found
    def __init__(self, config):
        #BEGIN_CONSTRUCTOR
        #END_CONSTRUCTOR
        pass

    def wipe_dev03(self):
        # self.ctx is set by the wsgi application class
        # return variables are: err_code, output
        #BEGIN wipe_dev03
        #END wipe_dev03

        #At some point might do deeper type checking...
        if not isinstance(err_code, int):
            raise ValueError('Method wipe_dev03 return value ' +
                             'err_code is not type int as required.')
        if not isinstance(output, basestring):
            raise ValueError('Method wipe_dev03 return value ' +
                             'output is not type basestring as required.')
        # return the results
        return [err_code, output]
<commit_msg>Add db dropping code, needs testing on dev03<commit_after>#BEGIN_HEADER
import subprocess
from subprocess import CalledProcessError
import pymongo

ALLOWED_USER = 'lolcatservice'
KB_SERVICES = 'kb/deployment/services/'
WS_START = KB_SERVICES + 'workspace/start_service'
WS_STOP = KB_SERVICES + 'workspace/stop_service'
SHOCK_START = KB_SERVICES + 'shock_service/start_service'
SHOCK_STOP = KB_SERVICES + 'shock_service/stop_service'
MONGO_HOST = 'localhost'
WS_DB = 'workspace'
WS_COL_SETTINGS = 'settings'
SHOCK_DB = 'ShockDB'
SHOCK_FILES = '/mnt/Shock/data/*'
MYSQL_CMD = 'mysql -u root -e "truncate table hsi.Handle;"'

def run_command(self, command):
    err = 0
    try:
        output = subprocess.check_output(command, stderr=subprocess.STDOUT)
    except CalledProcessError as cpe:
        err = cpe.returncode
        output = cpe.output
    return err, output

#END_HEADER

class WipeDev03:
    '''
    Module Name:
    WipeDev03

    Module Description:
    Destroy the dev03 workspace, handle, and shock databases.
    '''

    ######## WARNING FOR GEVENT USERS #######
    # Since asynchronous IO can lead to methods - even the same method -
    # interrupting each other, you must be *very* careful when using global
    # state. A method could easily clobber the state set by another while
    # the latter method is running.
    #########################################
    #BEGIN_CLASS_HEADER
    #END_CLASS_HEADER

    # config contains contents of config file in a hash or None if it couldn't
    # be found
    def __init__(self, config):
        #BEGIN_CONSTRUCTOR
        #END_CONSTRUCTOR
        pass

    def wipe_dev03(self):
        # self.ctx is set by the wsgi application class
        # return variables are: err_code, output
        #BEGIN wipe_dev03
        output = ''
        if self.ctx.user_id != ALLOWED_USER:
            raise Exception("User unauthorized")

        err_code, out = run_command(WS_STOP)
        output += out
        if err_code > 0:
            return err_code, output

        err_code, out = run_command(SHOCK_STOP)
        output += out
        if err_code > 0:
            return err_code, output

        # save the workspace settings
        mc = pymongo.MongoClient(MONGO_HOST)
        settings = mc[WS_DB][WS_COL_SETTINGS].find_one()

        mc.drop_database(WS_DB)
        mc.drop_database(SHOCK_DB)

        # restore the workspace settings
        mc[WS_DB][WS_COL_SETTINGS].save(settings)

        # delete the shock files
        err_code, out = run_command(['rm', '-r', SHOCK_FILES])
        output += out
        if err_code > 0:
            return err_code, output

        # delete the handle service DB
        # should really do this via a client, but it's not worth the effort
        err_code, out = run_command(MYSQL_CMD)
        output += out
        if err_code > 0:
            return err_code, output

        err_code, out = run_command(SHOCK_START)
        output += out
        if err_code > 0:
            return err_code, output

        err_code, out = run_command(WS_START)
        output += out
        if err_code > 0:
            return err_code, output

        #END wipe_dev03

        #At some point might do deeper type checking...
        if not isinstance(err_code, int):
            raise ValueError('Method wipe_dev03 return value ' +
                             'err_code is not type int as required.')
        if not isinstance(output, basestring):
            raise ValueError('Method wipe_dev03 return value ' +
                             'output is not type basestring as required.')
        # return the results
        return [err_code, output]
<|endoftext|>


<commit_before>import helpmio.question
import helpmio.session
import os
import tornado.httpserver
import tornado.web
import tornado.websocket
import json
import re

def init(port):
    template_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "..", "assets", "templates")
    static_files_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "..", "assets", "web")
    favicon_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "..", "assets")
    app = tornado.web.Application([
        tornado.web.url(r"/", MainHandler, name="main"),
        tornado.web.url(r"/login", LoginHandler, name="login"),
        tornado.web.url(r"/questions/new", NewQuestionHandler, name="new_question"),
        tornado.web.url(r"/questions/(.*)", QuestionHandler, name="question"),
        tornado.web.url(r"/ws/(.*)", QuestionWebSocketHandler, name="question_websocket"),
        tornado.web.url(r"/assets/(.*)", tornado.web.StaticFileHandler, {"path": static_files_path}, name="static"),
        tornado.web.url(r"/(favicon\.ico)", tornado.web.StaticFileHandler, {"path": favicon_path}, name="favicon")
    ], template_path=template_path, debug=True)
    server = tornado.httpserver.HTTPServer(app)
    server.listen(port)

def _inject_sessions(func):
    def inner(self, *args, **kwargs):
        sid = self.get_cookie("sid", None)
        session = helpmio.session.get_session(sid)
        if session is None:
            session = helpmio.session.new_session()
            self.set_cookie("sid", session.get_sid())
        self.session = session
        func(self, *args, **kwargs)
    return inner

class BaseHandler(tornado.web.RequestHandler):

    def get_template_namespace(self):
        namespace = super(BaseHandler, self).get_template_namespace()
        namespace["session"] = self.session
        return namespace

class MainHandler(BaseHandler):

    @_inject_sessions
    def get(self):
        tag = self.get_argument("tag", default=None)
        questions = helpmio.question.filter_questions(is_resolved=False, tag=tag)
        self.render("question_list.html", questions=questions)

class LoginHandler(BaseHandler):

    @_inject_sessions
    def get(self):
        if self.get_query_argument("lo", "") == "1":
            del self.session["logged_in"]
            del self.session["nickname"]
            self.redirect(self.reverse_url("main"))
            return
        self.render("login.html", error=None)

    @_inject_sessions
    def post(self):
        username = self.get_body_argument("username")
        if username == "":
            self.render("login.html", error="This field cannot be empty.")
            return
        self.session["logged_in"] = True
        self.session["nickname"] = username
        self.redirect(self.reverse_url("main"))

class NewQuestionHandler(BaseHandler):

    @_inject_sessions
    def get(self):
        self.render("new_question.html",
            title_error=None, description_error=None,
            title="", description="", tags="")

    @_inject_sessions
    def post(self):
        nickname = self.session["nickname"]
        title = self.get_body_argument("title")
        description = self.get_body_argument("description")
        tags = self.get_body_argument("tags")
        title_error = None
        description_error = None
        if title == "":
            title_error = "This field cannot be empty."
        if description == "":
            description_error = "This field cannot be empty."
        if title_error or description_error:
            self.render("new_question.html",
                title_error=title_error, description_error=description_error,
                title=title, description=description, tags=tags)
            return
        self.redirect(self.reverse_url("question",
            helpmio.question.new_question(
                nickname, title, description, re.split(r"[, ]+", tags)).get_qid()))

class QuestionHandler(BaseHandler):

    @_inject_sessions
    def get(self, qid):
        self.render("question.html", question=helpmio.question.get_question(qid))

class QuestionWebSocketHandler(tornado.websocket.WebSocketHandler):

    @_inject_sessions
    def open(self, qid):
        self._nickname = self.session["nickname"]
        question = helpmio.question.get_question(qid)
        self._chatroom = question.get_chatroom()
        self._connection_id = self._chatroom.connect(self._nickname)
        self._connect_cid = self._chatroom.on_connect.subscribe(self.connect_recieved)
        self._disconnect_cid = self._chatroom.on_disconnect.subscribe(self.disconnect_recieved)
        self._chat_cid = self._chatroom.on_chat.subscribe(self.chat_recieved)
        userlist = [{"connection_id": connection_id,
                     "nickname": nickname,
                     "is_asker": self._chatroom.get_asker_name() == nickname}
                     for connection_id, nickname in self._chatroom.get_all_users().items()]
        onlinelist = [connection_id for connection_id in self._chatroom.get_connected_users()]
        chat_history = self._chatroom.get_chat_history()
        message = {"type": "curstate", "data": {"userlist": userlist, "onlinelist": onlinelist, "history": chat_history}}
        self.write_message(json.dumps(message))

    def on_message(self, message):
        message_obj = json.loads(message)
        message_type = message_obj["type"]
        data = message_obj["data"]
        if message_type == "message":
            if self._nickname != None:
                self._chatroom.add_chat(self._connection_id, data)
            else:
                print("user cannot send message without nickname")
        else:
            print("invalid client message type: '{}'".format(message_type))

    def on_close(self):
        self._chatroom.on_connect.unsubscribe(self._connect_cid)
        self._chatroom.on_disconnect.unsubscribe(self._disconnect_cid)
        self._chatroom.on_chat.unsubscribe(self._chat_cid)
        self._chatroom.disconnect(self._connection_id)

    def connect_recieved(self, connected_id):
        nickname = self._chatroom.get_user(connected_id)
        message = {"type": "connect", "data": {"connection_id": connected_id, "nickname": nickname, "is_asker": self._chatroom.get_asker_name() == nickname}}
        self.write_message(json.dumps(message))

    def disconnect_recieved(self, disconnected_id):
        message = {"type": "disconnect", "data": {"connection_id": disconnected_id}}
        self.write_message(json.dumps(message))

    def chat_recieved(self, chat):
        sender_connection_id, text = chat
        if text == "":
            return
        nickname = self._chatroom.get_user(sender_connection_id)
        if nickname != None:
            message = {"type": "message", "data": {"connection_id": sender_connection_id, "message": text}}
            self.write_message(json.dumps(message))
<commit_msg>added notification websocket<commit_after>import helpmio.question
import helpmio.session
import os
import tornado.httpserver
import tornado.web
import tornado.websocket
import json
import re

def init(port):
    template_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "..", "assets", "templates")
    static_files_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "..", "assets", "web")
    favicon_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "..", "assets")
    app = tornado.web.Application([
        tornado.web.url(r"/", MainHandler, name="main"),
        tornado.web.url(r"/login", LoginHandler, name="login"),
        tornado.web.url(r"/questions/new", NewQuestionHandler, name="new_question"),
        tornado.web.url(r"/questions/(.*)", QuestionHandler, name="question"),
        tornado.web.url(r"/ws/(.*)", QuestionWebSocketHandler, name="question_websocket"),
        tornado.web.url(r"/assets/(.*)", tornado.web.StaticFileHandler, {"path": static_files_path}, name="static"),
        tornado.web.url(r"/(favicon\.ico)", tornado.web.StaticFileHandler, {"path": favicon_path}, name="favicon")
    ], template_path=template_path, debug=True)
    server = tornado.httpserver.HTTPServer(app)
    server.listen(port)

def _inject_sessions(func):
    def inner(self, *args, **kwargs):
        sid = self.get_cookie("sid", None)
        session = helpmio.session.get_session(sid)
        if session is None:
            session = helpmio.session.new_session()
            self.set_cookie("sid", session.get_sid())
        self.session = session
        func(self, *args, **kwargs)
    return inner

class BaseHandler(tornado.web.RequestHandler):

    def get_template_namespace(self):
        namespace = super(BaseHandler, self).get_template_namespace()
        namespace["session"] = self.session
        return namespace

class MainHandler(BaseHandler):

    @_inject_sessions
    def get(self):
        tag = self.get_argument("tag", default=None)
        questions = helpmio.question.filter_questions(is_resolved=False, tag=tag)
        self.render("question_list.html", questions=questions)

class LoginHandler(BaseHandler):

    @_inject_sessions
    def get(self):
        if self.get_query_argument("lo", "") == "1":
            del self.session["logged_in"]
            del self.session["nickname"]
            self.redirect(self.reverse_url("main"))
            return
        self.render("login.html", error=None)

    @_inject_sessions
    def post(self):
        username = self.get_body_argument("username")
        if username == "":
            self.render("login.html", error="This field cannot be empty.")
            return
        self.session["logged_in"] = True
        self.session["nickname"] = username
        self.redirect(self.reverse_url("main"))

class NewQuestionHandler(BaseHandler):

    @_inject_sessions
    def get(self):
        self.render("new_question.html",
            title_error=None, description_error=None,
            title="", description="", tags="")

    @_inject_sessions
    def post(self):
        nickname = self.session["nickname"]
        title = self.get_body_argument("title")
        description = self.get_body_argument("description")
        tags = self.get_body_argument("tags")
        title_error = None
        description_error = None
        if title == "":
            title_error = "This field cannot be empty."
        if description == "":
            description_error = "This field cannot be empty."
        if title_error or description_error:
            self.render("new_question.html",
                title_error=title_error, description_error=description_error,
                title=title, description=description, tags=tags)
            return
        self.redirect(self.reverse_url("question",
            helpmio.question.new_question(
                nickname, title, description, re.split(r"[, ]+", tags)).get_qid()))

class QuestionHandler(BaseHandler):

    @_inject_sessions
    def get(self, qid):
        self.render("question.html", question=helpmio.question.get_question(qid))

class QuestionWebSocketHandler(tornado.websocket.WebSocketHandler):

    @_inject_sessions
    def open(self, qid):
        self._nickname = self.session["nickname"]
        question = helpmio.question.get_question(qid)
        self._chatroom = question.get_chatroom()
        self._connection_id = self._chatroom.connect(self._nickname)
        self._connect_cid = self._chatroom.on_connect.subscribe(self.connect_recieved)
        self._disconnect_cid = self._chatroom.on_disconnect.subscribe(self.disconnect_recieved)
        self._chat_cid = self._chatroom.on_chat.subscribe(self.chat_recieved)
        userlist = [{"connection_id": connection_id,
                     "nickname": nickname,
                     "is_asker": self._chatroom.get_asker_name() == nickname}
                     for connection_id, nickname in self._chatroom.get_all_users().items()]
        onlinelist = [connection_id for connection_id in self._chatroom.get_connected_users()]
        chat_history = self._chatroom.get_chat_history()
        message = {"type": "curstate", "data": {"userlist": userlist, "onlinelist": onlinelist, "history": chat_history}}
        self.write_message(json.dumps(message))

    def on_message(self, message):
        message_obj = json.loads(message)
        message_type = message_obj["type"]
        data = message_obj["data"]
        if message_type == "message":
            if self._nickname != None:
                self._chatroom.add_chat(self._connection_id, data)
            else:
                print("user cannot send message without nickname")
        else:
            print("invalid client message type: '{}'".format(message_type))

    def on_close(self):
        self._chatroom.on_connect.unsubscribe(self._connect_cid)
        self._chatroom.on_disconnect.unsubscribe(self._disconnect_cid)
        self._chatroom.on_chat.unsubscribe(self._chat_cid)
        self._chatroom.disconnect(self._connection_id)

    def connect_recieved(self, connected_id):
        nickname = self._chatroom.get_user(connected_id)
        message = {"type": "connect", "data": {"connection_id": connected_id, "nickname": nickname, "is_asker": self._chatroom.get_asker_name() == nickname}}
        self.write_message(json.dumps(message))

    def disconnect_recieved(self, disconnected_id):
        message = {"type": "disconnect", "data": {"connection_id": disconnected_id}}
        self.write_message(json.dumps(message))

    def chat_recieved(self, chat):
        sender_connection_id, text = chat
        if text == "":
            return
        nickname = self._chatroom.get_user(sender_connection_id)
        if nickname != None:
            message = {"type": "message", "data": {"connection_id": sender_connection_id, "message": text}}
            self.write_message(json.dumps(message))

class NotificationWebSocketHandler(tornado.websocket.WebSocketHandler):

    @_inject_sessions
    def open(self):
        self._connect_cids = dict()
        self._disconnect_cids = dict()
        self._chat_cids = dict()
        for qid in self._qids:
            chatroom = helpmio.question.get_question(qid).get_chatroom()
            self._connect_cids[qid] = chatroom.on_connect.subscribe(lambda connected_id: self.notification_recieved(qid, connected_id, "connect"))
            self._disconnect_cids[qid] = chatroom.on_disconnect.subscribe(lambda disconnected_id: self.notification_recieved(qid, disconnected_id, "disconnect"))
            self._chat_cids[qid] = chatroom.on_chat.subscribe(lambda chat: self.notification_recieved(qid, chat[0]), "chat")

    def on_close(self):
        for qid in self._qids:
            chatroom = helpmio.question.get_question(qid).get_chatroom()
            chatroom.on_connect.unsubscribe(self._connect_cids[qid])
            chatroom.on_disconnect.unsubscribe(self._disconnect_cids[qid])
            chatroom.on_chat.unsubscribe(self._chat_cids[qid])

    def notification_recieved(self, qid, connection_id, activity):
        q = question.get_question(qid)
        message = {"type": "connect", "data": {
            "qid": qid,
            "nickname": q.get_chatroom().get_user(connection_id),
            "activity": activity
            }}
        self.write_message(json.dumps(message))
<|endoftext|>


<commit_before>import sys, os, argparse, pkg_resources
from hermes.GrammarFileParser import GrammarFileParser, HermesParserFactory
from hermes.GrammarAnalyzer import GrammarAnalyzer
from hermes.GrammarCodeGenerator import FactoryFactory as TemplateFactoryFactory
from hermes.GrammarCodeGenerator import TemplateWriter
from hermes.Logger import Factory as LoggerFactory
from hermes.Theme import TerminalDefaultTheme, TerminalColorTheme

def Cli():

  ver = sys.version_info

  # Version 3.2 required for argparse
  if ver.major < 3 or (ver.major == 3 and ver.minor < 2):
    print("Python 3.2+ required. {}.{}.{} installed".format(ver.major, ver.minor, ver.micro))
    sys.exit(-1)

  command_help = {
    "analyze": "Analyze a grammer, find conflicts, and print out first/follow sets",
    "generate": "Generate the code for a parser"
  }

  parser = argparse.ArgumentParser(description='Hermes Parser Generator', epilog='(c) 2011-2013 Scott Frazer')
  parser.add_argument(
    '--version', action='version', version=str(pkg_resources.get_distribution('hermes-parser'))
  )
  parser.add_argument(
    '-D', '--debug', required=False, action='store_true', help='Open the floodgates'
  )
  parser.add_argument(
    '-c', '--color', required=False, action='store_true', help='Colorized output!'
  )

  subparsers = parser.add_subparsers(help='Parser Generator Actions', dest='action')
  commands = {}

  commands['analyze'] = subparsers.add_parser(
    'analyze', description=command_help['analyze'], help=command_help['analyze']
  )
  commands['generate'] = subparsers.add_parser(
    'generate', description=command_help['generate'], help=command_help['generate']
  )
  commands['generate'].add_argument(
    '--name', help='The name of the module'
  )
  commands['generate'].add_argument(
    'grammar', metavar='GRAMMAR', nargs='+', help='Grammar file'
  )
  commands['generate'].add_argument(
    '-d', '--directory', required=False, default='.', help='Directory to write generated code to'
  )
  commands['generate'].add_argument(
    '-l', '--language', required=False, default='python', choices=['c', 'java', 'python'], help = 'Language for generated parser'
  )
  commands['generate'].add_argument(
    '--java-package', required=False, help='If generating Java code, this is the package.'
  )
  commands['generate'].add_argument(
    '-m', '--add-main', required=False, action='store_true', help='If this is specified, a main() function will be generated in the source code.'
  )

  cli = parser.parse_args()
  logger = LoggerFactory().initialize(cli.debug)
  logger.debug('CLI Parameters: %s' % (cli))

  factory = HermesParserFactory()
  fp = GrammarFileParser(factory.create())

  grammars = []
  for grammar in cli.grammar:
    if not os.path.isfile( grammar ):
      sys.stderr.write("Error: Grammar file doesn't exist\n")
      sys.exit(-1)

    name = os.path.basename(grammar)

    if not name.endswith('.zgr'):
      sys.stderr.write("Error: Grammar file must have .zgr extension\n")
      sys.exit(-1)

    name = name[:-4]
    grammars.append( fp.parse(name, open(grammar)) )

  if cli.action == 'analyze':

    if cli.color:
      theme = TerminalColorTheme()
    else:
      theme = TerminalDefaultTheme()

    for grammar in grammars:
      analyzer = GrammarAnalyzer(grammar)
      analyzer.analyze( theme=theme )

  if cli.action == 'generate':
    cli.directory = os.path.abspath(os.path.expanduser(cli.directory))

    if not os.path.isdir( cli.directory ):
      sys.stderr.write("Error: Directory doesn't exist\n")
      sys.exit(-1)
    elif not os.access(cli.directory, os.W_OK):
      sys.stderr.write("Error: Directory not writable\n")
      sys.exit(-1)

    templateFactory = TemplateFactoryFactory().create(outputLanguage=cli.language.lower())
    templateWriter = TemplateWriter(templateFactory)
    templateWriter.write(grammars, cli.directory, addMain=cli.add_main, javaPackage=cli.java_package)

if __name__ == '__main__':
    Cli()
<commit_msg>Fixing CLI parser<commit_after>import sys, os, argparse, pkg_resources
from hermes.GrammarFileParser import GrammarFileParser, HermesParserFactory
from hermes.GrammarAnalyzer import GrammarAnalyzer
from hermes.GrammarCodeGenerator import FactoryFactory as TemplateFactoryFactory
from hermes.GrammarCodeGenerator import TemplateWriter
from hermes.Logger import Factory as LoggerFactory
from hermes.Theme import TerminalDefaultTheme, TerminalColorTheme

def Cli():

  ver = sys.version_info

  # Version 3.2 required for argparse
  if ver.major < 3 or (ver.major == 3 and ver.minor < 2):
    print("Python 3.2+ required. {}.{}.{} installed".format(ver.major, ver.minor, ver.micro))
    sys.exit(-1)

  command_help = {
    "analyze": "Analyze a grammer, find conflicts, and print out first/follow sets",
    "generate": "Generate the code for a parser"
  }

  parser = argparse.ArgumentParser(description='Hermes Parser Generator', epilog='(c) 2011-2013 Scott Frazer')
  parser.add_argument(
    '--version', action='version', version=str(pkg_resources.get_distribution('hermes-parser'))
  )
  parser.add_argument(
    '-D', '--debug', required=False, action='store_true', help='Open the floodgates'
  )
  parser.add_argument(
    '-c', '--color', required=False, action='store_true', help='Colorized output!'
  )

  subparsers = parser.add_subparsers(help='Parser Generator Actions', dest='action')
  commands = {}

  commands['analyze'] = subparsers.add_parser(
    'analyze', description=command_help['analyze'], help=command_help['analyze']
  )
  commands['analyze'].add_argument(
    'grammar', metavar='GRAMMAR', nargs='+', help='Grammar file'
  )
  commands['generate'] = subparsers.add_parser(
    'generate', description=command_help['generate'], help=command_help['generate']
  )
  commands['generate'].add_argument(
    '--name', help='The name of the module'
  )
  commands['generate'].add_argument(
    'grammar', metavar='GRAMMAR', nargs='+', help='Grammar file'
  )
  commands['generate'].add_argument(
    '-d', '--directory', required=False, default='.', help='Directory to write generated code to'
  )
  commands['generate'].add_argument(
    '-l', '--language', required=False, default='python', choices=['c', 'java', 'python'], help = 'Language for generated parser'
  )
  commands['generate'].add_argument(
    '--java-package', required=False, help='If generating Java code, this is the package.'
  )
  commands['generate'].add_argument(
    '-m', '--add-main', required=False, action='store_true', help='If this is specified, a main() function will be generated in the source code.'
  )

  cli = parser.parse_args()
  logger = LoggerFactory().initialize(cli.debug)
  logger.debug('CLI Parameters: %s' % (cli))

  factory = HermesParserFactory()
  fp = GrammarFileParser(factory.create())

  grammars = []
  for grammar in cli.grammar:
    if not os.path.isfile( grammar ):
      sys.stderr.write("Error: Grammar file doesn't exist\n")
      sys.exit(-1)

    name = os.path.basename(grammar)

    if not name.endswith('.zgr'):
      sys.stderr.write("Error: Grammar file must have .zgr extension\n")
      sys.exit(-1)

    name = name[:-4]
    grammars.append( fp.parse(name, open(grammar)) )

  if cli.action == 'analyze':

    if cli.color:
      theme = TerminalColorTheme()
    else:
      theme = TerminalDefaultTheme()

    for grammar in grammars:
      analyzer = GrammarAnalyzer(grammar)
      analyzer.analyze( theme=theme )

  if cli.action == 'generate':
    cli.directory = os.path.abspath(os.path.expanduser(cli.directory))

    if not os.path.isdir( cli.directory ):
      sys.stderr.write("Error: Directory doesn't exist\n")
      sys.exit(-1)
    elif not os.access(cli.directory, os.W_OK):
      sys.stderr.write("Error: Directory not writable\n")
      sys.exit(-1)

    templateFactory = TemplateFactoryFactory().create(outputLanguage=cli.language.lower())
    templateWriter = TemplateWriter(templateFactory)
    templateWriter.write(grammars, cli.directory, addMain=cli.add_main, javaPackage=cli.java_package)

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


<commit_before>import pandas
import matplotlib
import matplotlib.backends.backend_pdf
import seaborn

def get_degrees_for_metanode(graph, metanode):
    """
    Return a dataframe that reports the degree of each metaedge for
    each node of kind metanode.
    """
    metanode_to_nodes = graph.get_metanode_to_nodes()
    nodes = metanode_to_nodes.get(metanode, [])
    rows = list()
    for node in nodes:
        for metaedge, edges in node.edges.items():
            rows.append((str(node), node.name, str(metaedge), len(edges)))
    df = pandas.DataFrame(rows, columns=['node_id', 'node_name', 'metaedge', 'degree'])
    return df.sort_values(['node_name', 'metaedge'])

def plot_degrees_for_metanode(graph, metanode, col_wrap=2, facet_height=4):
    """
    Plots histograms of the degree distribution of each metaedge
    incident to the metanode. Each metaedge receives a facet in
    a seaborn.FacetGrid.
    """
    degree_df = get_degrees_for_metanode(graph, metanode)
    grid = seaborn.FacetGrid(degree_df, col='metaedge', sharex=False, sharey=False, col_wrap=col_wrap, size=facet_height)
    grid.map(seaborn.distplot, 'degree', kde=False)
    grid.set_titles('{col_name}')
    return grid

def plot_degrees(graph, path):
    """
    Creates a multipage pdf with a page for each metanode showing degree
    distributions.
    """
    # Temporarily disable `figure.max_open_warning`
    max_open = matplotlib.rcParams['figure.max_open_warning']
    matplotlib.rcParams['figure.max_open_warning'] = 0
    pdf_pages = matplotlib.backends.backend_pdf.PdfPages(path)
    for metanode in graph.metagraph.get_nodes():
        grid = plot_degrees_for_metanode(graph, metanode)
        grid.savefig(pdf_pages, format='pdf')
    pdf_pages.close()
    matplotlib.rcParams['figure.max_open_warning'] = max_open

def get_metanode_df(graph):
    rows = list()
    for metanode, nodes in graph.get_metanode_to_nodes().items():
        series = pandas.Series()
        series['metanode'] = metanode
        series['abbreviation'] = metanode.abbrev
        metaedges = set()
        for metaedge in metanode.edges:
            metaedges |= {metaedge, metaedge.inverse}
        series['metaedges'] = sum([not metaedge.inverted for metaedge in metaedges])
        series['nodes'] = len(nodes)
        series['unconnected_nodes'] = sum(not any(node.edges.values()) for node in nodes)
        rows.append(series)
    return pandas.DataFrame(rows).sort_values('metanode')

def get_metaedge_df(graph):
    rows = list()
    for metaedge, edges in graph.get_metaedge_to_edges().items():
        series = pandas.Series()
        series['metaedge'] = str(metaedge)
        series['abbreviation'] = metaedge.get_abbrev()
        series['inverted'] = int(metaedge.inverted)
        series['edges'] = len(edges)
        series['source_nodes'] = len(set(edge.source for edge in edges))
        series['target_nodes'] = len(set(edge.target for edge in edges))
        rows.append(series)
    return pandas.DataFrame(rows).sort_values('metaedge')
<commit_msg>Exclude inverted edges<commit_after>import pandas
import matplotlib
import matplotlib.backends.backend_pdf
import seaborn

def get_degrees_for_metanode(graph, metanode):
    """
    Return a dataframe that reports the degree of each metaedge for
    each node of kind metanode.
    """
    metanode_to_nodes = graph.get_metanode_to_nodes()
    nodes = metanode_to_nodes.get(metanode, [])
    rows = list()
    for node in nodes:
        for metaedge, edges in node.edges.items():
            rows.append((str(node), node.name, str(metaedge), len(edges)))
    df = pandas.DataFrame(rows, columns=['node_id', 'node_name', 'metaedge', 'degree'])
    return df.sort_values(['node_name', 'metaedge'])

def plot_degrees_for_metanode(graph, metanode, col_wrap=2, facet_height=4):
    """
    Plots histograms of the degree distribution of each metaedge
    incident to the metanode. Each metaedge receives a facet in
    a seaborn.FacetGrid.
    """
    degree_df = get_degrees_for_metanode(graph, metanode)
    grid = seaborn.FacetGrid(degree_df, col='metaedge', sharex=False, sharey=False, col_wrap=col_wrap, size=facet_height)
    grid.map(seaborn.distplot, 'degree', kde=False)
    grid.set_titles('{col_name}')
    return grid

def plot_degrees(graph, path):
    """
    Creates a multipage pdf with a page for each metanode showing degree
    distributions.
    """
    # Temporarily disable `figure.max_open_warning`
    max_open = matplotlib.rcParams['figure.max_open_warning']
    matplotlib.rcParams['figure.max_open_warning'] = 0
    pdf_pages = matplotlib.backends.backend_pdf.PdfPages(path)
    for metanode in graph.metagraph.get_nodes():
        grid = plot_degrees_for_metanode(graph, metanode)
        grid.savefig(pdf_pages, format='pdf')
    pdf_pages.close()
    matplotlib.rcParams['figure.max_open_warning'] = max_open

def get_metanode_df(graph):
    rows = list()
    for metanode, nodes in graph.get_metanode_to_nodes().items():
        series = pandas.Series()
        series['metanode'] = metanode
        series['abbreviation'] = metanode.abbrev
        metaedges = set()
        for metaedge in metanode.edges:
            metaedges |= {metaedge, metaedge.inverse}
        series['metaedges'] = sum([not metaedge.inverted for metaedge in metaedges])
        series['nodes'] = len(nodes)
        series['unconnected_nodes'] = sum(not any(node.edges.values()) for node in nodes)
        rows.append(series)

    metanode_df = pandas.DataFrame(rows).sort_values('metanode')
    return metanode_df

def get_metaedge_df(graph):
    rows = list()
    for metaedge, edges in graph.get_metaedge_to_edges(exclude_inverts=True).items():
        series = pandas.Series()
        series['metaedge'] = str(metaedge)
        series['abbreviation'] = metaedge.get_abbrev()
        series['edges'] = len(edges)
        series['source_nodes'] = len(set(edge.source for edge in edges))
        series['target_nodes'] = len(set(edge.target for edge in edges))
        rows.append(series)
    metaedge_df = pandas.DataFrame(rows).sort_values('metaedge')
    return metaedge_df
<|endoftext|>