<commit_before>import commands
import os
import Queue
import settings
import time
import threading
import serial
import sys
import traceback

from thirtybirds_2_0.Network.manager import init as network_init
from thirtybirds_2_0.Updates.manager import init as updates_init

class Network(object):
    def __init__(self, hostname, network_message_handler, network_status_handler):
        self.hostname = hostname
        self.thirtybirds = network_init(
            hostname=hostname,
            role="client",
            discovery_multicastGroup=settings.discovery_multicastGroup,
            discovery_multicastPort=settings.discovery_multicastPort,
            discovery_responsePort=settings.discovery_responsePort,
            pubsub_pubPort=settings.pubsub_pubPort,
            message_callback=network_message_handler,
            status_callback=network_status_handler
        )

########################
## UTILS
########################

class Utils(object):
    def __init__(self, hostname):
        self.hostname = hostname
    def reboot(self):
        os.system("sudo reboot now")

    def remote_update_git(self, oratio, thirtybirds, update, upgrade):
        if oratio:
            subprocess.call(['sudo', 'git', 'pull'], cwd='/home/pi/oratio')
        if thirtybirds:
            subprocess.call(['sudo', 'git', 'pull'], cwd='/home/pi/thirtybirds_2_0')
        return

    def remote_update_scripts(self):
        updates_init("/home/pi/oratio", False, True)
        return

    def get_update_script_version(self):
        (updates, ghStatus, bsStatus) = updates_init("/home/pi/oratio", False, False)
        return updates.read_version_pickle()

    def get_git_timestamp(self):
        return commands.getstatusoutput("cd /home/pi/oratio/; git log -1 --format=%cd")[1]

    def get_temp(self):
        return commands.getstatusoutput("/opt/vc/bin/vcgencmd measure_temp")[1]

    def get_cpu(self):
        bash_output = commands.getstatusoutput("uptime")[1]
        split_output = bash_output.split(" ")
        return split_output[12]

    def get_uptime(self):
        bash_output = commands.getstatusoutput("uptime")[1]
        split_output = bash_output.split(" ")
        return split_output[4]

    def get_disk(self):
        # stub for now
        return "0"

    def get_client_status(self):
        return (self.hostname, self.get_update_script_version(), self.get_git_timestamp(), self.get_temp(), self.get_cpu(), self.get_uptime(), self.get_disk())

# Main handles network send/recv and can see all other classes directly
class Main(threading.Thread):
    def __init__(self, hostname):
        threading.Thread.__init__(self)
        print os. system("stty -F /dev/ttyACM0 -hupcl")

        print 10000
        self.network = Network(hostname, self.network_message_handler, self.network_status_handler)
        print 10001
        self.queue = Queue.Queue()
        self.arduino_connection = open("/dev/ttyACM0",'w')
        print 10002
        self.utils = Utils(hostname)
        self.network.thirtybirds.subscribe_to_topic("mandala_device_status")
        self.UNSET = 0
        self.FAIL = 2000
        self.PASS = 4000
        self.mandala_device_status = None
        self.mandala_tlc_ids = {
            "avl-controller":39,
            "avl-formant-1":15,
            "avl-formant-1-amplifier":4,
            "avl-formant-2":16,
            "avl-formant-2-amplifier":5,
            "avl-formant-3":17,
            "avl-formant-3-amplifier":6,
            "avl-layer-1":40,
            "avl-layer-2":11,
            "avl-layer-3":12,
            "avl-medulla":35,
            "avl-pitch-keys":18,
            "avl-pitch-keys-sensor-1":7,
            "avl-pitch-keys-sensor-2":8,
            "avl-pitch-keys-sensor-3":9,
            "avl-pitch-keys-sensor-4":10,
            "avl-settings":34,
            "avl-settings-adcs":24,
            "avl-transport":13,
            "avl-transport-encoder":0,
            "avl-voice-1":36,
            "avl-voice-1-crystal-frequency-counter":25,
            "avl-voice-1-harmonic-generators":26,
            "avl-voice-1-harmonic-volume":27,
            "avl-voice-2":37,
            "avl-voice-2-crystal-frequency-counter":28,
            "avl-voice-2-harmonic-generators":29,
            "avl-voice-2-harmonic-volume":30,
            "avl-voice-3":38,
            "avl-voice-3-crystal-frequency-counter":31,
            "avl-voice-3-harmonic-generators":32,
            "avl-voice-3-harmonic-volume":33,
            "avl-voice-keys":14,
            "avl-voice-keys-encoder-1":1,
            "avl-voice-keys-encoder-2":2,
            "avl-voice-keys-encoder-3":3
        }

        self.arduino_delay_time = 0.05
        print 10003

    def network_message_handler(self, topic_msg):
        # this method runs in the thread of the caller, not the tread of Main
        topic, msg =  topic_msg # separating just to eval msg.  best to do it early.  it should be done in TB.
        print "network_message_handler", topic, msg
        #if len(msg) > 0:
        #    msg = eval(msg)
        self.add_to_queue(topic, msg)

    def network_status_handler(self, topic_msg):
        # this method runs in the thread of the caller, not the tread of Mains
        print "Main.network_status_handler", topic_msg

    def add_to_queue(self, topic, msg):
        self.queue.put((topic, msg))

    def write_to_arduino(self, id, level):
        time.sleep(self.arduino_delay_time)
        self.arduino_connection.write(id)
        time.sleep(self.arduino_delay_time)
        self.arduino_connection.write(level)

    def run(self):

        print 10003.1
        devicenames = self.mandala_tlc_ids.keys()
        devicenames.sort()
        print 10003.2
        for devicename in devicenames:
            tlc_id_int = self.mandala_tlc_ids[devicename] + 5000
            tlc_id_str = "{}\n".format(tlc_id_int)
            tlc_level_str = "0/n"
            self.write_to_arduino(tlc_id_str,tlc_level_str)
        print 10004
        while True:
            print 10005
            if self.mandala_device_status == None:
                print 10006
                self.network.thirtybirds.send("mandala_device_request", True)
                print 10007

            print 10008
            try:
                print 10009
                try:
                    topic, msg = self.queue.get(True, 5)
                except Queue.Empty:
                    continue
                print topic, msg
                if topic == "mandala_device_status":
                    mandala_device_status = eval(msg)
                    devicenames = mandala_device_status.keys()
                    if self.mandala_device_status == None: # if this is the first data
                        for devicename in devicenames:
                            tlc_id_int = self.mandala_tlc_ids[devicename] + 5000
                            tlc_id_str = "{}\n".format(tlc_id_int)
                            if mandala_device_status[devicename] == "unset":
                                tlc_level_int = 0
                            if mandala_device_status[devicename] == "fail":
                                tlc_level_int = 100
                            if mandala_device_status[devicename] == "pass":
                                tlc_level_int = 4000
                            tlc_level_str = "{}\n".format(tlc_level_int)

                            self.write_to_arduino(tlc_id_str,tlc_level_str)

                        self.mandala_device_status = mandala_device_status
                    else:
                        for devicename in devicenames:
                            if self.mandala_device_status[devicename] != mandala_device_status[devicename]:
                                self.mandala_device_status[devicename] = mandala_device_status[devicename]
                                tlc_id_int = self.mandala_tlc_ids[devicename] + 5000
                                tlc_id_str = "{}\n".format(tlc_id_int)
                                if mandala_device_status[devicename] == "unset":
                                    tlc_level_int = 0
                                if mandala_device_status[devicename] == "fail":
                                    tlc_level_int = 100
                                if mandala_device_status[devicename] == "pass":
                                    tlc_level_int = 4000
                                tlc_level_str = "{}\n".format(tlc_level_int)
                                self.write_to_arduino(tlc_id_str,tlc_level_str)

                time.sleep(0.01)
            except Exception as e:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                print e, repr(traceback.format_exception(exc_type, exc_value,exc_traceback))

def init(hostname):
    main = Main(hostname)
    main.daemon = True
    main.start()
    return main

<commit_msg>fixing mandala event chain<commit_after>import commands
import os
import Queue
import settings
import time
import threading
import serial
import sys
import traceback

from thirtybirds_2_0.Network.manager import init as network_init
from thirtybirds_2_0.Updates.manager import init as updates_init

class Network(object):
    def __init__(self, hostname, network_message_handler, network_status_handler):
        self.hostname = hostname
        self.thirtybirds = network_init(
            hostname=hostname,
            role="client",
            discovery_multicastGroup=settings.discovery_multicastGroup,
            discovery_multicastPort=settings.discovery_multicastPort,
            discovery_responsePort=settings.discovery_responsePort,
            pubsub_pubPort=settings.pubsub_pubPort,
            message_callback=network_message_handler,
            status_callback=network_status_handler
        )

########################
## UTILS
########################

class Utils(object):
    def __init__(self, hostname):
        self.hostname = hostname
    def reboot(self):
        os.system("sudo reboot now")

    def remote_update_git(self, oratio, thirtybirds, update, upgrade):
        if oratio:
            subprocess.call(['sudo', 'git', 'pull'], cwd='/home/pi/oratio')
        if thirtybirds:
            subprocess.call(['sudo', 'git', 'pull'], cwd='/home/pi/thirtybirds_2_0')
        return

    def remote_update_scripts(self):
        updates_init("/home/pi/oratio", False, True)
        return

    def get_update_script_version(self):
        (updates, ghStatus, bsStatus) = updates_init("/home/pi/oratio", False, False)
        return updates.read_version_pickle()

    def get_git_timestamp(self):
        return commands.getstatusoutput("cd /home/pi/oratio/; git log -1 --format=%cd")[1]

    def get_temp(self):
        return commands.getstatusoutput("/opt/vc/bin/vcgencmd measure_temp")[1]

    def get_cpu(self):
        bash_output = commands.getstatusoutput("uptime")[1]
        split_output = bash_output.split(" ")
        return split_output[12]

    def get_uptime(self):
        bash_output = commands.getstatusoutput("uptime")[1]
        split_output = bash_output.split(" ")
        return split_output[4]

    def get_disk(self):
        # stub for now
        return "0"

    def get_client_status(self):
        return (self.hostname, self.get_update_script_version(), self.get_git_timestamp(), self.get_temp(), self.get_cpu(), self.get_uptime(), self.get_disk())

# Main handles network send/recv and can see all other classes directly
class Main(threading.Thread):
    def __init__(self, hostname):
        threading.Thread.__init__(self)
        print os. system("stty -F /dev/ttyACM0 -hupcl")

        print 10000
        self.network = Network(hostname, self.network_message_handler, self.network_status_handler)
        print 10001
        self.queue = Queue.Queue()
        self.arduino_connection = open("/dev/ttyACM0",'w')
        print 10002
        self.utils = Utils(hostname)
        self.network.thirtybirds.subscribe_to_topic("mandala_device_status")
        self.UNSET = 0
        self.FAIL = 2000
        self.PASS = 4000
        self.mandala_device_status = None
        self.mandala_tlc_ids = {
            "avl-controller":39,
            "avl-formant-1":15,
            "avl-formant-1-amplifier":4,
            "avl-formant-2":16,
            "avl-formant-2-amplifier":5,
            "avl-formant-3":17,
            "avl-formant-3-amplifier":6,
            "avl-layer-1":40,
            "avl-layer-2":11,
            "avl-layer-3":12,
            "avl-medulla":35,
            "avl-pitch-keys":18,
            "avl-pitch-keys-sensor-1":7,
            "avl-pitch-keys-sensor-2":8,
            "avl-pitch-keys-sensor-3":9,
            "avl-pitch-keys-sensor-4":10,
            "avl-settings":34,
            "avl-settings-adcs":24,
            "avl-transport":13,
            "avl-transport-encoder":0,
            "avl-voice-1":36,
            "avl-voice-1-crystal-frequency-counter":25,
            "avl-voice-1-harmonic-generators":26,
            "avl-voice-1-harmonic-volume":27,
            "avl-voice-2":37,
            "avl-voice-2-crystal-frequency-counter":28,
            "avl-voice-2-harmonic-generators":29,
            "avl-voice-2-harmonic-volume":30,
            "avl-voice-3":38,
            "avl-voice-3-crystal-frequency-counter":31,
            "avl-voice-3-harmonic-generators":32,
            "avl-voice-3-harmonic-volume":33,
            "avl-voice-keys":14,
            "avl-voice-keys-encoder-1":1,
            "avl-voice-keys-encoder-2":2,
            "avl-voice-keys-encoder-3":3
        }

        self.arduino_delay_time = 0.05
        print 10003

    def network_message_handler(self, topic_msg):
        # this method runs in the thread of the caller, not the tread of Main
        topic, msg =  topic_msg # separating just to eval msg.  best to do it early.  it should be done in TB.
        print "network_message_handler", topic, msg
        #if len(msg) > 0:
        #    msg = eval(msg)
        self.add_to_queue(topic, msg)

    def network_status_handler(self, topic_msg):
        # this method runs in the thread of the caller, not the tread of Mains
        print "Main.network_status_handler", topic_msg

    def add_to_queue(self, topic, msg):
        self.queue.put((topic, msg))

    def write_to_arduino(self, id, level):
        time.sleep(self.arduino_delay_time)
        self.arduino_connection.write(id)
        time.sleep(self.arduino_delay_time)
        self.arduino_connection.write(level)

    def run(self):

        print 10003.1
        devicenames = self.mandala_tlc_ids.keys()
        devicenames.sort()
        print 10003.2
        for devicename in devicenames:
            tlc_id_int = self.mandala_tlc_ids[devicename] + 5000
            tlc_id_str = "{}\n".format(tlc_id_int)
            tlc_level_str = "0/n"
            self.write_to_arduino(tlc_id_str,tlc_level_str)
        print 10004
        while True:
            print 10005
            #if self.mandala_device_status == None:
            #    print 10006
            self.network.thirtybirds.send("mandala_device_request", True)
            #    print 10007

            print 10008
            try:
                print 10009
                try:
                    topic, msg = self.queue.get(True, 5)
                except Queue.Empty:
                    continue
                print topic, msg
                if topic == "mandala_device_status":
                    mandala_device_status = eval(msg)
                    devicenames = mandala_device_status.keys()
                    if self.mandala_device_status == None: # if this is the first data
                        for devicename in devicenames:
                            tlc_id_int = self.mandala_tlc_ids[devicename] + 5000
                            tlc_id_str = "{}\n".format(tlc_id_int)
                            if mandala_device_status[devicename] == "unset":
                                tlc_level_int = 0
                            if mandala_device_status[devicename] == "fail":
                                tlc_level_int = 100
                            if mandala_device_status[devicename] == "pass":
                                tlc_level_int = 4000
                            tlc_level_str = "{}\n".format(tlc_level_int)

                            self.write_to_arduino(tlc_id_str,tlc_level_str)

                        self.mandala_device_status = mandala_device_status
                    else:
                        for devicename in devicenames:
                            if self.mandala_device_status[devicename] != mandala_device_status[devicename]:
                                self.mandala_device_status[devicename] = mandala_device_status[devicename]
                                tlc_id_int = self.mandala_tlc_ids[devicename] + 5000
                                tlc_id_str = "{}\n".format(tlc_id_int)
                                if mandala_device_status[devicename] == "unset":
                                    tlc_level_int = 0
                                if mandala_device_status[devicename] == "fail":
                                    tlc_level_int = 100
                                if mandala_device_status[devicename] == "pass":
                                    tlc_level_int = 4000
                                tlc_level_str = "{}\n".format(tlc_level_int)
                                self.write_to_arduino(tlc_id_str,tlc_level_str)

                time.sleep(0.01)
            except Exception as e:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                print e, repr(traceback.format_exception(exc_type, exc_value,exc_traceback))

def init(hostname):
    main = Main(hostname)
    main.daemon = True
    main.start()
    return main

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2016 Red Hat, Inc
#
# 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.
from dci.api import v1 as api_v1
from dci.common import exceptions
from dci.common import utils
from dci.db import models
from dci import dci_config

import flask
import logging
import sys
import time
import traceback
import zmq

import sqlalchemy
from sqlalchemy import exc as sa_exc

logger = logging.getLogger(__name__)

zmq_sender = None

class DciControlServer(flask.Flask):
    def __init__(self):
        super(DciControlServer, self).__init__(__name__)
        self.config.update(dci_config.CONFIG)
        self.url_map.strict_slashes = False
        self.engine = dci_config.get_engine()
        conf = dci_config.CONFIG
        self.sender = self._get_zmq_sender(conf['ZMQ_CONN'])
        engine = dci_config.get_engine()
        self.team_admin_id = self._get_team_id(engine, 'admin')
        self.team_redhat_id = self._get_team_id(engine, 'Red Hat')
        self.team_epm_id = self._get_team_id(engine, 'EPM')

    def _get_zmq_sender(self, zmq_conn):
        global zmq_sender
        if not zmq_sender:
            zmq_sender = zmq.Context().socket(zmq.PUSH)
            zmq_sender.connect(zmq_conn)
        return zmq_sender

    def make_default_options_response(self):
        resp = super(DciControlServer, self).make_default_options_response()
        methods = ['GET', 'POST', 'PUT', 'DELETE']
        headers = resp.headers

        headers.add_header('Access-Control-Allow-Methods', ', '.join(methods))
        headers.add_header('Access-Control-Allow-Headers',
                           self.config['X_HEADERS'])
        return resp

    def process_response(self, resp):
        headers = resp.headers
        headers.add_header('Access-Control-Expose-Headers',
                           self.config['X_HEADERS'])
        headers.add_header('Access-Control-Allow-Origin',
                           self.config['X_DOMAINS'])

        return super(DciControlServer, self).process_response(resp)

    def _get_team_id(self, engine, name):
        db_conn = engine.connect()
        query = sqlalchemy.sql.select([models.TEAMS]).where(
            models.TEAMS.c.name == name)
        row = db_conn.execute(query).fetchone()
        db_conn.close()

        if row is None:
            print("%s team not found. Please init the database"
                  " with the '%s' team and 'admin' user." % (name, name))
            sys.exit(1)
        return row.id

def configure_root_logger():
    conf = dci_config.CONFIG
    logging.basicConfig(level=conf['LOG_LEVEL'])

    console_handler = logging.StreamHandler()
    formatter = logging.Formatter(conf['LOG_FORMAT'])
    console_handler.setFormatter(formatter)
    console_handler.setLevel(conf['LOG_LEVEL'])

    root_logger = logging.getLogger()
    # remove all handlers before adding the console handler
    del root_logger.handlers[:]
    root_logger.addHandler(console_handler)

def werkzeug_logger_to_error():
    werkzeug_logger = logging.getLogger('werkzeug')
    werkzeug_logger.setLevel("ERROR")

def create_app(param=None):
    dci_app = DciControlServer()
    dci_app.url_map.converters['uuid'] = utils.UUIDConverter

    werkzeug_logger_to_error()
    configure_root_logger()

    logger.info('dci control server startup')

    def handle_api_exception(api_exception):
        response = flask.jsonify(api_exception.to_dict())
        response.status_code = api_exception.status_code
        logger.info(api_exception.message)
        logger.info(traceback.format_exc())
        return response

    def handle_dbapi_exception(dbapi_exception):
        db_exception = exceptions.DCIException(str(dbapi_exception)).to_dict()
        response = flask.jsonify(db_exception)
        response.status_code = 400
        logger.info(db_exception.message)
        logger.info(traceback.format_exc())
        return response

    @dci_app.before_request
    def before_request():
        flask.g.team_admin_id = dci_app.team_admin_id
        flask.g.team_redhat_id = dci_app.team_redhat_id
        flask.g.team_epm_id = dci_app.team_epm_id

        for i in range(5):
            try:
                flask.g.db_conn = dci_app.engine.connect()
                break
            except:
                logging.warning('failed to connect to the database, '
                                'will retry in 1 second...')
                time.sleep(1)
                pass

        flask.g.sender = dci_app.sender

    @dci_app.teardown_request
    def teardown_request(_):
        try:
            flask.g.db_conn.close()
        except:
            logging.warning('disconnected from the database..')
            pass

    # Registering REST error handler
    dci_app.register_error_handler(exceptions.DCIException,
                                   handle_api_exception)
    dci_app.register_error_handler(sa_exc.DBAPIError,
                                   handle_dbapi_exception)

    # Registering REST API v1
    dci_app.register_blueprint(api_v1.api, url_prefix='/api/v1')

    # Registering custom encoder
    dci_app.json_encoder = utils.JSONEncoder

    return dci_app
<commit_msg>Use logger exception to log an exception<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2016 Red Hat, Inc
#
# 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.
from dci.api import v1 as api_v1
from dci.common import exceptions
from dci.common import utils
from dci.db import models
from dci import dci_config

import flask
import logging
import sys
import time
import zmq

import sqlalchemy
from sqlalchemy import exc as sa_exc

logger = logging.getLogger(__name__)

zmq_sender = None

class DciControlServer(flask.Flask):
    def __init__(self):
        super(DciControlServer, self).__init__(__name__)
        self.config.update(dci_config.CONFIG)
        self.url_map.strict_slashes = False
        self.engine = dci_config.get_engine()
        conf = dci_config.CONFIG
        self.sender = self._get_zmq_sender(conf['ZMQ_CONN'])
        engine = dci_config.get_engine()
        self.team_admin_id = self._get_team_id(engine, 'admin')
        self.team_redhat_id = self._get_team_id(engine, 'Red Hat')
        self.team_epm_id = self._get_team_id(engine, 'EPM')

    def _get_zmq_sender(self, zmq_conn):
        global zmq_sender
        if not zmq_sender:
            zmq_sender = zmq.Context().socket(zmq.PUSH)
            zmq_sender.connect(zmq_conn)
        return zmq_sender

    def make_default_options_response(self):
        resp = super(DciControlServer, self).make_default_options_response()
        methods = ['GET', 'POST', 'PUT', 'DELETE']
        headers = resp.headers

        headers.add_header('Access-Control-Allow-Methods', ', '.join(methods))
        headers.add_header('Access-Control-Allow-Headers',
                           self.config['X_HEADERS'])
        return resp

    def process_response(self, resp):
        headers = resp.headers
        headers.add_header('Access-Control-Expose-Headers',
                           self.config['X_HEADERS'])
        headers.add_header('Access-Control-Allow-Origin',
                           self.config['X_DOMAINS'])

        return super(DciControlServer, self).process_response(resp)

    def _get_team_id(self, engine, name):
        db_conn = engine.connect()
        query = sqlalchemy.sql.select([models.TEAMS]).where(
            models.TEAMS.c.name == name)
        row = db_conn.execute(query).fetchone()
        db_conn.close()

        if row is None:
            print("%s team not found. Please init the database"
                  " with the '%s' team and 'admin' user." % (name, name))
            sys.exit(1)
        return row.id

def configure_root_logger():
    conf = dci_config.CONFIG
    logging.basicConfig(level=conf['LOG_LEVEL'])

    console_handler = logging.StreamHandler()
    formatter = logging.Formatter(conf['LOG_FORMAT'])
    console_handler.setFormatter(formatter)
    console_handler.setLevel(conf['LOG_LEVEL'])

    root_logger = logging.getLogger()
    # remove all handlers before adding the console handler
    del root_logger.handlers[:]
    root_logger.addHandler(console_handler)

def werkzeug_logger_to_error():
    werkzeug_logger = logging.getLogger('werkzeug')
    werkzeug_logger.setLevel("ERROR")

def create_app(param=None):
    dci_app = DciControlServer()
    dci_app.url_map.converters['uuid'] = utils.UUIDConverter

    werkzeug_logger_to_error()
    configure_root_logger()

    logger.info('dci control server startup')

    def handle_api_exception(api_exception):
        response = flask.jsonify(api_exception.to_dict())
        response.status_code = api_exception.status_code
        logger.exception(api_exception)
        return response

    def handle_dbapi_exception(dbapi_exception):
        db_exception = exceptions.DCIException(str(dbapi_exception)).to_dict()
        response = flask.jsonify(db_exception)
        response.status_code = 400
        logger.exception(dbapi_exception)
        return response

    @dci_app.before_request
    def before_request():
        flask.g.team_admin_id = dci_app.team_admin_id
        flask.g.team_redhat_id = dci_app.team_redhat_id
        flask.g.team_epm_id = dci_app.team_epm_id

        for i in range(5):
            try:
                flask.g.db_conn = dci_app.engine.connect()
                break
            except:
                logging.warning('failed to connect to the database, '
                                'will retry in 1 second...')
                time.sleep(1)
                pass

        flask.g.sender = dci_app.sender

    @dci_app.teardown_request
    def teardown_request(_):
        try:
            flask.g.db_conn.close()
        except:
            logging.warning('disconnected from the database..')
            pass

    # Registering REST error handler
    dci_app.register_error_handler(exceptions.DCIException,
                                   handle_api_exception)
    dci_app.register_error_handler(sa_exc.DBAPIError,
                                   handle_dbapi_exception)

    # Registering REST API v1
    dci_app.register_blueprint(api_v1.api, url_prefix='/api/v1')

    # Registering custom encoder
    dci_app.json_encoder = utils.JSONEncoder

    return dci_app
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

# Calculadora de energia
# ======================
# Esta calculadora é uma reimplementação com objetivo de
# aprimorar um script bash

# (c) Fábio Rodrigues Ribeiro - http://farribeiro.blogspot.com

# Copying and distribution of this file, with or without modification, are permitted
# in any medium without royalty provided the copyright notice and this notice are
# preserved.  This file is offered as-is, without any warranty.

import os

class gastoenergia:
	def __init__(self):
		ptnc = raw_input("Digite a Potencia(W): ")
		KW = float(ptnc) / 1000;

		preco = raw_input ("Digite o preço(KW): ")
		horas4dia = raw_input("Quantas horas por dias usa o equipamento: ")
		dias = raw_input("Quantos dias usa o equipamento: ")

		preco_final = ((KW * float(preco) * float(horas4dia)) * float(dias))

		print "\nO gasto é de R$ %.2f" % preco_final

g = gastoenergia();
<commit_msg>Trocado nome da classe<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

# Calculadora de energia
# ======================
# Esta calculadora é uma reimplementação com objetivo de
# aprimorar um script bash

# (c) Fábio Rodrigues Ribeiro - http://farribeiro.blogspot.com

# Copying and distribution of this file, with or without modification, are permitted
# in any medium without royalty provided the copyright notice and this notice are
# preserved.  This file is offered as-is, without any warranty.

import os

class energia:
	def __init__(self):
		ptnc = raw_input("Digite a Potencia(W): ")
		KW = float(ptnc) / 1000;

		preco = raw_input ("Digite o preço(KW): ")
		horas4dia = raw_input("Quantas horas por dias usa o equipamento: ")
		dias = raw_input("Quantos dias usa o equipamento: ")

		preco_final = ((KW * float(preco) * float(horas4dia)) * float(dias))

		print "\nO gasto é de R$ %.2f" % preco_final

g = gastoenergia();
<|endoftext|>


<commit_before>import csv
import unittest
from contextlib import contextmanager
from datetime import date
from StringIO import StringIO
from textwrap import dedent

from playhouse.csv_loader import *

class TestRowConverter(RowConverter):
    @contextmanager
    def get_reader(self, csv_data, **reader_kwargs):
        reader = csv.reader(StringIO(csv_data), **reader_kwargs)
        yield reader

class TestLoader(Loader):
    @contextmanager
    def get_reader(self, csv_data, **reader_kwargs):
        reader = csv.reader(StringIO(csv_data), **reader_kwargs)
        yield reader

    def get_converter(self):
        return self.converter or TestRowConverter(
            self.database,
            has_header=self.has_header,
            sample_size=self.sample_size)

db = SqliteDatabase(':memory:')

class TestCSVConversion(unittest.TestCase):
    header = 'id,name,dob,salary,is_admin'
    simple = '10,"F1 L1",1983-01-01,10000,t'
    float_sal = '20,"F2 L2",1983-01-02,20000.5,f'
    only_name = ',"F3 L3",,,'
    mismatch = 'foo,F4 L4,dob,sal,x'

    def setUp(self):
        db.execute_sql('drop table if exists csv_test;')

    def build_csv(self, *lines):
        return '\r\n'.join(lines)

    def load(self, *lines, **loader_kwargs):
        csv = self.build_csv(*lines)
        loader_kwargs['file_or_name'] = csv
        loader_kwargs.setdefault('db_table', 'csv_test')
        loader_kwargs.setdefault('db_or_model', db)
        return TestLoader(**loader_kwargs).load()

    def assertData(self, ModelClass, expected):
        name_field = ModelClass._meta.get_fields()[2]
        query = ModelClass.select().order_by(name_field).tuples()
        self.assertEqual([row[1:] for row in query], expected)

    def test_defaults(self):
        ModelClass = self.load(
            self.header,
            self.simple,
            self.float_sal,
            self.only_name)
        self.assertData(ModelClass, [
            (10, 'F1 L1', date(1983, 1, 1), 10000., 't'),
            (20, 'F2 L2', date(1983, 1, 2), 20000.5, 'f'),
            (0, 'F3 L3', None, 0., ''),
        ])

    def test_no_header(self):
        ModelClass = self.load(
            self.simple,
            self.float_sal,
            field_names=['f1', 'f2', 'f3', 'f4', 'f5'],
            has_header=False)
        self.assertEqual(ModelClass._meta.get_field_names(), [
            '_auto_pk', 'f1', 'f2', 'f3', 'f4', 'f5'])
        self.assertData(ModelClass, [
            (10, 'F1 L1', date(1983, 1, 1), 10000., 't'),
            (20, 'F2 L2', date(1983, 1, 2), 20000.5, 'f')])

    def test_no_header_no_fieldnames(self):
        ModelClass = self.load(
            self.simple,
            self.float_sal,
            has_header=False)
        self.assertEqual(ModelClass._meta.get_field_names(), [
            '_auto_pk', 'field_0', 'field_1', 'field_2', 'field_3', 'field_4'])

    def test_mismatch_types(self):
        ModelClass = self.load(
            self.header,
            self.simple,
            self.mismatch)
        self.assertData(ModelClass, [
            ('10', 'F1 L1', '1983-01-01', '10000', 't'),
            ('foo', 'F4 L4', 'dob', 'sal', 'x')])

    def test_fields(self):
        fields = [
            IntegerField(),
            CharField(),
            DateField(),
            FloatField(),
            CharField()]
        ModelClass = self.load(
            self.header,
            self.simple,
            self.float_sal,
            fields=fields)
        self.assertEqual(
            map(type, fields),
            map(type, ModelClass._meta.get_fields()[1:]))
        self.assertData(ModelClass, [
            (10, 'F1 L1', date(1983, 1, 1), 10000., 't'),
            (20, 'F2 L2', date(1983, 1, 2), 20000.5, 'f')])
<commit_msg>py3 compat for stringio<commit_after>import csv
import unittest
from contextlib import contextmanager
from datetime import date
try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO
from textwrap import dedent

from playhouse.csv_loader import *

class TestRowConverter(RowConverter):
    @contextmanager
    def get_reader(self, csv_data, **reader_kwargs):
        reader = csv.reader(StringIO(csv_data), **reader_kwargs)
        yield reader

class TestLoader(Loader):
    @contextmanager
    def get_reader(self, csv_data, **reader_kwargs):
        reader = csv.reader(StringIO(csv_data), **reader_kwargs)
        yield reader

    def get_converter(self):
        return self.converter or TestRowConverter(
            self.database,
            has_header=self.has_header,
            sample_size=self.sample_size)

db = SqliteDatabase(':memory:')

class TestCSVConversion(unittest.TestCase):
    header = 'id,name,dob,salary,is_admin'
    simple = '10,"F1 L1",1983-01-01,10000,t'
    float_sal = '20,"F2 L2",1983-01-02,20000.5,f'
    only_name = ',"F3 L3",,,'
    mismatch = 'foo,F4 L4,dob,sal,x'

    def setUp(self):
        db.execute_sql('drop table if exists csv_test;')

    def build_csv(self, *lines):
        return '\r\n'.join(lines)

    def load(self, *lines, **loader_kwargs):
        csv = self.build_csv(*lines)
        loader_kwargs['file_or_name'] = csv
        loader_kwargs.setdefault('db_table', 'csv_test')
        loader_kwargs.setdefault('db_or_model', db)
        return TestLoader(**loader_kwargs).load()

    def assertData(self, ModelClass, expected):
        name_field = ModelClass._meta.get_fields()[2]
        query = ModelClass.select().order_by(name_field).tuples()
        self.assertEqual([row[1:] for row in query], expected)

    def test_defaults(self):
        ModelClass = self.load(
            self.header,
            self.simple,
            self.float_sal,
            self.only_name)
        self.assertData(ModelClass, [
            (10, 'F1 L1', date(1983, 1, 1), 10000., 't'),
            (20, 'F2 L2', date(1983, 1, 2), 20000.5, 'f'),
            (0, 'F3 L3', None, 0., ''),
        ])

    def test_no_header(self):
        ModelClass = self.load(
            self.simple,
            self.float_sal,
            field_names=['f1', 'f2', 'f3', 'f4', 'f5'],
            has_header=False)
        self.assertEqual(ModelClass._meta.get_field_names(), [
            '_auto_pk', 'f1', 'f2', 'f3', 'f4', 'f5'])
        self.assertData(ModelClass, [
            (10, 'F1 L1', date(1983, 1, 1), 10000., 't'),
            (20, 'F2 L2', date(1983, 1, 2), 20000.5, 'f')])

    def test_no_header_no_fieldnames(self):
        ModelClass = self.load(
            self.simple,
            self.float_sal,
            has_header=False)
        self.assertEqual(ModelClass._meta.get_field_names(), [
            '_auto_pk', 'field_0', 'field_1', 'field_2', 'field_3', 'field_4'])

    def test_mismatch_types(self):
        ModelClass = self.load(
            self.header,
            self.simple,
            self.mismatch)
        self.assertData(ModelClass, [
            ('10', 'F1 L1', '1983-01-01', '10000', 't'),
            ('foo', 'F4 L4', 'dob', 'sal', 'x')])

    def test_fields(self):
        fields = [
            IntegerField(),
            CharField(),
            DateField(),
            FloatField(),
            CharField()]
        ModelClass = self.load(
            self.header,
            self.simple,
            self.float_sal,
            fields=fields)
        self.assertEqual(
            map(type, fields),
            map(type, ModelClass._meta.get_fields()[1:]))
        self.assertData(ModelClass, [
            (10, 'F1 L1', date(1983, 1, 1), 10000., 't'),
            (20, 'F2 L2', date(1983, 1, 2), 20000.5, 'f')])
<|endoftext|>


<commit_before>#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import threading, datetime, time

PORT_NUMBER = 8080

# holds stuff to go between threads or config
class common:
  cycleLen = 5 # length of a cycle (in minutes)
  cycleTime = (9, 38) # when the cycle should happen

  doCycle = False
  cycleInProgress = False

# for http
class myHandler(BaseHTTPRequestHandler):

  # handler for GET requests
  def do_GET(self):

    self.timeout = 2000

    # manually start the cycle
    if self.path == "/manual":

      # do a cycle
      if common.cycleInProgress == False:
        common.doCycle = True

        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.send_header("Access-Control-Allow-Origin", "*")
        self.end_headers()
        self.wfile.write('{"OK": null}')
        return

      else:

        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.send_header("Access-Control-Allow-Origin", "*")
        self.end_headers()
        self.wfile.write('{"ERR": "cycle in progress already"}')
        return

    else:
      self.send_response(404)
      self.send_header('Content-type','text/html')
      self.end_headers()
      self.wfile.write('Nothing here')
      return

def hydroponicBackgroundThread():
  while 1:
    if common.doCycle:
      # do a cycle

      common.cycleInProgress = True
      print "START WATER-CYCLE", time.strftime('%c')

      # pause (convert from minutes to seconds)
      time.sleep( common.cycleLen * 60 )

      print "END WATER-CYCLE", time.strftime('%c')
      print
      common.cycleInProgress = False
      common.doCycle = False

def markThread():

  def autoMarkCycleToStart():
    if common.cycleInProgress == False:
      print "MARKING WATER-CYCLE"
      common.doCycle = True

  while 1:
    m = datetime.datetime.now()

    if m.hour == common.cycleTime[0] and m.minute == common.cycleTime[1]:
      autoMarkCycleToStart()

    time.sleep(1)

try:
  server = HTTPServer(('', PORT_NUMBER), myHandler)
  print 'Started hydroponic\'s server on port' , PORT_NUMBER

  bgThread = threading.Thread(target=hydroponicBackgroundThread)
  bgThread.daemon = True
  bgThread.start()
  print 'Started background thread'

  cryThread = threading.Thread(target=markThread)
  cryThread.daemon = True
  cryThread.start()
  print 'Started crython thread\n'

  server.serve_forever()

except KeyboardInterrupt:
  print '^C received, shutting down the server'
  server.socket.close()<commit_msg>removed old code<commit_after><|endoftext|>


<commit_before># Copyright 2015 Google Inc. All Rights Reserved.
#
# 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.

from io import open
import collections
import os
import logging

from glyphsLib.classes import GSFont, __all__ as __all_classes__
from glyphsLib.classes import *  # noqa
from glyphsLib.builder import to_ufos, to_designspace, to_glyphs  # noqa
from glyphsLib.parser import load, loads  # noqa
from glyphsLib.writer import dump, dumps  # noqa
from glyphsLib.util import clean_ufo, ufo_create_background_layer_for_all_glyphs

try:
    from ._version import version as __version__
except ImportError:
    __version__ = "0.0.0+unknown"

__all__ = [
    "build_masters",
    "load_to_ufos",
    "to_ufos",
    "to_designspace",
    "to_glyphs",
    "load",
    "loads",
    "dump",
    "dumps",
] + __all_classes__

logger = logging.getLogger(__name__)

Masters = collections.namedtuple("Masters", ["ufos", "designspace_path"])

def load_to_ufos(
    file_or_path,
    include_instances=False,
    family_name=None,
    propagate_anchors=None,
    ufo_module=None,
    minimal=False,
):
    """Load an unpacked .glyphs object to UFO objects."""

    if hasattr(file_or_path, "read"):
        font = load(file_or_path)
    else:
        with open(file_or_path, "r", encoding="utf-8") as ifile:
            font = load(ifile)
    logger.info("Loading to UFOs")
    return to_ufos(
        font,
        include_instances=include_instances,
        family_name=family_name,
        propagate_anchors=propagate_anchors,
        ufo_module=ufo_module,
        minimal=minimal,
    )

def build_masters(
    filename,
    master_dir,
    designspace_instance_dir=None,
    designspace_path=None,
    family_name=None,
    propagate_anchors=None,
    minimize_glyphs_diffs=False,
    normalize_ufos=False,
    create_background_layers=False,
    generate_GDEF=True,
    store_editor_state=True,
    write_skipexportglyphs=False,
    ufo_module=None,
    minimal=True,
):
    """Write and return UFOs from the masters and the designspace defined in a
    .glyphs file.

    Args:
        master_dir: Directory where masters are written.
        designspace_instance_dir: If provided, a designspace document will be
            written alongside the master UFOs though no instances will be built.
        family_name: If provided, the master UFOs will be given this name and
            only instances with this name will be included in the designspace.

    Returns:
        A named tuple of master UFOs (`ufos`) and the path to the designspace
        file (`designspace_path`).
    """

    font = GSFont(filename)

    if not os.path.isdir(master_dir):
        os.mkdir(master_dir)

    if designspace_instance_dir is None:
        instance_dir = None
    else:
        instance_dir = os.path.relpath(designspace_instance_dir, master_dir)

    designspace = to_designspace(
        font,
        family_name=family_name,
        propagate_anchors=propagate_anchors,
        instance_dir=instance_dir,
        minimize_glyphs_diffs=minimize_glyphs_diffs,
        generate_GDEF=generate_GDEF,
        store_editor_state=store_editor_state,
        write_skipexportglyphs=write_skipexportglyphs,
        ufo_module=ufo_module,
        minimal=minimal,
    )

    # Only write full masters to disk. This assumes that layer sources are always part
    # of another full master source, which must always be the case in a .glyphs file.
    ufos = {}
    for source in designspace.sources:
        if source.filename in ufos:
            assert source.font is ufos[source.filename]
            continue

        if create_background_layers and not minimal:
            ufo_create_background_layer_for_all_glyphs(source.font)

        ufo_path = os.path.join(master_dir, source.filename)
        clean_ufo(ufo_path)
        source.font.save(ufo_path)

        if normalize_ufos:
            import ufonormalizer

            ufonormalizer.normalizeUFO(ufo_path, writeModTimes=False)

        ufos[source.filename] = source.font

    if not designspace_path:
        designspace_path = os.path.join(master_dir, designspace.filename)
    designspace.write(designspace_path)

    return Masters(ufos, designspace_path)
<commit_msg>Missed one<commit_after># Copyright 2015 Google Inc. All Rights Reserved.
#
# 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.

from io import open
import collections
import os
import logging

from glyphsLib.classes import GSFont, __all__ as __all_classes__
from glyphsLib.classes import *  # noqa
from glyphsLib.builder import to_ufos, to_designspace, to_glyphs  # noqa
from glyphsLib.parser import load, loads  # noqa
from glyphsLib.writer import dump, dumps  # noqa
from glyphsLib.util import clean_ufo, ufo_create_background_layer_for_all_glyphs

try:
    from ._version import version as __version__
except ImportError:
    __version__ = "0.0.0+unknown"

__all__ = [
    "build_masters",
    "load_to_ufos",
    "to_ufos",
    "to_designspace",
    "to_glyphs",
    "load",
    "loads",
    "dump",
    "dumps",
] + __all_classes__

logger = logging.getLogger(__name__)

Masters = collections.namedtuple("Masters", ["ufos", "designspace_path"])

def load_to_ufos(
    file_or_path,
    include_instances=False,
    family_name=None,
    propagate_anchors=None,
    ufo_module=None,
    minimal=False,
):
    """Load an unpacked .glyphs object to UFO objects."""

    if hasattr(file_or_path, "read"):
        font = load(file_or_path)
    else:
        with open(file_or_path, "r", encoding="utf-8") as ifile:
            font = load(ifile)
    logger.info("Loading to UFOs")
    return to_ufos(
        font,
        include_instances=include_instances,
        family_name=family_name,
        propagate_anchors=propagate_anchors,
        ufo_module=ufo_module,
        minimal=minimal,
    )

def build_masters(
    filename,
    master_dir,
    designspace_instance_dir=None,
    designspace_path=None,
    family_name=None,
    propagate_anchors=None,
    minimize_glyphs_diffs=False,
    normalize_ufos=False,
    create_background_layers=False,
    generate_GDEF=True,
    store_editor_state=True,
    write_skipexportglyphs=False,
    ufo_module=None,
    minimal=False,
):
    """Write and return UFOs from the masters and the designspace defined in a
    .glyphs file.

    Args:
        master_dir: Directory where masters are written.
        designspace_instance_dir: If provided, a designspace document will be
            written alongside the master UFOs though no instances will be built.
        family_name: If provided, the master UFOs will be given this name and
            only instances with this name will be included in the designspace.

    Returns:
        A named tuple of master UFOs (`ufos`) and the path to the designspace
        file (`designspace_path`).
    """

    font = GSFont(filename)

    if not os.path.isdir(master_dir):
        os.mkdir(master_dir)

    if designspace_instance_dir is None:
        instance_dir = None
    else:
        instance_dir = os.path.relpath(designspace_instance_dir, master_dir)

    designspace = to_designspace(
        font,
        family_name=family_name,
        propagate_anchors=propagate_anchors,
        instance_dir=instance_dir,
        minimize_glyphs_diffs=minimize_glyphs_diffs,
        generate_GDEF=generate_GDEF,
        store_editor_state=store_editor_state,
        write_skipexportglyphs=write_skipexportglyphs,
        ufo_module=ufo_module,
        minimal=minimal,
    )

    # Only write full masters to disk. This assumes that layer sources are always part
    # of another full master source, which must always be the case in a .glyphs file.
    ufos = {}
    for source in designspace.sources:
        if source.filename in ufos:
            assert source.font is ufos[source.filename]
            continue

        if create_background_layers and not minimal:
            ufo_create_background_layer_for_all_glyphs(source.font)

        ufo_path = os.path.join(master_dir, source.filename)
        clean_ufo(ufo_path)
        source.font.save(ufo_path)

        if normalize_ufos:
            import ufonormalizer

            ufonormalizer.normalizeUFO(ufo_path, writeModTimes=False)

        ufos[source.filename] = source.font

    if not designspace_path:
        designspace_path = os.path.join(master_dir, designspace.filename)
    designspace.write(designspace_path)

    return Masters(ufos, designspace_path)
<|endoftext|>


<commit_before># Tests invocation of the interpreter with various command line arguments
# All tests are executed with environment variables ignored
# See test_cmd_line_script.py for testing of script execution

import test.support, unittest
import os
import sys
from test.script_helper import spawn_python, kill_python, python_exit_code

# XXX (ncoghlan): there are assorted gratuitous inconsistencies between the
# support code in the Py3k version and the 2.x version that unnecessarily
# complicate test suite merges. See issue 7331

# spawn_python normally enforces use of -E to avoid environmental effects
# but one test checks PYTHONPATH behaviour explicitly
# XXX (ncoghlan): Give script_helper.spawn_python an option to switch
# off the -E flag that is normally inserted automatically
import subprocess
def _spawn_python_with_env(*args):
    cmd_line = [sys.executable]
    cmd_line.extend(args)
    return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

# XXX (ncoghlan): Move to script_helper and make consistent with run_python
def _kill_python_and_exit_code(p):
    data = kill_python(p)
    returncode = p.wait()
    return data, returncode

class CmdLineTest(unittest.TestCase):
    def start_python(self, *args):
        p = spawn_python(*args)
        return kill_python(p)

    def start_python_and_exit_code(self, *args):
        p = spawn_python(*args)
        return _kill_python_and_exit_code(p)

    def exit_code(self, *args):
        return python_exit_code(*args)

    def test_directories(self):
        self.assertNotEqual(self.exit_code('.'), 0)
        self.assertNotEqual(self.exit_code('< .'), 0)

    def verify_valid_flag(self, cmd_line):
        data = self.start_python(cmd_line)
        self.assertTrue(data == b'' or data.endswith(b'\n'))
        self.assertNotIn(b'Traceback', data)

    def test_optimize(self):
        self.verify_valid_flag('-O')
        self.verify_valid_flag('-OO')

    def test_q(self):
        self.verify_valid_flag('-Qold')
        self.verify_valid_flag('-Qnew')
        self.verify_valid_flag('-Qwarn')
        self.verify_valid_flag('-Qwarnall')

    def test_site_flag(self):
        self.verify_valid_flag('-S')

    def test_usage(self):
        self.assertIn(b'usage', self.start_python('-h'))

    def test_version(self):
        version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii")
        self.assertTrue(self.start_python('-V').startswith(version))

    def test_verbose(self):
        # -v causes imports to write to stderr.  If the write to
        # stderr itself causes an import to happen (for the output
        # codec), a recursion loop can occur.
        data, rc = self.start_python_and_exit_code('-v')
        self.assertEqual(rc, 0)
        self.assertNotIn(b'stack overflow', data)
        data, rc = self.start_python_and_exit_code('-vv')
        self.assertEqual(rc, 0)
        self.assertNotIn(b'stack overflow', data)

    def test_run_module(self):
        # Test expected operation of the '-m' switch
        # Switch needs an argument
        self.assertNotEqual(self.exit_code('-m'), 0)
        # Check we get an error for a nonexistent module
        self.assertNotEqual(
            self.exit_code('-m', 'fnord43520xyz'),
            0)
        # Check the runpy module also gives an error for
        # a nonexistent module
        self.assertNotEqual(
            self.exit_code('-m', 'runpy', 'fnord43520xyz'),
            0)
        # All good if module is located and run successfully
        self.assertEqual(
            self.exit_code('-m', 'timeit', '-n', '1'),
            0)

    def test_run_module_bug1764407(self):
        # -m and -i need to play well together
        # Runs the timeit module and checks the __main__
        # namespace has been populated appropriately
        p = spawn_python('-i', '-m', 'timeit', '-n', '1')
        p.stdin.write(b'Timer\n')
        p.stdin.write(b'exit()\n')
        data = kill_python(p)
        self.assertTrue(data.find(b'1 loop') != -1)
        self.assertTrue(data.find(b'__main__.Timer') != -1)

    def test_run_code(self):
        # Test expected operation of the '-c' switch
        # Switch needs an argument
        self.assertNotEqual(self.exit_code('-c'), 0)
        # Check we get an error for an uncaught exception
        self.assertNotEqual(
            self.exit_code('-c', 'raise Exception'),
            0)
        # All good if execution is successful
        self.assertEqual(
            self.exit_code('-c', 'pass'),
            0)

        # Test handling of non-ascii data
        if sys.getfilesystemencoding() != 'ascii':
            command = "assert(ord('\xe9') == 0xe9)"
            self.assertEqual(
                self.exit_code('-c', command),
                0)

    def test_unbuffered_output(self):
        # Test expected operation of the '-u' switch
        for stream in ('stdout', 'stderr'):
            # Binary is unbuffered
            code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)"
                % stream)
            data, rc = self.start_python_and_exit_code('-u', '-c', code)
            self.assertEqual(rc, 0)
            self.assertEqual(data, b'x', "binary %s not unbuffered" % stream)
            # Text is line-buffered
            code = ("import os, sys; sys.%s.write('x\\n'); os._exit(0)"
                % stream)
            data, rc = self.start_python_and_exit_code('-u', '-c', code)
            self.assertEqual(rc, 0)
            self.assertEqual(data.strip(), b'x',
                "text %s not line-buffered" % stream)

    def test_unbuffered_input(self):
        # sys.stdin still works with '-u'
        code = ("import sys; sys.stdout.write(sys.stdin.read(1))")
        p = spawn_python('-u', '-c', code)
        p.stdin.write(b'x')
        p.stdin.flush()
        data, rc = _kill_python_and_exit_code(p)
        self.assertEqual(rc, 0)
        self.assertTrue(data.startswith(b'x'), data)

    def test_large_PYTHONPATH(self):
        with test.support.EnvironmentVarGuard() as env:
            path1 = "ABCDE" * 100
            path2 = "FGHIJ" * 100
            env['PYTHONPATH'] = path1 + os.pathsep + path2
            p = _spawn_python_with_env('-S', '-c',
                                       'import sys; print(sys.path)')
            stdout, _ = p.communicate()
            self.assertIn(path1.encode('ascii'), stdout)
            self.assertIn(path2.encode('ascii'), stdout)

def test_main():
    test.support.run_unittest(CmdLineTest)
    test.support.reap_children()

if __name__ == "__main__":
    test_main()
<commit_msg>Issue #7605: Fix test_cmd_line if the current working directory is not ASCII<commit_after># Tests invocation of the interpreter with various command line arguments
# All tests are executed with environment variables ignored
# See test_cmd_line_script.py for testing of script execution

import test.support, unittest
import os
import sys
from test.script_helper import spawn_python, kill_python, python_exit_code

# XXX (ncoghlan): there are assorted gratuitous inconsistencies between the
# support code in the Py3k version and the 2.x version that unnecessarily
# complicate test suite merges. See issue 7331

# spawn_python normally enforces use of -E to avoid environmental effects
# but one test checks PYTHONPATH behaviour explicitly
# XXX (ncoghlan): Give script_helper.spawn_python an option to switch
# off the -E flag that is normally inserted automatically
import subprocess
def _spawn_python_with_env(*args):
    cmd_line = [sys.executable]
    cmd_line.extend(args)
    return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

# XXX (ncoghlan): Move to script_helper and make consistent with run_python
def _kill_python_and_exit_code(p):
    data = kill_python(p)
    returncode = p.wait()
    return data, returncode

class CmdLineTest(unittest.TestCase):
    def start_python(self, *args):
        p = spawn_python(*args)
        return kill_python(p)

    def start_python_and_exit_code(self, *args):
        p = spawn_python(*args)
        return _kill_python_and_exit_code(p)

    def exit_code(self, *args):
        return python_exit_code(*args)

    def test_directories(self):
        self.assertNotEqual(self.exit_code('.'), 0)
        self.assertNotEqual(self.exit_code('< .'), 0)

    def verify_valid_flag(self, cmd_line):
        data = self.start_python(cmd_line)
        self.assertTrue(data == b'' or data.endswith(b'\n'))
        self.assertNotIn(b'Traceback', data)

    def test_optimize(self):
        self.verify_valid_flag('-O')
        self.verify_valid_flag('-OO')

    def test_q(self):
        self.verify_valid_flag('-Qold')
        self.verify_valid_flag('-Qnew')
        self.verify_valid_flag('-Qwarn')
        self.verify_valid_flag('-Qwarnall')

    def test_site_flag(self):
        self.verify_valid_flag('-S')

    def test_usage(self):
        self.assertIn(b'usage', self.start_python('-h'))

    def test_version(self):
        version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii")
        self.assertTrue(self.start_python('-V').startswith(version))

    def test_verbose(self):
        # -v causes imports to write to stderr.  If the write to
        # stderr itself causes an import to happen (for the output
        # codec), a recursion loop can occur.
        data, rc = self.start_python_and_exit_code('-v')
        self.assertEqual(rc, 0)
        self.assertNotIn(b'stack overflow', data)
        data, rc = self.start_python_and_exit_code('-vv')
        self.assertEqual(rc, 0)
        self.assertNotIn(b'stack overflow', data)

    def test_run_module(self):
        # Test expected operation of the '-m' switch
        # Switch needs an argument
        self.assertNotEqual(self.exit_code('-m'), 0)
        # Check we get an error for a nonexistent module
        self.assertNotEqual(
            self.exit_code('-m', 'fnord43520xyz'),
            0)
        # Check the runpy module also gives an error for
        # a nonexistent module
        self.assertNotEqual(
            self.exit_code('-m', 'runpy', 'fnord43520xyz'),
            0)
        # All good if module is located and run successfully
        self.assertEqual(
            self.exit_code('-m', 'timeit', '-n', '1'),
            0)

    def test_run_module_bug1764407(self):
        # -m and -i need to play well together
        # Runs the timeit module and checks the __main__
        # namespace has been populated appropriately
        p = spawn_python('-i', '-m', 'timeit', '-n', '1')
        p.stdin.write(b'Timer\n')
        p.stdin.write(b'exit()\n')
        data = kill_python(p)
        self.assertTrue(data.find(b'1 loop') != -1)
        self.assertTrue(data.find(b'__main__.Timer') != -1)

    def test_run_code(self):
        # Test expected operation of the '-c' switch
        # Switch needs an argument
        self.assertNotEqual(self.exit_code('-c'), 0)
        # Check we get an error for an uncaught exception
        self.assertNotEqual(
            self.exit_code('-c', 'raise Exception'),
            0)
        # All good if execution is successful
        self.assertEqual(
            self.exit_code('-c', 'pass'),
            0)

        # Test handling of non-ascii data
        if sys.getfilesystemencoding() != 'ascii':
            command = "assert(ord('\xe9') == 0xe9)"
            self.assertEqual(
                self.exit_code('-c', command),
                0)

    def test_unbuffered_output(self):
        # Test expected operation of the '-u' switch
        for stream in ('stdout', 'stderr'):
            # Binary is unbuffered
            code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)"
                % stream)
            data, rc = self.start_python_and_exit_code('-u', '-c', code)
            self.assertEqual(rc, 0)
            self.assertEqual(data, b'x', "binary %s not unbuffered" % stream)
            # Text is line-buffered
            code = ("import os, sys; sys.%s.write('x\\n'); os._exit(0)"
                % stream)
            data, rc = self.start_python_and_exit_code('-u', '-c', code)
            self.assertEqual(rc, 0)
            self.assertEqual(data.strip(), b'x',
                "text %s not line-buffered" % stream)

    def test_unbuffered_input(self):
        # sys.stdin still works with '-u'
        code = ("import sys; sys.stdout.write(sys.stdin.read(1))")
        p = spawn_python('-u', '-c', code)
        p.stdin.write(b'x')
        p.stdin.flush()
        data, rc = _kill_python_and_exit_code(p)
        self.assertEqual(rc, 0)
        self.assertTrue(data.startswith(b'x'), data)

    def test_large_PYTHONPATH(self):
        with test.support.EnvironmentVarGuard() as env:
            path1 = "ABCDE" * 100
            path2 = "FGHIJ" * 100
            env['PYTHONPATH'] = path1 + os.pathsep + path2

            code = """
import sys
path = ":".join(sys.path)
path = path.encode("ascii", "backslashreplace")
sys.stdout.buffer.write(path)"""
            code = code.strip().splitlines()
            code = '; '.join(code)
            p = _spawn_python_with_env('-S', '-c', code)
            stdout, _ = p.communicate()
            self.assertIn(path1.encode('ascii'), stdout)
            self.assertIn(path2.encode('ascii'), stdout)

def test_main():
    test.support.run_unittest(CmdLineTest)
    test.support.reap_children()

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


<commit_before>from test.test_support import vereq, TestFailed

import symtable

symbols = symtable.symtable("def f(x): return x", "?", "exec")

## XXX
## Test disabled because symtable module needs to be rewritten for new compiler

##vereq(symbols[0].name, "global")
##vereq(len([ste for ste in symbols.values() if ste.name == "f"]), 1)

### Bug tickler: SyntaxError file name correct whether error raised
### while parsing or building symbol table.
##def checkfilename(brokencode):
##    try:
##        _symtable.symtable(brokencode, "spam", "exec")
##    except SyntaxError, e:
##        vereq(e.filename, "spam")
##    else:
##        raise TestFailed("no SyntaxError for %r" % (brokencode,))
##checkfilename("def f(x): foo)(")  # parse-time
##checkfilename("def f(x): global x")  # symtable-build-time
<commit_msg>Use unittest and make sure a few other cases don't crash<commit_after>from test import test_support

import symtable
import unittest

## XXX
## Test disabled because symtable module needs to be rewritten for new compiler

##vereq(symbols[0].name, "global")
##vereq(len([ste for ste in symbols.values() if ste.name == "f"]), 1)

### Bug tickler: SyntaxError file name correct whether error raised
### while parsing or building symbol table.
##def checkfilename(brokencode):
##    try:
##        _symtable.symtable(brokencode, "spam", "exec")
##    except SyntaxError, e:
##        vereq(e.filename, "spam")
##    else:
##        raise TestFailed("no SyntaxError for %r" % (brokencode,))
##checkfilename("def f(x): foo)(")  # parse-time
##checkfilename("def f(x): global x")  # symtable-build-time

class SymtableTest(unittest.TestCase):
    def test_invalid_args(self):
        self.assertRaises(TypeError, symtable.symtable, "42")
        self.assertRaises(ValueError, symtable.symtable, "42", "?", "")

    def test_eval(self):
        symbols = symtable.symtable("42", "?", "eval")

    def test_single(self):
        symbols = symtable.symtable("42", "?", "single")

    def test_exec(self):
        symbols = symtable.symtable("def f(x): return x", "?", "exec")

def test_main():
    test_support.run_unittest(SymtableTest)

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


<commit_before># -*- coding: utf-8 -*-
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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 os
import requests

from pluralsight.exceptions import PluralsightApiException

BASE_URL = "https://app.pluralsight.com/plans/api/reports/v1/"

class ReportsAPIClient(object):
    """
    Reports API client
    """

    def __init__(self, plan, api_key):
        """
        Instantiate a new reports API client

        :param plan: The plan name
        :type  plan: ``str``

        :param api_key: The API token (from the pluralsight team)
        :type  api_key: ``str``
        """
        self._plan = plan
        self._api_key = api_key

        self.base_url = BASE_URL

        self.session = requests.Session()
        self.session.headers.update(
            {"Accept": "application/json", "Authorization": "Token {0}".format(api_key)}
        )

    def download_user_report(self, plan, path):
        """
        Download the user report and store in a file

        :param plan: The plan name
        :type  plan: ``str``

        :param path: Path to the downloaded CSV
        :type  path: ``str``

        :returns: The filename
        """
        return self._download_file("users/{0}".format(plan), "users.csv", path)

    def download_course_completion_report(
        self, plan, path, start_date=None, end_date=None
    ):
        """
        Download the course completion report and store in a file

        :param plan: The plan name
        :type  plan: ``str``

        :param path: Path to the downloaded CSV
        :type  path: ``str``

        :param start_date: (optional) Start date in format YYYY-MM-DD
        :type  start_date: ``str``

        :param end_date: (optional) End date in format YYYY-MM-DD
        :type  end_date: ``str``

        :returns: The filename
        """
        params = {}

        if start_date is not None:
            params["startDate"] = start_date
        if end_date is not None:
            params["endDate"] = end_date

        return self._download_file(
            "course-completion/{0}".format(plan), "courses.csv", path, params
        )

    def download_course_usage_report(self, plan, path, start_date=None, end_date=None):
        """
        Download the course usage report and store in a file

        :param plan: The plan name
        :type  plan: ``str``

        :param path: Path to the downloaded CSV
        :type  path: ``str``

        :param start_date: (optional) Start date in format YYYY-MM-DD
        :type  start_date: ``str``

        :param end_date: (optional) End date in format YYYY-MM-DD
        :type  end_date: ``str``

        :returns: The filename
        """
        params = {}

        if start_date is not None:
            params["startDate"] = start_date
        if end_date is not None:
            params["endDate"] = end_date

        return self._download_file(
            "course-usage/{0}".format(plan), "course_usage.csv", path, params
        )

    def _download_file(self, url, local_filename, path, params=None):
        try:
            r = self.session.get(
                "{0}{1}".format(self.base_url, url), stream=True, params=params
            )
            r.raise_for_status()
            r.encoding = "utf-8-sig"
            with open(os.path.join(path, local_filename), "w") as f:
                for chunk in r.iter_content(chunk_size=1024, decode_unicode=True):
                    if chunk:  # filter out keep-alive new chunks
                        f.write(chunk)
            return local_filename
        except requests.HTTPError as e:
            raise PluralsightApiException(e)
<commit_msg>stop trying to be fancy<commit_after># -*- coding: utf-8 -*-
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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 os
import requests

from pluralsight.exceptions import PluralsightApiException

BASE_URL = "https://app.pluralsight.com/plans/api/reports/v1/"

class ReportsAPIClient(object):
    """
    Reports API client
    """

    def __init__(self, plan, api_key):
        """
        Instantiate a new reports API client

        :param plan: The plan name
        :type  plan: ``str``

        :param api_key: The API token (from the pluralsight team)
        :type  api_key: ``str``
        """
        self._plan = plan
        self._api_key = api_key

        self.base_url = BASE_URL

        self.session = requests.Session()
        self.session.headers.update(
            {"Accept": "application/json", "Authorization": "Token {0}".format(api_key)}
        )

    def download_user_report(self, plan, path):
        """
        Download the user report and store in a file

        :param plan: The plan name
        :type  plan: ``str``

        :param path: Path to the downloaded CSV
        :type  path: ``str``

        :returns: The filename
        """
        return self._download_file("users/{0}".format(plan), "users.csv", path)

    def download_course_completion_report(
        self, plan, path, start_date=None, end_date=None
    ):
        """
        Download the course completion report and store in a file

        :param plan: The plan name
        :type  plan: ``str``

        :param path: Path to the downloaded CSV
        :type  path: ``str``

        :param start_date: (optional) Start date in format YYYY-MM-DD
        :type  start_date: ``str``

        :param end_date: (optional) End date in format YYYY-MM-DD
        :type  end_date: ``str``

        :returns: The filename
        """
        params = {}

        if start_date is not None:
            params["startDate"] = start_date
        if end_date is not None:
            params["endDate"] = end_date

        return self._download_file(
            "course-completion/{0}".format(plan), "courses.csv", path, params
        )

    def download_course_usage_report(self, plan, path, start_date=None, end_date=None):
        """
        Download the course usage report and store in a file

        :param plan: The plan name
        :type  plan: ``str``

        :param path: Path to the downloaded CSV
        :type  path: ``str``

        :param start_date: (optional) Start date in format YYYY-MM-DD
        :type  start_date: ``str``

        :param end_date: (optional) End date in format YYYY-MM-DD
        :type  end_date: ``str``

        :returns: The filename
        """
        params = {}

        if start_date is not None:
            params["startDate"] = start_date
        if end_date is not None:
            params["endDate"] = end_date

        return self._download_file(
            "course-usage/{0}".format(plan), "course_usage.csv", path, params
        )

    def _download_file(self, url, local_filename, path, params=None):
        try:
            r = self.session.get(
                "{0}{1}".format(self.base_url, url), params=params
            )
            r.raise_for_status()
            with open(os.path.join(path, local_filename), "w") as f:
                f.write(r.text)
            return local_filename
        except requests.HTTPError as e:
            raise PluralsightApiException(e)
<|endoftext|>


<commit_before>import os
from re import findall,split
from pylab import arange,pi,cos,sin
from pmag import dimap
import programs.cit_magic as cit_magic
from ipmag import combine_magic
from time import time

def specimens_comparator(s1,s2):
    if type(s1) != str and type(s2) != str: return 0
    elif type(s1) != str: return -1
    elif type(s2) != str: return 1
    sam_sp1 = split(r'[-,.]+',s1)
    sam_sp2 = split(r'[-,.]+',s2)
    for e1,e2 in zip(sam_sp1,sam_sp2):
        for c1,c2 in zip(e1,e2): #sort by letters
            if c1 != c2 and c1.isalpha() and c2.isalpha():
                return ord(c1)-ord(c2)
        l1 = map(int, findall('\d+', e1)) #retrieves numbers from names
        l2 = map(int, findall('\d+', e2))
        for i1,i2 in zip(l1,l2): #sort by numbers
            if i1-i2 != 0:
                return i1-i2
    return 0

def get_all_inp_files(WD=None):
    if not os.path.isdir(WD): print("%s is does not exist, aborting"%WD)
    try:
        all_inp_files = []
        for root, dirs, files in os.walk(WD):
            for d in dirs:
                all_inp_files += get_all_inp_files(d)
            for f in files:
                if f.endswith(".inp"):
                     all_inp_files.append(os.path.join(root, f))
        return all_inp_files
    except RuntimeError:
        print("Recursion depth exceded, please use different working directory there are too many sub-directeries to walk")

def read_inp(WD,inp_file_name,magic_files):
    inp_file = open(inp_file_name, "r")
    new_inp_file = ""

    if type(magic_files) != dict: magic_files = {}
    if 'measurements' not in magic_files.keys(): magic_files['measurements']=[]
    if 'specimens' not in magic_files.keys(): magic_files['specimens']=[]
    if 'samples' not in magic_files.keys(): magic_files['samples']=[]
    if 'sites' not in magic_files.keys(): magic_files['sites']=[]

    lines = inp_file.read().split("\n")
    if len(lines) < 3: print(".inp file improperly formated"); return
    new_inp_file = lines[0] + "\n" + lines[1] + "\n"
    [lines.remove('') for i in range(lines.count(''))]
    format = lines[0].strip()
    header = lines[1].split('\t')
    update_files = lines[2:]
    update_data = False
    for i,update_file in enumerate(update_files):
        update_lines = update_file.split('\t')
        if not os.path.isfile(update_lines[0]):
            print("%s not found searching for location of file"%(update_lines[0]))
            sam_file_name = os.path.split(update_lines[0])[-1]
            new_file_path = find_file(sam_file_name, WD)
            if new_file_path == None or not os.path.isfile(new_file_path):
                print("%s does not exist in any subdirectory of %s and will be skipped"%(update_lines[0], WD))
                new_inp_file += update_file+"\n"
                continue
            else:
                print("new location for file found at %s"%(new_file_path))
                update_lines[0] = new_file_path
        d = os.path.dirname(update_lines[0])
        name = os.path.basename(os.path.splitext(update_lines[0])[0])
        erspecf = name + "_er_specimens.txt"
        ersampf = name + "_er_samples.txt"
        ersitef = name + "_er_sites.txt"
        f = name + ".magic"
        if os.path.join(d,f) in magic_files:
            new_inp_file += update_file+"\n"
            continue
        if float(update_lines[-1]) >= os.path.getmtime(update_lines[0]):
            no_changes = True
            #check specimen files for changes
            sam_file = open(update_lines[0])
            sam_file_lines = sam_file.readlines()
            spec_file_paths = map(lambda x: os.path.join(d,x.strip('\r \n')), sam_file_lines[2:])
            for spec_file_path in spec_file_paths:
                if float(update_lines[-1]) < os.path.getmtime(spec_file_path):
                    no_changes=False; break
            if no_changes and os.path.isfile(os.path.join(WD,f)) \
                          and os.path.isfile(os.path.join(WD,erspecf)) \
                          and os.path.isfile(os.path.join(WD,ersampf)) \
                          and os.path.isfile(os.path.join(WD,ersitef)):
                magic_files['measurements'].append(os.path.join(WD,f))
                magic_files['specimens'].append(os.path.join(WD,erspecf))
                magic_files['samples'].append(os.path.join(WD,ersampf))
                magic_files['sites'].append(os.path.join(WD,ersitef))
                new_inp_file += update_file+"\n"
                continue
        if len(header) != len(update_lines):
            print("length of header and length of enteries for the file %s are different and will be skipped"%(update_lines[0]))
            new_inp_file += update_file+"\n"
            continue
        update_dict = {}
        for head,entry in zip(header,update_lines):
            update_dict[head] = entry
        if format == "CIT":
            CIT_kwargs = {}
            CIT_name = os.path.basename(os.path.splitext(update_dict["sam_path"])[0])

            CIT_kwargs["dir_path"] = WD #reduce(lambda x,y: x+"/"+y, update_dict["sam_path"].split("/")[:-1])
            CIT_kwargs["user"] = ""
            CIT_kwargs["meas_file"] = CIT_name + ".magic"
            CIT_kwargs["spec_file"] = CIT_name + "_er_specimens.txt"
            CIT_kwargs["samp_file"] = CIT_name + "_er_samples.txt"
            CIT_kwargs["site_file"] = CIT_name + "_er_sites.txt"
            CIT_kwargs["locname"] = update_dict["location"]
            CIT_kwargs["methods"] = update_dict["field_magic_codes"]
            CIT_kwargs["specnum"] = update_dict["num_terminal_char"]
            CIT_kwargs["avg"] = update_dict["dont_average_replicate_measurements"]
            CIT_kwargs["samp_con"] = update_dict["naming_convention"]
            CIT_kwargs["peak_AF"] = update_dict["peak_AF"]
            CIT_kwargs["magfile"] = os.path.basename(update_dict["sam_path"])
            CIT_kwargs["input_dir_path"] = os.path.dirname(update_dict["sam_path"])

            program_ran, error_message = cit_magic.main(command_line=False, **CIT_kwargs)

            measp = os.path.join(CIT_kwargs["dir_path"],CIT_kwargs["meas_file"])
            specp = os.path.join(CIT_kwargs["dir_path"],CIT_kwargs["spec_file"])
            sampp = os.path.join(CIT_kwargs["dir_path"],CIT_kwargs["samp_file"])
            sitep = os.path.join(CIT_kwargs["dir_path"],CIT_kwargs["site_file"])

            if program_ran:
                update_data = True
                update_lines[-1] = time()
                new_inp_file += reduce(lambda x,y: str(x)+"\t"+str(y), update_lines)+"\n"
                magic_files['measurements'].append(measp)
                magic_files['specimens'].append(specp)
                magic_files['samples'].append(sampp)
                magic_files['sites'].append(sitep)
            else:
                new_inp_file += update_file
                if os.path.isfile(measp) and \
                   os.path.isfile(specp) and \
                   os.path.isfile(sampp) and \
                   os.path.isfile(sitep):
                    magic_files['measurements'].append(measp)
                    magic_files['specimens'].append(specp)
                    magic_files['samples'].append(sampp)
                    magic_files['sites'].append(sitep)

    inp_file.close()
    out_file = open(inp_file_name, "w")
    out_file.write(new_inp_file)
    return update_data

def combine_magic_files(WD,magic_files):
    if type(magic_files) != dict: return
    if 'measurements' in magic_files.keys():
        combine_magic(magic_files['measurements'], WD+"/magic_measurements.txt")
    if 'specimens' in magic_files.keys():
        combine_magic(magic_files['specimens'], WD+"/er_specimens.txt")
    if 'samples' in magic_files.keys():
        combine_magic(magic_files['samples'], WD+"/er_samples.txt")
    if 'sites' in magic_files.keys():
        combine_magic(magic_files['sites'], WD+"/er_sites.txt")

def pick_inp(parent,WD):
    dlg = wx.FileDialog(
        parent, message="choose .inp file",
        defaultDir=WD,
        defaultFile="magic.inp",
        wildcard="*.inp",
        style=wx.OPEN
        )
    if dlg.ShowModal() == wx.ID_OK:
        inp_file_name = dlg.GetPath()
    else:
        inp_file_name = None
    dlg.Destroy()
    return inp_file_name

def read_LSQ(filepath):
    fin = open(filepath, 'r')
    interps = fin.readlines()
    interps_out = []
    parse_LSQ_bound = lambda x: ord(x)-ord("A") if ord(x)-ord("A") < 25 else ord(x)-ord("A")-6
    for i,interp in enumerate(interps):
        interps_out.append({})
        enteries = interp.split()
        interps_out[i]['er_specimen_name'] = enteries[0]
        if enteries[1] == 'L':
            interps_out[i]['magic_method_codes'] = 'DE-BFL:DA-DIR-GEO'
        j = 2
        if len(enteries) > 9: interps_out[i]['specimen_comp_name'] = enteries[j]; j += 1;
        else: interps_out[i]['specimen_comp_name'] = None
        interps_out[i]['specimen_dec'] = enteries[j]
        interps_out[i]['specimen_inc'] = enteries[j+1]
        j += 4
        bounds = enteries[j].split('-')
        interps_out[i]['measurement_min_index'] = parse_LSQ_bound(bounds[0])
        interps_out[i]['measurement_max_index'] = parse_LSQ_bound(bounds[-1])
        bad_meas = [bounds[k] for k in range(len(bounds)) if len(bounds[k]) > 1]
        for bad_m in bad_meas:
             fc = parse_LSQ_bound(bad_m[0])
             lc = parse_LSQ_bound(bad_m[-1])
             interps_out[i]['bad_measurement_index'] = []
             for k in range(1,lc-fc+1):
                     interps_out[i]['bad_measurement_index'].append(fc+k)
        interps_out[i]['specimen_n'] = enteries[j+1]
        interps_out[i]['specimen_mad'] = enteries[j+2]
    fin.close()
    return interps_out

def find_file(name, path):
    for root, dirs, files in os.walk(path):
        if name in files:
            return os.path.join(root, name)

def draw_net(FIG):
    FIG.clear()
    eq=FIG
    eq.axis((-1,1,-1,1))
    eq.axis('off')
    theta=arange(0.,2*pi,2*pi/1000)
    eq.plot(cos(theta),sin(theta),'k',clip_on=False,lw=1)
    #eq.vlines((0,0),(0.9,-0.9),(1.0,-1.0),'k')
    #eq.hlines((0,0),(0.9,-0.9),(1.0,-1.0),'k')
    #eq.plot([0.0],[0.0],'+k')

    Xsym,Ysym=[],[]
    for I in range(10,100,10):
        XY=dimap(0.,I)
        Xsym.append(XY[0])
        Ysym.append(XY[1])
    for I in range(10,90,10):
        XY=dimap(90.,I)
        Xsym.append(XY[0])
        Ysym.append(XY[1])
    for I in range(10,90,10):
        XY=dimap(180.,I)
        Xsym.append(XY[0])
        Ysym.append(XY[1])
    for I in range(10,90,10):
        XY=dimap(270.,I)
        Xsym.append(XY[0])
        Ysym.append(XY[1])
    eq.plot(Xsym,Ysym,'k+',clip_on=False,mew=0.5)
    for D in range(0,360,10):
        Xtick,Ytick=[],[]
        for I in range(4):
            XY=dimap(D,I)
            Xtick.append(XY[0])
            Ytick.append(XY[1])
        eq.plot(Xtick,Ytick,'k',clip_on=False,lw=0.5)
    eq.axes.set_aspect('equal')
<commit_msg>fixed LSQ line break issue<commit_after>import os
from re import findall,split
from pylab import arange,pi,cos,sin
from pmag import dimap
import programs.cit_magic as cit_magic
from ipmag import combine_magic
from time import time

def specimens_comparator(s1,s2):
    if type(s1) != str and type(s2) != str: return 0
    elif type(s1) != str: return -1
    elif type(s2) != str: return 1
    sam_sp1 = split(r'[-,.]+',s1)
    sam_sp2 = split(r'[-,.]+',s2)
    for e1,e2 in zip(sam_sp1,sam_sp2):
        for c1,c2 in zip(e1,e2): #sort by letters
            if c1 != c2 and c1.isalpha() and c2.isalpha():
                return ord(c1)-ord(c2)
        l1 = map(int, findall('\d+', e1)) #retrieves numbers from names
        l2 = map(int, findall('\d+', e2))
        for i1,i2 in zip(l1,l2): #sort by numbers
            if i1-i2 != 0:
                return i1-i2
    return 0

def get_all_inp_files(WD=None):
    if not os.path.isdir(WD): print("%s is does not exist, aborting"%WD)
    try:
        all_inp_files = []
        for root, dirs, files in os.walk(WD):
            for d in dirs:
                all_inp_files += get_all_inp_files(d)
            for f in files:
                if f.endswith(".inp"):
                     all_inp_files.append(os.path.join(root, f))
        return all_inp_files
    except RuntimeError:
        print("Recursion depth exceded, please use different working directory there are too many sub-directeries to walk")

def read_inp(WD,inp_file_name,magic_files):
    inp_file = open(inp_file_name, "r")
    new_inp_file = ""

    if type(magic_files) != dict: magic_files = {}
    if 'measurements' not in magic_files.keys(): magic_files['measurements']=[]
    if 'specimens' not in magic_files.keys(): magic_files['specimens']=[]
    if 'samples' not in magic_files.keys(): magic_files['samples']=[]
    if 'sites' not in magic_files.keys(): magic_files['sites']=[]

    lines = inp_file.read().split("\n")
    if len(lines) < 3: print(".inp file improperly formated"); return
    new_inp_file = lines[0] + "\n" + lines[1] + "\n"
    [lines.remove('') for i in range(lines.count(''))]
    format = lines[0].strip()
    header = lines[1].split('\t')
    update_files = lines[2:]
    update_data = False
    for i,update_file in enumerate(update_files):
        update_lines = update_file.split('\t')
        if not os.path.isfile(update_lines[0]):
            print("%s not found searching for location of file"%(update_lines[0]))
            sam_file_name = os.path.split(update_lines[0])[-1]
            new_file_path = find_file(sam_file_name, WD)
            if new_file_path == None or not os.path.isfile(new_file_path):
                print("%s does not exist in any subdirectory of %s and will be skipped"%(update_lines[0], WD))
                new_inp_file += update_file+"\n"
                continue
            else:
                print("new location for file found at %s"%(new_file_path))
                update_lines[0] = new_file_path
        d = os.path.dirname(update_lines[0])
        name = os.path.basename(os.path.splitext(update_lines[0])[0])
        erspecf = name + "_er_specimens.txt"
        ersampf = name + "_er_samples.txt"
        ersitef = name + "_er_sites.txt"
        f = name + ".magic"
        if os.path.join(d,f) in magic_files:
            new_inp_file += update_file+"\n"
            continue
        if float(update_lines[-1]) >= os.path.getmtime(update_lines[0]):
            no_changes = True
            #check specimen files for changes
            sam_file = open(update_lines[0])
            sam_file_lines = sam_file.readlines()
            spec_file_paths = map(lambda x: os.path.join(d,x.strip('\r \n')), sam_file_lines[2:])
            for spec_file_path in spec_file_paths:
                if float(update_lines[-1]) < os.path.getmtime(spec_file_path):
                    no_changes=False; break
            if no_changes and os.path.isfile(os.path.join(WD,f)) \
                          and os.path.isfile(os.path.join(WD,erspecf)) \
                          and os.path.isfile(os.path.join(WD,ersampf)) \
                          and os.path.isfile(os.path.join(WD,ersitef)):
                magic_files['measurements'].append(os.path.join(WD,f))
                magic_files['specimens'].append(os.path.join(WD,erspecf))
                magic_files['samples'].append(os.path.join(WD,ersampf))
                magic_files['sites'].append(os.path.join(WD,ersitef))
                new_inp_file += update_file+"\n"
                continue
        if len(header) != len(update_lines):
            print("length of header and length of enteries for the file %s are different and will be skipped"%(update_lines[0]))
            new_inp_file += update_file+"\n"
            continue
        update_dict = {}
        for head,entry in zip(header,update_lines):
            update_dict[head] = entry
        if format == "CIT":
            CIT_kwargs = {}
            CIT_name = os.path.basename(os.path.splitext(update_dict["sam_path"])[0])

            CIT_kwargs["dir_path"] = WD #reduce(lambda x,y: x+"/"+y, update_dict["sam_path"].split("/")[:-1])
            CIT_kwargs["user"] = ""
            CIT_kwargs["meas_file"] = CIT_name + ".magic"
            CIT_kwargs["spec_file"] = CIT_name + "_er_specimens.txt"
            CIT_kwargs["samp_file"] = CIT_name + "_er_samples.txt"
            CIT_kwargs["site_file"] = CIT_name + "_er_sites.txt"
            CIT_kwargs["locname"] = update_dict["location"]
            CIT_kwargs["methods"] = update_dict["field_magic_codes"]
            CIT_kwargs["specnum"] = update_dict["num_terminal_char"]
            CIT_kwargs["avg"] = update_dict["dont_average_replicate_measurements"]
            CIT_kwargs["samp_con"] = update_dict["naming_convention"]
            CIT_kwargs["peak_AF"] = update_dict["peak_AF"]
            CIT_kwargs["magfile"] = os.path.basename(update_dict["sam_path"])
            CIT_kwargs["input_dir_path"] = os.path.dirname(update_dict["sam_path"])

            program_ran, error_message = cit_magic.main(command_line=False, **CIT_kwargs)

            measp = os.path.join(CIT_kwargs["dir_path"],CIT_kwargs["meas_file"])
            specp = os.path.join(CIT_kwargs["dir_path"],CIT_kwargs["spec_file"])
            sampp = os.path.join(CIT_kwargs["dir_path"],CIT_kwargs["samp_file"])
            sitep = os.path.join(CIT_kwargs["dir_path"],CIT_kwargs["site_file"])

            if program_ran:
                update_data = True
                update_lines[-1] = time()
                new_inp_file += reduce(lambda x,y: str(x)+"\t"+str(y), update_lines)+"\n"
                magic_files['measurements'].append(measp)
                magic_files['specimens'].append(specp)
                magic_files['samples'].append(sampp)
                magic_files['sites'].append(sitep)
            else:
                new_inp_file += update_file
                if os.path.isfile(measp) and \
                   os.path.isfile(specp) and \
                   os.path.isfile(sampp) and \
                   os.path.isfile(sitep):
                    magic_files['measurements'].append(measp)
                    magic_files['specimens'].append(specp)
                    magic_files['samples'].append(sampp)
                    magic_files['sites'].append(sitep)

    inp_file.close()
    out_file = open(inp_file_name, "w")
    out_file.write(new_inp_file)
    return update_data

def combine_magic_files(WD,magic_files):
    if type(magic_files) != dict: return
    if 'measurements' in magic_files.keys():
        combine_magic(magic_files['measurements'], WD+"/magic_measurements.txt")
    if 'specimens' in magic_files.keys():
        combine_magic(magic_files['specimens'], WD+"/er_specimens.txt")
    if 'samples' in magic_files.keys():
        combine_magic(magic_files['samples'], WD+"/er_samples.txt")
    if 'sites' in magic_files.keys():
        combine_magic(magic_files['sites'], WD+"/er_sites.txt")

def pick_inp(parent,WD):
    dlg = wx.FileDialog(
        parent, message="choose .inp file",
        defaultDir=WD,
        defaultFile="magic.inp",
        wildcard="*.inp",
        style=wx.OPEN
        )
    if dlg.ShowModal() == wx.ID_OK:
        inp_file_name = dlg.GetPath()
    else:
        inp_file_name = None
    dlg.Destroy()
    return inp_file_name

def read_LSQ(filepath):
    fin = open(filepath, 'r')
    interps = fin.read().splitlines()
    interps_out = []
    parse_LSQ_bound = lambda x: ord(x)-ord("A") if ord(x)-ord("A") < 25 else ord(x)-ord("A")-6
    for i,interp in enumerate(interps):
        interps_out.append({})
        enteries = interp.split()
        interps_out[i]['er_specimen_name'] = enteries[0]
        if enteries[1] == 'L':
            interps_out[i]['magic_method_codes'] = 'DE-BFL:DA-DIR-GEO'
        j = 2
        if len(enteries) > 9: interps_out[i]['specimen_comp_name'] = enteries[j]; j += 1;
        else: interps_out[i]['specimen_comp_name'] = None
        interps_out[i]['specimen_dec'] = enteries[j]
        interps_out[i]['specimen_inc'] = enteries[j+1]
        j += 4
        bounds = enteries[j].split('-')
        interps_out[i]['measurement_min_index'] = parse_LSQ_bound(bounds[0])
        interps_out[i]['measurement_max_index'] = parse_LSQ_bound(bounds[-1])
        bad_meas = [bounds[k] for k in range(len(bounds)) if len(bounds[k]) > 1]
        for bad_m in bad_meas:
             fc = parse_LSQ_bound(bad_m[0])
             lc = parse_LSQ_bound(bad_m[-1])
             interps_out[i]['bad_measurement_index'] = []
             for k in range(1,lc-fc+1):
                     interps_out[i]['bad_measurement_index'].append(fc+k)
        interps_out[i]['specimen_n'] = enteries[j+1]
        interps_out[i]['specimen_mad'] = enteries[j+2]
    fin.close()
    return interps_out

def find_file(name, path):
    for root, dirs, files in os.walk(path):
        if name in files:
            return os.path.join(root, name)

def draw_net(FIG):
    FIG.clear()
    eq=FIG
    eq.axis((-1,1,-1,1))
    eq.axis('off')
    theta=arange(0.,2*pi,2*pi/1000)
    eq.plot(cos(theta),sin(theta),'k',clip_on=False,lw=1)
    #eq.vlines((0,0),(0.9,-0.9),(1.0,-1.0),'k')
    #eq.hlines((0,0),(0.9,-0.9),(1.0,-1.0),'k')
    #eq.plot([0.0],[0.0],'+k')

    Xsym,Ysym=[],[]
    for I in range(10,100,10):
        XY=dimap(0.,I)
        Xsym.append(XY[0])
        Ysym.append(XY[1])
    for I in range(10,90,10):
        XY=dimap(90.,I)
        Xsym.append(XY[0])
        Ysym.append(XY[1])
    for I in range(10,90,10):
        XY=dimap(180.,I)
        Xsym.append(XY[0])
        Ysym.append(XY[1])
    for I in range(10,90,10):
        XY=dimap(270.,I)
        Xsym.append(XY[0])
        Ysym.append(XY[1])
    eq.plot(Xsym,Ysym,'k+',clip_on=False,mew=0.5)
    for D in range(0,360,10):
        Xtick,Ytick=[],[]
        for I in range(4):
            XY=dimap(D,I)
            Xtick.append(XY[0])
            Ytick.append(XY[1])
        eq.plot(Xtick,Ytick,'k',clip_on=False,lw=0.5)
    eq.axes.set_aspect('equal')
<|endoftext|>


<commit_before>#!/usr/bin/python3
# OMDB_api_scrape.py - parses a movie and year from the command line, grabs the
#                      JSON and saves a copy for later

import json, requests, sys, os

URL_BASE = 'http://www.omdbapi.com/?'

if len(sys.argv) > 1:
	# Get address from command line.
    mTitle = '+'.join(sys.argv[1:-1])
    mYear = sys.argv[-1]
    print(mTitle)
    print(mYear)
else:
    print("Usage: OMDB_api_scrape.py <Movie Title> <Year>")
    sys.exit(1)

# Craft the URL
url = URL_BASE + 't=' + mTitle + '&y=' + mYear + '&plot=full&r=json'

# Try to get the url
try:
	response = requests.get(url)
	response.raise_for_status()
except requests.exceptions.RequestException as err:
	print(err)
	sys.exit(1)

theJSON = json.loads(response.text)

# Save the JSON file
with open((os.path.join(os.getcwd(), (mTitle + '_' + mYear + '.json'))), 'w') as outfile:
    json.dump(theJSON, outfile)
<commit_msg>Convert OMDB scrapper to grab xml<commit_after>#!/usr/bin/python3
# OMDB_api_scrape.py - parses a movie and year from the command line, grabs the
#                      xml and saves a copy for later

import requests, sys, os
import lxml.etree

URL_BASE = 'http://www.omdbapi.com/?'

if len(sys.argv) > 1:
	# Get address from command line.
    mTitle = '+'.join(sys.argv[1:-1])
    mYear = sys.argv[-1]
    print(mTitle)
    print(mYear)
else:
    print("Usage: OMDB_api_scrape.py <Movie Title> <Year>")
    sys.exit(1)

# Craft the URL
url = URL_BASE + 't=' + mTitle + '&y=' + mYear + '&plot=full&r=xml'

# Try to get the url
try:
	response = requests.get(url)
	response.raise_for_status()
except requests.exceptions.RequestException as err:
	print(err)
	sys.exit(1)

# Save the XML file
with open((os.path.join(os.getcwd(), (mTitle + '_' + mYear + '.xml'))), 'wb') as outfile:
    outfile.write(response.text)
<|endoftext|>