<commit_before>__author__ = 'tdpreece'
__author__ = 'tdpreece'
import logging
import time
import json
from collections import OrderedDict

import stomp

logger = logging.getLogger('tdl.client')
logger.addHandler(logging.NullHandler())

class Client(object):
    def __init__(self, hostname, port, username):
        self.hostname = hostname
        self.port = port

    def go_live_with(self, implementation_map):
        try:
            remote_broker = RemoteBroker(self.hostname, self.port)
            handling_strategy = RespondToAllRequests(implementation_map)
            remote_broker.subscribe(handling_strategy)
            time.sleep(1)
            remote_broker.close()
        except Exception as e:
            logger.exception('Problem communicating with the broker.')

    def trial_run_with(self, implementation_map):
        remote_broker = RemoteBroker(self.hostname, self.port)
        handling_strategy = PeekAtFirstRequest(implementation_map)
        remote_broker.subscribe(handling_strategy)
        time.sleep(1)
        remote_broker.close()

class HandlingStrategy(object):
    def __init__(self, implementation_map):
        self.implementation_map = implementation_map

    def respond_to(self, message):
        decoded_message = json.loads(message)
        method = decoded_message['method']
        params = decoded_message['params']
        id = decoded_message['id']
        implementation = self.implementation_map[method]
        try:
           result = implementation(params)
        except Exception as e:
           logger.info('The user implementation has thrown an exception: {}'.format(e.message))
           result = None
        params_str = ", ".join([str(p) for p in params])
        print('id = {id}, req = {method}({params}), resp = {result}'.format(id=id, method=method, params=params_str,
                                                                           result=result))
        if result is not None:
            response = OrderedDict([
                ('result', result),
                ('error', None),
                ('id', id),
                ])
        return response

class RespondToAllRequests(HandlingStrategy):
    def process_next_message_from(self, remote_broker, headers, message):
        response = self.respond_to(message)
        if response is not None:
            remote_broker.acknowledge(headers)
            remote_broker.publish(response)

class PeekAtFirstRequest(HandlingStrategy):
    def process_next_message_from(self, remote_broker, headers, message):
        self.respond_to(message)

class Listener(stomp.ConnectionListener):
    def __init__(self, remote_broker, handling_strategy):
        self.remote_broker = remote_broker
        self.handling_strategy = handling_strategy

    def on_message(self, headers, message):
        self.handling_strategy.process_next_message_from(self.remote_broker, headers, message)

class RemoteBroker(object):
    def __init__(self, hostname, port):
        hosts = [(hostname, port)]
        self.conn = stomp.Connection(host_and_ports=hosts)
        self.conn.start()
        self.conn.connect(wait=True)

    def acknowledge(self, headers):
        self.conn.ack(headers['message-id'], headers['subscription'])

    def publish(self, response):
        self.conn.send(
            body=json.dumps(response, separators=(',', ':')),
            destination='test.resp'
        )

    def subscribe(self, handling_strategy):
        listener = Listener(self, handling_strategy)
        self.conn.set_listener('listener', listener)
        self.conn.subscribe(destination='test.req', id=1, ack='client-individual')

    def close(self):
        self.conn.disconnect()<commit_msg>Extract duplication into run method.<commit_after>__author__ = 'tdpreece'
__author__ = 'tdpreece'
import logging
import time
import json
from collections import OrderedDict

import stomp

logger = logging.getLogger('tdl.client')
logger.addHandler(logging.NullHandler())

class Client(object):
    def __init__(self, hostname, port, username):
        self.hostname = hostname
        self.port = port

    def go_live_with(self, implementation_map):
        handling_strategy = RespondToAllRequests(implementation_map)
        self.run(handling_strategy)

    def trial_run_with(self, implementation_map):
        handling_strategy = PeekAtFirstRequest(implementation_map)
        self.run(handling_strategy)

    def run(self, handling_strategy):
        try:
            remote_broker = RemoteBroker(self.hostname, self.port)
            remote_broker.subscribe(handling_strategy)
            time.sleep(1)
            remote_broker.close()
        except Exception as e:
            logger.exception('Problem communicating with the broker.')

class HandlingStrategy(object):
    def __init__(self, implementation_map):
        self.implementation_map = implementation_map

    def respond_to(self, message):
        decoded_message = json.loads(message)
        method = decoded_message['method']
        params = decoded_message['params']
        id = decoded_message['id']
        implementation = self.implementation_map[method]
        try:
           result = implementation(params)
        except Exception as e:
           logger.info('The user implementation has thrown an exception: {}'.format(e.message))
           result = None
        params_str = ", ".join([str(p) for p in params])
        print('id = {id}, req = {method}({params}), resp = {result}'.format(id=id, method=method, params=params_str,
                                                                           result=result))
        if result is not None:
            response = OrderedDict([
                ('result', result),
                ('error', None),
                ('id', id),
                ])
        return response

class RespondToAllRequests(HandlingStrategy):
    def process_next_message_from(self, remote_broker, headers, message):
        response = self.respond_to(message)
        if response is not None:
            remote_broker.acknowledge(headers)
            remote_broker.publish(response)

class PeekAtFirstRequest(HandlingStrategy):
    def process_next_message_from(self, remote_broker, headers, message):
        self.respond_to(message)

class Listener(stomp.ConnectionListener):
    def __init__(self, remote_broker, handling_strategy):
        self.remote_broker = remote_broker
        self.handling_strategy = handling_strategy

    def on_message(self, headers, message):
        self.handling_strategy.process_next_message_from(self.remote_broker, headers, message)

class RemoteBroker(object):
    def __init__(self, hostname, port):
        hosts = [(hostname, port)]
        self.conn = stomp.Connection(host_and_ports=hosts)
        self.conn.start()
        self.conn.connect(wait=True)

    def acknowledge(self, headers):
        self.conn.ack(headers['message-id'], headers['subscription'])

    def publish(self, response):
        self.conn.send(
            body=json.dumps(response, separators=(',', ':')),
            destination='test.resp'
        )

    def subscribe(self, handling_strategy):
        listener = Listener(self, handling_strategy)
        self.conn.set_listener('listener', listener)
        self.conn.subscribe(destination='test.req', id=1, ack='client-individual')

    def close(self):
        self.conn.disconnect()<|endoftext|>


<commit_before>import cPickle
import carmcmc
import numpy as np
import matplotlib.pyplot as plt

buff = open("CHIC.pickle", "rb")
x, y, dy = cPickle.load(buff)
buff.close()

p = 5
q = 1
nsample = 100
nburnin = 10

car = carmcmc.CarmaMCMC(x, y, dy, p, nsample, q=q, nburnin=nburnin)
post = car.RunMCMC()

secInDay = 3600 * 24.
nplot = 25000
time_predict = np.linspace(x.min(), x.max() + 100 * secInDay, nplot)
predicted_mean, predicted_var = post.predict_lightcurve(time_predict, bestfit="med")
import pdb; pdb.set_trace()

idx = np.where(predicted_var < 0)
print "NEGATIVE VARIANCE", idx

idx = np.where(predicted_var > 0)
time_predict = time_predict[idx]
predicted_mean = predicted_mean[idx]
predicted_var = predicted_var[idx]

fig = plt.figure()
sp = fig.add_subplot(111)
sp.errorbar(x, y, yerr=dy, fmt='ko', label='Data', ms=4, capsize=1)
sp.plot(time_predict, predicted_mean, '-r', label='Kalman Filter')
predicted_low = predicted_mean - np.sqrt(predicted_var)
predicted_high = predicted_mean + np.sqrt(predicted_var)
sp.fill_between(time_predict, predicted_low, predicted_high,
                edgecolor=None, facecolor='blue', alpha=0.25, label="1-sigma range")
sp.set_xlabel('Time')
sp.legend(loc=1)
plt.show()
<commit_msg>New plots<commit_after>import cPickle
import carmcmc
import numpy as np
import matplotlib.pyplot as plt

buff = open("CHIC.pickle", "rb")
x, y, dy = cPickle.load(buff)
buff.close()

p = 5
q = 1
nsample = 5000
nburnin = 10000

car = carmcmc.CarmaMCMC(x, y, dy, p, nsample, q=q, nburnin=nburnin)
post = car.RunMCMC()

secInDay = 3600 * 24.
nplot = 25000
time_predict = np.linspace(x.min(), x.max() + 100 * secInDay, nplot)
print "getting predicted values..."
predicted_mean, predicted_var = post.predict_lightcurve(time_predict, bestfit="med")
# import pdb; pdb.set_trace()

idx = np.where(predicted_var < 0)
print "NEGATIVE VARIANCE", idx

idx = np.where(predicted_var > 0)
time_predict = time_predict[idx]
predicted_mean = predicted_mean[idx]
predicted_var = predicted_var[idx]

fig = plt.figure()
sp = fig.add_subplot(111)
sp.errorbar(x, y, yerr=dy, fmt='ko', label='Data', ms=4, capsize=1)
sp.plot(time_predict, predicted_mean, '-r', label='Kalman Filter')
predicted_low = predicted_mean - np.sqrt(predicted_var)
predicted_high = predicted_mean + np.sqrt(predicted_var)
sp.fill_between(time_predict, predicted_low, predicted_high,
                edgecolor=None, facecolor='blue', alpha=0.25, label="1-sigma range")
sp.set_xlabel('Time')
sp.legend(loc=1)
plt.show()
<|endoftext|>


<commit_before>from uctf import generate_init_script
from uctf import get_ground_control_port
from uctf import get_launch_snippet
from uctf import get_vehicle_pose
from uctf import spawn_model
from uctf import VEHICLE_BASE_PORT
from uctf import write_launch_file

def spawn_team(color):
    # ensure valid team color
    assert color in ['blue', 'gold']

    launch_snippet = ''

    # spawn 50 vehicles
    for i in range(50):
        # valid MAV_SYS_IDs 1 to 250
        # BLUE uses 1 to 50
        # GOLD uses 101 to 150
        mav_sys_id = 1 + i
        if color == 'gold':
            mav_sys_id += 100

        # the first 25 vehicles per team are iris
        # the second 25 vehicles per team are plane
        vehicle_type = 'iris' if i < 25 else 'plane'

        # each vehicle uses 4 consecutive ports
        # the first one is VEHICLE_BASE_PORT + MAV_SYS_IDs
        # the first two are used between the mavlink gazebo plugin and the px4
        # the second two are used between the px4 and and the mavros node
        vehicle_base_port = VEHICLE_BASE_PORT + mav_sys_id * 4

        # generate the vehicle specific init script
        init_script_path = generate_init_script(
            mav_sys_id, vehicle_type, vehicle_base_port,
            get_ground_control_port(color))

        # spawn the vehicle model in gazebo
        vehicle_pose = get_vehicle_pose(mav_sys_id, vehicle_type, color)
        spawn_model(
            mav_sys_id, vehicle_type, vehicle_base_port, color, vehicle_pose)

        launch_snippet += get_launch_snippet(
            mav_sys_id, vehicle_type, vehicle_base_port, init_script_path)

    launch_path = write_launch_file(launch_snippet)
    print('roslaunch %s' % launch_path)
<commit_msg>allow spawning only a subset of the team<commit_after>import argparse

from uctf import generate_init_script
from uctf import get_ground_control_port
from uctf import get_launch_snippet
from uctf import get_vehicle_pose
from uctf import spawn_model
from uctf import VEHICLE_BASE_PORT
from uctf import write_launch_file

def vehicle_id_type(value):
    value = int(value)
    if value < 1 or value > 50:
        raise argparse.ArgumentTypeError('Vehicle id must be in [1, 50]')
    return value

def spawn_team(color):
    parser = argparse.ArgumentParser('Spawn vehicle for one team.')
    parser.add_argument(
        'vehicle_id', nargs='*', metavar='VEHICLE_ID', type=vehicle_id_type,
        default=range(1, 51),
        help='The vehicle ids to spawn (default: 1-50)')
    args = parser.parse_args()

    # ensure valid team color
    assert color in ['blue', 'gold']

    launch_snippet = ''

    # spawn 50 vehicles
    for i in args.vehicle_id:
        # valid MAV_SYS_IDs 1 to 250
        # BLUE uses 1 to 50
        # GOLD uses 101 to 150
        mav_sys_id = i
        if color == 'gold':
            mav_sys_id += 100

        # the first 25 vehicles per team are iris
        # the second 25 vehicles per team are plane
        vehicle_type = 'iris' if i <= 25 else 'plane'

        # each vehicle uses 4 consecutive ports
        # the first one is VEHICLE_BASE_PORT + MAV_SYS_IDs
        # the first two are used between the mavlink gazebo plugin and the px4
        # the second two are used between the px4 and and the mavros node
        vehicle_base_port = VEHICLE_BASE_PORT + mav_sys_id * 4

        # generate the vehicle specific init script
        init_script_path = generate_init_script(
            mav_sys_id, vehicle_type, vehicle_base_port,
            get_ground_control_port(color))

        # spawn the vehicle model in gazebo
        vehicle_pose = get_vehicle_pose(mav_sys_id, vehicle_type, color)
        spawn_model(
            mav_sys_id, vehicle_type, vehicle_base_port, color, vehicle_pose)

        launch_snippet += get_launch_snippet(
            mav_sys_id, vehicle_type, vehicle_base_port, init_script_path)

    launch_path = write_launch_file(launch_snippet)
    print('roslaunch %s' % launch_path)
<|endoftext|>


<commit_before>import argparse
import os
import shutil
import subprocess

from uctf import generate_config
from uctf import get_ground_control_port
from uctf import generate_launch_file
from uctf import get_vehicle_base_port
from uctf import get_vehicle_pose
from uctf import delete_model
from uctf import spawn_model

def vehicle_id_type(value):
    """Validate the vehicle_id_type from string to int."""
    value = int(value)
    if value < 1 or value > 50:
        raise argparse.ArgumentTypeError('Vehicle id must be in [1, 50]')
    return value

def vehicle_type_and_mav_sys_id(vehicle_id, vehicle_color):
    """Get the vehicle_type and mav_sys_id from the vehicle's id and color."""
    # valid MAV_SYS_IDs 1 to 250

    # the first 25 vehicles per team are iris
    # the second 25 vehicles per team are plane (delta_wing)
    vehicle_type = 'iris' if vehicle_id <= 25 else 'delta_wing'

    # BLUE uses 1 to 50
    # GOLD uses 101 to 150
    mav_sys_id = vehicle_id
    if vehicle_color == 'gold':
        mav_sys_id += 100
    return vehicle_type, mav_sys_id

def spawn_team(color):
    parser = argparse.ArgumentParser('Spawn vehicle for one team.')
    parser.add_argument(
        'vehicle_id', nargs='*', metavar='VEHICLE_ID', type=vehicle_id_type,
        default=range(1, 51),
        help='The vehicle ids to spawn (default: 1-50)')
    parser.add_argument(
        '--gazebo-ros-master-uri',
        help='The uri used to spawn the models')
    parser.add_argument(
        '--mavlink-address',
        help='The IP address for mavlink (default: INADDR_ANY)')
    parser.add_argument(
        '--acs-network-interface', default='lo',
        help='The interface name for acs to talk on.')
    parser.add_argument(
        '--no-launch', action='store_false', dest='launch',
        help='Run generate launch file')
    parser.add_argument(
        '--no-delete', action='store_false', dest='delete',
        help='Despawn when killed')
    parser.add_argument(
        '--no-payload', action='store_false', dest='include_payload',
        help='Do not launch the payload.')
    parser.add_argument('--debug', action='store_true')
    parser.add_argument('--px4', action='store_true', default=False)
    parser.add_argument('--mavros', action='store_true', default=False)
    parser.add_argument('--gazebo-ip', default='127.0.0.1')
    parser.add_argument('--local-ip', default='127.0.0.1')
    parser.add_argument('--tactic-module', default='increasing_altitude')
    parser.add_argument('--tactic-name', default='IncreasingAltitude')
    args = parser.parse_args()
    autopilot = 'px4' if args.px4 else 'ardupilot'
    if not args.gazebo_ros_master_uri:
        args.gazebo_ros_master_uri = 'http://%s:11311' % args.gazebo_ip
    # ensure valid team color
    assert color in ['blue', 'gold']

    cmds = []

    #  Insert ~/blessed/* template files needed for autonomy-payload
    template_dir = os.path.join(
            os.path.dirname(__file__), 'templates', 'blessed')
    target_dir = os.path.join(os.path.expanduser('~'), 'blessed')
    if not os.path.exists(target_dir):
        shutil.copytree(template_dir, target_dir)

    # spawn 50 vehicles
    for i in args.vehicle_id:

        vehicle_type, mav_sys_id = vehicle_type_and_mav_sys_id(i, color)

        vehicle_base_port = get_vehicle_base_port(mav_sys_id)

        # generate the vehicle specific init script
        init_script_path = generate_config(
            mav_sys_id, vehicle_type, vehicle_base_port,
            get_ground_control_port(color), autopilot=autopilot)

        # spawn the vehicle model in gazebo
        vehicle_pose = get_vehicle_pose(mav_sys_id, vehicle_type, color)
        spawn_model(
            mav_sys_id, vehicle_type, vehicle_base_port, color, vehicle_pose,
            ros_master_uri=args.gazebo_ros_master_uri,
            mavlink_address=args.mavlink_address,
            debug=args.debug, autopilot=autopilot,
            gazebo_ip=args.gazebo_ip,
            local_ip=args.local_ip,
        )

        launch_path = generate_launch_file(
            mav_sys_id, vehicle_type, vehicle_base_port,
            init_script_path, args.debug,
            ground_port=get_ground_control_port(color),
            autopilot=autopilot,
            gazebo_ip=args.gazebo_ip,
            local_ip=args.local_ip,
            include_payload=args.include_payload,
            launch_mavros=args.mavros,
            acs_network_inteface=args.acs_network_interface,
        )

        ros_master_port = 11311 + mav_sys_id
        env = {'ROS_MASTER_URI': 'http://localhost:%d' % ros_master_port}
        cmd = ['roslaunch', launch_path]
        cmds.append((env, cmd))
        env_str = ' '.join(['%s=%s' % (k, v) for k, v in env.items()])
        print(env_str + ' ' + ' '.join(cmd))

    retcode = 0
    if args.launch:
        processes = []
        for (add_env, cmd) in cmds:
            env = dict(os.environ)
            env.update(add_env)
            p = subprocess.Popen(cmd, env=env)
            processes.append(p)
        try:
            for p in processes:
                p.wait()
        except KeyboardInterrupt:
            pass
        finally:
            for p in processes:
                try:
                    p.terminate()
                except OSError:
                    pass
    if args.delete:
        for i in args.vehicle_id:
            vehicle_type, mav_sys_id = vehicle_type_and_mav_sys_id(i, color)
            delete_model(
                mav_sys_id,
                vehicle_type,
                ros_master_uri=args.gazebo_ros_master_uri)
    return retcode
<commit_msg>Cleaning up unused arguments<commit_after>import argparse
import os
import shutil
import subprocess

from uctf import generate_config
from uctf import get_ground_control_port
from uctf import generate_launch_file
from uctf import get_vehicle_base_port
from uctf import get_vehicle_pose
from uctf import delete_model
from uctf import spawn_model

def vehicle_id_type(value):
    """Validate the vehicle_id_type from string to int."""
    value = int(value)
    if value < 1 or value > 50:
        raise argparse.ArgumentTypeError('Vehicle id must be in [1, 50]')
    return value

def vehicle_type_and_mav_sys_id(vehicle_id, vehicle_color):
    """Get the vehicle_type and mav_sys_id from the vehicle's id and color."""
    # valid MAV_SYS_IDs 1 to 250

    # the first 25 vehicles per team are iris
    # the second 25 vehicles per team are plane (delta_wing)
    vehicle_type = 'iris' if vehicle_id <= 25 else 'delta_wing'

    # BLUE uses 1 to 50
    # GOLD uses 101 to 150
    mav_sys_id = vehicle_id
    if vehicle_color == 'gold':
        mav_sys_id += 100
    return vehicle_type, mav_sys_id

def spawn_team(color):
    parser = argparse.ArgumentParser('Spawn vehicle for one team.')
    parser.add_argument(
        'vehicle_id', nargs='*', metavar='VEHICLE_ID', type=vehicle_id_type,
        default=range(1, 51),
        help='The vehicle ids to spawn (default: 1-50)')
    parser.add_argument(
        '--gazebo-ros-master-uri',
        help='The uri used to spawn the models')
    parser.add_argument(
        '--mavlink-address',
        help='The IP address for mavlink (default: INADDR_ANY)')
    parser.add_argument(
        '--acs-network-interface', default='lo',
        help='The interface name for acs to talk on.')
    parser.add_argument(
        '--no-launch', action='store_false', dest='launch',
        help='Run generate launch file')
    parser.add_argument(
        '--no-delete', action='store_false', dest='delete',
        help='Despawn when killed')
    parser.add_argument(
        '--no-payload', action='store_false', dest='include_payload',
        help='Do not launch the payload.')
    parser.add_argument('--debug', action='store_true')
    parser.add_argument('--px4', action='store_true', default=False)
    parser.add_argument('--mavros', action='store_true', default=False)
    parser.add_argument('--gazebo-ip', default='127.0.0.1')
    parser.add_argument('--local-ip', default='127.0.0.1')
    args = parser.parse_args()
    autopilot = 'px4' if args.px4 else 'ardupilot'
    if not args.gazebo_ros_master_uri:
        args.gazebo_ros_master_uri = 'http://%s:11311' % args.gazebo_ip
    # ensure valid team color
    assert color in ['blue', 'gold']

    cmds = []

    #  Insert ~/blessed/* template files needed for autonomy-payload
    template_dir = os.path.join(
            os.path.dirname(__file__), 'templates', 'blessed')
    target_dir = os.path.join(os.path.expanduser('~'), 'blessed')
    if not os.path.exists(target_dir):
        shutil.copytree(template_dir, target_dir)

    # spawn 50 vehicles
    for i in args.vehicle_id:

        vehicle_type, mav_sys_id = vehicle_type_and_mav_sys_id(i, color)

        vehicle_base_port = get_vehicle_base_port(mav_sys_id)

        # generate the vehicle specific init script
        init_script_path = generate_config(
            mav_sys_id, vehicle_type, vehicle_base_port,
            get_ground_control_port(color), autopilot=autopilot)

        # spawn the vehicle model in gazebo
        vehicle_pose = get_vehicle_pose(mav_sys_id, vehicle_type, color)
        spawn_model(
            mav_sys_id, vehicle_type, vehicle_base_port, color, vehicle_pose,
            ros_master_uri=args.gazebo_ros_master_uri,
            mavlink_address=args.mavlink_address,
            debug=args.debug, autopilot=autopilot,
            gazebo_ip=args.gazebo_ip,
            local_ip=args.local_ip,
        )

        launch_path = generate_launch_file(
            mav_sys_id, vehicle_type, vehicle_base_port,
            init_script_path, args.debug,
            ground_port=get_ground_control_port(color),
            autopilot=autopilot,
            gazebo_ip=args.gazebo_ip,
            local_ip=args.local_ip,
            include_payload=args.include_payload,
            launch_mavros=args.mavros,
            acs_network_inteface=args.acs_network_interface,
        )

        ros_master_port = 11311 + mav_sys_id
        env = {'ROS_MASTER_URI': 'http://localhost:%d' % ros_master_port}
        cmd = ['roslaunch', launch_path]
        cmds.append((env, cmd))
        env_str = ' '.join(['%s=%s' % (k, v) for k, v in env.items()])
        print(env_str + ' ' + ' '.join(cmd))

    retcode = 0
    if args.launch:
        processes = []
        for (add_env, cmd) in cmds:
            env = dict(os.environ)
            env.update(add_env)
            p = subprocess.Popen(cmd, env=env)
            processes.append(p)
        try:
            for p in processes:
                p.wait()
        except KeyboardInterrupt:
            pass
        finally:
            for p in processes:
                try:
                    p.terminate()
                except OSError:
                    pass
    if args.delete:
        for i in args.vehicle_id:
            vehicle_type, mav_sys_id = vehicle_type_and_mav_sys_id(i, color)
            delete_model(
                mav_sys_id,
                vehicle_type,
                ros_master_uri=args.gazebo_ros_master_uri)
    return retcode
<|endoftext|>


<commit_before>import matplotlib.pyplot as plt
import numpy as np
from utils import get_labels

class Visualizer(object):
    """Visualizer class"""
    def __init__(self, dataset_name):
        self.dataset_name = dataset_name
        self.labels = get_labels(self.dataset_name)

    def plot_image(self, image_array, one_hot_encoder=None):
        image_array = np.squeeze(image_array)
        image_array = image_array.astype('uint8')
        plt.imshow(image_array)
        if one_hot_encoder != None:
            label_arg = np.argmax(one_hot_encoder)
            plt.title(self.labels[label_arg])
        plt.show()

<commit_msg>Deleted unused visualizer class<commit_after><|endoftext|>


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

import os
import signal
from gi.repository import Gtk
from gi.repository import AppIndicator3 as appindicator

import tasks

APPINDICATOR_ID = 'bzoing'

class MyBzoing:
    def __init__(self):
        self.task_list = []
        self.tasklist_id = -1
        indicator = appindicator.Indicator.new(APPINDICATOR_ID,
                                           os.path.abspath('sinoamarelo.svg'),
                                           appindicator.IndicatorCategory.APPLICATION_STATUS)
        indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
        indicator.set_menu(self.build_menu())
        Gtk.main()

    def build_menu(self):
        """
        Builds all the menus
        :rtype: object
        """
        menu = Gtk.Menu()
        item_new_task = Gtk.MenuItem('New task')
        item_new_task.connect('activate', self.new_task)
        menu.append(item_new_task)

        item_see_tasks = Gtk.MenuItem('See tasks')
        item_see_tasks.connect('activate', self.see_tasks)
        menu.append(item_see_tasks)

        item_separator = Gtk.SeparatorMenuItem()
        menu.append(item_separator)

        item_stop_alarms = Gtk.MenuItem('Stop Alarms')
        item_stop_alarms.connect('activate', self.stop_alarms)
        menu.append(item_stop_alarms)

        item_quit = Gtk.MenuItem('Quit')
        item_quit.connect('activate', self.quit)
        menu.append(item_quit)

        menu.show_all()
        return menu

    def new_task(self, _):
        window_task = Gtk.Window(title='Create Task')
        window_task.connect('destroy', self.quit_window)

        vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
        hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 6)

        task_label = Gtk.Label()
        task_label.set_text('Task: ')
        hbox.pack_start(task_label, False, False, 2)

        task_field = Gtk.Entry()
        hbox.pack_start(task_field, False, False, 2)

        vbox.pack_start(hbox, False, False, 6)

        button_alarm = Gtk.Button()
        button_alarm.set_label("Alarm")
        button_alarm.connect("clicked", self.on_alarm_clicked)
        vbox.pack_start(button_alarm, False, False, 2)

        button_ok = Gtk.Button()
        button_ok.set_label("Ok")
        passvalues = [window_task, task_field]
        button_ok.connect("clicked", self.on_task_ok, passvalues)
        vbox.pack_start(button_ok, 0, 0, 2)

        window_task.add(vbox)
        window_task.show_all()

        return window_task

    def see_tasks(self, _):
        """
        Shows a window with all the tasks and alarms
        :param _:
        :return:
        """
        window = Gtk.Window(title="List of tasks")
        box = Gtk.Box(spacing=6)
        box.set_orientation(Gtk.Orientation.VERTICAL)
        window.add(box)

        list_of_gtk_labels = []

        for each_task in self.task_list:
            # creates one Gtk.Label for each item in the list_of_tasks
            list_of_gtk_labels.append(Gtk.Label(each_task.get_task_desc()))

        for label in list_of_gtk_labels:
            box.pack_start(label, 0, 0, 2)

        window.show_all()

    def stop_alarms(self, _):
        """
        Stops all playing alarms
        :param _:
        :return:
        """
        pass

    def on_alarm_clicked(self, _):
        """
        Shows a window to define the alarm
        :param _:
        :return: datetime object
        """
        window_alarm = Gtk.Window(title="Define alarm")
        # TODO show calendar and time fields and start alarm (implement alarm using threading?)
        alarm = 0
        window_alarm.show_all()
        return alarm

    def on_task_ok(self, widget, passvalues):
        self.tasklist_id += 1

        # create task from task data
        new_task = tasks.Task(self.tasklist_id, passvalues[1].get_text())
        self.task_list.append(new_task)

        # TODO update task list window if open

        # close new task window
        passvalues[0].destroy()

    def quit_window(self,window):
        window.destroy()

    def quit(self, widget):
        Gtk.main_quit()

def main():
    app = MyBzoing()

if __name__ == "__main__":
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    main()
<commit_msg>learning gtk<commit_after>#!/usr/bin/env python3

import os
import signal
from gi.repository import Gtk
from gi.repository import AppIndicator3 as appindicator

import tasks

APPINDICATOR_ID = 'bzoing'

class MyBzoing:
    def __init__(self):
        self.task_list = []
        self.tasklist_id = -1
        self.task_window_open = False
        indicator = appindicator.Indicator.new(APPINDICATOR_ID,
                                           os.path.abspath('sinoamarelo.svg'),
                                           appindicator.IndicatorCategory.APPLICATION_STATUS)
        indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
        indicator.set_menu(self.build_menu())
        Gtk.main()

    def build_menu(self):
        """
        Builds all the menus
        :rtype: object
        """
        menu = Gtk.Menu()
        item_new_task = Gtk.MenuItem('New task')
        item_new_task.connect('activate', self.new_task)
        menu.append(item_new_task)

        item_see_tasks = Gtk.MenuItem('See tasks')
        item_see_tasks.connect('activate', self.see_tasks)
        menu.append(item_see_tasks)

        item_separator = Gtk.SeparatorMenuItem()
        menu.append(item_separator)

        item_stop_alarms = Gtk.MenuItem('Stop Alarms')
        item_stop_alarms.connect('activate', self.stop_alarms)
        menu.append(item_stop_alarms)

        item_quit = Gtk.MenuItem('Quit')
        item_quit.connect('activate', self.quit)
        menu.append(item_quit)

        menu.show_all()
        return menu

    def new_task(self, _):
        window_task = Gtk.Window(title='Create Task')
        window_task.connect('destroy', self.quit_window)

        vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
        hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 6)

        task_label = Gtk.Label()
        task_label.set_text('Task: ')
        hbox.pack_start(task_label, False, False, 2)

        task_field = Gtk.Entry()
        hbox.pack_start(task_field, False, False, 2)

        vbox.pack_start(hbox, False, False, 6)

        button_alarm = Gtk.Button()
        button_alarm.set_label("Alarm")
        button_alarm.connect("clicked", self.on_alarm_clicked)
        vbox.pack_start(button_alarm, False, False, 2)

        button_ok = Gtk.Button()
        button_ok.set_label("Ok")
        passvalues = [window_task, task_field]
        button_ok.connect("clicked", self.on_task_ok, passvalues)
        vbox.pack_start(button_ok, 0, 0, 2)

        window_task.add(vbox)
        window_task.show_all()

        return window_task

    def see_tasks(self, _):
        """
        Shows a window with all the tasks and alarms
        :param _:
        :return:
        """
        if self.task_window_open == False:
            self.task_window_open = True
            window = Gtk.Window(title="List of tasks")
            window.connect('destroy', self.quit_listview_window)
            box = Gtk.Box(spacing=6)
            box.set_orientation(Gtk.Orientation.VERTICAL)
            window.add(box)

            list_of_gtk_labels = []

            for each_task in self.task_list:
                # creates one Gtk.Label for each item in the list_of_tasks
                list_of_gtk_labels.append(Gtk.Label(each_task.get_task_desc()))

            for label in list_of_gtk_labels:
                box.pack_start(label, 0, 0, 2)

            window.show_all()

    def stop_alarms(self, _):
        """
        Stops all playing alarms
        :param _:
        :return:
        """
        pass

    def on_alarm_clicked(self, _):
        """
        Shows a window to define the alarm
        :param _:
        :return: datetime object
        """
        window_alarm = Gtk.Window(title="Define alarm")
        # TODO show calendar and time fields and start alarm (implement alarm using threading?)
        alarm = 0
        window_alarm.show_all()
        return alarm

    def on_task_ok(self, widget, passvalues):
        self.tasklist_id += 1

        # create task from task data
        new_task = tasks.Task(self.tasklist_id, passvalues[1].get_text())
        self.task_list.append(new_task)

        # TODO update task list window if open

        # close new task window
        passvalues[0].destroy()

    def quit_listview_window(self, window):
        self.task_window_open = False
        window.destroy()

    def quit_window(self,window):
        window.destroy()

    def quit(self, widget):
        Gtk.main_quit()

def main():
    app = MyBzoing()

if __name__ == "__main__":
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    main()
<|endoftext|>


<commit_before># msg.getFrom().getStripped() has disappeared
# I've replaced it with msg.frm.person
from errbot import BotPlugin, botcmd

import subprocess,os,time
import math
import time
#from RPIO import PWM

MAX=2340
MIN=600
VEL=0.4

import cv2, time
# You can install opencv in the usual way and then make it available to your
# virtual env
# http://www.pyimagesearch.com/2015/07/27/installing-opencv-3-0-for-both-python-2-7-and-python-3-on-your-raspberry-pi-2/
#
# In our case:
# cd ~/.pyenv/versions/2.7.10/lib/python2.7/site-packages
# ln -s /usr/lib/pyshared/python2.7/cv2.so

import smtplib, mimetypes, time, datetime
#from email import Encoders
#from email.MIMEText import MIMEText
#from email.MIMEBase import MIMEBase
#from email.MIMEMultipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.utils import formatdate
from email import encoders

class Camera(BotPlugin):
    """Example 'Hello, world!' plugin for Err"""
    cam=0
    posCam=90
    servoGPIO=4

    def get_configuration_template(self):
        #self.movePos(self.posCam)
        return{'ADDRESS' : u'kk@kk.com', 'FROMADD' : u'kk@kk.com', 'TOADDRS' : u'kk@kk.com', 'SUBJECT' : u'Imagen', u'SMTPSRV' : u'smtp.gmail.com:587', 'LOGINID' : u'changeme', 'LOGINPW' : u'changeme'}

    @botcmd
    def hello(self, msg, args):
        """Say hello to the world"""
        #yield "Hello, %s!"%msg.getFrom().getStripped()
        yield "Hello, %s!"%msg.frm.person
        yield "Hello, %s!"%msg.getTo()
        yield "Hello, world!"

    @botcmd
    def tellpos(self, msg, args):
        """Say my internal ip"""
        yield self.posCam

    @botcmd
    def ip(self, msg, args):
        """Say my internal ip"""
        arg='ip route list'
        p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
        data = p.communicate()
        split_data = data[0].split()
        ipaddr = split_data[split_data.index('src')+1]
        my_ip = 'La ip de la raspberry es %s' % ipaddr
        #yield "Thanks for sending\n**%(body)s**" % msg
        yield my_ip

    @botcmd
    def ls(self, msg, args):
        """List current dir"""
        lista=os.listdir('.')
        for filename in lista:
            yield "> %s "%filename

    @botcmd
    def foto(self, msg, args):
        """Take a picture"""
        if hasattr(msg, 'frm'):
            if hasattr(msg.frm, 'person'):
                quien=msg.frm.person
            else:
                quien = msg.frm
        else:
           quien=""
        yield "I'm taking the picture, wait a second "
        if (args):
            try:
                cam=int(args)
            except:
                cam=0
        else:
            cam=0
        tt = time.gmtime()
        imgFile = "/tmp/%s-%s-%s-%s%s%s_image.png" % (tt[0], tt[1],
                tt[2], tt[3], tt[4], tt[5])
        yield "Camera %s"%cam
        yield "Cheese..."
        self.camera(imgFile,self.cam)
        yield "Now I'm sending it"
        self.mail(imgFile, quien)
        my_msg = "I've sent it to ... %s with file name %s" % (quien, imgFile)
        yield my_msg

#    # This function maps the angle we want to move the servo to, to the needed
#    # PWM value
#    #
#    # https://github.com/MattStultz/PiCam
#    #
#    def angleMap(self, angle):
#        return int((round((1950.0/180.0),0)*angle)/10)*10+550
#
#    def movePos(self, pos):
#        servo = PWM.Servo()
#        print(pos)
#        print(self.posCam)
#        servo.set_servo(self.servoGPIO, self.angleMap(pos))
#        time.sleep(VEL)
#        servo.stop_servo(self.servoGPIO)
#        self.posCam=pos
#
#    def move(self, pos, inc=1):
#        if (pos < self.posCam):
#            inc = -1*inc
#        for i in range(self.posCam, pos, inc):
#            self.movePos(i)

    def camera(self, imgFile, whichCam):
        """Take a picture"""
        cam=cv2.VideoCapture(whichCam)
        cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
        cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 960)
        retval, img = cam.read()
        cv2.imwrite(imgFile, img)
        del(cam)

    def mail(self, imgFile, address=""):
        """Send a file by mail"""

        destaddr = self.config['ADDRESS']
        fromaddr = self.config['FROMADD']
        if (address==""):
            toaddrs=self.config['TOADDRS']
        else:
            toaddrs  = address
        subject  = self.config['SUBJECT']
        smtpsrv  = self.config['SMTPSRV']
        loginId  = self.config['LOGINID']
        loginPw  = self.config['LOGINPW']

        mensaje = MIMEMultipart()

        format, enc = mimetypes.guess_type(imgFile)
        main, sub = format.split('/')
        text = "Picture taken on: %s"%datetime.datetime.now().isoformat()
        # We are including the date in the body of the message.
        part1 = MIMEText(text, 'plain')

        part2 = MIMEImage(open(imgFile,"rb").read(),name=os.path.basename(imgFile))
        part2.add_header('Content-Disposition', 'attachment: filename="{}"'.format(imgFile))
        mensaje.attach(part1)
        mensaje.attach(part2)

        mensaje['Subject'] = subject
        mensaje['From'] = fromaddr
        mensaje['To'] = destaddr
        mensaje['Cc'] = toaddrs

        server = smtplib.SMTP()
        #server.set_debuglevel(1)
        server.connect(smtpsrv)
        server.ehlo()
        server.starttls()
        server.login(loginId, loginPw)
        server.sendmail(fromaddr, [destaddr]+[toaddrs], mensaje.as_string())
        server.quit()

#    @botcmd
#    def rfoto(self, msg, args):
#        #Move the servo, take the picture, send it.
#        #It won't return to the initial position
#
#        if (args):
#            try:
#                mov = int(args)
#            except:
#                mov = 90
#        else:
#            mov = 90
#
#        yield "Going to %d"%mov
#        self.movePos(mov)
#
#        quien=msg.frm.person
#
#        yield "I'm taking the picture, wait a second "
#        self.camera("/tmp/imagen.png",self.cam)
#
#        yield "Now I'm sending the picture"
#        self.mail("/tmp/imagen.png", quien)
#
#        my_msg = "I've sent it to ... %s"%quien
#
#    @botcmd
#    def mfoto(self, msg, args):
#        # Move the servo, take the picture, send it, return to
#        # the initial position. Now with angles instead percentages."""
#
#        if (args):
#            try:
#                mov = int(args)
#            except:
#                mov = 90
#        else:
#            mov = 90
#
#        yield "Going to %d"%mov
#        self.move(mov)
#        yield "In %d"%mov
#
#        quien=msg.frm.person
#
#        yield "I'm taking the picture, wait a second "
#        self.camera("/tmp/imagen.png",self.cam)
#
#        yield "Now I'm sending the picture"
#        self.mail("/tmp/imagen.png", quien)
#
#        my_msg = "I've sent it to ... %s"%quien

<commit_msg>Changes in mail sending, no more movement, and python3<commit_after># msg.getFrom().getStripped() has disappeared
# I've replaced it with msg.frm.person
from errbot import BotPlugin, botcmd

import subprocess,os,time
import math
import time
#from RPIO import PWM

MAX=2340
MIN=600
VEL=0.4

import cv2, time
# You can install opencv in the usual way and then make it available to your
# virtual env
# http://www.pyimagesearch.com/2015/07/27/installing-opencv-3-0-for-both-python-2-7-and-python-3-on-your-raspberry-pi-2/
#
# In our case:
# cd ~/.pyenv/versions/2.7.10/lib/python2.7/site-packages
# ln -s /usr/lib/pyshared/python2.7/cv2.so

import smtplib, mimetypes, time, datetime
#from email import Encoders
#from email.MIMEText import MIMEText
#from email.MIMEBase import MIMEBase
#from email.MIMEMultipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.utils import formatdate
from email import encoders

class Camera(BotPlugin):
    """Example 'Hello, world!' plugin for Err"""
    cam=0
    posCam=90
    servoGPIO=4

    def get_configuration_template(self):
        #self.movePos(self.posCam)
        return{'ADDRESS' : u'kk@kk.com', 'FROMADD' : u'kk@kk.com', 'TOADDRS' : u'kk@kk.com', 'SUBJECT' : u'Imagen', u'SMTPSRV' : u'smtp.gmail.com:587', 'LOGINID' : u'changeme', 'LOGINPW' : u'changeme'}

    @botcmd
    def hello(self, msg, args):
        """Say hello to the world"""
        #yield "Hello, %s!"%msg.getFrom().getStripped()
        yield "Hello, %s!"%msg.frm.person
        yield "Hello, %s!"%msg.getTo()
        yield "Hello, world!"

    @botcmd
    def tellpos(self, msg, args):
        """Say my internal ip"""
        yield self.posCam

    @botcmd
    def ip(self, msg, args):
        """Say my internal ip"""
        arg='ip route list'
        p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
        data = p.communicate()
        split_data = data[0].split()
        ipaddr = split_data[split_data.index(b'src')+1]
        my_ip = 'La ip de la raspberry es %s' % ipaddr
        #yield "Thanks for sending\n**%(body)s**" % msg
        yield my_ip

    @botcmd
    def ls(self, msg, args):
        """List current dir"""
        lista=os.listdir('.')
        for filename in lista:
            yield "> %s "%filename

    @botcmd
    def foto(self, msg, args):
        """Take a picture"""
        if hasattr(msg, 'frm'):
            if hasattr(msg.frm, 'person'):
                quien=msg.frm.person
            else:
                quien = msg.frm
        else:
           quien=""
        yield "I'm taking the picture, wait a second "
        if (args):
            try:
                cam=int(args)
            except:
                cam=0
        else:
            cam=0
        tt = time.gmtime()
        imgFile = "/tmp/%s-%s-%s-%s%s%s_image.png" % (tt[0], tt[1],
                tt[2], tt[3], tt[4], tt[5])
        yield "Camera %s"%cam
        yield "Cheese..."
        self.camera(imgFile,self.cam)
        yield "Now I'm sending it"
        self.mail(imgFile, quien)
        my_msg = "I've sent it to ... %s with file name %s" % (quien, imgFile)
        yield my_msg

#    # This function maps the angle we want to move the servo to, to the needed
#    # PWM value
#    #
#    # https://github.com/MattStultz/PiCam
#    #
#    def angleMap(self, angle):
#        return int((round((1950.0/180.0),0)*angle)/10)*10+550
#
#    def movePos(self, pos):
#        servo = PWM.Servo()
#        print(pos)
#        print(self.posCam)
#        servo.set_servo(self.servoGPIO, self.angleMap(pos))
#        time.sleep(VEL)
#        servo.stop_servo(self.servoGPIO)
#        self.posCam=pos
#
#    def move(self, pos, inc=1):
#        if (pos < self.posCam):
#            inc = -1*inc
#        for i in range(self.posCam, pos, inc):
#            self.movePos(i)

    def camera(self, imgFile, whichCam):
        """Take a picture"""
        cam=cv2.VideoCapture(whichCam)
        cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
        cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 960)
        retval, img = cam.read()
        cv2.imwrite(imgFile, img)
        del(cam)

    def mail(self, imgFile, address=""):
        """Send a file by mail"""

        destaddr = self.config['ADDRESS']
        fromaddr = self.config['FROMADD']
        if (address==""):
            toaddrs=self.config['TOADDRS']
        else:
            toaddrs  = address
        subject  = self.config['SUBJECT']
        smtpsrv  = self.config['SMTPSRV']
        loginId  = self.config['LOGINID']
        loginPw  = self.config['LOGINPW']

        mensaje = MIMEMultipart()

        format, enc = mimetypes.guess_type(imgFile)
        main, sub = format.split('/')
        text = "Picture taken on: %s"%datetime.datetime.now().isoformat()
        # We are including the date in the body of the message.
        part1 = MIMEText(text, 'plain')

        part2 = MIMEImage(open(imgFile,"rb").read(),name=os.path.basename(imgFile))
        part2.add_header('Content-Disposition', 'attachment: filename="{}"'.format(imgFile))
        mensaje.attach(part1)
        mensaje.attach(part2)

        mensaje['Subject'] = subject
        mensaje['From'] = fromaddr
        mensaje['To'] = destaddr
        mensaje['Cc'] = toaddrs

        server = smtplib.SMTP()
        #server.set_debuglevel(1)
        server.connect(smtpsrv)
        server.ehlo()
        server.starttls()
        server.login(loginId, loginPw)
        server.sendmail(fromaddr, [destaddr]+[toaddrs], mensaje.as_string())
        server.quit()

#    @botcmd
#    def rfoto(self, msg, args):
#        #Move the servo, take the picture, send it.
#        #It won't return to the initial position
#
#        if (args):
#            try:
#                mov = int(args)
#            except:
#                mov = 90
#        else:
#            mov = 90
#
#        yield "Going to %d"%mov
#        self.movePos(mov)
#
#        quien=msg.frm.person
#
#        yield "I'm taking the picture, wait a second "
#        self.camera("/tmp/imagen.png",self.cam)
#
#        yield "Now I'm sending the picture"
#        self.mail("/tmp/imagen.png", quien)
#
#        my_msg = "I've sent it to ... %s"%quien
#
#    @botcmd
#    def mfoto(self, msg, args):
#        # Move the servo, take the picture, send it, return to
#        # the initial position. Now with angles instead percentages."""
#
#        if (args):
#            try:
#                mov = int(args)
#            except:
#                mov = 90
#        else:
#            mov = 90
#
#        yield "Going to %d"%mov
#        self.move(mov)
#        yield "In %d"%mov
#
#        quien=msg.frm.person
#
#        yield "I'm taking the picture, wait a second "
#        self.camera("/tmp/imagen.png",self.cam)
#
#        yield "Now I'm sending the picture"
#        self.mail("/tmp/imagen.png", quien)
#
#        my_msg = "I've sent it to ... %s"%quien

<|endoftext|>


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

"""Detects problems in CoreCommerce product data.

Requires a [website] section in config file for login info.

The [lint_product] section of the config file contains product value
checks.  The key is the product value (column) to check and the value
is a regular expression which the product value must match.  You can
specify multiple checks for a product value by putting them on
multiple lines.  The regex can be followed by a Python "if"
expression.  For example:

    [lint_product]
    Teaser: \S.{9,59}  ; 10-59 chars
    Discontinued Item: Y|N  ; Y or N
    Available:
        Y|N
        N if item["Discontinued Item"] == "Y"
    # UPC must be blank
    UPC:
    Cost: [0-9]+\.[0-9]{2}  ; positive float
    Price: [0-9]+\.[0-9]{2}  ; positive float
    # SKU must be 5 digits.
    # SKU must start with 8 if the category is Cat or Dog.
    SKU:
        [1-9][0-9]{4}
        [8][0-9]{4} if item["Category"] in ("Cat", "Dog")

The [lint_category] section is similar and is used for category value
checks.

The [lint_personalization] section is similar and is used for
personalization value checks.
"""

import ConfigParser
import cctools
import optparse
import os
import re
import sys

def parse_checks(config, section):
    """Parse value check definitions in config file."""
    checks = list()
    for name, value in config.items(section):
        for check in value.split("\n"):
            check = check.strip()
            if check != "":
                checks.append((name, check))
    return checks

def product_display_name(product):
    """Construct a display name for a product."""
    display_name = "%s %s" % (product["SKU"], product["Product Name"])
    return display_name.strip()

def personalization_display_name(personalization):
    """Construct a display name for a personalization."""
    display_name = "%s %s %s" % (
        personalization["Product SKU"],
        personalization["Product Name"],
        personalization["Question|Answer"]
    )
    return display_name.strip()

def check_skus(products):
    """Check SKUs for uniqueness."""
    skus = dict()
    for product in products:
        display_name = product_display_name(product)
        sku = product["SKU"]
        if sku != "":
            if sku in skus:
                print "%s '%s': SKU already used by '%s'" % (
                    "Product",
                    display_name,
                    skus[sku]
                )
            else:
                skus[sku] = display_name

def check_item(item_checks, item_type_name, item, item_name):
    """Check category or product for problems."""

    for key, check in item_checks:
        check_parts = check.split(None, 2)
        if len(check_parts) == 1:
            pass
        elif len(check_parts) == 3 and check_parts[1].lower() == "if":
            predicate = check_parts[2]
            if not eval(predicate, {"__builtins__": {}}, {"item": item}):
                continue
        else:
            print "ERROR: Unknown check syntax '%s'" % check
            sys.exit(1)
        pattern = "^" + check_parts[0] + "$"  # entire value must match
        if not re.match(pattern, item[key]):
            print "%s '%s': Invalid '%s' of '%s' (does not match %s)" % (
                item_type_name,
                item_name,
                key,
                item[key],
                pattern
            )

def main():
    """main"""
    defaultConfig = os.path.join(
        os.path.dirname(os.path.abspath(__file__)),
        "cctools.cfg"
    )

    option_parser = optparse.OptionParser(
        usage="usage: %prog [options]\n" +
        "  Detects problems in data exported from CoreCommerce."
    )
    option_parser.add_option(
        "--config",
        action="store",
        metavar="FILE",
        default=defaultConfig,
        help="configuration filename (default=%default)"
    )
    option_parser.add_option(
        "--no-clean",
        action="store_false",
        dest="clean",
        default=True,
        help="do not clean data before checking"
    )
    option_parser.add_option(
        "--cache-ttl",
        action="store",
        type=int,
        metavar="SEC",
        default=3600,
        help="cache TTL in seconds (default=%default)"
    )
    option_parser.add_option(
        "--verbose",
        action="store_true",
        default=False,
        help="display progress messages"
    )

    # Parse command line arguments.
    (options, args) = option_parser.parse_args()
    if len(args) != 0:
        option_parser.error("invalid argument")

    # Read config file.
    config = ConfigParser.RawConfigParser()
    config.optionxform = str  # preserve case of option names
    config.readfp(open(options.config))
    category_checks = parse_checks(config, "lint_category")
    product_checks = parse_checks(config, "lint_product")
    personalization_checks = parse_checks(config, "lint_personalization")

    # Create a connection to CoreCommerce.
    cc_browser = cctools.CCBrowser(
        config.get("website", "host"),
        config.get("website", "site"),
        config.get("website", "username"),
        config.get("website", "password"),
        verbose=options.verbose,
        clean=options.clean,
        cache_ttl=options.cache_ttl
    )

    # Check category list.
    categories = cc_browser.get_categories()
    for category in categories:
        check_item(
            category_checks,
            "Category",
            category,
            category["Category Name"]
        )

    # Check products list.
    products = cc_browser.get_products()
    check_skus(products)
    for product in products:
        for key in ["Teaser"]:
            product[key] = cctools.html_to_plain_text(product[key])
        check_item(
            product_checks,
            "Product",
            product,
            product_display_name(product)
        )

    # Check personalizations list.
    personalizations = cc_browser.get_personalizations()
    for personalization in personalizations:
        check_item(
            personalization_checks,
            "Personalization",
            personalization,
            personalization_display_name(personalization)
        )

    if options.verbose:
        sys.stderr.write("Checks complete\n")
    return 0

if __name__ == "__main__":
    main()
<commit_msg>cclint.py: enable pylint checks to reference entire list<commit_after>#!/usr/bin/env python

"""Detects problems in CoreCommerce product data.

Requires a [website] section in config file for login info.

The [lint_product] section of the config file contains product value
checks.  The key is the product value (column) to check and the value
is a regular expression which the product value must match.  You can
specify multiple checks for a product value by putting them on
multiple lines.  The regex can be followed by a Python "if"
expression.  For example:

    [lint_product]
    Teaser: \S.{9,59}  ; 10-59 chars
    Discontinued Item: Y|N  ; Y or N
    Available:
        Y|N
        N if item["Discontinued Item"] == "Y"
    # UPC must be blank
    UPC:
    Cost: [0-9]+\.[0-9]{2}  ; positive float
    Price: [0-9]+\.[0-9]{2}  ; positive float
    # SKU must be 5 digits.
    # SKU must start with 8 if the category is Cat or Dog.
    SKU:
        [1-9][0-9]{4}
        [8][0-9]{4} if item["Category"] in ("Cat", "Dog")

The [lint_category] section is similar and is used for category value
checks.

The [lint_personalization] section is similar and is used for
personalization value checks.
"""

import ConfigParser
import cctools
import optparse
import os
import re
import sys

def parse_checks(config, section):
    """Parse value check definitions in config file."""
    checks = list()
    for name, value in config.items(section):
        for check in value.split("\n"):
            check = check.strip()
            if check != "":
                checks.append((name, check))
    return checks

def product_display_name(product):
    """Construct a display name for a product."""
    display_name = "%s %s" % (product["SKU"], product["Product Name"])
    return display_name.strip()

def personalization_display_name(personalization):
    """Construct a display name for a personalization."""
    display_name = "%s %s %s" % (
        personalization["Product SKU"],
        personalization["Product Name"],
        personalization["Question|Answer"]
    )
    return display_name.strip()

def check_skus(products):
    """Check SKUs for uniqueness."""
    skus = dict()
    for product in products:
        display_name = product_display_name(product)
        sku = product["SKU"]
        if sku != "":
            if sku in skus:
                print "%s '%s': SKU already used by '%s'" % (
                    "Product",
                    display_name,
                    skus[sku]
                )
            else:
                skus[sku] = display_name

def check_item(item_checks, item_type_name, items, item, item_name):
    """Check category or product for problems."""

    for key, check in item_checks:
        check_parts = check.split(None, 2)
        if len(check_parts) == 1:
            pass
        elif len(check_parts) == 3 and check_parts[1].lower() == "if":
            predicate = check_parts[2]
            if not eval(
                predicate,
                {"__builtins__": { "len": len }},
                {"items": items, "item": item}
            ):
                continue
        else:
            print "ERROR: Unknown check syntax '%s'" % check
            sys.exit(1)
        pattern = "^" + check_parts[0] + "$"  # entire value must match
        if not re.match(pattern, item[key]):
            print "%s '%s': Invalid '%s' of '%s' (does not match %s)" % (
                item_type_name,
                item_name,
                key,
                item[key],
                pattern
            )

def main():
    """main"""
    defaultConfig = os.path.join(
        os.path.dirname(os.path.abspath(__file__)),
        "cctools.cfg"
    )

    option_parser = optparse.OptionParser(
        usage="usage: %prog [options]\n" +
        "  Detects problems in data exported from CoreCommerce."
    )
    option_parser.add_option(
        "--config",
        action="store",
        metavar="FILE",
        default=defaultConfig,
        help="configuration filename (default=%default)"
    )
    option_parser.add_option(
        "--no-clean",
        action="store_false",
        dest="clean",
        default=True,
        help="do not clean data before checking"
    )
    option_parser.add_option(
        "--cache-ttl",
        action="store",
        type=int,
        metavar="SEC",
        default=3600,
        help="cache TTL in seconds (default=%default)"
    )
    option_parser.add_option(
        "--verbose",
        action="store_true",
        default=False,
        help="display progress messages"
    )

    # Parse command line arguments.
    (options, args) = option_parser.parse_args()
    if len(args) != 0:
        option_parser.error("invalid argument")

    # Read config file.
    config = ConfigParser.RawConfigParser()
    config.optionxform = str  # preserve case of option names
    config.readfp(open(options.config))
    category_checks = parse_checks(config, "lint_category")
    product_checks = parse_checks(config, "lint_product")
    personalization_checks = parse_checks(config, "lint_personalization")

    # Create a connection to CoreCommerce.
    cc_browser = cctools.CCBrowser(
        config.get("website", "host"),
        config.get("website", "site"),
        config.get("website", "username"),
        config.get("website", "password"),
        verbose=options.verbose,
        clean=options.clean,
        cache_ttl=options.cache_ttl
    )

    # Check category list.
    categories = cc_browser.get_categories()
    for category in categories:
        check_item(
            category_checks,
            "Category",
            categories,
            category,
            category["Category Name"]
        )

    # Check products list.
    products = cc_browser.get_products()
    check_skus(products)
    for product in products:
        for key in ["Teaser"]:
            product[key] = cctools.html_to_plain_text(product[key])
        check_item(
            product_checks,
            "Product",
            products,
            product,
            product_display_name(product)
        )

    # Check personalizations list.
    personalizations = cc_browser.get_personalizations()
    for personalization in personalizations:
        check_item(
            personalization_checks,
            "Personalization",
            personalizations,
            personalization,
            personalization_display_name(personalization)
        )

    if options.verbose:
        sys.stderr.write("Checks complete\n")
    return 0

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


<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Business Applications
#    Copyright (C) 2004-2012 OpenERP S.A. (<http://openerp.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import fields, osv
import pooler
from tools.translate import _

class mrp_config_settings(osv.osv_memory):
    _name = 'mrp.config.settings'
    _inherit = 'res.config.settings'

    _columns = {
        'module_stock_planning': fields.boolean('manage master production shedule',
            help ="""This allows to create a manual procurement plan apart of the normal MRP scheduling,
                which works automatically based on minimum stock rules.
                This installs the module stock_planning."""),
        'module_mrp_repair': fields.boolean("manage repairs of products ",
            help="""Allows to manage all product repairs.
                    * Add/remove products in the reparation
                    * Impact for stocks
                    * Invoicing (products and/or services)
                    * Warranty concept
                    * Repair quotation report
                    * Notes for the technician and for the final customer.
                This installs the module mrp_repair."""),
        'module_mrp_operations': fields.boolean("allow detailed planning of work order",
            help="""This allows to add state, date_start,date_stop in production order operation lines (in the "Work Centers" tab).
                This installs the module mrp_operations."""),
        'module_mrp_subproduct': fields.boolean("produce several products from one manufacturing order",
            help="""You can configure sub-products in the bill of material.
                Without this module: A + B + C -> D.
                With this module: A + B + C -> D + E.
                This installs the module mrp_subproduct."""),
        'module_mrp_jit': fields.boolean("generate procurement in real time",
            help="""This allows Just In Time computation of procurement orders.
                All procurement orders will be processed immediately, which could in some
                cases entail a small performance impact.
                This installs the module mrp_jit."""),
        'module_stock_no_autopicking': fields.boolean("manage manual picking to fulfill manufacturing orders ",
            help="""This module allows an intermediate picking process to provide raw materials to production orders.
                For example to manage production made by your suppliers (sub-contracting).
                To achieve this, set the assembled product which is sub-contracted to "No Auto-Picking"
                and put the location of the supplier in the routing of the assembly operation.
                This installs the module stock_no_autopicking."""),
        'group_mrp_routings': fields.boolean("manage routings and work orders ",
            implied_group='mrp.group_mrp_routings',
            help="""Routings allow you to create and manage the manufacturing operations that should be followed
                within your work centers in order to produce a product. They are attached to bills of materials
                that will define the required raw materials."""),
        'group_mrp_properties': fields.boolean("allow several bill of materials per products",
            implied_group='product.group_mrp_properties',
            help="""The selection of the right Bill of Material to use will depend on the  properties specified on the sale order and the Bill of Material."""),
        'module_product_manufacturer': fields.boolean("define manufacturers on products ",
            help="""This allows you to define the following for a product:
                    * Manufacturer
                    * Manufacturer Product Name
                    * Manufacturer Product Code
                    * Product Attributes.
                This installs the module product_manufacturer."""),
    }

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<commit_msg>[IMP]In stage object replace state to status<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Business Applications
#    Copyright (C) 2004-2012 OpenERP S.A. (<http://openerp.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import fields, osv
import pooler
from tools.translate import _

class mrp_config_settings(osv.osv_memory):
    _name = 'mrp.config.settings'
    _inherit = 'res.config.settings'

    _columns = {
        'module_stock_planning': fields.boolean('manage master production shedule',
            help ="""This allows to create a manual procurement plan apart of the normal MRP scheduling,
                which works automatically based on minimum stock rules.
                This installs the module stock_planning."""),
        'module_mrp_repair': fields.boolean("manage repairs of products ",
            help="""Allows to manage all product repairs.
                    * Add/remove products in the reparation
                    * Impact for stocks
                    * Invoicing (products and/or services)
                    * Warranty concept
                    * Repair quotation report
                    * Notes for the technician and for the final customer.
                This installs the module mrp_repair."""),
        'module_mrp_operations': fields.boolean("allow detailed planning of work order",
            help="""This allows to add state, date_start,date_stop in production order operation lines (in the "Work Centers" tab).
                This installs the module mrp_operations."""),
        'module_mrp_subproduct': fields.boolean("produce several products from one manufacturing order",
            help="""You can configure sub-products in the bill of material.
                Without this module: A + B + C -> D.
                With this module: A + B + C -> D + E.
                This installs the module mrp_subproduct."""),
        'module_mrp_jit': fields.boolean("generate procurement in real time",
            help="""This allows Just In Time computation of procurement orders.
                All procurement orders will be processed immediately, which could in some
                cases entail a small performance impact.
                This installs the module mrp_jit."""),
        'module_stock_no_autopicking': fields.boolean("manage manual picking to fulfill manufacturing orders ",
            help="""This module allows an intermediate picking process to provide raw materials to production orders.
                For example to manage production made by your suppliers (sub-contracting).
                To achieve this, set the assembled product which is sub-contracted to "No Auto-Picking"
                and put the location of the supplier in the routing of the assembly operation.
                This installs the module stock_no_autopicking."""),
        'group_mrp_routings': fields.boolean("manage routings and work orders ",
            implied_group='mrp.group_mrp_routings',
            help="""Routings allow you to create and manage the manufacturing operations that should be followed
                within your work centers in order to produce a product. They are attached to bills of materials
                that will define the required raw materials."""),
        'group_mrp_properties': fields.boolean("allow several bill of materials per products using properties",
            implied_group='product.group_mrp_properties',
            help="""The selection of the right Bill of Material to use will depend on the  properties specified on the sale order and the Bill of Material."""),
        'module_product_manufacturer': fields.boolean("define manufacturers on products ",
            help="""This allows you to define the following for a product:
                    * Manufacturer
                    * Manufacturer Product Name
                    * Manufacturer Product Code
                    * Product Attributes.
                This installs the module product_manufacturer."""),
    }

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<|endoftext|>


<commit_before>import math
from datetime import datetime
from util import timedelta2seconds
import event.stats as estats

def delegateable_label(entities):
    return sorted(entities, key=lambda e: e.label.lower())

def instance_label(entities):
    return sorted(entities, key=lambda e: e.label.lower())

def delegateable_title(entities):
    return sorted(entities, key=lambda e: e.title.lower())

def delegateable_full_title(entities):
    return sorted(entities, key=lambda e: e.full_title.lower())

def delegateable_latest_comment(entities):
    return sorted(entities, key=lambda e: e.find_latest_comment_time(),
                  reverse=True)

def proposal_support(entities):
    return sorted(entities, key=lambda e: e.rate_poll.tally.score if e.rate_poll else 0,
                  reverse=True)

def user_name(entities):
    return sorted(entities, key=lambda e: e.name.lower())

def entity_newest(entities):
    return sorted(entities, key=lambda e: e.create_time, reverse=True)

def entity_oldest(entities):
    return sorted(entities, key=lambda e: e.create_time, reverse=False)

def entity_stable(entities):
    return entities

def issue_activity(issues):
    return sorted(issues, key=lambda i: estats.issue_activity(i),
                  reverse=True)

def proposal_activity(proposals):
    return sorted(proposals, key=lambda m: estats.proposal_activity(m),
                  reverse=True)

def instance_activity(instances):
    return sorted(instances, key=lambda i: estats.instance_activity(i),
                  reverse=True)

def user_activity(users):
    return sorted(users, key=lambda u: estats.instance_activity(u),
                  reverse=True)

def comment_score(comments):
    return sorted(comments, key=lambda c: c.poll.tally.score,
                  reverse=True)

def comment_order(comments):
    max_age = 84600 / 2 # 0.5 days
    def sorter(comment):
        score = comment.poll.tally.score
        freshness = 0
        if score > -1:
            age = timedelta2seconds(datetime.utcnow() - comment.create_time)
            freshness = max(1, math.log10(max(1, max_age - age)))
        #print "FRESH", freshness, "COMM", repr(comment), "TALLY", comment.poll.tally.score, "CT", comment.create_time
        return (freshness * score, comment.create_time)
    return sorted(comments, key=lambda c: sorter(c), reverse=True)

def dict_value_sorter(dict):
    def _sort(items):
        return sorted(items, key=lambda i: dict.get(i))
    return _sort

def comment_id(comments):
    return sorted(comments, key=lambda c: c.id)

#
# Unadapted Ruby, either find a python lib with p distribution tables or
# hardcode the "power" argument.
#
#def wilson_confidence_interval(pos, n, power):
#    if n == 0:
#        return 0
#
#    z = Statistics2.pnormaldist(1-power/2)
#    phat = 1.0*pos/n
#    (phat + z*z/(2*n) - z * Math.sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)<commit_msg>[svn r1063] bias towards new stuff<commit_after>import math
from datetime import datetime
from util import timedelta2seconds
import event.stats as estats

def delegateable_label(entities):
    return sorted(entities, key=lambda e: e.label.lower())

def instance_label(entities):
    return sorted(entities, key=lambda e: e.label.lower())

def delegateable_title(entities):
    return sorted(entities, key=lambda e: e.title.lower())

def delegateable_full_title(entities):
    return sorted(entities, key=lambda e: e.full_title.lower())

def delegateable_latest_comment(entities):
    return sorted(entities, key=lambda e: e.find_latest_comment_time(),
                  reverse=True)

def score_and_freshness_sorter(max_age):
    def _with_age(poll, time):
        freshness = 1
        if poll.tally.score > -1:
            age = timedelta2seconds(datetime.utcnow() - time)
            freshness = max(1, math.log10(max(1, max_age - age)))
        return (freshness * poll.tally.score, time)
    return _with_age

def proposal_support(entities):
    max_age = 84600 * 2 # 2 days
    p_key = lambda p: score_and_freshness_sorter(max_age)(p.rate_poll, p.create_time)
    return sorted(entities, key=p_key, reverse=True)

def comment_order(comments):
    max_age = 84600 / 2 # 0.5 days
    p_key = lambda c: score_and_freshness_sorter(max_age)(c.poll, c.create_time)
    return sorted(comments, key=p_key, reverse=True)

def user_name(entities):
    return sorted(entities, key=lambda e: e.name.lower())

def entity_newest(entities):
    return sorted(entities, key=lambda e: e.create_time, reverse=True)

def entity_oldest(entities):
    return sorted(entities, key=lambda e: e.create_time, reverse=False)

def entity_stable(entities):
    return entities

def issue_activity(issues):
    return sorted(issues, key=lambda i: estats.issue_activity(i),
                  reverse=True)

def proposal_activity(proposals):
    return sorted(proposals, key=lambda m: estats.proposal_activity(m),
                  reverse=True)

def instance_activity(instances):
    return sorted(instances, key=lambda i: estats.instance_activity(i),
                  reverse=True)

def user_activity(users):
    return sorted(users, key=lambda u: estats.instance_activity(u),
                  reverse=True)

def comment_score(comments):
    return sorted(comments, key=lambda c: c.poll.tally.score,
                  reverse=True)

def dict_value_sorter(dict):
    def _sort(items):
        return sorted(items, key=lambda i: dict.get(i))
    return _sort

def comment_id(comments):
    return sorted(comments, key=lambda c: c.id)

#
# Unadapted Ruby, either find a python lib with p distribution tables or
# hardcode the "power" argument.
#
#def wilson_confidence_interval(pos, n, power):
#    if n == 0:
#        return 0
#
#    z = Statistics2.pnormaldist(1-power/2)
#    phat = 1.0*pos/n
#    (phat + z*z/(2*n) - z * Math.sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)<|endoftext|>


<commit_before>def check_brackets(string):
    pass
<commit_msg>Add initial solution<commit_after>BRACKETS = ["()", "[]", "{}"]

def check_brackets(s):
    brackets = "".join(filter(is_bracket, s))
    length = 0
    while brackets and length != len(brackets):
        length = len(brackets)
        for pair in BRACKETS:
            brackets = brackets.replace(pair, "")
    return len(brackets) == 0

def is_bracket(ch):
    return ch in {"{", "}", "[", "]", "(", ")"}
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from django.db import migrations, models
import django.db.models.deletion
import uuid

class Migration(migrations.Migration):

    dependencies = [
        ("models", "6843_manifest_file_model"),
    ]

    sql = """
        insert into plugins (
            pluginid,
            name,
            icon,
            component,
            componentname,
            config,
            slug,
            sortorder)
        values (
            '6f707d86-d49c-4ece-9883-8cbb2ecda1b5',
            'Manifest Manager',
            'fa fa-image',
            'views/components/plugins/manifest-manager',
            'manifest-manager',
            '{"show": false}',
            'manifest-manager',
            1);
        """
    reverse_sql = """
        delete from plugins where pluginid = '6f707d86-d49c-4ece-9883-8cbb2ecda1b5';
        """

    operations = [
        migrations.RunSQL(
            sql,
            reverse_sql,
        )
    ]
<commit_msg>Changes name of manifest manager to Image Service Manager, re #7108<commit_after># -*- coding: utf-8 -*-
from django.db import migrations, models
import django.db.models.deletion
import uuid

class Migration(migrations.Migration):

    dependencies = [
        ("models", "6843_manifest_file_model"),
    ]

    sql = """
        insert into plugins (
            pluginid,
            name,
            icon,
            component,
            componentname,
            config,
            slug,
            sortorder)
        values (
            '6f707d86-d49c-4ece-9883-8cbb2ecda1b5',
            'Image Service Manager',
            'fa fa-image',
            'views/components/plugins/manifest-manager',
            'manifest-manager',
            '{"show": false}',
            'manifest-manager',
            1);
        """
    reverse_sql = """
        delete from plugins where pluginid = '6f707d86-d49c-4ece-9883-8cbb2ecda1b5';
        """

    operations = [
        migrations.RunSQL(
            sql,
            reverse_sql,
        )
    ]
<|endoftext|>