<commit_before>#  Licensed under the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License. You may obtain
#  a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations
#  under the License.

import copy
import itertools
import operator

from keystoneauth1 import loading as ks_loading
from oslo_config import cfg

import neutron.agent.common.config
import neutron.agent.common.ovs_lib
import neutron.agent.dhcp.config
import neutron.agent.l2.extensions.manager
import neutron.agent.l3.config
import neutron.agent.l3.ha
import neutron.agent.linux.interface
import neutron.agent.linux.pd
import neutron.agent.linux.ra
import neutron.agent.metadata.config
import neutron.agent.ovsdb.api
import neutron.agent.securitygroups_rpc
import neutron.common.cache_utils
import neutron.conf.quota
import neutron.conf.service
import neutron.db.agents_db
import neutron.db.agentschedulers_db
import neutron.db.dvr_mac_db
import neutron.db.extraroute_db
import neutron.db.l3_agentschedulers_db
import neutron.db.l3_dvr_db
import neutron.db.l3_gwmode_db
import neutron.db.l3_hamode_db
import neutron.db.migration.cli
import neutron.extensions.allowedaddresspairs
import neutron.extensions.l3
import neutron.extensions.securitygroup
import neutron.plugins.ml2.config
import neutron.plugins.ml2.drivers.agent.config
import neutron.plugins.ml2.drivers.linuxbridge.agent.common.config
import neutron.plugins.ml2.drivers.macvtap.agent.config
import neutron.plugins.ml2.drivers.mech_sriov.agent.common.config
import neutron.plugins.ml2.drivers.mech_sriov.mech_driver.mech_driver
import neutron.plugins.ml2.drivers.openvswitch.agent.common.config
import neutron.plugins.ml2.drivers.type_flat
import neutron.plugins.ml2.drivers.type_geneve
import neutron.plugins.ml2.drivers.type_gre
import neutron.plugins.ml2.drivers.type_vlan
import neutron.plugins.ml2.drivers.type_vxlan
import neutron.services.metering.agents.metering_agent
import neutron.services.qos.notification_drivers.manager
import neutron.wsgi

NOVA_GROUP = 'nova'

CONF = cfg.CONF

deprecations = {'nova.cafile': [cfg.DeprecatedOpt('ca_certificates_file',
                                                  group=NOVA_GROUP)],
                'nova.insecure': [cfg.DeprecatedOpt('api_insecure',
                                                    group=NOVA_GROUP)],
                'nova.timeout': [cfg.DeprecatedOpt('url_timeout',
                                                   group=NOVA_GROUP)]}

_nova_options = ks_loading.register_session_conf_options(
            CONF, NOVA_GROUP, deprecated_opts=deprecations)

def list_agent_opts():
    return [
        ('agent',
         itertools.chain(
             neutron.agent.common.config.ROOT_HELPER_OPTS,
             neutron.agent.common.config.AGENT_STATE_OPTS,
             neutron.agent.common.config.IPTABLES_OPTS,
             neutron.agent.common.config.PROCESS_MONITOR_OPTS,
             neutron.agent.common.config.AVAILABILITY_ZONE_OPTS)
         ),
        ('DEFAULT',
         itertools.chain(
             neutron.agent.common.config.INTERFACE_DRIVER_OPTS,
             neutron.agent.metadata.config.SHARED_OPTS,
             neutron.agent.metadata.config.DRIVER_OPTS)
         )
    ]

def list_extension_opts():
    return [
        ('DEFAULT',
         neutron.extensions.allowedaddresspairs.allowed_address_pair_opts),
        ('quotas',
         itertools.chain(
             neutron.conf.quota.l3_quota_opts,
             neutron.conf.quota.security_group_quota_opts)
         )
    ]

def list_db_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.db.agents_db.AGENT_OPTS,
             neutron.db.extraroute_db.extra_route_opts,
             neutron.db.l3_gwmode_db.OPTS,
             neutron.db.agentschedulers_db.AGENTS_SCHEDULER_OPTS,
             neutron.db.dvr_mac_db.dvr_mac_address_opts,
             neutron.db.l3_dvr_db.router_distributed_opts,
             neutron.db.l3_agentschedulers_db.L3_AGENTS_SCHEDULER_OPTS,
             neutron.db.l3_hamode_db.L3_HA_OPTS)
         ),
        ('database',
         neutron.db.migration.cli.get_engine_config())
    ]

def list_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.common.config.core_cli_opts,
             neutron.common.config.core_opts,
             neutron.wsgi.socket_opts,
             neutron.conf.service.service_opts)
         ),
        (neutron.common.config.NOVA_CONF_SECTION,
         itertools.chain(
              neutron.common.config.nova_opts)
         ),
        ('quotas', neutron.conf.quota.core_quota_opts)
    ]

def list_qos_opts():
    return [
        ('qos',
         neutron.services.qos.notification_drivers.manager.QOS_PLUGIN_OPTS)
    ]

def list_base_agent_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.agent.linux.interface.OPTS,
             neutron.agent.common.config.INTERFACE_DRIVER_OPTS,
             neutron.agent.common.ovs_lib.OPTS)
         ),
        ('AGENT', neutron.agent.common.config.AGENT_STATE_OPTS)
    ]

def list_dhcp_agent_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.agent.dhcp.config.DHCP_AGENT_OPTS,
             neutron.agent.dhcp.config.DHCP_OPTS,
             neutron.agent.dhcp.config.DNSMASQ_OPTS)
         )
    ]

def list_linux_bridge_opts():
    return [
        ('linux_bridge',
         neutron.plugins.ml2.drivers.linuxbridge.agent.common.config.
         bridge_opts),
        ('vxlan',
         neutron.plugins.ml2.drivers.linuxbridge.agent.common.config.
         vxlan_opts),
        ('agent',
         neutron.plugins.ml2.drivers.agent.config.agent_opts),
        ('securitygroup',
         neutron.agent.securitygroups_rpc.security_group_opts)
    ]

def list_l3_agent_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.agent.l3.config.OPTS,
             neutron.conf.service.service_opts,
             neutron.agent.l3.ha.OPTS,
             neutron.agent.linux.pd.OPTS,
             neutron.agent.linux.ra.OPTS)
         )
    ]

def list_macvtap_opts():
    return [
        ('macvtap',
         neutron.plugins.ml2.drivers.macvtap.agent.config.macvtap_opts),
        ('agent',
         neutron.plugins.ml2.drivers.agent.config.agent_opts),
        ('securitygroup',
         neutron.agent.securitygroups_rpc.security_group_opts)
    ]

def list_metadata_agent_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.agent.metadata.config.SHARED_OPTS,
             neutron.agent.metadata.config.METADATA_PROXY_HANDLER_OPTS,
             neutron.agent.metadata.config.UNIX_DOMAIN_METADATA_PROXY_OPTS,
             neutron.common.cache_utils.cache_opts)
         ),
        ('AGENT', neutron.agent.common.config.AGENT_STATE_OPTS)
    ]

def list_metering_agent_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.services.metering.agents.metering_agent.MeteringAgent.
             Opts,
             neutron.agent.common.config.INTERFACE_DRIVER_OPTS)
         )
    ]

def list_ml2_conf_opts():
    return [
        ('ml2',
         neutron.plugins.ml2.config.ml2_opts),
        ('ml2_type_flat',
         neutron.plugins.ml2.drivers.type_flat.flat_opts),
        ('ml2_type_vlan',
         neutron.plugins.ml2.drivers.type_vlan.vlan_opts),
        ('ml2_type_gre',
         neutron.plugins.ml2.drivers.type_gre.gre_opts),
        ('ml2_type_vxlan',
         neutron.plugins.ml2.drivers.type_vxlan.vxlan_opts),
        ('ml2_type_geneve',
         neutron.plugins.ml2.drivers.type_geneve.geneve_opts),
        ('securitygroup',
         neutron.agent.securitygroups_rpc.security_group_opts)
    ]

def list_ml2_conf_sriov_opts():
    return [
        ('ml2_sriov',
         neutron.plugins.ml2.drivers.mech_sriov.mech_driver.mech_driver.
         sriov_opts)
    ]

def list_ovs_opts():
    return [
        ('ovs',
         itertools.chain(
             neutron.plugins.ml2.drivers.openvswitch.agent.common.config.
             ovs_opts,
             neutron.agent.ovsdb.api.OPTS)
         ),
        ('agent',
         neutron.plugins.ml2.drivers.openvswitch.agent.common.config.
         agent_opts),
        ('securitygroup',
         neutron.agent.securitygroups_rpc.security_group_opts)
    ]

def list_sriov_agent_opts():
    return [
        ('sriov_nic',
         neutron.plugins.ml2.drivers.mech_sriov.agent.common.config.
         sriov_nic_opts),
        ('agent',
         neutron.agent.l2.extensions.manager.L2_AGENT_EXT_MANAGER_OPTS)
    ]

def list_auth_opts():
    opt_list = copy.deepcopy(_nova_options)
    opt_list.insert(0, ks_loading.get_auth_common_conf_options()[0])
    # NOTE(mhickey): There are a lot of auth plugins, we just generate
    # the config options for a few common ones
    plugins = ['password', 'v2password', 'v3password']
    for name in plugins:
        for plugin_option in ks_loading.get_auth_plugin_conf_options(name):
            if all(option.name != plugin_option.name for option in opt_list):
                opt_list.append(plugin_option)
    opt_list.sort(key=operator.attrgetter('name'))
    return [(NOVA_GROUP, opt_list)]
<commit_msg>Fix missing availability_zone in dhcp and l3 conf<commit_after>#  Licensed under the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License. You may obtain
#  a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations
#  under the License.

import copy
import itertools
import operator

from keystoneauth1 import loading as ks_loading
from oslo_config import cfg

import neutron.agent.common.config
import neutron.agent.common.ovs_lib
import neutron.agent.dhcp.config
import neutron.agent.l2.extensions.manager
import neutron.agent.l3.config
import neutron.agent.l3.ha
import neutron.agent.linux.interface
import neutron.agent.linux.pd
import neutron.agent.linux.ra
import neutron.agent.metadata.config
import neutron.agent.ovsdb.api
import neutron.agent.securitygroups_rpc
import neutron.common.cache_utils
import neutron.conf.quota
import neutron.conf.service
import neutron.db.agents_db
import neutron.db.agentschedulers_db
import neutron.db.dvr_mac_db
import neutron.db.extraroute_db
import neutron.db.l3_agentschedulers_db
import neutron.db.l3_dvr_db
import neutron.db.l3_gwmode_db
import neutron.db.l3_hamode_db
import neutron.db.migration.cli
import neutron.extensions.allowedaddresspairs
import neutron.extensions.l3
import neutron.extensions.securitygroup
import neutron.plugins.ml2.config
import neutron.plugins.ml2.drivers.agent.config
import neutron.plugins.ml2.drivers.linuxbridge.agent.common.config
import neutron.plugins.ml2.drivers.macvtap.agent.config
import neutron.plugins.ml2.drivers.mech_sriov.agent.common.config
import neutron.plugins.ml2.drivers.mech_sriov.mech_driver.mech_driver
import neutron.plugins.ml2.drivers.openvswitch.agent.common.config
import neutron.plugins.ml2.drivers.type_flat
import neutron.plugins.ml2.drivers.type_geneve
import neutron.plugins.ml2.drivers.type_gre
import neutron.plugins.ml2.drivers.type_vlan
import neutron.plugins.ml2.drivers.type_vxlan
import neutron.services.metering.agents.metering_agent
import neutron.services.qos.notification_drivers.manager
import neutron.wsgi

NOVA_GROUP = 'nova'

CONF = cfg.CONF

deprecations = {'nova.cafile': [cfg.DeprecatedOpt('ca_certificates_file',
                                                  group=NOVA_GROUP)],
                'nova.insecure': [cfg.DeprecatedOpt('api_insecure',
                                                    group=NOVA_GROUP)],
                'nova.timeout': [cfg.DeprecatedOpt('url_timeout',
                                                   group=NOVA_GROUP)]}

_nova_options = ks_loading.register_session_conf_options(
            CONF, NOVA_GROUP, deprecated_opts=deprecations)

def list_agent_opts():
    return [
        ('agent',
         itertools.chain(
             neutron.agent.common.config.ROOT_HELPER_OPTS,
             neutron.agent.common.config.AGENT_STATE_OPTS,
             neutron.agent.common.config.IPTABLES_OPTS,
             neutron.agent.common.config.PROCESS_MONITOR_OPTS,
             neutron.agent.common.config.AVAILABILITY_ZONE_OPTS)
         ),
        ('DEFAULT',
         itertools.chain(
             neutron.agent.common.config.INTERFACE_DRIVER_OPTS,
             neutron.agent.metadata.config.SHARED_OPTS,
             neutron.agent.metadata.config.DRIVER_OPTS)
         )
    ]

def list_extension_opts():
    return [
        ('DEFAULT',
         neutron.extensions.allowedaddresspairs.allowed_address_pair_opts),
        ('quotas',
         itertools.chain(
             neutron.conf.quota.l3_quota_opts,
             neutron.conf.quota.security_group_quota_opts)
         )
    ]

def list_db_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.db.agents_db.AGENT_OPTS,
             neutron.db.extraroute_db.extra_route_opts,
             neutron.db.l3_gwmode_db.OPTS,
             neutron.db.agentschedulers_db.AGENTS_SCHEDULER_OPTS,
             neutron.db.dvr_mac_db.dvr_mac_address_opts,
             neutron.db.l3_dvr_db.router_distributed_opts,
             neutron.db.l3_agentschedulers_db.L3_AGENTS_SCHEDULER_OPTS,
             neutron.db.l3_hamode_db.L3_HA_OPTS)
         ),
        ('database',
         neutron.db.migration.cli.get_engine_config())
    ]

def list_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.common.config.core_cli_opts,
             neutron.common.config.core_opts,
             neutron.wsgi.socket_opts,
             neutron.conf.service.service_opts)
         ),
        (neutron.common.config.NOVA_CONF_SECTION,
         itertools.chain(
              neutron.common.config.nova_opts)
         ),
        ('quotas', neutron.conf.quota.core_quota_opts)
    ]

def list_qos_opts():
    return [
        ('qos',
         neutron.services.qos.notification_drivers.manager.QOS_PLUGIN_OPTS)
    ]

def list_base_agent_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.agent.linux.interface.OPTS,
             neutron.agent.common.config.INTERFACE_DRIVER_OPTS,
             neutron.agent.common.ovs_lib.OPTS)
         ),
        ('AGENT',
         itertools.chain(
             neutron.agent.common.config.AGENT_STATE_OPTS,
             neutron.agent.common.config.AVAILABILITY_ZONE_OPTS)
         )
    ]

def list_dhcp_agent_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.agent.dhcp.config.DHCP_AGENT_OPTS,
             neutron.agent.dhcp.config.DHCP_OPTS,
             neutron.agent.dhcp.config.DNSMASQ_OPTS)
         )
    ]

def list_linux_bridge_opts():
    return [
        ('linux_bridge',
         neutron.plugins.ml2.drivers.linuxbridge.agent.common.config.
         bridge_opts),
        ('vxlan',
         neutron.plugins.ml2.drivers.linuxbridge.agent.common.config.
         vxlan_opts),
        ('agent',
         neutron.plugins.ml2.drivers.agent.config.agent_opts),
        ('securitygroup',
         neutron.agent.securitygroups_rpc.security_group_opts)
    ]

def list_l3_agent_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.agent.l3.config.OPTS,
             neutron.conf.service.service_opts,
             neutron.agent.l3.ha.OPTS,
             neutron.agent.linux.pd.OPTS,
             neutron.agent.linux.ra.OPTS)
         )
    ]

def list_macvtap_opts():
    return [
        ('macvtap',
         neutron.plugins.ml2.drivers.macvtap.agent.config.macvtap_opts),
        ('agent',
         neutron.plugins.ml2.drivers.agent.config.agent_opts),
        ('securitygroup',
         neutron.agent.securitygroups_rpc.security_group_opts)
    ]

def list_metadata_agent_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.agent.metadata.config.SHARED_OPTS,
             neutron.agent.metadata.config.METADATA_PROXY_HANDLER_OPTS,
             neutron.agent.metadata.config.UNIX_DOMAIN_METADATA_PROXY_OPTS,
             neutron.common.cache_utils.cache_opts)
         ),
        ('AGENT', neutron.agent.common.config.AGENT_STATE_OPTS)
    ]

def list_metering_agent_opts():
    return [
        ('DEFAULT',
         itertools.chain(
             neutron.services.metering.agents.metering_agent.MeteringAgent.
             Opts,
             neutron.agent.common.config.INTERFACE_DRIVER_OPTS)
         )
    ]

def list_ml2_conf_opts():
    return [
        ('ml2',
         neutron.plugins.ml2.config.ml2_opts),
        ('ml2_type_flat',
         neutron.plugins.ml2.drivers.type_flat.flat_opts),
        ('ml2_type_vlan',
         neutron.plugins.ml2.drivers.type_vlan.vlan_opts),
        ('ml2_type_gre',
         neutron.plugins.ml2.drivers.type_gre.gre_opts),
        ('ml2_type_vxlan',
         neutron.plugins.ml2.drivers.type_vxlan.vxlan_opts),
        ('ml2_type_geneve',
         neutron.plugins.ml2.drivers.type_geneve.geneve_opts),
        ('securitygroup',
         neutron.agent.securitygroups_rpc.security_group_opts)
    ]

def list_ml2_conf_sriov_opts():
    return [
        ('ml2_sriov',
         neutron.plugins.ml2.drivers.mech_sriov.mech_driver.mech_driver.
         sriov_opts)
    ]

def list_ovs_opts():
    return [
        ('ovs',
         itertools.chain(
             neutron.plugins.ml2.drivers.openvswitch.agent.common.config.
             ovs_opts,
             neutron.agent.ovsdb.api.OPTS)
         ),
        ('agent',
         neutron.plugins.ml2.drivers.openvswitch.agent.common.config.
         agent_opts),
        ('securitygroup',
         neutron.agent.securitygroups_rpc.security_group_opts)
    ]

def list_sriov_agent_opts():
    return [
        ('sriov_nic',
         neutron.plugins.ml2.drivers.mech_sriov.agent.common.config.
         sriov_nic_opts),
        ('agent',
         neutron.agent.l2.extensions.manager.L2_AGENT_EXT_MANAGER_OPTS)
    ]

def list_auth_opts():
    opt_list = copy.deepcopy(_nova_options)
    opt_list.insert(0, ks_loading.get_auth_common_conf_options()[0])
    # NOTE(mhickey): There are a lot of auth plugins, we just generate
    # the config options for a few common ones
    plugins = ['password', 'v2password', 'v3password']
    for name in plugins:
        for plugin_option in ks_loading.get_auth_plugin_conf_options(name):
            if all(option.name != plugin_option.name for option in opt_list):
                opt_list.append(plugin_option)
    opt_list.sort(key=operator.attrgetter('name'))
    return [(NOVA_GROUP, opt_list)]
<|endoftext|>


<commit_before>import topo
import param
import imagen

from topo import projection, responsefn, learningfn, transferfn, sheet
import topo.learningfn.optimized
import topo.transferfn.optimized
import topo.responsefn.optimized
import topo.sheet.optimized
import topo.transferfn.misc

from topo.submodel import Model, ArraySpec, order_projections # pyflakes:ignore (API import)
from topo.submodel.earlyvision import EarlyVisionModel

@Model.definition
class ModelGCAL(EarlyVisionModel):

    cortex_density=param.Number(default=47.0,bounds=(0,None),
        inclusive_bounds=(False,True),doc="""
        The nominal_density to use for V1.""")

    homeostasis = param.Boolean(default=True, doc="""
        Whether or not the homeostatic adaption should be applied in V1""")

    t_init = param.Number(default=0.15, doc="""
        The initial V1 threshold value. This value is static in the L and GCL models
        and adaptive in the AL and GCAL models.""")

    latexc_radius=param.Number(default=0.104,bounds=(0,None),doc="""
        Radius of the lateral excitatory bounds within V1.""")

    latinh_radius=param.Number(default=0.22917,bounds=(0,None),doc="""
        Radius of the lateral inhibitory bounds within V1.""")

    latexc_size=param.Number(default=0.05,bounds=(0,None),doc="""
        Size of the lateral excitatory connections within V1.""")

    latinh_size=param.Number(default=0.15,bounds=(0,None),doc="""
        Size of the lateral inhibitory connections within V1.""")

    aff_lr=param.Number(default=0.1,bounds=(0.0,None),doc="""
        Learning rate for the afferent projection(s) to V1.""")

    exc_lr=param.Number(default=0.0,bounds=(0.0,None),doc="""
        Learning rate for the lateral excitatory projection to V1.""")

    inh_lr=param.Number(default=0.3,bounds=(0.0,None),doc="""
        Learning rate for the lateral inhibitory projection to V1.""")

    aff_strength=param.Number(default=1.0,bounds=(0.0,None),doc="""
        Overall strength of the afferent projection to V1.""")

    exc_strength=param.Number(default=1.7,bounds=(0.0,None),doc="""
        Overall strength of the lateral excitatory projection to V1.""")

    inh_strength=param.Number(default=1.4,bounds=(0.0,None),doc="""
        Overall strength of the lateral inhibitory projection to V1.""")

    expand_sf_test_range=param.Boolean(default=False,doc="""
        By default, measure_sine_pref() measures SF at the sizes of RF
        used, for speed, but if expand_sf_test_range is True, it will
        test over a larger range, including half the size of the
        smallest and twice the size of the largest.""")

    def property_setup(self, properties):
        properties = super(ModelGCAL, self).property_setup(properties)
        "Specify weight initialization, response function, and learning function"

        projection.CFProjection.cf_shape=imagen.Disk(smoothing=0.0)
        projection.CFProjection.response_fn=responsefn.optimized.CFPRF_DotProduct_opt()
        projection.CFProjection.learning_fn=learningfn.optimized.CFPLF_Hebbian_opt()
        projection.CFProjection.weights_output_fns=[transferfn.optimized.CFPOF_DivisiveNormalizeL1_opt()]
        projection.SharedWeightCFProjection.response_fn=responsefn.optimized.CFPRF_DotProduct_opt()
        return properties

    def sheet_setup(self):
        sheets = super(ModelGCAL,self).sheet_setup()
        sheets['V1'] = [{}]
        return sheets

    @Model.SettlingCFSheet
    def V1(self, properties):
        return Model.SettlingCFSheet.params(
            tsettle=16,
            plastic=True,
            joint_norm_fn=topo.sheet.optimized.compute_joint_norm_totals_opt,
            output_fns=[transferfn.misc.HomeostaticResponse(t_init=self.t_init,
                                                            learning_rate=0.01 if self.homeostasis else 0.0)],
            nominal_density=self.cortex_density,
            nominal_bounds=sheet.BoundingBox(radius=self.area/2.0))

    @Model.matchconditions('V1', 'V1_afferent')
    def V1_afferent_conditions(self, properties):
        return {'level': 'LGN'}

    @Model.CFProjection
    def V1_afferent(self, src_properties, dest_properties):
        sf_channel = src_properties['SF'] if 'SF' in src_properties else 1
        # Adjust delays so same measurement protocol can be used with and without gain control.
        LGN_V1_delay = 0.05 if self.gain_control else 0.10

        name=''
        if 'eye' in src_properties: name+=src_properties['eye']
        if 'opponent' in src_properties:
            name+=src_properties['opponent']+src_properties['surround']
        name+=('LGN'+src_properties['polarity']+'Afferent')
        if sf_channel>1: name+=('SF'+str(src_properties['SF']))

        gaussian_size = 2.0 * self.v1aff_radius *self.sf_spacing**(sf_channel-1)
        weights_generator = imagen.random.GaussianCloud(gaussian_size=gaussian_size)

        return [Model.CFProjection.params(
                delay=LGN_V1_delay+lag,
                dest_port=('Activity','JointNormalize','Afferent'),
                name= name if lag==0 else name+('Lag'+str(lag)),
                learning_rate=self.aff_lr,
                strength=self.aff_strength*(1.5 if self.gain_control else 1.0),
                weights_generator=weights_generator,
                nominal_bounds_template=sheet.BoundingBox(radius=
                                            self.v1aff_radius*self.sf_spacing**(sf_channel-1)))
                for lag in self['lags']]

    @Model.matchconditions('V1', 'lateral_excitatory')
    def lateral_excitatory_conditions(self, properties):
        return {'level': 'V1'}

    @Model.CFProjection
    def lateral_excitatory(self, src_properties, dest_properties):
        return Model.CFProjection.params(
            delay=0.05,
            name='LateralExcitatory',
            weights_generator=imagen.Gaussian(aspect_ratio=1.0, size=self.latexc_size),
            strength=self.exc_strength,
            learning_rate=self.exc_lr,
            nominal_bounds_template=sheet.BoundingBox(radius=self.latexc_radius))

    @Model.matchconditions('V1', 'lateral_inhibitory')
    def lateral_inhibitory_conditions(self, properties):
        return {'level': 'V1'}

    @Model.CFProjection
    def lateral_inhibitory(self, src_properties, dest_properties):
        return Model.CFProjection.params(
            delay=0.05,
            name='LateralInhibitory',
            weights_generator=imagen.random.GaussianCloud(gaussian_size=self.latinh_size),
            strength=-1.0*self.inh_strength,
            learning_rate=self.inh_lr,
            nominal_bounds_template=sheet.BoundingBox(radius=self.latinh_radius))

    def analysis_setup(self):
        # TODO: This is different in gcal.ty, stevens/gcal.ty and gcal_od.ty
        # And depends whether gain control is used or not
        import topo.analysis.featureresponses
        topo.analysis.featureresponses.FeatureMaps.selectivity_multiplier=2.0
        topo.analysis.featureresponses.FeatureCurveCommand.contrasts=[1, 10, 30, 50, 100]
        if 'dr' in self.dims:
            topo.analysis.featureresponses.MeasureResponseCommand.durations=[(max(self['lags'])+1)*1.0]
        if 'sf' in self.dims:
            from topo.analysis.command import measure_sine_pref
            sf_relative_sizes = [self.sf_spacing**(sf_channel-1) for sf_channel in self['SF']]
            wide_relative_sizes=[0.5*sf_relative_sizes[0]] + sf_relative_sizes + [2.0*sf_relative_sizes[-1]]
            relative_sizes=(wide_relative_sizes if self.expand_sf_test_range else sf_relative_sizes)
            #The default 2.4 spatial frequency value here is
            #chosen because it results in a sine grating with bars whose
            #width approximately matches the width of the Gaussian training
            #patterns, and thus the typical width of an ON stripe in one of the
            #receptive fields
            measure_sine_pref.frequencies = [2.4*s for s in relative_sizes]

@Model.definition
class ExamplesGCAL(ModelGCAL):
    """
    Reproduces the results of the legacy examples/gcal.ty file.
    """

    def __init__(self, **params):
        super(ExamplesGCAL, self).__init__(time_dependent=False, **params)
        if set(self.dims) != set([ 'xy', 'or']):
            raise Exception("ExamplesGCAL only reproducible for dims = ['xy', 'or']")

    def setup(self,setup_options=True):
        model = super(ExamplesGCAL, self).setup(setup_options)
        if setup_options is True or 'sheets' in setup_options:
            model.sheets.Retina.update(nominal_bounds=sheet.BoundingBox(radius=self.area/2.0+1.125))
            model.sheets.LGNOn.update(nominal_bounds=sheet.BoundingBox(radius=self.area/2.0+0.75))
            model.sheets.LGNOff.update(nominal_bounds=sheet.BoundingBox(radius=self.area/2.0+0.75))
            model.sheets.V1.update(nominal_density=48)
        if setup_options is True or 'projections' in setup_options:
            order_projections(model, ['afferent',
                                      'lateral_gain_control',
                                      ('V1_afferent', {'polarity':'On'}),
                                      ('V1_afferent', {'polarity':'Off'}),
                                      'lateral_excitatory',
                                      'lateral_inhibitory'])
        return model
<commit_msg>Added target_activity parameter to ModelGCAL<commit_after>import topo
import param
import imagen

from topo import projection, responsefn, learningfn, transferfn, sheet
import topo.learningfn.optimized
import topo.transferfn.optimized
import topo.responsefn.optimized
import topo.sheet.optimized
import topo.transferfn.misc

from topo.submodel import Model, ArraySpec, order_projections # pyflakes:ignore (API import)
from topo.submodel.earlyvision import EarlyVisionModel

@Model.definition
class ModelGCAL(EarlyVisionModel):

    cortex_density=param.Number(default=47.0,bounds=(0,None),
        inclusive_bounds=(False,True),doc="""
        The nominal_density to use for V1.""")

    homeostasis = param.Boolean(default=True, doc="""
        Whether or not the homeostatic adaption should be applied in V1""")

    t_init = param.Number(default=0.15, doc="""
        The initial V1 threshold value. This value is static in the L and GCL models
        and adaptive in the AL and GCAL models.""")

    target_activity = param.Number(default=0.024,doc="""
        The target average activity for the homeostatic threshold mechanism.""")

    latexc_radius=param.Number(default=0.104,bounds=(0,None),doc="""
        Radius of the lateral excitatory bounds within V1.""")

    latinh_radius=param.Number(default=0.22917,bounds=(0,None),doc="""
        Radius of the lateral inhibitory bounds within V1.""")

    latexc_size=param.Number(default=0.05,bounds=(0,None),doc="""
        Size of the lateral excitatory connections within V1.""")

    latinh_size=param.Number(default=0.15,bounds=(0,None),doc="""
        Size of the lateral inhibitory connections within V1.""")

    aff_lr=param.Number(default=0.1,bounds=(0.0,None),doc="""
        Learning rate for the afferent projection(s) to V1.""")

    exc_lr=param.Number(default=0.0,bounds=(0.0,None),doc="""
        Learning rate for the lateral excitatory projection to V1.""")

    inh_lr=param.Number(default=0.3,bounds=(0.0,None),doc="""
        Learning rate for the lateral inhibitory projection to V1.""")

    aff_strength=param.Number(default=1.0,bounds=(0.0,None),doc="""
        Overall strength of the afferent projection to V1.""")

    exc_strength=param.Number(default=1.7,bounds=(0.0,None),doc="""
        Overall strength of the lateral excitatory projection to V1.""")

    inh_strength=param.Number(default=1.4,bounds=(0.0,None),doc="""
        Overall strength of the lateral inhibitory projection to V1.""")

    expand_sf_test_range=param.Boolean(default=False,doc="""
        By default, measure_sine_pref() measures SF at the sizes of RF
        used, for speed, but if expand_sf_test_range is True, it will
        test over a larger range, including half the size of the
        smallest and twice the size of the largest.""")

    def property_setup(self, properties):
        properties = super(ModelGCAL, self).property_setup(properties)
        "Specify weight initialization, response function, and learning function"

        projection.CFProjection.cf_shape=imagen.Disk(smoothing=0.0)
        projection.CFProjection.response_fn=responsefn.optimized.CFPRF_DotProduct_opt()
        projection.CFProjection.learning_fn=learningfn.optimized.CFPLF_Hebbian_opt()
        projection.CFProjection.weights_output_fns=[transferfn.optimized.CFPOF_DivisiveNormalizeL1_opt()]
        projection.SharedWeightCFProjection.response_fn=responsefn.optimized.CFPRF_DotProduct_opt()
        return properties

    def sheet_setup(self):
        sheets = super(ModelGCAL,self).sheet_setup()
        sheets['V1'] = [{}]
        return sheets

    @Model.SettlingCFSheet
    def V1(self, properties):
        return Model.SettlingCFSheet.params(
            tsettle=16,
            plastic=True,
            joint_norm_fn=topo.sheet.optimized.compute_joint_norm_totals_opt,
            output_fns=[transferfn.misc.HomeostaticResponse(t_init=self.t_init,
                                                            target_activity = self.target_activity,
                                                            learning_rate=0.01 if self.homeostasis else 0.0)],
            nominal_density=self.cortex_density,
            nominal_bounds=sheet.BoundingBox(radius=self.area/2.0))

    @Model.matchconditions('V1', 'V1_afferent')
    def V1_afferent_conditions(self, properties):
        return {'level': 'LGN'}

    @Model.CFProjection
    def V1_afferent(self, src_properties, dest_properties):
        sf_channel = src_properties['SF'] if 'SF' in src_properties else 1
        # Adjust delays so same measurement protocol can be used with and without gain control.
        LGN_V1_delay = 0.05 if self.gain_control else 0.10

        name=''
        if 'eye' in src_properties: name+=src_properties['eye']
        if 'opponent' in src_properties:
            name+=src_properties['opponent']+src_properties['surround']
        name+=('LGN'+src_properties['polarity']+'Afferent')
        if sf_channel>1: name+=('SF'+str(src_properties['SF']))

        gaussian_size = 2.0 * self.v1aff_radius *self.sf_spacing**(sf_channel-1)
        weights_generator = imagen.random.GaussianCloud(gaussian_size=gaussian_size)

        return [Model.CFProjection.params(
                delay=LGN_V1_delay+lag,
                dest_port=('Activity','JointNormalize','Afferent'),
                name= name if lag==0 else name+('Lag'+str(lag)),
                learning_rate=self.aff_lr,
                strength=self.aff_strength*(1.5 if self.gain_control else 1.0),
                weights_generator=weights_generator,
                nominal_bounds_template=sheet.BoundingBox(radius=
                                            self.v1aff_radius*self.sf_spacing**(sf_channel-1)))
                for lag in self['lags']]

    @Model.matchconditions('V1', 'lateral_excitatory')
    def lateral_excitatory_conditions(self, properties):
        return {'level': 'V1'}

    @Model.CFProjection
    def lateral_excitatory(self, src_properties, dest_properties):
        return Model.CFProjection.params(
            delay=0.05,
            name='LateralExcitatory',
            weights_generator=imagen.Gaussian(aspect_ratio=1.0, size=self.latexc_size),
            strength=self.exc_strength,
            learning_rate=self.exc_lr,
            nominal_bounds_template=sheet.BoundingBox(radius=self.latexc_radius))

    @Model.matchconditions('V1', 'lateral_inhibitory')
    def lateral_inhibitory_conditions(self, properties):
        return {'level': 'V1'}

    @Model.CFProjection
    def lateral_inhibitory(self, src_properties, dest_properties):
        return Model.CFProjection.params(
            delay=0.05,
            name='LateralInhibitory',
            weights_generator=imagen.random.GaussianCloud(gaussian_size=self.latinh_size),
            strength=-1.0*self.inh_strength,
            learning_rate=self.inh_lr,
            nominal_bounds_template=sheet.BoundingBox(radius=self.latinh_radius))

    def analysis_setup(self):
        # TODO: This is different in gcal.ty, stevens/gcal.ty and gcal_od.ty
        # And depends whether gain control is used or not
        import topo.analysis.featureresponses
        topo.analysis.featureresponses.FeatureMaps.selectivity_multiplier=2.0
        topo.analysis.featureresponses.FeatureCurveCommand.contrasts=[1, 10, 30, 50, 100]
        if 'dr' in self.dims:
            topo.analysis.featureresponses.MeasureResponseCommand.durations=[(max(self['lags'])+1)*1.0]
        if 'sf' in self.dims:
            from topo.analysis.command import measure_sine_pref
            sf_relative_sizes = [self.sf_spacing**(sf_channel-1) for sf_channel in self['SF']]
            wide_relative_sizes=[0.5*sf_relative_sizes[0]] + sf_relative_sizes + [2.0*sf_relative_sizes[-1]]
            relative_sizes=(wide_relative_sizes if self.expand_sf_test_range else sf_relative_sizes)
            #The default 2.4 spatial frequency value here is
            #chosen because it results in a sine grating with bars whose
            #width approximately matches the width of the Gaussian training
            #patterns, and thus the typical width of an ON stripe in one of the
            #receptive fields
            measure_sine_pref.frequencies = [2.4*s for s in relative_sizes]

@Model.definition
class ExamplesGCAL(ModelGCAL):
    """
    Reproduces the results of the legacy examples/gcal.ty file.
    """

    def __init__(self, **params):
        super(ExamplesGCAL, self).__init__(time_dependent=False, **params)
        if set(self.dims) != set([ 'xy', 'or']):
            raise Exception("ExamplesGCAL only reproducible for dims = ['xy', 'or']")

    def setup(self,setup_options=True):
        model = super(ExamplesGCAL, self).setup(setup_options)
        if setup_options is True or 'sheets' in setup_options:
            model.sheets.Retina.update(nominal_bounds=sheet.BoundingBox(radius=self.area/2.0+1.125))
            model.sheets.LGNOn.update(nominal_bounds=sheet.BoundingBox(radius=self.area/2.0+0.75))
            model.sheets.LGNOff.update(nominal_bounds=sheet.BoundingBox(radius=self.area/2.0+0.75))
            model.sheets.V1.update(nominal_density=48)
        if setup_options is True or 'projections' in setup_options:
            order_projections(model, ['afferent',
                                      'lateral_gain_control',
                                      ('V1_afferent', {'polarity':'On'}),
                                      ('V1_afferent', {'polarity':'Off'}),
                                      'lateral_excitatory',
                                      'lateral_inhibitory'])
        return model
<|endoftext|>


<commit_before>import re
from collections import OrderedDict

from parsimonious.grammar import Grammar

from .errors import IndentationError
from .globals import BLK_OPEN, BLK_CLOSE, INDENT_SIZE, COMMENT_OPEN, COMMENT_CLOSE
from .types import RuleBlock, UnpackMe, RootBlock, IgnoreMe, ParentReference

ly_grammar = ""
funcmap = {}
defer_children_eval = []

### GRAMMAR HANDLING ###

class GDef(object):
    '''
    Decorator for defining LightYear syntax.
    '''
    def __init__(self, ruletxt, defer=False):
        global ly_grammar
        ly_grammar += ruletxt + '\n'

        self.rulenames = []
        for line in ruletxt.split('\n'):
            line = line.strip()
            if line:
                name = line.split('=')[0].strip()
                self.rulenames.append(name)
                if defer:
                    defer_children_eval.append(name)

    def __call__(self, f):
        for name in self.rulenames:
            funcmap[name] = f

### LIGHTYEAR PARSER ###

class LY(object):
    '''
    Parses LightYear code and generates CSS as output.
    '''
    grammar = None

    def __init__(self, env=None, debug=False, path=None):
        if not self.grammar:
            self.__class__.grammar = Grammar(ly_grammar)['ltree']
        self.env = env or {}
        self.debug = debug
        self.path = path

    def eval(self, ly_code):
        '''
        Accept a string containing LightYear code as input, and recursively
        evaluate the root node.
        '''
        lines = ly_code.split('\n')
        lines = tokenize_whitespace(lines)
        lines = tokenize_comments(lines)
        ly_code = '\n'.join(lines)

        self.debug = DebugGenerator(ly_code) if self.debug else False

        node = self.grammar.parse(ly_code)
        self.ltree = self._evalnode(node)
        self.flatten()

    def _evalnode(self, node):
        '''
        Evaluate a Parsimonious node.
        '''
        fn = funcmap.get(node.expr_name, lambda env, node, children: children)
        if node.expr_name in defer_children_eval:
            return fn(self.env, node)
        children = [self._evalnode(child) for child in node]

        # Mixins return lists that need to be unpacked.
        for i, child in enumerate(children):
            if isinstance(child, UnpackMe):
                for packed_child in reversed(child):
                    children.insert(i+1, packed_child)

        return fn(self.env, node, children)

    def flatten(self):
        '''
        Flatten all nested rules and convert parent references
        to standard selectors.  Execute only after LightYear
        code evaluation.
        '''
        for i, element in enumerate(self.ltree):
            if isinstance(element, RuleBlock):
                for j, child_element in reversed(list(enumerate(element.block))):
                    # Move nested RuleBlock objects to ltree and modify selectors.
                    if isinstance(child_element, RuleBlock):
                        child_element.selectors = element.selectors + child_element.selectors
                        self.ltree.insert(i+1, child_element)
                        element.block[j] = IgnoreMe()

                    # Find parent selectors and convert to standard RuleBlocks.
                    elif isinstance(child_element, ParentReference):
                        ps_rule_block = child_element.rule_block
                        if len(ps_rule_block.selectors) > 1:
                            new_selectors = (
                                child_element.selectors[:-1] +
                                [child_element.selectors[-1] + ps_rule_block.selectors[0]] +
                                ps_rule_block.selectors[1:]
                                )
                        else:
                            new_selectors = (
                                element.selectors[:-1] +
                                [element.selectors[-1] + ps_rule_block.selectors[0]]
                                )
                        new_block = RuleBlock(
                            tag=ps_rule_block.tag,
                            selectors=new_selectors,
                            block=ps_rule_block.block,
                            index=ps_rule_block.index)
                        self.ltree.insert(i+1, new_block)

                        element.block[j] = IgnoreMe()

    def reduce(self):
        '''
        Consolidate rules with identical selectors into single rules.
        '''

        # Reduce blocks.
        ltree_reduced = OrderedDict()
        non_block_count = 0
        for element in self.ltree:
            if hasattr(element, 'selectors'):
                hash_ = repr(element.selectors)
            elif hasattr(element, 'text'):
                hash_ = element.text
            else:
                hash_ = non_block_count
                non_block_count += 1

            if hasattr(element, 'block'):
                if hash_ in ltree_reduced:
                    ltree_reduced[hash_].block += element.block
                else:
                    ltree_reduced[hash_] = element

            else:
                ltree_reduced[hash_] = element

        ltree_reduced = [ltree_reduced[k] for k in ltree_reduced]

        # Reduce properties.
        for element in ltree_reduced:
            non_property_count = 0
            if hasattr(element, 'block'):
                block_reduced = OrderedDict()
                for child in element.block:
                    if hasattr(child, 'prop'):
                        block_reduced[child.prop] = child
                    else:
                        block_reduced[non_property_count] = child
                        non_property_count += 1
                element.block = [block_reduced[k] for k in block_reduced]

        self.ltree = ltree_reduced

    def css(self):
        '''
        Output minified CSS.  Should not be run until LightYear code is
        evaluated and the resulting structure flattened.
        '''
        root_blocks = []
        for e in self.ltree:
            if isinstance(e, RootBlock):
                root_blocks.append(e)
        if not root_blocks:
            root_blocks.append(RootBlock(tag_name=None, prefix=''))

        output = ''
        for root_block in root_blocks:
            output += root_block.prefix
            if root_block.prefix:
                output += '{'
            output += ''.join(e.css(tag=root_block.tag_name, debug=self.debug)
                              if hasattr(e, 'css')
                              else ''
                              for e in self.ltree)
            if root_block.prefix:
                output += '}'

        return output

    def pretty_css(self):
        '''
        Output prettified CSS.
        '''
        css_chars = list(self.css())

        # Insert spaces and newlines.
        skip = False
        for i, c in enumerate(css_chars):
            this_two = ''.join(css_chars[i:i+2]) if len(css_chars) > i+1 else None
            next_two = ''.join(css_chars[i+1:i+3]) if len(css_chars) > i+2 else None
            third = css_chars[i+2] if len(css_chars) > i+2 else None

            if c == ';' and not next_two == '/*':
                css_chars.insert(i+1, '\n')
            elif this_two == '/*':
                if skip:
                    skip = False
                    continue
                css_chars.insert(i, ' ')
                skip = True
            elif c == ':':
                css_chars.insert(i+1, ' ')
            elif c == '{':
                if skip:
                    skip = False
                    continue
                css_chars.insert(i+1, '\n')
                css_chars.insert(i, ' ')
                skip = True
            elif c == '}':
                css_chars.insert(i+1, '\n')
            elif this_two == '*/' and not third == '{':
                css_chars.insert(i+2, '\n')

        # Insert Indentation
        dent = 0
        tab = '    '
        for i, c in enumerate(css_chars):
            next = css_chars[i+1] if len(css_chars) > i+1 else None

            if next == '}':
                dent -= 1
            if c == '{':
                dent += 1

            elif c == '\n':
                css_chars.insert(i+1, tab*dent)
                if next == '}':
                    dent += 1

        return ''.join(css_chars)

# Import LightYear grammar after LY class definition.
from . import lang

### PRE-PEG TOKENIZATION ###

def tokenize_whitespace(lines):
    """
    For each line, indentify current level of indendation and compare
    against indentation of previous line.  Insert BLK_OPEN or BLK_CLOSE
    as appropriate.
    """

    firstline = True
    prevdent = 0

    for line in lines:
        line = line.expandtabs(INDENT_SIZE)

        # Don't allow empty lines to effect tracking of indentation.
        stripped = line.strip()
        if stripped == '' or stripped[:2] == '//':
            yield line
            continue

        # Check for indentation on the first line.
        if firstline:
            if line[0] == " ":
                raise IndentationError
            firstline = False

        leading_spaces = re.match('[ ]*', line).group()
        curdent = len(leading_spaces) // INDENT_SIZE

        if curdent == prevdent:
            yield line
        elif curdent == prevdent + 1:
            yield BLK_OPEN + line
        elif curdent < prevdent:
            yield BLK_CLOSE * (prevdent - curdent) + line
        else:
            raise IndentationError(line)

        prevdent = curdent

    # Handle indented last line.
    yield BLK_CLOSE * prevdent

def tokenize_comments(lines):
    '''
    Identify and tokenize comments.
    '''
    for line in lines:
        for possible in (x.start(0) for x in re.finditer('//', line)):
            if not _isquoted(line, possible):
                line = line[:possible] + COMMENT_OPEN + line[possible:] + COMMENT_CLOSE
                break
        yield line

def _isquoted(line, pos):
    '''
    Return boolean value indicating whether the character at position
    pos resides within a quote.
    '''
    DQUO = False
    SQUO = False

    for i in range(0, pos):
        if not DQUO and not SQUO:
            if line[i] == '"':
                DQUO = True
            elif line[i] == "'":
                SQUO = True
        elif DQUO:
            if line[i] == '"':
                DQUO = False
        elif SQUO:
            if line[i] == "'":
                SQUO = False

    return (DQUO or SQUO)

### DEBUG ###

class DebugGenerator():
    def __init__(self, ly_code):
        self.ly_code = ly_code

    def line_number(self, index):
        return self.ly_code[:index].count('\n') + 1

    def line_number_comment(self, index):
        if index == 'generated':
            return '/*GENERATED*/'
        return '/*line{}*/'.format(self.line_number(index))

    def __nonzero__(self):
        return True
<commit_msg>prettify commas<commit_after>import re
from collections import OrderedDict

from parsimonious.grammar import Grammar

from .errors import IndentationError
from .globals import BLK_OPEN, BLK_CLOSE, INDENT_SIZE, COMMENT_OPEN, COMMENT_CLOSE
from .types import RuleBlock, UnpackMe, RootBlock, IgnoreMe, ParentReference

ly_grammar = ""
funcmap = {}
defer_children_eval = []

### GRAMMAR HANDLING ###

class GDef(object):
    '''
    Decorator for defining LightYear syntax.
    '''
    def __init__(self, ruletxt, defer=False):
        global ly_grammar
        ly_grammar += ruletxt + '\n'

        self.rulenames = []
        for line in ruletxt.split('\n'):
            line = line.strip()
            if line:
                name = line.split('=')[0].strip()
                self.rulenames.append(name)
                if defer:
                    defer_children_eval.append(name)

    def __call__(self, f):
        for name in self.rulenames:
            funcmap[name] = f

### LIGHTYEAR PARSER ###

class LY(object):
    '''
    Parses LightYear code and generates CSS as output.
    '''
    grammar = None

    def __init__(self, env=None, debug=False, path=None):
        if not self.grammar:
            self.__class__.grammar = Grammar(ly_grammar)['ltree']
        self.env = env or {}
        self.debug = debug
        self.path = path

    def eval(self, ly_code):
        '''
        Accept a string containing LightYear code as input, and recursively
        evaluate the root node.
        '''
        lines = ly_code.split('\n')
        lines = tokenize_whitespace(lines)
        lines = tokenize_comments(lines)
        ly_code = '\n'.join(lines)

        self.debug = DebugGenerator(ly_code) if self.debug else False

        node = self.grammar.parse(ly_code)
        self.ltree = self._evalnode(node)
        self.flatten()

    def _evalnode(self, node):
        '''
        Evaluate a Parsimonious node.
        '''
        fn = funcmap.get(node.expr_name, lambda env, node, children: children)
        if node.expr_name in defer_children_eval:
            return fn(self.env, node)
        children = [self._evalnode(child) for child in node]

        # Mixins return lists that need to be unpacked.
        for i, child in enumerate(children):
            if isinstance(child, UnpackMe):
                for packed_child in reversed(child):
                    children.insert(i+1, packed_child)

        return fn(self.env, node, children)

    def flatten(self):
        '''
        Flatten all nested rules and convert parent references
        to standard selectors.  Execute only after LightYear
        code evaluation.
        '''
        for i, element in enumerate(self.ltree):
            if isinstance(element, RuleBlock):
                for j, child_element in reversed(list(enumerate(element.block))):
                    # Move nested RuleBlock objects to ltree and modify selectors.
                    if isinstance(child_element, RuleBlock):
                        child_element.selectors = element.selectors + child_element.selectors
                        self.ltree.insert(i+1, child_element)
                        element.block[j] = IgnoreMe()

                    # Find parent selectors and convert to standard RuleBlocks.
                    elif isinstance(child_element, ParentReference):
                        ps_rule_block = child_element.rule_block
                        if len(ps_rule_block.selectors) > 1:
                            new_selectors = (
                                child_element.selectors[:-1] +
                                [child_element.selectors[-1] + ps_rule_block.selectors[0]] +
                                ps_rule_block.selectors[1:]
                                )
                        else:
                            new_selectors = (
                                element.selectors[:-1] +
                                [element.selectors[-1] + ps_rule_block.selectors[0]]
                                )
                        new_block = RuleBlock(
                            tag=ps_rule_block.tag,
                            selectors=new_selectors,
                            block=ps_rule_block.block,
                            index=ps_rule_block.index)
                        self.ltree.insert(i+1, new_block)

                        element.block[j] = IgnoreMe()

    def reduce(self):
        '''
        Consolidate rules with identical selectors into single rules.
        '''

        # Reduce blocks.
        ltree_reduced = OrderedDict()
        non_block_count = 0
        for element in self.ltree:
            if hasattr(element, 'selectors'):
                hash_ = repr(element.selectors)
            elif hasattr(element, 'text'):
                hash_ = element.text
            else:
                hash_ = non_block_count
                non_block_count += 1

            if hasattr(element, 'block'):
                if hash_ in ltree_reduced:
                    ltree_reduced[hash_].block += element.block
                else:
                    ltree_reduced[hash_] = element

            else:
                ltree_reduced[hash_] = element

        ltree_reduced = [ltree_reduced[k] for k in ltree_reduced]

        # Reduce properties.
        for element in ltree_reduced:
            non_property_count = 0
            if hasattr(element, 'block'):
                block_reduced = OrderedDict()
                for child in element.block:
                    if hasattr(child, 'prop'):
                        block_reduced[child.prop] = child
                    else:
                        block_reduced[non_property_count] = child
                        non_property_count += 1
                element.block = [block_reduced[k] for k in block_reduced]

        self.ltree = ltree_reduced

    def css(self):
        '''
        Output minified CSS.  Should not be run until LightYear code is
        evaluated and the resulting structure flattened.
        '''
        root_blocks = []
        for e in self.ltree:
            if isinstance(e, RootBlock):
                root_blocks.append(e)
        if not root_blocks:
            root_blocks.append(RootBlock(tag_name=None, prefix=''))

        output = ''
        for root_block in root_blocks:
            output += root_block.prefix
            if root_block.prefix:
                output += '{'
            output += ''.join(e.css(tag=root_block.tag_name, debug=self.debug)
                              if hasattr(e, 'css')
                              else ''
                              for e in self.ltree)
            if root_block.prefix:
                output += '}'

        return output

    def pretty_css(self):
        '''
        Output prettified CSS.
        '''
        css_chars = list(self.css())

        # Insert spaces and newlines.
        skip = False
        for i, c in enumerate(css_chars):
            this_two = ''.join(css_chars[i:i+2]) if len(css_chars) > i+1 else None
            next_two = ''.join(css_chars[i+1:i+3]) if len(css_chars) > i+2 else None
            third = css_chars[i+2] if len(css_chars) > i+2 else None

            if c == ';' and not next_two == '/*':
                css_chars.insert(i+1, '\n')
            elif this_two == '/*':
                if skip:
                    skip = False
                    continue
                css_chars.insert(i, ' ')
                skip = True
            elif c == ':':
                css_chars.insert(i+1, ' ')
            elif c == '{':
                if skip:
                    skip = False
                    continue
                css_chars.insert(i+1, '\n')
                css_chars.insert(i, ' ')
                skip = True
            elif c == '}':
                css_chars.insert(i+1, '\n')
            elif this_two == '*/' and not third == '{':
                css_chars.insert(i+2, '\n')
            elif c == ',':
                if not css_chars[i+1] == ' ':
                    css_chars[i] = ', '
                if css_chars[i-1] == ' ':
                    css_chars[i-1] = ''

        # Insert Indentation
        dent = 0
        tab = '    '
        for i, c in enumerate(css_chars):
            next = css_chars[i+1] if len(css_chars) > i+1 else None

            if next == '}':
                dent -= 1
            if c == '{':
                dent += 1

            elif c == '\n':
                css_chars.insert(i+1, tab*dent)
                if next == '}':
                    dent += 1

        return ''.join(css_chars)

# Import LightYear grammar after LY class definition.
from . import lang

### PRE-PEG TOKENIZATION ###

def tokenize_whitespace(lines):
    """
    For each line, indentify current level of indendation and compare
    against indentation of previous line.  Insert BLK_OPEN or BLK_CLOSE
    as appropriate.
    """

    firstline = True
    prevdent = 0

    for line in lines:
        line = line.expandtabs(INDENT_SIZE)

        # Don't allow empty lines to effect tracking of indentation.
        stripped = line.strip()
        if stripped == '' or stripped[:2] == '//':
            yield line
            continue

        # Check for indentation on the first line.
        if firstline:
            if line[0] == " ":
                raise IndentationError
            firstline = False

        leading_spaces = re.match('[ ]*', line).group()
        curdent = len(leading_spaces) // INDENT_SIZE

        if curdent == prevdent:
            yield line
        elif curdent == prevdent + 1:
            yield BLK_OPEN + line
        elif curdent < prevdent:
            yield BLK_CLOSE * (prevdent - curdent) + line
        else:
            raise IndentationError(line)

        prevdent = curdent

    # Handle indented last line.
    yield BLK_CLOSE * prevdent

def tokenize_comments(lines):
    '''
    Identify and tokenize comments.
    '''
    for line in lines:
        for possible in (x.start(0) for x in re.finditer('//', line)):
            if not _isquoted(line, possible):
                line = line[:possible] + COMMENT_OPEN + line[possible:] + COMMENT_CLOSE
                break
        yield line

def _isquoted(line, pos):
    '''
    Return boolean value indicating whether the character at position
    pos resides within a quote.
    '''
    DQUO = False
    SQUO = False

    for i in range(0, pos):
        if not DQUO and not SQUO:
            if line[i] == '"':
                DQUO = True
            elif line[i] == "'":
                SQUO = True
        elif DQUO:
            if line[i] == '"':
                DQUO = False
        elif SQUO:
            if line[i] == "'":
                SQUO = False

    return (DQUO or SQUO)

### DEBUG ###

class DebugGenerator():
    def __init__(self, ly_code):
        self.ly_code = ly_code

    def line_number(self, index):
        return self.ly_code[:index].count('\n') + 1

    def line_number_comment(self, index):
        if index == 'generated':
            return '/*GENERATED*/'
        return '/*line{}*/'.format(self.line_number(index))

    def __nonzero__(self):
        return True
<|endoftext|>


<commit_before># This is the gtk-dependent History module.
# For the pyjamas/javascript version, see platform/HistoryPyJS.py

import sys
from __pyjamas__ import JS, doc, wnd
if sys.platform not in ['mozilla', 'ie6', 'opera', 'oldmoz', 'safari']:
    from __pyjamas__ import get_main_frame

global historyToken
historyToken = ''

historyListeners = []

"""
    Simple History management class for back/forward button support.

    This class allows your AJAX application to use a history.  Each time you
    call newItem(), a new item is added to the history and the history
    listeners are notified.  If the user clicks the browser's forward or back
    buttons, the appropriate item (a string passed to newItem) is fetched
    from the history and the history listeners are notified.

    The address bar of the browser contains the current token, using
    the "#" seperator (for implementation reasons, not because we love
    the # mark).

    You may want to check whether the hash already contains a history
    token when the page loads and use that to show appropriate content;
    this allows users of the site to store direct links in their
    bookmarks or send them in emails.

    To make this work properly in all browsers, you must add a specially
    named iframe to your html page, like this:

    <iframe id='__pygwt_historyFrame' style='width:0;height:0;border:0'></iframe>
"""

def addHistoryListener(listener):
    historyListeners.append(listener)

def back():
    wnd().history.back()

def forward():
    wnd().history.forward()

def getToken():
    global historyToken
    return historyToken

def newItem(ht):
    print "History - new item", ht
    onHistoryChanged(ht)
    return

    JS("""
    if(historyToken == "" || historyToken == null){
        historyToken = "#";
    }
    $wnd.location.hash = encodeURIComponent(historyToken);
    """)

# TODO - fireHistoryChangedAndCatch not implemented
def onHistoryChanged(ht):
    #UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler();
    #if (handler != null)
    #   fireHistoryChangedAndCatch(historyToken, handler);
    #else
    fireHistoryChangedImpl(ht)

# TODO
def fireHistoryChangedAndCatch():
    pass

def fireHistoryChangedImpl(ht):
    for listener in historyListeners:
        listener.onHistoryChanged(ht)

def removeHistoryListener(listener):
    historyListeners.remove(listener)

def init():
    print "history: TODO"
    global historyToken
    historyToken = ''
    onHistoryChanged(historyToken)
    return
    JS("""
    $wnd.__historyToken = '';

    // Get the initial token from the url's hash component.
    var hash = $wnd.location.hash;
    if (hash.length > 0)
        $wnd.__historyToken = decodeURIComponent(hash.substring(1));

    // Create the timer that checks the browser's url hash every 1/4 s.
    $wnd.__checkHistory = function() {
        var token = '', hash = $wnd.location.hash;
        if (hash.length > 0)
            token = decodeURIComponent(hash.substring(1));

        if (token != $wnd.__historyToken) {
            $wnd.__historyToken = token;
            // TODO - move init back into History
            // this.onHistoryChanged(token);
            var h = new __History_History();
            h.onHistoryChanged(token);
        }

        $wnd.setTimeout('__checkHistory()', 250);
    };

    // Kick off the timer.
    $wnd.__checkHistory();

    return true;
    """)

init()
<commit_msg>pep8.py whitespace cleanup<commit_after># This is the gtk-dependent History module.
# For the pyjamas/javascript version, see platform/HistoryPyJS.py

import sys
from __pyjamas__ import JS, doc, wnd
if sys.platform not in ['mozilla', 'ie6', 'opera', 'oldmoz', 'safari']:
    from __pyjamas__ import get_main_frame

global historyToken
historyToken = ''

historyListeners = []

"""
    Simple History management class for back/forward button support.

    This class allows your AJAX application to use a history.  Each time you
    call newItem(), a new item is added to the history and the history
    listeners are notified.  If the user clicks the browser's forward or back
    buttons, the appropriate item (a string passed to newItem) is fetched
    from the history and the history listeners are notified.

    The address bar of the browser contains the current token, using
    the "#" seperator (for implementation reasons, not because we love
    the # mark).

    You may want to check whether the hash already contains a history
    token when the page loads and use that to show appropriate content;
    this allows users of the site to store direct links in their
    bookmarks or send them in emails.

    To make this work properly in all browsers, you must add a specially
    named iframe to your html page, like this:

    <iframe id='__pygwt_historyFrame' style='width:0;height:0;border:0' />
"""

def addHistoryListener(listener):
    historyListeners.append(listener)

def back():
    wnd().history.back()

def forward():
    wnd().history.forward()

def getToken():
    global historyToken
    return historyToken

def newItem(ht):
    print "History - new item", ht
    onHistoryChanged(ht)
    return

    JS("""
    if(historyToken == "" || historyToken == null){
        historyToken = "#";
    }
    $wnd.location.hash = encodeURIComponent(historyToken);
    """)

# TODO - fireHistoryChangedAndCatch not implemented
def onHistoryChanged(ht):
    #UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler();
    #if (handler != null)
    #   fireHistoryChangedAndCatch(historyToken, handler);
    #else
    fireHistoryChangedImpl(ht)

# TODO
def fireHistoryChangedAndCatch():
    pass

def fireHistoryChangedImpl(ht):
    for listener in historyListeners:
        listener.onHistoryChanged(ht)

def removeHistoryListener(listener):
    historyListeners.remove(listener)

def init():
    print "history: TODO"
    global historyToken
    historyToken = ''
    onHistoryChanged(historyToken)
    return
    JS("""
    $wnd.__historyToken = '';

    // Get the initial token from the url's hash component.
    var hash = $wnd.location.hash;
    if (hash.length > 0)
        $wnd.__historyToken = decodeURIComponent(hash.substring(1));

    // Create the timer that checks the browser's url hash every 1/4 s.
    $wnd.__checkHistory = function() {
        var token = '', hash = $wnd.location.hash;
        if (hash.length > 0)
            token = decodeURIComponent(hash.substring(1));

        if (token != $wnd.__historyToken) {
            $wnd.__historyToken = token;
            // TODO - move init back into History
            // this.onHistoryChanged(token);
            var h = new __History_History();
            h.onHistoryChanged(token);
        }

        $wnd.setTimeout('__checkHistory()', 250);
    };

    // Kick off the timer.
    $wnd.__checkHistory();

    return true;
    """)

init()
<|endoftext|>


<commit_before><commit_msg>Minor cleanup of bader_caller.py<commit_after><|endoftext|>


<commit_before>from __future__ import unicode_literals

__version__ = '2.1.1'
<commit_msg>Prep for 2.2.0 release<commit_after>from __future__ import unicode_literals

__version__ = '2.2.0'
<|endoftext|>


<commit_before>#!/usr/bin/env python

import sys
import os
import gettext
import scratch
import mcpi.minecraft as minecraft
import mcturtle.minecraftturtle as turtle
import mcstuff.minecraftstuff as stuff
import mcpi.block as block
import time

VERSION = "2.0.0"
localedir = os.path.join(os.path.dirname(__file__), 'locale')
_ = gettext.translation(domain = 'scratch2mcpi', localedir = localedir, fallback = True).ugettext

def is_number(value):
    if (isinstance(value, (int, float))):
        return True
    else:
        return False

def connect():
    try:
        return scratch.Scratch()
    except scratch.ScratchError:
        print _("Error: Unable to connect to Scratch. Scratch may be not running or the remote sensor connections may be not enabled.")
        return None

def _listen(s):
    while True:
        try:
            yield s.receive()
        except scratch.ScratchError:
            print _("Error: Disconnected from Scratch.")
            raise StopIteration

def listen(s, mc):
    mcpiX = 0
    mcpiY = 0
    mcpiZ = 0
    mcpiX0 = 0
    mcpiY0 = 0
    mcpiZ0 = 0
    mcpiX1 = 0
    mcpiY1 = 0
    mcpiZ1 = 0
    blockTypeId = 1
    blockData = 0

    # Minecraft Graphics Turtle
    speed = 10
    steps = 0
    degrees = 0

    mc = minecraft.Minecraft.create()
    pos = mc.player.getPos()
    steve = None

    # Minecraft Stuff
    radius = 0
    shapePoints = []
    fill = True
    mcDrawing = stuff.MinecraftDrawing(mc)

    for msg in _listen(s):
        if (msg):
            print "Received: %s" % str(msg)
            if msg[0] == 'broadcast':
                if not mc:
                    mc = minecraft.Minecraft.create()
                if msg[1] == 'hello_minecraft':
                    mc.postToChat("hello minecraft")
                elif msg[1] == 'setPos':
                    if (is_number(mcpiX) and is_number(mcpiY) and is_number(mcpiZ)):
                        mc.player.setPos(mcpiX, mcpiY, mcpiZ)
                        print "setPos: %.1f %.1f %.1f" % (mcpiX, mcpiY, mcpiZ)
                elif msg[1] == 'setBlock':
                    if (is_number(mcpiX) and is_number(mcpiY) and is_number(mcpiZ) and is_number(blockTypeId) and is_number(blockData)):
                        mc.setBlock(mcpiX, mcpiY, mcpiZ, blockTypeId, blockData)
                        print "setBlock: %d %d %d %d %d" % (mcpiX, mcpiY, mcpiZ, blockTypeId, blockData)
                # Minecraft Graphics Turtle(Start)
                elif msg[1] == 'turtle:activate':
                    if steve == None and is_number(mcpiX) and is_number(mcpiY) and is_number(mcpiZ):
                        steve = turtle.MinecraftTurtle(mc, (mcpiX, mcpiY, mcpiZ))
                    else:
                        steve.setposition(mcpiX, mcpiY, mcpiZ)
                elif msg[1] == 'turtle:setPos' and steve != None:
                    if is_number(mcpiX) and is_number(mcpiY) and is_number(mcpiZ):
                        steve.setposition(mcpiX, mcpiY, mcpiZ)
                        print "turtle:setPos: %.1f %.1f %.1f" % (mcpiX, mcpiY, mcpiZ)
                elif msg[1] == 'turtle:forward' and steve != None:
                    if is_number(steps):
                        steve.forward(steps)
                        print "steve.forward: (%d)" % (steps)
                elif msg[1] == 'turtle:backward' and steve != None:
                    if is_number(steps):
                        steve.backward(steps)
                        print "steve.backward: (%d)" % (steps)
                elif msg[1] == 'turtle:right' and steve != None:
                    if is_number(degrees):
                        steve.right(degrees)
                        print "steve.right: (%d)" % (degrees)
                elif msg[1] == 'turtle:left' and steve != None:
                    if is_number(degrees):
                        steve.left(degrees)
                        print "steve.left: (%d)" % (degrees)
                elif msg[1] == 'turtle:up' and steve != None:
                    if is_number(degrees):
                        steve.up(degrees)
                        print "steve.up: (%d)" % (degrees)
                elif msg[1] == 'turtle:down' and steve != None:
                    if is_number(degrees):
                        steve.down(degrees)
                        print "steve.down: (%d)" % (degrees)
                elif msg[1] == 'turtle:penup' and steve != None:
                    steve.penup()
                    print "steve.penup"
                elif msg[1] == 'turtle:pendown' and steve != None:
                    steve.pendown()
                    print "steve.pendown"
                elif msg[1] == 'turtle:setheading' and steve != None:
                    if is_number(degrees):
                        steve.setheading(degrees)
                        print "steve.setheading: (%d)" % (degrees)
                elif msg[1] == 'turtle:setverticalheading' and steve != None:
                    if is_number(degrees):
                        steve.setverticalheading(degrees)
                        print "steve.setverticalheading: (%d)" % (degrees)
                # Minecraft Graphics Turtle(End)
                # Minecraft Stuff(Start)
                elif msg[1] == 'stuff:drawLine':
                    mcDrawing.drawLine(int(mcpiX1), int(mcpiY1), int(mcpiZ1), int(mcpiX), int(mcpiY), int(mcpiZ), blockTypeId, blockData)
                    print "mcDrawing.drawLine: (%d, %d, %d, %d, %d, %d, %d, %d)" % (mcpiX1, mcpiY1, mcpiZ1, mcpiX, mcpiY, mcpiZ, blockTypeId, blockData)
                elif msg[1] == 'stuff:drawSphere':
                    mcDrawing.drawSphere(mcpiX, mcpiY, mcpiZ, radius, blockTypeId, blockData)
                    print "mcDrawing.drawSphere: (%d, %d, %d, %d, %d, %d)" % (mcpiX, mcpiY, mcpiZ, radius, blockTypeId, blockData)
                elif msg[1] == 'stuff:drawCircle':
                    mcDrawing.drawCircle(mcpiX, mcpiY, mcpiZ, radius, blockTypeId, blockData)
                    print "mcDrawing.drawCircle: (%d, %d, %d, %d, %d, %d)" % (mcpiX, mcpiY, mcpiZ, radius, blockTypeId, blockData)
                elif msg[1] == 'stuff:resetShapePoints':
                    shapePoints = []
                    mcDrawing = stuff.MinecraftDrawing(mc)
                elif msg[1] == 'stuff:setShapePoints':
                    shapePoints.append(minecraft.Vec3(int(mcpiX), int(mcpiY), int(mcpiZ)))
                    print "append.shapePoints:"
                    print ' '.join(str(p) for p in shapePoints)
                elif msg[1] == 'stuff:drawFace':
                    if (fill == 'True'):
                        fillFlag = True
                    elif (fill == 'False'):
                        fillFlag = False
                    mcDrawing.drawFace(shapePoints, fillFlag, blockTypeId)
                    print "mcDrawing.drawFace:"
                    print ' '.join(str(p) for p in shapePoints)
                    print(fill)
                    print(blockTypeId)
                # Minecraft Stuff(End)

                elif msg[1] == 'setBlocks':
                    if (is_number(mcpiX0) and is_number(mcpiY0) and is_number(mcpiZ0) and is_number(mcpiX1) and is_number(mcpiY1) and is_number(mcpiZ1) and is_number(blockTypeId) and is_number(blockData)):
                        mc.setBlocks(mcpiX0, mcpiY0, mcpiZ0, mcpiX1, mcpiY1, mcpiZ1, blockTypeId, blockData)
                        print "setBlocks(%d, %d, %d, %d, %d, %d, %d, %d" % (mcpiX0, mcpiY0, mcpiZ0, mcpiX1, mcpiY1, mcpiZ1, blockTypeId, blockData)
                elif msg[1] == 'getPos':
                    playerPos = mc.player.getPos()
                    pos = playerPos
                    s.sensorupdate({
                        'playerX': playerPos.x,
                        'playerY': playerPos.y,
                        'playerZ': playerPos.z
                    })
                elif msg[1] == 'getHeight':
                    posY = mc.getHeight(mcpiX, mcpiZ)
                    s.sensorupdate({'posY': posY})
                    mc.postToChat("posY: %d" % posY)
                elif msg[1] == 'getBlock':
                    blockFound = mc.getBlockWithData(mcpiX, mcpiY, mcpiZ)
                    s.sensorupdate(
                        {'blockType': blockFound.id,
                         'blockDataType': blockFound.data}
                        )
                elif msg[1] == 'pollBlockHits':
                    blockEvents = mc.events.pollBlockHits()
                    print blockEvents
                    if blockEvents:
                        blockEvent = blockEvents[-1]
                        s.sensorupdate({
                            'blockEventX': blockEvent.pos.x,
                            'blockEventY': blockEvent.pos.y,
                            'blockEventZ': blockEvent.pos.z,
                            'blockEventFace': blockEvent.face,
                            'blockEventEntityId': blockEvent.entityId
                        })
                    else:
                        s.sensorupdate({
                            'blockEventX': '',
                            'blockEventY': '',
                            'blockEventZ': '',
                            'blockEventFace': '',
                            'blockEventEntityId': ''
                        })
                elif msg[1] == 'reset':
                    mc.postToChat('reset the world')
                    mc.setBlocks(-100, 0, -100, 100, 63, 100, 0, 0)
                    mc.setBlocks(-100, -63, -100, 100, -2, 100, 1, 0)
                    mc.setBlocks(-100, -1, -100, 100, -1, 100, 2, 0)
                    mc.player.setPos(0, 0, 0)
                elif (len(msg[1])==4 and msg[1]=='echo') or (len(msg[1])>4 and msg[1][:5]=='echo '):
                    words = msg[1].split()
                    if len(words) == 1:
                        mc.postToChat("echo")
                        s.broadcast("echo_back")
                    else:
                        mc.postToChat(" ".join(words[1:]))
            elif msg[0] == 'sensor-update':
                mcpiX = msg[1].get('mcpiX', mcpiX)
                mcpiY = msg[1].get('mcpiY', mcpiY)
                mcpiZ = msg[1].get('mcpiZ', mcpiZ)
                mcpiX0 = msg[1].get('mcpiX0', mcpiX0)
                mcpiY0 = msg[1].get('mcpiY0', mcpiY0)
                mcpiZ0 = msg[1].get('mcpiZ0', mcpiZ0)
                mcpiX1 = msg[1].get('mcpiX1', mcpiX1)
                mcpiY1 = msg[1].get('mcpiY1', mcpiY1)
                mcpiZ1 = msg[1].get('mcpiZ1', mcpiZ1)
                blockTypeId = msg[1].get('blockTypeId', blockTypeId)
                blockData = msg[1].get('blockData', blockData)

                # Minecraft Graphics Turtle
                speed = msg[1].get('speed', speed)
                steps = msg[1].get('steps', steps)
                degrees = msg[1].get('degrees', degrees)

                if steve != None:
                    steve.speed(speed)
                    steve.penblock(blockTypeId, blockData)

                # Minecraft Stuff
                radius = msg[1].get('radius', radius)
                fill = msg[1].get('fill', fill)

def main():
    print "================="
    print "Sratch2MCPI %s" % VERSION
    print "================="
    print ""

    while True:
        s = connect()
        mc = minecraft.Minecraft.create()

        if (s):
            mc.postToChat("Scratch2MCPI connected to Minecraft Pi.")
            print _("Connected to Scratch")

            s.broadcast("hello_minecraft")
            s.broadcast("setPos")
            s.broadcast("setBlock")
            # s.broadcast("setBlocks")
            s.broadcast("getPos")
            s.broadcast("getHeight")
            s.broadcast("getBlock")
            s.broadcast("pollBlockHits")
            s.broadcast("reset")

            s.broadcast("turtle:activate")
            s.broadcast("turtle:forward")
            s.broadcast("turtle:backward")
            s.broadcast("turtle:right")
            s.broadcast("turtle:left")
            s.broadcast("turtle:up")
            s.broadcast("turtle:down")
            s.broadcast("turtle:setPos")
            s.broadcast("turtle:penup")
            s.broadcast("turtle:pendown")
            s.broadcast("turtle:setheading")
            s.broadcast("turtle:setverticalheading")

            s.broadcast("stuff:drawSphere")
            s.broadcast("stuff:drawCircle")
            s.broadcast("stuff:drawLine")
            s.broadcast("stuff:drawFace")
            s.broadcast("stuff:resetShapePoints")
            s.broadcast("stuff:setShapePoints")

            s.sensorupdate(
                {'blockType': 0,
                 'blockDataType': 0}
                )

            listen(s, mc)
            time.sleep(5)

main()
<commit_msg>Refactoring of echo command.<commit_after>#!/usr/bin/env python

import sys
import os
import gettext
import scratch
import mcpi.minecraft as minecraft
import mcturtle.minecraftturtle as turtle
import mcstuff.minecraftstuff as stuff
import mcpi.block as block
import time

VERSION = "2.0.0"
localedir = os.path.join(os.path.dirname(__file__), 'locale')
_ = gettext.translation(domain = 'scratch2mcpi', localedir = localedir, fallback = True).ugettext

def is_number(value):
    if (isinstance(value, (int, float))):
        return True
    else:
        return False

def connect():
    try:
        return scratch.Scratch()
    except scratch.ScratchError:
        print _("Error: Unable to connect to Scratch. Scratch may be not running or the remote sensor connections may be not enabled.")
        return None

def _listen(s):
    while True:
        try:
            yield s.receive()
        except scratch.ScratchError:
            print _("Error: Disconnected from Scratch.")
            raise StopIteration

def listen(s, mc):
    mcpiX = 0
    mcpiY = 0
    mcpiZ = 0
    mcpiX0 = 0
    mcpiY0 = 0
    mcpiZ0 = 0
    mcpiX1 = 0
    mcpiY1 = 0
    mcpiZ1 = 0
    blockTypeId = 1
    blockData = 0

    # Minecraft Graphics Turtle
    speed = 10
    steps = 0
    degrees = 0

    mc = minecraft.Minecraft.create()
    pos = mc.player.getPos()
    steve = None

    # Minecraft Stuff
    radius = 0
    shapePoints = []
    fill = True
    mcDrawing = stuff.MinecraftDrawing(mc)

    for msg in _listen(s):
        if (msg):
            print "Received: %s" % str(msg)
            if msg[0] == 'broadcast':
                if not mc:
                    mc = minecraft.Minecraft.create()
                if msg[1] == 'hello_minecraft':
                    mc.postToChat("hello minecraft")
                elif msg[1] == 'setPos':
                    if (is_number(mcpiX) and is_number(mcpiY) and is_number(mcpiZ)):
                        mc.player.setPos(mcpiX, mcpiY, mcpiZ)
                        print "setPos: %.1f %.1f %.1f" % (mcpiX, mcpiY, mcpiZ)
                elif msg[1] == 'setBlock':
                    if (is_number(mcpiX) and is_number(mcpiY) and is_number(mcpiZ) and is_number(blockTypeId) and is_number(blockData)):
                        mc.setBlock(mcpiX, mcpiY, mcpiZ, blockTypeId, blockData)
                        print "setBlock: %d %d %d %d %d" % (mcpiX, mcpiY, mcpiZ, blockTypeId, blockData)
                # Minecraft Graphics Turtle(Start)
                elif msg[1] == 'turtle:activate':
                    if steve == None and is_number(mcpiX) and is_number(mcpiY) and is_number(mcpiZ):
                        steve = turtle.MinecraftTurtle(mc, (mcpiX, mcpiY, mcpiZ))
                    else:
                        steve.setposition(mcpiX, mcpiY, mcpiZ)
                elif msg[1] == 'turtle:setPos' and steve != None:
                    if is_number(mcpiX) and is_number(mcpiY) and is_number(mcpiZ):
                        steve.setposition(mcpiX, mcpiY, mcpiZ)
                        print "turtle:setPos: %.1f %.1f %.1f" % (mcpiX, mcpiY, mcpiZ)
                elif msg[1] == 'turtle:forward' and steve != None:
                    if is_number(steps):
                        steve.forward(steps)
                        print "steve.forward: (%d)" % (steps)
                elif msg[1] == 'turtle:backward' and steve != None:
                    if is_number(steps):
                        steve.backward(steps)
                        print "steve.backward: (%d)" % (steps)
                elif msg[1] == 'turtle:right' and steve != None:
                    if is_number(degrees):
                        steve.right(degrees)
                        print "steve.right: (%d)" % (degrees)
                elif msg[1] == 'turtle:left' and steve != None:
                    if is_number(degrees):
                        steve.left(degrees)
                        print "steve.left: (%d)" % (degrees)
                elif msg[1] == 'turtle:up' and steve != None:
                    if is_number(degrees):
                        steve.up(degrees)
                        print "steve.up: (%d)" % (degrees)
                elif msg[1] == 'turtle:down' and steve != None:
                    if is_number(degrees):
                        steve.down(degrees)
                        print "steve.down: (%d)" % (degrees)
                elif msg[1] == 'turtle:penup' and steve != None:
                    steve.penup()
                    print "steve.penup"
                elif msg[1] == 'turtle:pendown' and steve != None:
                    steve.pendown()
                    print "steve.pendown"
                elif msg[1] == 'turtle:setheading' and steve != None:
                    if is_number(degrees):
                        steve.setheading(degrees)
                        print "steve.setheading: (%d)" % (degrees)
                elif msg[1] == 'turtle:setverticalheading' and steve != None:
                    if is_number(degrees):
                        steve.setverticalheading(degrees)
                        print "steve.setverticalheading: (%d)" % (degrees)
                # Minecraft Graphics Turtle(End)
                # Minecraft Stuff(Start)
                elif msg[1] == 'stuff:drawLine':
                    mcDrawing.drawLine(int(mcpiX1), int(mcpiY1), int(mcpiZ1), int(mcpiX), int(mcpiY), int(mcpiZ), blockTypeId, blockData)
                    print "mcDrawing.drawLine: (%d, %d, %d, %d, %d, %d, %d, %d)" % (mcpiX1, mcpiY1, mcpiZ1, mcpiX, mcpiY, mcpiZ, blockTypeId, blockData)
                elif msg[1] == 'stuff:drawSphere':
                    mcDrawing.drawSphere(mcpiX, mcpiY, mcpiZ, radius, blockTypeId, blockData)
                    print "mcDrawing.drawSphere: (%d, %d, %d, %d, %d, %d)" % (mcpiX, mcpiY, mcpiZ, radius, blockTypeId, blockData)
                elif msg[1] == 'stuff:drawCircle':
                    mcDrawing.drawCircle(mcpiX, mcpiY, mcpiZ, radius, blockTypeId, blockData)
                    print "mcDrawing.drawCircle: (%d, %d, %d, %d, %d, %d)" % (mcpiX, mcpiY, mcpiZ, radius, blockTypeId, blockData)
                elif msg[1] == 'stuff:resetShapePoints':
                    shapePoints = []
                    mcDrawing = stuff.MinecraftDrawing(mc)
                elif msg[1] == 'stuff:setShapePoints':
                    shapePoints.append(minecraft.Vec3(int(mcpiX), int(mcpiY), int(mcpiZ)))
                    print "append.shapePoints:"
                    print ' '.join(str(p) for p in shapePoints)
                elif msg[1] == 'stuff:drawFace':
                    if (fill == 'True'):
                        fillFlag = True
                    elif (fill == 'False'):
                        fillFlag = False
                    mcDrawing.drawFace(shapePoints, fillFlag, blockTypeId)
                    print "mcDrawing.drawFace:"
                    print ' '.join(str(p) for p in shapePoints)
                    print(fill)
                    print(blockTypeId)
                # Minecraft Stuff(End)

                elif msg[1] == 'setBlocks':
                    if (is_number(mcpiX0) and is_number(mcpiY0) and is_number(mcpiZ0) and is_number(mcpiX1) and is_number(mcpiY1) and is_number(mcpiZ1) and is_number(blockTypeId) and is_number(blockData)):
                        mc.setBlocks(mcpiX0, mcpiY0, mcpiZ0, mcpiX1, mcpiY1, mcpiZ1, blockTypeId, blockData)
                        print "setBlocks(%d, %d, %d, %d, %d, %d, %d, %d" % (mcpiX0, mcpiY0, mcpiZ0, mcpiX1, mcpiY1, mcpiZ1, blockTypeId, blockData)
                elif msg[1] == 'getPos':
                    playerPos = mc.player.getPos()
                    pos = playerPos
                    s.sensorupdate({
                        'playerX': playerPos.x,
                        'playerY': playerPos.y,
                        'playerZ': playerPos.z
                    })
                elif msg[1] == 'getHeight':
                    posY = mc.getHeight(mcpiX, mcpiZ)
                    s.sensorupdate({'posY': posY})
                    mc.postToChat("posY: %d" % posY)
                elif msg[1] == 'getBlock':
                    blockFound = mc.getBlockWithData(mcpiX, mcpiY, mcpiZ)
                    s.sensorupdate(
                        {'blockType': blockFound.id,
                         'blockDataType': blockFound.data}
                        )
                elif msg[1] == 'pollBlockHits':
                    blockEvents = mc.events.pollBlockHits()
                    print blockEvents
                    if blockEvents:
                        blockEvent = blockEvents[-1]
                        s.sensorupdate({
                            'blockEventX': blockEvent.pos.x,
                            'blockEventY': blockEvent.pos.y,
                            'blockEventZ': blockEvent.pos.z,
                            'blockEventFace': blockEvent.face,
                            'blockEventEntityId': blockEvent.entityId
                        })
                    else:
                        s.sensorupdate({
                            'blockEventX': '',
                            'blockEventY': '',
                            'blockEventZ': '',
                            'blockEventFace': '',
                            'blockEventEntityId': ''
                        })
                elif msg[1] == 'reset':
                    mc.postToChat('reset the world')
                    mc.setBlocks(-100, 0, -100, 100, 63, 100, 0, 0)
                    mc.setBlocks(-100, -63, -100, 100, -2, 100, 1, 0)
                    mc.setBlocks(-100, -1, -100, 100, -1, 100, 2, 0)
                    mc.player.setPos(0, 0, 0)
                elif msg[1] == 'echo' or msg[1].startswith('echo '):
                    words = msg[1].split()
                    mc.postToChat(" ".join(words[1:]))
            elif msg[0] == 'sensor-update':
                mcpiX = msg[1].get('mcpiX', mcpiX)
                mcpiY = msg[1].get('mcpiY', mcpiY)
                mcpiZ = msg[1].get('mcpiZ', mcpiZ)
                mcpiX0 = msg[1].get('mcpiX0', mcpiX0)
                mcpiY0 = msg[1].get('mcpiY0', mcpiY0)
                mcpiZ0 = msg[1].get('mcpiZ0', mcpiZ0)
                mcpiX1 = msg[1].get('mcpiX1', mcpiX1)
                mcpiY1 = msg[1].get('mcpiY1', mcpiY1)
                mcpiZ1 = msg[1].get('mcpiZ1', mcpiZ1)
                blockTypeId = msg[1].get('blockTypeId', blockTypeId)
                blockData = msg[1].get('blockData', blockData)

                # Minecraft Graphics Turtle
                speed = msg[1].get('speed', speed)
                steps = msg[1].get('steps', steps)
                degrees = msg[1].get('degrees', degrees)

                if steve != None:
                    steve.speed(speed)
                    steve.penblock(blockTypeId, blockData)

                # Minecraft Stuff
                radius = msg[1].get('radius', radius)
                fill = msg[1].get('fill', fill)

def main():
    print "================="
    print "Sratch2MCPI %s" % VERSION
    print "================="
    print ""

    while True:
        s = connect()
        mc = minecraft.Minecraft.create()

        if (s):
            mc.postToChat("Scratch2MCPI connected to Minecraft Pi.")
            print _("Connected to Scratch")

            s.broadcast("hello_minecraft")
            s.broadcast("setPos")
            s.broadcast("setBlock")
            # s.broadcast("setBlocks")
            s.broadcast("getPos")
            s.broadcast("getHeight")
            s.broadcast("getBlock")
            s.broadcast("pollBlockHits")
            s.broadcast("reset")

            s.broadcast("turtle:activate")
            s.broadcast("turtle:forward")
            s.broadcast("turtle:backward")
            s.broadcast("turtle:right")
            s.broadcast("turtle:left")
            s.broadcast("turtle:up")
            s.broadcast("turtle:down")
            s.broadcast("turtle:setPos")
            s.broadcast("turtle:penup")
            s.broadcast("turtle:pendown")
            s.broadcast("turtle:setheading")
            s.broadcast("turtle:setverticalheading")

            s.broadcast("stuff:drawSphere")
            s.broadcast("stuff:drawCircle")
            s.broadcast("stuff:drawLine")
            s.broadcast("stuff:drawFace")
            s.broadcast("stuff:resetShapePoints")
            s.broadcast("stuff:setShapePoints")

            s.sensorupdate(
                {'blockType': 0,
                 'blockDataType': 0}
                )

            listen(s, mc)
            time.sleep(5)

main()
<|endoftext|>