<commit_before>import math
import pandas as pd
from google.cloud import bigquery
from IPython.display import display

from learntools.core import *

# Setup
client = bigquery.Client()

# (1) TaxiDemand
avg_num_trips_query = """
                      WITH trips_by_day AS
                      (
                      SELECT DATE(trip_start_timestamp) AS trip_date,
                          COUNT(*) as num_trips
                      FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                      WHERE trip_start_timestamp >= '2016-01-01' AND trip_start_timestamp < '2018-01-01'
                      GROUP BY trip_date
                      ORDER BY trip_date
                      )
                      SELECT trip_date,
                          AVG(num_trips)
                          OVER (
                               ORDER BY trip_date
                               ROWS BETWEEN 15 PRECEDING AND 15 FOLLOWING
                               ) AS avg_num_trips
                      FROM trips_by_day
                      """
avg_num_trips_answer = client.query(avg_num_trips_query).result().to_dataframe()

# (2) CommunityArea
trip_number_query = """
                    SELECT pickup_community_area,
                        trip_start_timestamp,
                        trip_end_timestamp,
                        RANK()
                            OVER (
                                  PARTITION BY pickup_community_area
                                  ORDER BY trip_start_timestamp
                                 ) AS trip_number
                    FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                    WHERE DATE(trip_start_timestamp) = '2017-05-01'
                    """
trip_number_answer = client.query(trip_number_query).result().to_dataframe()

# (3) BreakTime
break_time_query = """
                   SELECT taxi_id,
                       trip_start_timestamp,
                       trip_end_timestamp,
                       TIMESTAMP_DIFF(
                           trip_start_timestamp,
                           LAG(trip_end_timestamp, 1) OVER (PARTITION BY taxi_id ORDER BY trip_start_timestamp),
                           MINUTE) as prev_break
                   FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                   WHERE DATE(trip_start_timestamp) = '2017-05-01'
                   """
break_time_answer = client.query(break_time_query).result().to_dataframe()

# (1)
class TaxiDemand(CodingProblem):
    _vars = ['avg_num_trips_query', 'avg_num_trips_result']

    def print_table(self):
      display(avg_num_trips_answer.head())

    def check(self, query, results):
        assert (type(query) == str), ("You don't have a valid query yet. Try again.")
        # check 1: words appear in query
        assert ("over" in query.lower()), ('Your query is missing an **OVER** clause.')
        assert ("15" in query.lower()), ("Your window should include the current date, along with the preceding 15 days and the following 15 days.")
        assert ("avg" in query.lower()), ("Your query should calculate a rolling average.  For this, you need to use the **AVG()** function.")
        # check 2: column names
        lowered_colnames = [c.lower() for c in results.columns]
        assert ('trip_date' in results.columns), ("You didn't select the `trip_date` column. Try again.")
        assert ('avg_num_trips' in results.columns), ("You didn't select the `avg_num_trips` column. Try again.")
        # check 3: length of df
        assert (len(results) == len(avg_num_trips_answer)), ("Your results do not seem to have the correct dates.  You should have one entry "
                                                             "for each date from January 1, 2016, to December 31, 2017.  Note that the provided partial "
                                                             "query already selects the appropriate dates for you (in the `trips_by_day` column).")
        # check 4: check specific value in df
        # get a date to check
        first_date = list(avg_num_trips_answer['trip_date'])[0]
        # get corresponding value
        correct_number = avg_num_trips_answer.loc[avg_num_trips_answer['trip_date']==first_date]['avg_num_trips'].values[0]
        # want this to equal the corresponding value above
        check_number = results.loc[results['trip_date']==first_date]['avg_num_trips'].values[0]
        assert (int(check_number)==int(correct_number)), ("The results don't look right. Try again.")
        display(avg_num_trips_answer.head())

    _solution = CS(\
"""
avg_num_trips_query = \"""
                      WITH trips_by_day AS
                      (
                      SELECT DATE(trip_start_timestamp) AS trip_date,
                          COUNT(*) as num_trips
                      FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                      WHERE trip_start_timestamp >= '2016-01-01' AND trip_start_timestamp < '2018-01-01'
                      GROUP BY trip_date
                      ORDER BY trip_date
                      )
                      SELECT trip_date,
                          AVG(num_trips)
                          OVER (
                               ORDER BY trip_date
                               ROWS BETWEEN 15 PRECEDING AND 15 FOLLOWING
                               ) AS avg_num_trips
                      FROM trips_by_day
                      \"""

avg_num_trips_result = client.query(avg_num_trips_query).result().to_dataframe()
"""
    )
    _hint = ("Use the **AVG()** function. Write an **OVER** clause with that orders the rows with the `trip_date` column and uses a window that "
             "includes the 15 preceding rows, the current row, and the following 15 rows.")

# (2)
class CommunityArea(CodingProblem):
    _vars = ['trip_number_query', 'trip_number_result']
    def check(self, query, results):
        assert (type(query) == str), ("You don't have a valid query yet. Try again.")
        # check 1: query contains certain words
        assert ('rank' in query.lower()), ("There are many different numbering functions that enumerate the rows in the input. "
                                           "For this exercise, please use the **RANK()** function.")
        # check 2: correct columns selected
        assert ('pickup_community_area' in set(results.columns)), ("You didn't select the `pickup_community_area` column.")
        assert ('trip_start_timestamp' in set(results.columns)), ("You didn't select the `trip_start_timestamp` column.")
        assert ('trip_end_timestamp' in set(results.columns)), ("You didn't select the `trip_end_timestamp` column.")
        assert ('trip_number' in set(results.columns)), ("You didn't select the `trip_number` column.")
        # check 3: length of df
        assert (len(results)==len(trip_number_answer)), ("Your answer does not have the correct number of rows.")
        # check 4: specific value
        correct_number = len(trip_number_answer.loc[trip_number_answer["trip_number"]==3])
        check_number = len(results.loc[results["trip_number"]==3])
        assert (int(check_number)==int(correct_number)), ("The results don't look right. Try again.")

    _solution = CS( \
"""
trip_number_query = \"""
                    SELECT pickup_community_area,
                        trip_start_timestamp,
                        trip_end_timestamp,
                        RANK()
                            OVER (
                                  PARTITION BY pickup_community_area
                                  ORDER BY trip_start_timestamp
                                 ) AS trip_number
                    FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                    WHERE DATE(trip_start_timestamp) = '2017-05-01'
                    \"""

trip_number_result = client.query(trip_number_query).result().to_dataframe()
"""
)
    _hint = ("You don't need a **GROUP BY** clause.  Use the **RANK()** function.  Your **OVER** clause should order the rows by the "
             "`trip_start_timestamp` column and break the data into partitions based on `pickup_community_area`.")

class BreakTime(CodingProblem):
    _vars = ['break_time_query', 'break_time_result']
    def check(self, query, results):
        assert (type(query) == str), ("You don't have a valid query yet. Try again.")
        # check 1: query contains certain words
        assert ('lag' in query.lower()), ("Use the **LAG()** function to pull the value for `trip_end_timestamp` from the previous row.")
        # check 2: correct columns selected
        assert ('taxi_id' in set(results.columns)), ("You didn't select the `taxi_id` column.")
        assert ('trip_start_timestamp' in set(results.columns)), ("You didn't select the `trip_start_timestamp` column.")
        assert ('trip_end_timestamp' in set(results.columns)), ("You didn't select the `trip_end_timestamp` column.")
        assert ('prev_break' in set(results.columns)), ("You didn't select the `prev_break` column.")
        # check 3: check values, length of dataframe
        assert (len(results)==len(break_time_answer)), ("Your answer does not have the correct number of rows.")
        # check 4: specific number
        id_to_check = list(break_time_answer["taxi_id"])[0]
        correct_ans = [int(i) for i in list(break_time_answer.loc[break_time_answer["taxi_id"] == id_to_check]["prev_break"]) if math.isnan(i)==False]
        submitted_ans = [int(i) for i in list(results.loc[results["taxi_id"] == id_to_check]["prev_break"]) if math.isnan(i)==False]
        if len(correct_ans) > 0:
          assert (min(correct_ans)==min(submitted_ans)), ("The results don't look right. Try again.")
          assert (max(correct_ans)==max(submitted_ans)), ("The results don't look right. Try again.")

    _solution = CS( \
"""
break_time_query = \"""
                   SELECT taxi_id,
                       trip_start_timestamp,
                       trip_end_timestamp,
                       TIMESTAMP_DIFF(
                           trip_start_timestamp,
                           LAG(trip_end_timestamp, 1) OVER (PARTITION BY taxi_id ORDER BY trip_start_timestamp),
                           MINUTE) as prev_break
                   FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                   WHERE DATE(trip_start_timestamp) = '2017-05-01'
                   \"""

break_time_result = client.query(break_time_query).result().to_dataframe()
"""
)
    _hint = ("The `TIMESTAMP_DIFF()` function takes three arguments, where the first (`trip_start_timestamp`) and the last (`MINUTE`) "
             "are provided for you.  This function provides the time difference (in minutes) of the timestamps in the first two arguments. "
             "You need only fill in the second argument, which should use the **LAG()** function to pull the timestamp corresponding to "
             "the end of the previous trip (for the same `taxi_id`).")

qvars = bind_exercises(globals(), [
    TaxiDemand,
    CommunityArea,
    BreakTime
    ],
    tutorial_id=-1,
    var_format='q_{n}',
    )

__all__ = list(qvars)
<commit_msg>try some stuff checking code<commit_after>import math
import pandas as pd
from google.cloud import bigquery
from IPython.display import display

from learntools.core import *

# Setup
client = bigquery.Client()

# (1) TaxiDemand
avg_num_trips_query = """
                      WITH trips_by_day AS
                      (
                      SELECT DATE(trip_start_timestamp) AS trip_date,
                          COUNT(*) as num_trips
                      FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                      WHERE trip_start_timestamp >= '2016-01-01' AND trip_start_timestamp < '2018-01-01'
                      GROUP BY trip_date
                      ORDER BY trip_date
                      )
                      SELECT trip_date,
                          AVG(num_trips)
                          OVER (
                               ORDER BY trip_date
                               ROWS BETWEEN 15 PRECEDING AND 15 FOLLOWING
                               ) AS avg_num_trips
                      FROM trips_by_day
                      """
avg_num_trips_answer = client.query(avg_num_trips_query).result().to_dataframe()

# (2) CommunityArea
trip_number_query = """
                    SELECT pickup_community_area,
                        trip_start_timestamp,
                        trip_end_timestamp,
                        RANK()
                            OVER (
                                  PARTITION BY pickup_community_area
                                  ORDER BY trip_start_timestamp
                                 ) AS trip_number
                    FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                    WHERE DATE(trip_start_timestamp) = '2017-05-01'
                    """
trip_number_answer = client.query(trip_number_query).result().to_dataframe()

# (3) BreakTime
break_time_query = """
                   SELECT taxi_id,
                       trip_start_timestamp,
                       trip_end_timestamp,
                       TIMESTAMP_DIFF(
                           trip_start_timestamp,
                           LAG(trip_end_timestamp, 1) OVER (PARTITION BY taxi_id ORDER BY trip_start_timestamp),
                           MINUTE) as prev_break
                   FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                   WHERE DATE(trip_start_timestamp) = '2017-05-01'
                   """
break_time_answer = client.query(break_time_query).result().to_dataframe()

# (1)
class TaxiDemand(CodingProblem):
    _var = 'avg_num_trips_query'

    def check(self, query):
        assert (type(query) == str), ("You don't have a valid query yet. Try again.")
        # check 1: words appear in query
        assert ("over" in query.lower()), ('Your query is missing an **OVER** clause.')
        assert ("15" in query.lower()), ("Your window should include the current date, along with the preceding 15 days and the following 15 days.")
        assert ("avg" in query.lower()), ("Your query should calculate a rolling average.  For this, you need to use the **AVG()** function.")
        # EXPERIMENTAL: get results
        try:
            results = client.query(query).result().to_dataframe()
        except:
            assert False, "Your query didn't run."
        display(results.head())
        # check 2: column names
        lowered_colnames = [c.lower() for c in results.columns]
        assert ('trip_date' in results.columns), ("You didn't select the `trip_date` column. Try again.")
        assert ('avg_num_trips' in results.columns), ("You didn't select the `avg_num_trips` column. Try again.")
        # check 3: length of df
        assert (len(results) == len(avg_num_trips_answer)), ("Your results do not seem to have the correct dates.  You should have one entry "
                                                             "for each date from January 1, 2016, to December 31, 2017.  Note that the provided partial "
                                                             "query already selects the appropriate dates for you (in the `trips_by_day` column).")
        # check 4: check specific value in df
        # get a date to check
        first_date = list(avg_num_trips_answer['trip_date'])[0]
        # get corresponding value
        correct_number = avg_num_trips_answer.loc[avg_num_trips_answer['trip_date']==first_date]['avg_num_trips'].values[0]
        # want this to equal the corresponding value above
        check_number = results.loc[results['trip_date']==first_date]['avg_num_trips'].values[0]
        assert (int(check_number)==int(correct_number)), ("The results don't look right. Try again.")

    _solution = CS(\
"""
avg_num_trips_query = \"""
                      WITH trips_by_day AS
                      (
                      SELECT DATE(trip_start_timestamp) AS trip_date,
                          COUNT(*) as num_trips
                      FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                      WHERE trip_start_timestamp >= '2016-01-01' AND trip_start_timestamp < '2018-01-01'
                      GROUP BY trip_date
                      ORDER BY trip_date
                      )
                      SELECT trip_date,
                          AVG(num_trips)
                          OVER (
                               ORDER BY trip_date
                               ROWS BETWEEN 15 PRECEDING AND 15 FOLLOWING
                               ) AS avg_num_trips
                      FROM trips_by_day
                      \"""

avg_num_trips_result = client.query(avg_num_trips_query).result().to_dataframe()
"""
    )
    _hint = ("Use the **AVG()** function. Write an **OVER** clause with that orders the rows with the `trip_date` column and uses a window that "
             "includes the 15 preceding rows, the current row, and the following 15 rows.")

# (2)
class CommunityArea(CodingProblem):
    _vars = ['trip_number_query', 'trip_number_result']
    def check(self, query, results):
        assert (type(query) == str), ("You don't have a valid query yet. Try again.")
        # check 1: query contains certain words
        assert ('rank' in query.lower()), ("There are many different numbering functions that enumerate the rows in the input. "
                                           "For this exercise, please use the **RANK()** function.")
        # check 2: correct columns selected
        assert ('pickup_community_area' in set(results.columns)), ("You didn't select the `pickup_community_area` column.")
        assert ('trip_start_timestamp' in set(results.columns)), ("You didn't select the `trip_start_timestamp` column.")
        assert ('trip_end_timestamp' in set(results.columns)), ("You didn't select the `trip_end_timestamp` column.")
        assert ('trip_number' in set(results.columns)), ("You didn't select the `trip_number` column.")
        # check 3: length of df
        assert (len(results)==len(trip_number_answer)), ("Your answer does not have the correct number of rows.")
        # check 4: specific value
        correct_number = len(trip_number_answer.loc[trip_number_answer["trip_number"]==3])
        check_number = len(results.loc[results["trip_number"]==3])
        assert (int(check_number)==int(correct_number)), ("The results don't look right. Try again.")

    _solution = CS( \
"""
trip_number_query = \"""
                    SELECT pickup_community_area,
                        trip_start_timestamp,
                        trip_end_timestamp,
                        RANK()
                            OVER (
                                  PARTITION BY pickup_community_area
                                  ORDER BY trip_start_timestamp
                                 ) AS trip_number
                    FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                    WHERE DATE(trip_start_timestamp) = '2017-05-01'
                    \"""

trip_number_result = client.query(trip_number_query).result().to_dataframe()
"""
)
    _hint = ("You don't need a **GROUP BY** clause.  Use the **RANK()** function.  Your **OVER** clause should order the rows by the "
             "`trip_start_timestamp` column and break the data into partitions based on `pickup_community_area`.")

class BreakTime(CodingProblem):
    _vars = ['break_time_query', 'break_time_result']
    def check(self, query, results):
        assert (type(query) == str), ("You don't have a valid query yet. Try again.")
        # check 1: query contains certain words
        assert ('lag' in query.lower()), ("Use the **LAG()** function to pull the value for `trip_end_timestamp` from the previous row.")
        # check 2: correct columns selected
        assert ('taxi_id' in set(results.columns)), ("You didn't select the `taxi_id` column.")
        assert ('trip_start_timestamp' in set(results.columns)), ("You didn't select the `trip_start_timestamp` column.")
        assert ('trip_end_timestamp' in set(results.columns)), ("You didn't select the `trip_end_timestamp` column.")
        assert ('prev_break' in set(results.columns)), ("You didn't select the `prev_break` column.")
        # check 3: check values, length of dataframe
        assert (len(results)==len(break_time_answer)), ("Your answer does not have the correct number of rows.")
        # check 4: specific number
        id_to_check = list(break_time_answer["taxi_id"])[0]
        correct_ans = [int(i) for i in list(break_time_answer.loc[break_time_answer["taxi_id"] == id_to_check]["prev_break"]) if math.isnan(i)==False]
        submitted_ans = [int(i) for i in list(results.loc[results["taxi_id"] == id_to_check]["prev_break"]) if math.isnan(i)==False]
        if len(correct_ans) > 0:
          assert (min(correct_ans)==min(submitted_ans)), ("The results don't look right. Try again.")
          assert (max(correct_ans)==max(submitted_ans)), ("The results don't look right. Try again.")

    _solution = CS( \
"""
break_time_query = \"""
                   SELECT taxi_id,
                       trip_start_timestamp,
                       trip_end_timestamp,
                       TIMESTAMP_DIFF(
                           trip_start_timestamp,
                           LAG(trip_end_timestamp, 1) OVER (PARTITION BY taxi_id ORDER BY trip_start_timestamp),
                           MINUTE) as prev_break
                   FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                   WHERE DATE(trip_start_timestamp) = '2017-05-01'
                   \"""

break_time_result = client.query(break_time_query).result().to_dataframe()
"""
)
    _hint = ("The `TIMESTAMP_DIFF()` function takes three arguments, where the first (`trip_start_timestamp`) and the last (`MINUTE`) "
             "are provided for you.  This function provides the time difference (in minutes) of the timestamps in the first two arguments. "
             "You need only fill in the second argument, which should use the **LAG()** function to pull the timestamp corresponding to "
             "the end of the previous trip (for the same `taxi_id`).")

qvars = bind_exercises(globals(), [
    TaxiDemand,
    CommunityArea,
    BreakTime
    ],
    tutorial_id=-1,
    var_format='q_{n}',
    )

__all__ = list(qvars)
<|endoftext|>


<commit_before>
import dbus.glib
import gobject
import sys

from account import read_account, connect

from telepathy.client.channel import Channel
from telepathy.constants import (
    CONNECTION_HANDLE_TYPE_NONE, CONNECTION_HANDLE_TYPE_CONTACT,
    CONNECTION_STATUS_CONNECTED, CONNECTION_STATUS_DISCONNECTED,
    MEDIA_STREAM_TYPE_AUDIO, MEDIA_STREAM_TYPE_VIDEO)
from telepathy.interfaces import (
    CHANNEL_INTERFACE, CHANNEL_INTERFACE_GROUP, CHANNEL_TYPE_STREAMED_MEDIA,
    CONN_INTERFACE)

def get_stream_engine():
    bus = dbus.Bus()
    return bus.get_object(
        'org.freedesktop.Telepathy.StreamEngine',
        '/org/freedesktop/Telepathy/StreamEngine')

class Call:
    def __init__(self, conn):
        self.conn = conn
        self.channel = None

        conn[CONN_INTERFACE].connect_to_signal('StatusChanged',
            self.status_changed_cb)
        conn[CONN_INTERFACE].connect_to_signal('NewChannel',
            self.new_channel_cb)

    def run(self):
        self.loop = gobject.MainLoop()

        try:
            self.loop.run()
        except KeyboardInterrupt:
            print "killed"

            if self.channel:
                print "closing channel"
                self.channel[CHANNEL_INTERFACE].Close()

    def quit(self):
        if self.loop:
            self.loop.quit()
            self.loop = None

    def status_changed_cb(self, state, reason):
        if state == CONNECTION_STATUS_DISCONNECTED:
            print 'connection closed'
            self.quit()

    def request_channel_error_cb(self, exception):
        print 'error:', exception
        self.quit()

    def new_channel_cb(self, object_path, channel_type, handle_type, handle,
            suppress_handler):
        if channel_type != CHANNEL_TYPE_STREAMED_MEDIA:
            return

        self.chan_handle_type = handle_type
        self.chan_handle = handle

        print "new streamed media channel"
        Channel(self.conn._dbus_object._named_service, object_path,
                ready_handler=self.channel_ready_cb)

    def channel_ready_cb(self, channel):
        print "channel ready"
        channel[CHANNEL_INTERFACE].connect_to_signal('Closed', self.closed_cb)
        channel[CHANNEL_INTERFACE_GROUP].connect_to_signal('MembersChanged',
            self.members_changed_cb)

        stream_engine = get_stream_engine()
        handler = dbus.Interface(stream_engine,
            'org.freedesktop.Telepathy.ChannelHandler')
        handler.HandleChannel(
            self.conn._dbus_object._named_service,
            self.conn._dbus_object._object_path,
            CHANNEL_TYPE_STREAMED_MEDIA,
            channel._dbus_object._object_path,
            self.chan_handle_type,
            self.chan_handle)

        self.channel = channel

    def closed_cb(self):
        print "channel closed"
        self.quit()

    def members_changed_cb(self, message, added, removed, local_pending,
            remote_pending, actor, reason):
        print 'MembersChanged', (
            added, removed, local_pending, remote_pending, actor, reason)

class OutgoingCall(Call):
    def __init__(self, conn, contact):
        Call.__init__(self, conn)
        self.contact = contact

    def status_changed_cb (self, state, reason):
        if state == CONNECTION_STATUS_CONNECTED:
            handle = self.conn[CONN_INTERFACE].RequestHandles(
                CONNECTION_HANDLE_TYPE_CONTACT, [self.contact])[0]
            self.handle = handle

            print 'got handle %d for %s' % (handle, self.contact)

            # hack
            import time
            time.sleep(5)

            self.conn[CONN_INTERFACE].RequestChannel(
                CHANNEL_TYPE_STREAMED_MEDIA, CONNECTION_HANDLE_TYPE_NONE,
                handle, True,
                reply_handler=lambda *stuff: None,
                error_handler=self.request_channel_error_cb)

        Call.status_changed_cb(self, state, reason)

    def channel_ready_cb(self, channel):
        Call.channel_ready_cb(self, channel)

        channel[CHANNEL_INTERFACE_GROUP].AddMembers([self.handle], "")

        print "requesting audio/video streams"

        try:
            channel[CHANNEL_TYPE_STREAMED_MEDIA].RequestStreams(
                self.handle,
                [MEDIA_STREAM_TYPE_AUDIO, MEDIA_STREAM_TYPE_VIDEO]);
        except dbus.DBusException, e:
            print "failed:", e
            print "requesting audio stream"

            try:
                channel[CHANNEL_TYPE_STREAMED_MEDIA].RequestStreams(
                    self.handle, [MEDIA_STREAM_TYPE_AUDIO]);
            except dbus.DBusException, e:
                print "failed:", e
                print "giving up"
                self.quit()

class IncomingCall(Call):
    def status_changed_cb (self, state, reason):
        if state == CONNECTION_STATUS_CONNECTED:
            print "waiting for incoming call"

        Call.status_changed_cb(self, state, reason)

    def channel_ready_cb(self, channel):
        Call.channel_ready_cb(self, channel)

        print "accepting incoming call"
        pending = channel[CHANNEL_INTERFACE_GROUP].GetLocalPendingMembers()
        channel[CHANNEL_INTERFACE_GROUP].AddMembers(pending, "")

if __name__ == '__main__':
    assert len(sys.argv) in (2, 3)
    account_file = sys.argv[1]

    manager, protocol, account = read_account(account_file)
    conn = connect(manager, protocol, account)

    if len(sys.argv) > 2:
        contact = sys.argv[2]
        call = OutgoingCall(conn, sys.argv[2])
    else:
        call = IncomingCall(conn)

    print "connecting"
    conn[CONN_INTERFACE].Connect()
    call.run()

    try:
        print "disconnecting"
        conn[CONN_INTERFACE].Disconnect()
    except dbus.DBusException:
        pass

<commit_msg>examples/call.py: call AdvertiseCapabilities when we're connected<commit_after>
import dbus.glib
import gobject
import sys

from account import read_account, connect

from telepathy.client.channel import Channel
from telepathy.constants import (
    CONNECTION_HANDLE_TYPE_NONE, CONNECTION_HANDLE_TYPE_CONTACT,
    CONNECTION_STATUS_CONNECTED, CONNECTION_STATUS_DISCONNECTED,
    MEDIA_STREAM_TYPE_AUDIO, MEDIA_STREAM_TYPE_VIDEO)
from telepathy.interfaces import (
    CHANNEL_INTERFACE, CHANNEL_INTERFACE_GROUP, CHANNEL_TYPE_STREAMED_MEDIA,
    CONN_INTERFACE, CONN_INTERFACE_CAPABILITIES)

def get_stream_engine():
    bus = dbus.Bus()
    return bus.get_object(
        'org.freedesktop.Telepathy.StreamEngine',
        '/org/freedesktop/Telepathy/StreamEngine')

class Call:
    def __init__(self, conn):
        self.conn = conn
        self.channel = None

        conn[CONN_INTERFACE].connect_to_signal('StatusChanged',
            self.status_changed_cb)
        conn[CONN_INTERFACE].connect_to_signal('NewChannel',
            self.new_channel_cb)

    def run(self):
        self.loop = gobject.MainLoop()

        try:
            self.loop.run()
        except KeyboardInterrupt:
            print "killed"

            if self.channel:
                print "closing channel"
                self.channel[CHANNEL_INTERFACE].Close()

    def quit(self):
        if self.loop:
            self.loop.quit()
            self.loop = None

    def status_changed_cb(self, state, reason):
        if state == CONNECTION_STATUS_DISCONNECTED:
            print 'connection closed'
            self.quit()

    def request_channel_error_cb(self, exception):
        print 'error:', exception
        self.quit()

    def new_channel_cb(self, object_path, channel_type, handle_type, handle,
            suppress_handler):
        if channel_type != CHANNEL_TYPE_STREAMED_MEDIA:
            return

        self.chan_handle_type = handle_type
        self.chan_handle = handle

        print "new streamed media channel"
        Channel(self.conn._dbus_object._named_service, object_path,
                ready_handler=self.channel_ready_cb)

    def channel_ready_cb(self, channel):
        print "channel ready"
        channel[CHANNEL_INTERFACE].connect_to_signal('Closed', self.closed_cb)
        channel[CHANNEL_INTERFACE_GROUP].connect_to_signal('MembersChanged',
            self.members_changed_cb)

        stream_engine = get_stream_engine()
        handler = dbus.Interface(stream_engine,
            'org.freedesktop.Telepathy.ChannelHandler')
        handler.HandleChannel(
            self.conn._dbus_object._named_service,
            self.conn._dbus_object._object_path,
            CHANNEL_TYPE_STREAMED_MEDIA,
            channel._dbus_object._object_path,
            self.chan_handle_type,
            self.chan_handle)

        self.channel = channel

    def closed_cb(self):
        print "channel closed"
        self.quit()

    def members_changed_cb(self, message, added, removed, local_pending,
            remote_pending, actor, reason):
        print 'MembersChanged', (
            added, removed, local_pending, remote_pending, actor, reason)

class OutgoingCall(Call):
    def __init__(self, conn, contact):
        Call.__init__(self, conn)
        self.contact = contact

    def status_changed_cb (self, state, reason):
        if state == CONNECTION_STATUS_CONNECTED:
            handle = self.conn[CONN_INTERFACE].RequestHandles(
                CONNECTION_HANDLE_TYPE_CONTACT, [self.contact])[0]
            self.handle = handle

            print 'got handle %d for %s' % (handle, self.contact)

            # hack
            import time
            time.sleep(5)

            self.conn[CONN_INTERFACE].RequestChannel(
                CHANNEL_TYPE_STREAMED_MEDIA, CONNECTION_HANDLE_TYPE_NONE,
                handle, True,
                reply_handler=lambda *stuff: None,
                error_handler=self.request_channel_error_cb)

        Call.status_changed_cb(self, state, reason)

    def channel_ready_cb(self, channel):
        Call.channel_ready_cb(self, channel)

        channel[CHANNEL_INTERFACE_GROUP].AddMembers([self.handle], "")

        print "requesting audio/video streams"

        try:
            channel[CHANNEL_TYPE_STREAMED_MEDIA].RequestStreams(
                self.handle,
                [MEDIA_STREAM_TYPE_AUDIO, MEDIA_STREAM_TYPE_VIDEO]);
        except dbus.DBusException, e:
            print "failed:", e
            print "requesting audio stream"

            try:
                channel[CHANNEL_TYPE_STREAMED_MEDIA].RequestStreams(
                    self.handle, [MEDIA_STREAM_TYPE_AUDIO]);
            except dbus.DBusException, e:
                print "failed:", e
                print "giving up"
                self.quit()

class IncomingCall(Call):
    def status_changed_cb (self, state, reason):
        if state == CONNECTION_STATUS_CONNECTED:
            print "waiting for incoming call"

            self.conn._valid_interfaces.add(CONN_INTERFACE_CAPABILITIES)
            self.conn[CONN_INTERFACE_CAPABILITIES].AdvertiseCapabilities(
                [(CHANNEL_TYPE_STREAMED_MEDIA, 3)], [])

        Call.status_changed_cb(self, state, reason)

    def channel_ready_cb(self, channel):
        Call.channel_ready_cb(self, channel)

        print "accepting incoming call"
        pending = channel[CHANNEL_INTERFACE_GROUP].GetLocalPendingMembers()
        channel[CHANNEL_INTERFACE_GROUP].AddMembers(pending, "")

if __name__ == '__main__':
    assert len(sys.argv) in (2, 3)
    account_file = sys.argv[1]

    manager, protocol, account = read_account(account_file)
    conn = connect(manager, protocol, account)

    if len(sys.argv) > 2:
        contact = sys.argv[2]
        call = OutgoingCall(conn, sys.argv[2])
    else:
        call = IncomingCall(conn)

    print "connecting"
    conn[CONN_INTERFACE].Connect()
    call.run()

    try:
        print "disconnecting"
        conn[CONN_INTERFACE].Disconnect()
    except dbus.DBusException:
        pass

<|endoftext|>


<commit_before>from __future__ import absolute_import, division, print_function, unicode_literals

from graphics.pi3d import pi3d

from util import Openable

class Pi3dDisplay(Openable.Openable):
  def __init__(self, config):
    Openable.Openable.__init__(self)
    self.config = config
<commit_msg>Tiny changes to Pi3dDisplay<commit_after>from __future__ import absolute_import, division, print_function, unicode_literals

from graphics.pi3d import pi3d

from util import Openable

class Pi3dDisplay(Openable.Openable):
  def __init__(self, config):
    Openable.Openable.__init__(self)
    self.config = config
    self.display = pi3d.display()
    display.create2D(x, y, width, height)
    display.setBackColor(0, 0, 0, 0)  # last is alpha

  def run(self):
    pass
<|endoftext|>


<commit_before><commit_msg>bump version: 0.3.1<commit_after><|endoftext|>


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

## Definition of a path planning problem
#
#  This class wraps the Corba client to the server implemented by
#  libhpp-corbaserver.so
#
#  Some method implemented by the server can be considered as private. The
#  goal of this class is to hide them and to expose those that can be
#  considered as public.
class ProblemSolver (object):
    def __init__ (self, robot):
        self.client = robot.client
        self.robot = robot

    ## \name Initial and goal configurations
    # \{

    ## Set initial configuration of specified problem.
    #	\param dofArray Array of degrees of freedom
    #	\throw Error.
    def setInitialConfig (self, dofArray):
        return self.client.problem.setInitialConfig (dofArray)

    ## Get initial configuration of specified problem.
    #	\return Array of degrees of freedom
    def getInitialConfig (self):
        return self.client.problem.getInitialConfig ()

    ## Add goal configuration to specified problem.
    #	\param dofArray Array of degrees of freedom
    #	\throw Error.
    def addGoalConfig (self, dofArray):
        return self.client.problem.addGoalConfig (dofArray)

    ## Get goal configurations of specified problem.
    #	\return Array of degrees of freedom
    def getGoalConfigs (self):
        return self.client.problem.getGoalConfigs ()

    ## Reset goal configurations
    def resetGoalConfigs (self):
        return self.client.problem.resetGoalConfigs ()
    ## \}

    ## \name Obstacles
    # \{

    ## Load obstacle from urdf file
    #  \param package Name of the package containing the model,
    #  \param filename name of the urdf file in the package
    #         (without suffix .urdf)
    #  \param prefix prefix added to object names in case the same file is
    #         loaded several times
    #
    #  The ros url is built as follows:
    #  "package://${package}/urdf/${filename}.urdf"
    #
    #  The kinematic structure of the urdf file is ignored. Only the geometric
    #  objects are loaded as obstacles.
    def loadObstacleFromUrdf (self, package, filename, prefix):
        return self.client.obstacle.loadObstacleModel (package, filename,
                                                       prefix)

    ## Remove an obstacle from outer objects of a joint body
    #
    #  \param objectName name of the object to remove,
    #  \param jointName name of the joint owning the body,
    #  \param collision whether collision with object should be computed,
    #  \param distance whether distance to object should be computed.
    #  \throw Error.
    def removeObstacleFromJoint (self, objectName, jointName, collision,
                                 distance):
        return self.client.obstacle.removeObstacleFromJoint \
            (objectName, jointName, collision, distance)

    ## Move an obstacle to a given configuration.
    #  \param objectName name of the polyhedron.
    #  \param cfg the configuration of the obstacle.
    #  \throw Error.
    #
    #  \note The obstacle is not added to local map
    #  impl::Obstacle::collisionListMap.
    #
    #  \note Build the collision entity of polyhedron for KCD.
    def moveObstacle (self, objectName, cfg):
        return self.client.obstacle.moveObstacle (objectName, cfg)
    ## Get the position of an obstacle
    #
    #  \param objectName name of the polyhedron.
    #  \retval cfg Position of the obstacle.
    #  \throw Error.
    def getObstaclePosition (self, objectName):
        return self.client.obstacle.getObstaclePosition (objectName)

    ## Get list of obstacles
    #
    #  \param collision whether to return obstacle for collision,
    #  \param distance whether to return obstacles for distance computation
    # \return list of obstacles
    def getObstacleNames (self, collision, distance):
        return self.client.obstacle.getObstacleNames (collision, distance)

    ##\}

    ## \name Constraints
    #  \{

    ## Create orientation constraint between two joints
    #
    #  \param constraintName name of the constraint created,
    #  \param joint1Name name of first joint
    #  \param joint2Name name of second joint
    #  \param p quaternion representing the desired orientation
    #         of joint2 in the frame of joint1.
    #  \param mask Select which axis to be constrained.
    #  If joint1 or joint2 is "", the corresponding joint is replaced by
    #  the global frame.
    #  constraints are stored in ProblemSolver object
    def createOrientationConstraint (self, constraintName, joint1Name,
                                     joint2Name, p, mask):
        return self.client.problem.createOrientationConstraint \
            (constraintName, joint1Name, joint2Name, p, mask)

    ## Create ComBeetweenFeet constraint between two joints
    #
    #  \param constraintName name of the constraint created,
    #  \param comName name of CenterOfMassComputation
    #  \param jointLName name of first joint
    #  \param jointRName name of second joint
    #  \param pointL point in local frame of jointL.
    #  \param pointR point in local frame of jointR.
    #  \param jointRefName name of second joint
    #  \param mask Select axis to be constrained.
    #  If jointRef is "", the robot root joint is used.
    #  Constraints are stored in ProblemSolver object
    def createComBeetweenFeet (self, constraintName, comName, jointLName, jointRName,
        pointL, pointR, jointRefName, mask):
        return self.client.problem.createComBeetweenFeet \
            (constraintName, comName, jointLName, jointRName, pointL, pointR, jointRefName, mask)

    ## Create position constraint between two joints
    #
    #  \param constraintName name of the constraint created,
    #  \param joint1Name name of first joint
    #  \param joint2Name name of second joint
    #  \param point1 point in local frame of joint1,
    #  \param point2 point in local frame of joint2.
    #  \param mask Select which axis to be constrained.
    #  If joint1 of joint2 is "", the corresponding joint is replaced by
    #  the global frame.
    #  constraints are stored in ProblemSolver object
    def createPositionConstraint (self, constraintName, joint1Name,
                                  joint2Name, point1, point2, mask):
        return self.client.problem.createPositionConstraint \
            (constraintName, joint1Name, joint2Name, point1, point2, mask)

    ## Reset Constraints
    #
    #  Reset all constraints, including numerical constraints and locked
    #  joints
    def resetConstraints (self):
        return self.client.problem.resetConstraints ()

    ## Set numerical constraints in ConfigProjector
    #
    #  \param name name of the resulting numerical constraint obtained
    #         by stacking elementary numerical constraints,
    #  \param names list of names of the numerical constraints as
    #         inserted by method hpp::core::ProblemSolver::addNumericalConstraint.
    def setNumericalConstraints (self, name, names):
        return self.client.problem.setNumericalConstraints (name, names)

    ## Apply constraints
    #
    #  \param q initial configuration
    #  \return configuration projected in success,
    #  \throw Error if projection failed.
    def applyConstraints (self, q):
        return self.client.problem.applyConstraints (q)

    ## Create a vector of passive dofs.
    #
    #  \param name name of the vector in the ProblemSolver map.
    #  \param dofNames list of names of DOF that may
    def addPassiveDofs (self, name, dofNames):
        return self.client.problem.addPassiveDofs (name, dofNames)

    ## Generate a configuration satisfying the constraints
    #
    #  \param maxIter maximum number of tries,
    #  \return configuration projected in success,
    #  \throw Error if projection failed.
    def generateValidConfig (self, maxIter):
        return self.client.problem.generateValidConfig (maxIter)

    ## Lock joint with given joint configuration
    # \param jointName name of the joint
    # \param value value of the joint configuration
    def lockJoint (self, jointName, value):
        return self.client.problem.lockJoint (jointName, value)

    ## error threshold in numerical constraint resolution
    def setErrorThreshold (self, threshold):
        return self.client.problem.setErrorThreshold (threshold)

    ## Set the maximal number of iterations
    def setMaxIterations (self, iterations):
	return self.client.problem.setMaxIterations (iterations)

    ## \}

    ## \name Solve problem and get paths
    # \{

    ## Select path planner type
    #  \param Name of the path planner type, either "DiffusingPlanner",
    #         "VisibilityPrmPlanner", or any type added by method
    #         core::ProblemSolver::addPathPlannerType
    def selectPathPlanner (self, pathPlannerType):
        return self.client.problem.selectPathPlanner (pathPlannerType)

    ## Select path optimizer type
    #  \param Name of the path optimizer type, either "RandomShortcut" or
    #   any type added by core::ProblemSolver::addPathOptimizerType
    def selectPathOptimizer (self, pathOptimizerType):
        return self.client.problem.selectPathOptimizer (pathOptimizerType)

    ## Select path validation method
    #  \param Name of the path validation method, either "Discretized"
    #  "Progressive", "Dichotomy", or any type added by
    #  core::ProblemSolver::addPathValidationType,
    #  \param tolerance maximal acceptable penetration.
    def selectPathValidation (self, pathValidationType, tolerance):
        return self.client.problem.selectPathValidation (pathValidationType,
                                                         tolerance)

    ## Select path projector method
    #  \param Name of the path projector method, either "Discretized"
    #  "Progressive", "Dichotomy", or any type added by
    #  core::ProblemSolver::addPathProjectorType,
    #  \param tolerance maximal acceptable penetration.
    def selectPathProjector (self, pathProjectorType, tolerance):
        return self.client.problem.selectPathProjector (pathProjectorType,
                                                        tolerance)

    ## Solve the problem of corresponding ChppPlanner object
    def solve (self):
        return self.client.problem.solve ()

    ## Make direct connection between two configurations
    #  \param startConfig, endConfig: the configurations to link.
    #  \throw Error if steering method fails to create a direct path of if
    #  direct path is not valid
    def directPath (self, startConfig, endConfig):
        return self.client.problem.directPath (startConfig, endConfig)

    ## Get Number of paths
    def numberPaths (self):
        return self.client.problem.numberPaths ()

    ## Optimize a given path
    # \param inPathId Id of the path in this problem.
    # \throw Error.
    def optimizePath(self, inPathId):
        return self.client.problem.optimizePath (inPathId)

    ## Get length of path
    # \param inPathId rank of the path in the problem
    # \return length of path if path exists.
    def pathLength(self, inPathId):
        return self.client.problem.pathLength(inPathId)

    ## Get the robot's config at param on the a path
    # \param inPathId rank of the path in the problem
    # \param atDistance : the user parameter choice
    # \return dofseq : the config at param
    def configAtParam (self, inPathId, atDistance):
        return self.client.problem.configAtParam (inPathId, atDistance)

    ## Get way points of a path
    #  \param pathId rank of the path in the problem
    def getWaypoints (self, pathId):
        return self.client.problem.getWaypoints (pathId)

    ## \name Interruption of a path planning request
    #  \{

    ## \brief Interrupt path planning activity
    #   \note this method is effective only when multi-thread policy is used
    #         by CORBA server.
    #         See constructor of class Server for details.
    def interruptPathPlanning (self):
        return self.client.problem.interruptPathPlanning ()
    # \}

    ## \name exploring the roadmap
    #  \{

    ## Get nodes of the roadmap.
    def nodes(self):
	return self.client.problem.nodes ()

    ## Number of edges
    def numberEdges (self):
        return self.client.problem.numberEdges ()

    ## Edge at given rank
    def edge (self, edgeId):
        return self.client.problem.edge (edgeId)

    ## Number of connected components
    def numberConnectedComponents (self):
        return self.client.problem.numberConnectedComponents ()

    ## Nodes of a connected component
    #  \param connectedComponentId index of connected component in roadmap
    #  \return list of nodes of the connected component.
    def nodesConnectedComponent (self, ccId):
        return self.client.problem.nodesConnectedComponent (ccId)

    ## Clear the roadmap
    def clearRoadmap (self):
        return self.client.problem.clearRoadmap ()
    ## \}
<commit_msg>Update python class ProblemSolver<commit_after>#!/usr/bin/env python
# Copyright (c) 2014 CNRS
# Author: Florent Lamiraux
#
# This file is part of hpp-corbaserver.
# hpp-corbaserver is free software: you can redistribute it
# and/or modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, either version
# 3 of the License, or (at your option) any later version.
#
# hpp-corbaserver is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Lesser Public License for more details.  You should have
# received a copy of the GNU Lesser General Public License along with
# hpp-corbaserver.  If not, see
# <http://www.gnu.org/licenses/>.

## Definition of a path planning problem
#
#  This class wraps the Corba client to the server implemented by
#  libhpp-corbaserver.so
#
#  Some method implemented by the server can be considered as private. The
#  goal of this class is to hide them and to expose those that can be
#  considered as public.
class ProblemSolver (object):
    def __init__ (self, robot):
        self.client = robot.client
        self.robot = robot

    ## \name Initial and goal configurations
    # \{

    ## Set initial configuration of specified problem.
    #	\param dofArray Array of degrees of freedom
    #	\throw Error.
    def setInitialConfig (self, dofArray):
        return self.client.problem.setInitialConfig (dofArray)

    ## Get initial configuration of specified problem.
    #	\return Array of degrees of freedom
    def getInitialConfig (self):
        return self.client.problem.getInitialConfig ()

    ## Add goal configuration to specified problem.
    #	\param dofArray Array of degrees of freedom
    #	\throw Error.
    def addGoalConfig (self, dofArray):
        return self.client.problem.addGoalConfig (dofArray)

    ## Get goal configurations of specified problem.
    #	\return Array of degrees of freedom
    def getGoalConfigs (self):
        return self.client.problem.getGoalConfigs ()

    ## Reset goal configurations
    def resetGoalConfigs (self):
        return self.client.problem.resetGoalConfigs ()
    ## \}

    ## \name Obstacles
    # \{

    ## Load obstacle from urdf file
    #  \param package Name of the package containing the model,
    #  \param filename name of the urdf file in the package
    #         (without suffix .urdf)
    #  \param prefix prefix added to object names in case the same file is
    #         loaded several times
    #
    #  The ros url is built as follows:
    #  "package://${package}/urdf/${filename}.urdf"
    #
    #  The kinematic structure of the urdf file is ignored. Only the geometric
    #  objects are loaded as obstacles.
    def loadObstacleFromUrdf (self, package, filename, prefix):
        return self.client.obstacle.loadObstacleModel (package, filename,
                                                       prefix)

    ## Remove an obstacle from outer objects of a joint body
    #
    #  \param objectName name of the object to remove,
    #  \param jointName name of the joint owning the body,
    #  \param collision whether collision with object should be computed,
    #  \param distance whether distance to object should be computed.
    #  \throw Error.
    def removeObstacleFromJoint (self, objectName, jointName, collision,
                                 distance):
        return self.client.obstacle.removeObstacleFromJoint \
            (objectName, jointName, collision, distance)

    ## Move an obstacle to a given configuration.
    #  \param objectName name of the polyhedron.
    #  \param cfg the configuration of the obstacle.
    #  \throw Error.
    #
    #  \note The obstacle is not added to local map
    #  impl::Obstacle::collisionListMap.
    #
    #  \note Build the collision entity of polyhedron for KCD.
    def moveObstacle (self, objectName, cfg):
        return self.client.obstacle.moveObstacle (objectName, cfg)
    ## Get the position of an obstacle
    #
    #  \param objectName name of the polyhedron.
    #  \retval cfg Position of the obstacle.
    #  \throw Error.
    def getObstaclePosition (self, objectName):
        return self.client.obstacle.getObstaclePosition (objectName)

    ## Get list of obstacles
    #
    #  \param collision whether to return obstacle for collision,
    #  \param distance whether to return obstacles for distance computation
    # \return list of obstacles
    def getObstacleNames (self, collision, distance):
        return self.client.obstacle.getObstacleNames (collision, distance)

    ##\}

    ## \name Constraints
    #  \{

    ## Create orientation constraint between two joints
    #
    #  \param constraintName name of the constraint created,
    #  \param joint1Name name of first joint
    #  \param joint2Name name of second joint
    #  \param p quaternion representing the desired orientation
    #         of joint2 in the frame of joint1.
    #  \param mask Select which axis to be constrained.
    #  If joint1 or joint2 is "", the corresponding joint is replaced by
    #  the global frame.
    #  constraints are stored in ProblemSolver object
    def createOrientationConstraint (self, constraintName, joint1Name,
                                     joint2Name, p, mask):
        return self.client.problem.createOrientationConstraint \
            (constraintName, joint1Name, joint2Name, p, mask)

    ## Create RelativeCom constraint between two joints
    #
    #  \param constraintName name of the constraint created,
    #  \param comName name of CenterOfMassComputation
    #  \param jointName name of joint
    #  \param point point in local frame of joint.
    #  \param mask Select axis to be constrained.
    #  If jointName is "", the robot root joint is used.
    #  Constraints are stored in ProblemSolver object
    def createRelativeComConstraint (self, constraintName, comName, jointLName, point, mask):
        return self.client.problem.createRelativeComConstraint \
            (constraintName, comName, jointLName, point, mask)

    ## Create ComBeetweenFeet constraint between two joints
    #
    #  \param constraintName name of the constraint created,
    #  \param comName name of CenterOfMassComputation
    #  \param jointLName name of first joint
    #  \param jointRName name of second joint
    #  \param pointL point in local frame of jointL.
    #  \param pointR point in local frame of jointR.
    #  \param jointRefName name of second joint
    #  \param mask Select axis to be constrained.
    #  If jointRef is "", the robot root joint is used.
    #  Constraints are stored in ProblemSolver object
    def createComBeetweenFeet (self, constraintName, comName, jointLName, jointRName,
        pointL, pointR, jointRefName, mask):
        return self.client.problem.createComBeetweenFeet \
            (constraintName, comName, jointLName, jointRName, pointL, pointR, jointRefName, mask)

    ## Add an object to compute a partial COM of the robot.
    # \param name of the partial com
    # \param jointNames list of joint name of each tree ROOT to consider.
    # \note Joints are added recursively, it is not possible so far to add a
    # joint without addind all its children.
    def addPartialCom (self, comName, jointNames):
        return self.client.robot.addPartialCom (comName, jointNames);

    ## Create position constraint between two joints
    #
    #  \param constraintName name of the constraint created,
    #  \param joint1Name name of first joint
    #  \param joint2Name name of second joint
    #  \param point1 point in local frame of joint1,
    #  \param point2 point in local frame of joint2.
    #  \param mask Select which axis to be constrained.
    #  If joint1 of joint2 is "", the corresponding joint is replaced by
    #  the global frame.
    #  constraints are stored in ProblemSolver object
    def createPositionConstraint (self, constraintName, joint1Name,
                                  joint2Name, point1, point2, mask):
        return self.client.problem.createPositionConstraint \
            (constraintName, joint1Name, joint2Name, point1, point2, mask)

    ## Reset Constraints
    #
    #  Reset all constraints, including numerical constraints and locked
    #  joints
    def resetConstraints (self):
        return self.client.problem.resetConstraints ()

    ## Set numerical constraints in ConfigProjector
    #
    #  \param name name of the resulting numerical constraint obtained
    #         by stacking elementary numerical constraints,
    #  \param names list of names of the numerical constraints as
    #         inserted by method hpp::core::ProblemSolver::addNumericalConstraint.
    def setNumericalConstraints (self, name, names):
        return self.client.problem.setNumericalConstraints (name, names)

    ## Apply constraints
    #
    #  \param q initial configuration
    #  \return configuration projected in success,
    #  \throw Error if projection failed.
    def applyConstraints (self, q):
        return self.client.problem.applyConstraints (q)

    ## Create a vector of passive dofs.
    #
    #  \param name name of the vector in the ProblemSolver map.
    #  \param dofNames list of names of DOF that may
    def addPassiveDofs (self, name, dofNames):
        return self.client.problem.addPassiveDofs (name, dofNames)

    ## Generate a configuration satisfying the constraints
    #
    #  \param maxIter maximum number of tries,
    #  \return configuration projected in success,
    #  \throw Error if projection failed.
    def generateValidConfig (self, maxIter):
        return self.client.problem.generateValidConfig (maxIter)

    ## Lock joint with given joint configuration
    # \param jointName name of the joint
    # \param value value of the joint configuration
    def lockJoint (self, jointName, value):
        return self.client.problem.lockJoint (jointName, value)

    ## error threshold in numerical constraint resolution
    def setErrorThreshold (self, threshold):
        return self.client.problem.setErrorThreshold (threshold)

    ## Set the maximal number of iterations
    def setMaxIterations (self, iterations):
	return self.client.problem.setMaxIterations (iterations)

    ## \}

    ## \name Solve problem and get paths
    # \{

    ## Select path planner type
    #  \param Name of the path planner type, either "DiffusingPlanner",
    #         "VisibilityPrmPlanner", or any type added by method
    #         core::ProblemSolver::addPathPlannerType
    def selectPathPlanner (self, pathPlannerType):
        return self.client.problem.selectPathPlanner (pathPlannerType)

    ## Select path optimizer type
    #  \param Name of the path optimizer type, either "RandomShortcut" or
    #   any type added by core::ProblemSolver::addPathOptimizerType
    def selectPathOptimizer (self, pathOptimizerType):
        return self.client.problem.selectPathOptimizer (pathOptimizerType)

    ## Select path validation method
    #  \param Name of the path validation method, either "Discretized"
    #  "Progressive", "Dichotomy", or any type added by
    #  core::ProblemSolver::addPathValidationType,
    #  \param tolerance maximal acceptable penetration.
    def selectPathValidation (self, pathValidationType, tolerance):
        return self.client.problem.selectPathValidation (pathValidationType,
                                                         tolerance)

    ## Select path projector method
    #  \param Name of the path projector method, either "Discretized"
    #  "Progressive", "Dichotomy", or any type added by
    #  core::ProblemSolver::addPathProjectorType,
    #  \param tolerance maximal acceptable penetration.
    def selectPathProjector (self, pathProjectorType, tolerance):
        return self.client.problem.selectPathProjector (pathProjectorType,
                                                        tolerance)

    ## Solve the problem of corresponding ChppPlanner object
    def solve (self):
        return self.client.problem.solve ()

    ## Make direct connection between two configurations
    #  \param startConfig, endConfig: the configurations to link.
    #  \throw Error if steering method fails to create a direct path of if
    #  direct path is not valid
    def directPath (self, startConfig, endConfig):
        return self.client.problem.directPath (startConfig, endConfig)

    ## Get Number of paths
    def numberPaths (self):
        return self.client.problem.numberPaths ()

    ## Optimize a given path
    # \param inPathId Id of the path in this problem.
    # \throw Error.
    def optimizePath(self, inPathId):
        return self.client.problem.optimizePath (inPathId)

    ## Get length of path
    # \param inPathId rank of the path in the problem
    # \return length of path if path exists.
    def pathLength(self, inPathId):
        return self.client.problem.pathLength(inPathId)

    ## Get the robot's config at param on the a path
    # \param inPathId rank of the path in the problem
    # \param atDistance : the user parameter choice
    # \return dofseq : the config at param
    def configAtParam (self, inPathId, atDistance):
        return self.client.problem.configAtParam (inPathId, atDistance)

    ## Get way points of a path
    #  \param pathId rank of the path in the problem
    def getWaypoints (self, pathId):
        return self.client.problem.getWaypoints (pathId)

    ## \name Interruption of a path planning request
    #  \{

    ## \brief Interrupt path planning activity
    #   \note this method is effective only when multi-thread policy is used
    #         by CORBA server.
    #         See constructor of class Server for details.
    def interruptPathPlanning (self):
        return self.client.problem.interruptPathPlanning ()
    # \}

    ## \name exploring the roadmap
    #  \{

    ## Get nodes of the roadmap.
    def nodes(self):
	return self.client.problem.nodes ()

    ## Number of edges
    def numberEdges (self):
        return self.client.problem.numberEdges ()

    ## Edge at given rank
    def edge (self, edgeId):
        return self.client.problem.edge (edgeId)

    ## Number of connected components
    def numberConnectedComponents (self):
        return self.client.problem.numberConnectedComponents ()

    ## Nodes of a connected component
    #  \param connectedComponentId index of connected component in roadmap
    #  \return list of nodes of the connected component.
    def nodesConnectedComponent (self, ccId):
        return self.client.problem.nodesConnectedComponent (ccId)

    ## Clear the roadmap
    def clearRoadmap (self):
        return self.client.problem.clearRoadmap ()
    ## \}
<|endoftext|>


<commit_before>from django.db import connection, transaction

def run():
    cursor = connection.cursor()
    cursor.execute('select activity_log_id from log_activity_app;')
    ids = [r[0] for r in cursor.fetchall()]

    cursor.execute('insert into log_activity_mkt '
                   'select * from log_activity where id IN %(ids)s;',
                   {'ids':ids})
    cursor.execute('insert into log_activity_app_mkt '
                   'select * from log_activity_app;')
    transaction.commit()
<commit_msg>turn off foreign_key_checks<commit_after>from django.db import connection, transaction

def run():
    cursor = connection.cursor()
    cursor.execute('select activity_log_id from log_activity_app;')
    ids = [r[0] for r in cursor.fetchall()]

    cursor.execute('set foreign_key_checks = 0')
    cursor.execute('insert into log_activity_mkt '
                   'select * from log_activity where id IN %(ids)s;',
                   {'ids':ids})
    cursor.execute('insert into log_activity_app_mkt '
                   'select * from log_activity_app;')
    cursor.execute('set foreign_key_checks = 1')
    transaction.commit()
<|endoftext|>


<commit_before>import json
import time

import pytest

from netdumplings import Dumpling, DumplingDriver
from netdumplings.exceptions import InvalidDumplingPayload

@pytest.fixture
def mock_kitchen(mocker):
    kitchen = mocker.Mock()
    kitchen.name = 'TestKitchen'
    kitchen.interface = 'en0'
    kitchen.filter = 'tcp'
    kitchen.chef_poke_interval = 5

    return kitchen

@pytest.fixture
def mock_chef(mocker, mock_kitchen):
    chef = mocker.Mock()
    chef.name = 'TestChef'
    chef.kitchen = mock_kitchen

    return chef

class TestDumpling:
    """
    Test the Dumping class.
    """
    def test_init_default(self):
        """
        Test default Dumpling initialization.
        """
        dumpling = Dumpling()

        assert dumpling.chef is None
        assert dumpling.chef_name is None
        assert dumpling.kitchen is None
        assert dumpling.driver is DumplingDriver.packet
        assert dumpling.payload is None

    def test_init_with_chef_instance(self, mock_chef, mock_kitchen):
        """
        Test initialization of a Dumpling with a DumplingChef instance.
        """
        dumpling = Dumpling(chef=mock_chef)

        assert dumpling.chef is mock_chef
        assert dumpling.chef_name == 'TestChef'
        assert dumpling.kitchen is mock_kitchen
        assert dumpling.driver is DumplingDriver.packet
        assert dumpling.payload is None

    def test_init_with_chef_string(self):
        """
        Test initialization of a Dumpling with a chef string.
        """
        dumpling = Dumpling(chef='test_chef')

        assert dumpling.chef == 'test_chef'
        assert dumpling.chef_name == 'test_chef'
        assert dumpling.kitchen is None
        assert dumpling.driver is DumplingDriver.packet
        assert dumpling.payload is None

    def test_callable(self, mocker, mock_chef):
        """
        Test Dumpling instance is callable and returns the same as make().
        """
        mocker.patch.object(time, 'time')
        time.time.return_value = 'test_timestamp'

        dumpling = Dumpling(chef=mock_chef)
        assert dumpling() == dumpling.make()

    def test_metadata(self, mocker, mock_chef):
        """
        Test metadata keys in the make() result.
        """
        mocker.patch.object(time, 'time')
        time.time.return_value = 'test_timestamp'

        dumpling = Dumpling(chef=mock_chef)
        data = json.loads(dumpling.make())
        metadata = data['metadata']

        assert metadata['chef'] == mock_chef.name
        assert metadata['kitchen'] == mock_chef.kitchen.name
        assert metadata['driver'] == 'packet'
        assert metadata['creation_time'] == 'test_timestamp'

    def test_make_payload_string(self, mock_chef):
        """
        Test string payload in the make() result.
        """
        dumpling = Dumpling(chef=mock_chef, payload='test payload')
        data = json.loads(dumpling.make())

        assert data['payload'] == 'test payload'

    def test_make_payload_list(self, mock_chef):
        """
        Test list payload in the make() result.
        """
        test_payload = list(range(10))
        dumpling = Dumpling(chef=mock_chef, payload=test_payload)
        data = json.loads(dumpling.make())

        assert data['payload'] == test_payload

    def test_make_payload_dict(self, mock_chef):
        """
        Test dict payload in the make() result.
        """
        test_payload = {
            'one': 1,
            'two': [1, 2, 3],
            'three': {
                'four': 5,
            }
        }
        dumpling = Dumpling(chef=mock_chef, payload=test_payload)
        data = json.loads(dumpling.make())

        assert data['payload'] == test_payload

    def test_unserializable_payload(self, mock_chef):
        """
        Test an unserializable Dumpling payload.
        """
        dumpling = Dumpling(chef=mock_chef, payload=lambda x: x)

        with pytest.raises(InvalidDumplingPayload):
            dumpling.make()
<commit_msg>Fix doc typo<commit_after>import json
import time

import pytest

from netdumplings import Dumpling, DumplingDriver
from netdumplings.exceptions import InvalidDumplingPayload

@pytest.fixture
def mock_kitchen(mocker):
    kitchen = mocker.Mock()
    kitchen.name = 'TestKitchen'
    kitchen.interface = 'en0'
    kitchen.filter = 'tcp'
    kitchen.chef_poke_interval = 5

    return kitchen

@pytest.fixture
def mock_chef(mocker, mock_kitchen):
    chef = mocker.Mock()
    chef.name = 'TestChef'
    chef.kitchen = mock_kitchen

    return chef

class TestDumpling:
    """
    Test the Dumpling class.
    """
    def test_init_default(self):
        """
        Test default Dumpling initialization.
        """
        dumpling = Dumpling()

        assert dumpling.chef is None
        assert dumpling.chef_name is None
        assert dumpling.kitchen is None
        assert dumpling.driver is DumplingDriver.packet
        assert dumpling.payload is None

    def test_init_with_chef_instance(self, mock_chef, mock_kitchen):
        """
        Test initialization of a Dumpling with a DumplingChef instance.
        """
        dumpling = Dumpling(chef=mock_chef)

        assert dumpling.chef is mock_chef
        assert dumpling.chef_name == 'TestChef'
        assert dumpling.kitchen is mock_kitchen
        assert dumpling.driver is DumplingDriver.packet
        assert dumpling.payload is None

    def test_init_with_chef_string(self):
        """
        Test initialization of a Dumpling with a chef string.
        """
        dumpling = Dumpling(chef='test_chef')

        assert dumpling.chef == 'test_chef'
        assert dumpling.chef_name == 'test_chef'
        assert dumpling.kitchen is None
        assert dumpling.driver is DumplingDriver.packet
        assert dumpling.payload is None

    def test_callable(self, mocker, mock_chef):
        """
        Test Dumpling instance is callable and returns the same as make().
        """
        mocker.patch.object(time, 'time')
        time.time.return_value = 'test_timestamp'

        dumpling = Dumpling(chef=mock_chef)
        assert dumpling() == dumpling.make()

    def test_metadata(self, mocker, mock_chef):
        """
        Test metadata keys in the make() result.
        """
        mocker.patch.object(time, 'time')
        time.time.return_value = 'test_timestamp'

        dumpling = Dumpling(chef=mock_chef)
        data = json.loads(dumpling.make())
        metadata = data['metadata']

        assert metadata['chef'] == mock_chef.name
        assert metadata['kitchen'] == mock_chef.kitchen.name
        assert metadata['driver'] == 'packet'
        assert metadata['creation_time'] == 'test_timestamp'

    def test_make_payload_string(self, mock_chef):
        """
        Test string payload in the make() result.
        """
        dumpling = Dumpling(chef=mock_chef, payload='test payload')
        data = json.loads(dumpling.make())

        assert data['payload'] == 'test payload'

    def test_make_payload_list(self, mock_chef):
        """
        Test list payload in the make() result.
        """
        test_payload = list(range(10))
        dumpling = Dumpling(chef=mock_chef, payload=test_payload)
        data = json.loads(dumpling.make())

        assert data['payload'] == test_payload

    def test_make_payload_dict(self, mock_chef):
        """
        Test dict payload in the make() result.
        """
        test_payload = {
            'one': 1,
            'two': [1, 2, 3],
            'three': {
                'four': 5,
            }
        }
        dumpling = Dumpling(chef=mock_chef, payload=test_payload)
        data = json.loads(dumpling.make())

        assert data['payload'] == test_payload

    def test_unserializable_payload(self, mock_chef):
        """
        Test an unserializable Dumpling payload.
        """
        dumpling = Dumpling(chef=mock_chef, payload=lambda x: x)

        with pytest.raises(InvalidDumplingPayload):
            dumpling.make()
<|endoftext|>


<commit_before>import warnings
import urllib2
import tempfile
import string
from xml.etree.ElementTree import ElementTree

'''

API from

 http://irsa.ipac.caltech.edu/applications/Gator/GatorAid/irsa/catsearch.html

The URL of the IRSA catalog query service, CatQuery, is

 http://irsa.ipac.caltech.edu/cgi-bin/Gator/nph-query

The service accepts the following keywords, which are analogous to the search
fields on the Gator search form:

spatial     Required    Type of spatial query: Cone, Box, Polygon, and NONE

polygon                 Convex polygon of ra dec pairs, separated by comma(,)
                        Required if spatial=polygon

radius                  Cone search radius
                        Optional if spatial=Cone, otherwise ignore it
                        (default 10 arcsec)

radunits                Units of a Cone search: arcsec, arcmin, deg.
                        Optional if spatial=Cone
                        (default='arcsec')

size                    Width of a box in arcsec
                        Required if spatial=Box.

objstr                  Target name or coordinate of the center of a spatial
                        search center. Target names must be resolved by
                        SIMBAD or NED.

                        Required only when spatial=Cone or spatial=Box.

                        Examples: 'M31'
                                  '00 42 44.3 -41 16 08'
                                  '00h42m44.3s -41d16m08s'

catalog     Required    Catalog name in the IRSA database management system.

outfmt      Optional    Defines query's output format.
                        6 - returns a program interface in XML
                        3 - returns a VO Table (XML)
                        2 - returns SVC message
                        1 - returns an ASCII table
                        0 - returns Gator Status Page in HTML (default)

desc        Optional    Short description of a specific catalog, which will
                        appear in the result page.

order       Optional    Results ordered by this column.

constraint  Optional    User defined query constraint(s)
                        Note: The constraint should follow SQL syntax.

onlist      Optional    1 - catalog is visible through Gator web interface
                        (default)

                        0 - catalog has been ingested into IRSA but not yet
                        visible through web interface.

                        This parameter will generally only be set to 0 when
                        users are supporting testing and evaluation of new
                        catalogs at IRSA's request.

If onlist=0, the following parameters are required:

    server              Symbolic DataBase Management Server (DBMS) name

    database            Name of Database.

    ddfile              The data dictionary file is used to get column
                        information for a specific catalog.

    selcols             Target column list with value separated by a comma(,)

                        The input list always overwrites default selections
                        defined by a data dictionary.

    outrows             Number of rows retrieved from database.

                        The retrieved row number outrows is always less than or
                        equal to available to be retrieved rows under the same
                        constraints.
'''

def irsa_reader(self, spatial, catalog, polygon=None, radius=None,
                radunits='arcsec', size=None, objstr=None):

    base_url = 'http://irsa.ipac.caltech.edu/cgi-bin/Gator/nph-query'

    self.reset()

    # Convert to lowercase
    spatial = spatial.capitalize()

    # Set basic options
    options = {}
    options['spatial'] = spatial
    options['catalog'] = catalog
    options['outfmt'] = 3

    if spatial == "Cone":

        if not radius:
            raise Exception("radius is required for Cone search")
        options['radius'] = radius

        if not radunits:
            raise Exception("radunits is required for Cone search")
        options['radunits'] = radunits

        if not objstr:
            raise Exception("objstr is required for Cone search")
        options['objstr'] = objstr

    elif spatial == "Box":

        if not size:
            raise Exception("size is required for Box search")
        options['size'] = size

        if not objstr:
            raise Exception("objstr is required for Cone search")
        options['objstr'] = objstr

    elif spatial == "Polygon":

        if not polygon:
            raise Exception("polygon is required for Polygon search")
        pairs = []
        for pair in polygon:
            if pair[1] > 0:
                pairs.append(str(pair[0]) + '+' + str(pair[1]))
            else:
                pairs.append(str(pair[0]) + str(pair[1]))
        options['polygon'] = string.join(pairs, ',')

    elif spatial == "None":

        options['spatial'] = 'NONE'

    else:

        raise Exception("spatial should be one of cone/box/polygon/none")

    # Construct query URL
    url = base_url + "?" + \
          string.join(["%s=%s" % (x, options[x]) for x in options], "&")

    # Request page
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    result = response.read()

    # Check if results were returned
    if 'The catalog is not on the list' in result:
        raise Exception("Catalog not found")

    # Write table to temporary file
    output = tempfile.NamedTemporaryFile()
    output.write(result)
    output.flush()

    # Read it in using ATpy VO reader
    self.read(output.name, type='vo', verbose=False)

    # Check if table is empty
    if len(self) == 0:
        warnings.warn("Query returned no results, so the table will be empty")

    # Remove temporary file
    output.close()

def list_irsa_catalogs():

    url = 'http://irsa.ipac.caltech.edu/cgi-bin/Gator/nph-scan?mode=xml'

    req = urllib2.Request(url)
    response = urllib2.urlopen(req)

    tree = ElementTree()

    for catalog in tree.parse(response).findall('catalog'):
        catname = catalog.find('catname').text
        desc = catalog.find('desc').text
        print "%30s  %s" % (catname, desc)
<commit_msg>Set table name for IRSA query<commit_after>import warnings
import urllib2
import tempfile
import string
from xml.etree.ElementTree import ElementTree

'''

API from

 http://irsa.ipac.caltech.edu/applications/Gator/GatorAid/irsa/catsearch.html

The URL of the IRSA catalog query service, CatQuery, is

 http://irsa.ipac.caltech.edu/cgi-bin/Gator/nph-query

The service accepts the following keywords, which are analogous to the search
fields on the Gator search form:

spatial     Required    Type of spatial query: Cone, Box, Polygon, and NONE

polygon                 Convex polygon of ra dec pairs, separated by comma(,)
                        Required if spatial=polygon

radius                  Cone search radius
                        Optional if spatial=Cone, otherwise ignore it
                        (default 10 arcsec)

radunits                Units of a Cone search: arcsec, arcmin, deg.
                        Optional if spatial=Cone
                        (default='arcsec')

size                    Width of a box in arcsec
                        Required if spatial=Box.

objstr                  Target name or coordinate of the center of a spatial
                        search center. Target names must be resolved by
                        SIMBAD or NED.

                        Required only when spatial=Cone or spatial=Box.

                        Examples: 'M31'
                                  '00 42 44.3 -41 16 08'
                                  '00h42m44.3s -41d16m08s'

catalog     Required    Catalog name in the IRSA database management system.

outfmt      Optional    Defines query's output format.
                        6 - returns a program interface in XML
                        3 - returns a VO Table (XML)
                        2 - returns SVC message
                        1 - returns an ASCII table
                        0 - returns Gator Status Page in HTML (default)

desc        Optional    Short description of a specific catalog, which will
                        appear in the result page.

order       Optional    Results ordered by this column.

constraint  Optional    User defined query constraint(s)
                        Note: The constraint should follow SQL syntax.

onlist      Optional    1 - catalog is visible through Gator web interface
                        (default)

                        0 - catalog has been ingested into IRSA but not yet
                        visible through web interface.

                        This parameter will generally only be set to 0 when
                        users are supporting testing and evaluation of new
                        catalogs at IRSA's request.

If onlist=0, the following parameters are required:

    server              Symbolic DataBase Management Server (DBMS) name

    database            Name of Database.

    ddfile              The data dictionary file is used to get column
                        information for a specific catalog.

    selcols             Target column list with value separated by a comma(,)

                        The input list always overwrites default selections
                        defined by a data dictionary.

    outrows             Number of rows retrieved from database.

                        The retrieved row number outrows is always less than or
                        equal to available to be retrieved rows under the same
                        constraints.
'''

def irsa_reader(self, spatial, catalog, polygon=None, radius=None,
                radunits='arcsec', size=None, objstr=None):

    base_url = 'http://irsa.ipac.caltech.edu/cgi-bin/Gator/nph-query'

    self.reset()

    # Convert to lowercase
    spatial = spatial.capitalize()

    # Set basic options
    options = {}
    options['spatial'] = spatial
    options['catalog'] = catalog
    options['outfmt'] = 3

    if spatial == "Cone":

        if not radius:
            raise Exception("radius is required for Cone search")
        options['radius'] = radius

        if not radunits:
            raise Exception("radunits is required for Cone search")
        options['radunits'] = radunits

        if not objstr:
            raise Exception("objstr is required for Cone search")
        options['objstr'] = objstr

    elif spatial == "Box":

        if not size:
            raise Exception("size is required for Box search")
        options['size'] = size

        if not objstr:
            raise Exception("objstr is required for Cone search")
        options['objstr'] = objstr

    elif spatial == "Polygon":

        if not polygon:
            raise Exception("polygon is required for Polygon search")
        pairs = []
        for pair in polygon:
            if pair[1] > 0:
                pairs.append(str(pair[0]) + '+' + str(pair[1]))
            else:
                pairs.append(str(pair[0]) + str(pair[1]))
        options['polygon'] = string.join(pairs, ',')

    elif spatial == "None":

        options['spatial'] = 'NONE'

    else:

        raise Exception("spatial should be one of cone/box/polygon/none")

    # Construct query URL
    url = base_url + "?" + \
          string.join(["%s=%s" % (x, options[x]) for x in options], "&")

    # Request page
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    result = response.read()

    # Check if results were returned
    if 'The catalog is not on the list' in result:
        raise Exception("Catalog not found")

    # Write table to temporary file
    output = tempfile.NamedTemporaryFile()
    output.write(result)
    output.flush()

    # Read it in using ATpy VO reader
    self.read(output.name, type='vo', verbose=False)

    # Set table name
    self.table_name = "IRSA_query"

    # Check if table is empty
    if len(self) == 0:
        warnings.warn("Query returned no results, so the table will be empty")

    # Remove temporary file
    output.close()

def list_irsa_catalogs():

    url = 'http://irsa.ipac.caltech.edu/cgi-bin/Gator/nph-scan?mode=xml'

    req = urllib2.Request(url)
    response = urllib2.urlopen(req)

    tree = ElementTree()

    for catalog in tree.parse(response).findall('catalog'):
        catname = catalog.find('catname').text
        desc = catalog.find('desc').text
        print "%30s  %s" % (catname, desc)
<|endoftext|>


<commit_before>""" Convert a PCF file into a VPR io.place file. """
from __future__ import print_function
import argparse
import csv
import json
import sys
import vpr_io_place
from lib.parse_pcf import parse_simple_pcf

def main():
    parser = argparse.ArgumentParser(
        description='Convert a PCF file into a VPR io.place file.'
    )
    parser.add_argument(
        "--pcf",
        '-p',
        "-P",
        type=argparse.FileType('r'),
        required=True,
        help='PCF input file'
    )
    parser.add_argument(
        "--blif",
        '-b',
        type=argparse.FileType('r'),
        required=True,
        help='BLIF / eBLIF file'
    )
    parser.add_argument(
        "--map",
        '-m',
        "-M",
        type=argparse.FileType('r'),
        required=True,
        help='Pin map CSV file'
    )
    parser.add_argument(
        "--output",
        '-o',
        "-O",
        type=argparse.FileType('w'),
        default=sys.stdout,
        help='The output io.place file'
    )
    parser.add_argument(
        "--iostandard_defs", help='(optional) Output IOSTANDARD def file'
    )
    parser.add_argument(
        "--iostandard",
        default="LVCMOS33",
        help='IOSTANDARD to use for pins',
    )
    parser.add_argument(
        "--drive",
        type=int,
        default=12,
        help='Drive to use for pins',
    )
    parser.add_argument(
        "--net",
        '-n',
        type=argparse.FileType('r'),
        required=True,
        help='top.net file'
    )

    args = parser.parse_args()

    io_place = vpr_io_place.IoPlace()
    io_place.read_io_list_from_eblif(args.blif)
    io_place.load_block_names_from_net_file(args.net)

    # Map of pad names to VPR locations.
    pad_map = {}

    for pin_map_entry in csv.DictReader(args.map):
        pad_map[pin_map_entry['name']] = (
            (
                int(pin_map_entry['x']),
                int(pin_map_entry['y']),
                int(pin_map_entry['z']),
            ),
            pin_map_entry['is_output'],
            pin_map_entry['iob'],
        )

    iostandard_defs = {}

    for pcf_constraint in parse_simple_pcf(args.pcf):
        if not io_place.is_net(pcf_constraint.net):
            print(
                'PCF constraint "{}" from line {} constraints net {} which is not in available netlist:\n{}'
                .format(
                    pcf_constraint.line_str, pcf_constraint.line_num,
                    pcf_constraint.net, '\n'.join(io_place.get_nets())
                ),
                file=sys.stderr
            )
            sys.exit(1)

        if pcf_constraint.pad not in pad_map:
            print(
                'PCF constraint "{}" from line {} constraints pad {} which is not in available pad map:\n{}'
                .format(
                    pcf_constraint.line_str, pcf_constraint.line_num,
                    pcf_constraint.pad, '\n'.join(sorted(pad_map.keys()))
                ),
                file=sys.stderr
            )
            sys.exit(1)

        loc, is_output, iob = pad_map[pcf_constraint.pad]
        io_place.constrain_net(
            net_name=pcf_constraint.net,
            loc=loc,
            comment=pcf_constraint.line_str
        )

        if is_output:
            iostandard_defs[iob] = {
                'DRIVE': args.drive,
                'IOSTANDARD': args.iostandard,
            }
        else:
            iostandard_defs[iob] = {
                'IOSTANDARD': args.iostandard,
            }

    io_place.output_io_place(args.output)

    if args.iostandard_defs:
        with open(args.iostandard_defs, 'w') as f:
            json.dump(iostandard_defs, f, indent=2)

if __name__ == '__main__':
    main()
<commit_msg>Added a method of supplying IOSTANDARD constraints along with PCF to be later used by fasm2bels.<commit_after>""" Convert a PCF file into a VPR io.place file. """
from __future__ import print_function
import argparse
import csv
import json
import sys
import os
import vpr_io_place
from lib.parse_pcf import parse_simple_pcf

def main():
    parser = argparse.ArgumentParser(
        description='Convert a PCF file into a VPR io.place file.'
    )
    parser.add_argument(
        "--pcf",
        '-p',
        "-P",
        type=argparse.FileType('r'),
        required=True,
        help='PCF input file'
    )
    parser.add_argument(
        "--blif",
        '-b',
        type=argparse.FileType('r'),
        required=True,
        help='BLIF / eBLIF file'
    )
    parser.add_argument(
        "--map",
        '-m',
        "-M",
        type=argparse.FileType('r'),
        required=True,
        help='Pin map CSV file'
    )
    parser.add_argument(
        "--output",
        '-o',
        "-O",
        type=argparse.FileType('w'),
        default=sys.stdout,
        help='The output io.place file'
    )
    parser.add_argument(
        "--iostandard_defs", help='(optional) Output IOSTANDARD def file'
    )
    parser.add_argument(
        "--iostandard",
        default="LVCMOS33",
        help='Default IOSTANDARD to use for pins',
    )
    parser.add_argument(
        "--drive",
        type=int,
        default=12,
        help='Default drive to use for pins',
    )
    parser.add_argument(
        "--net",
        '-n',
        type=argparse.FileType('r'),
        required=True,
        help='top.net file'
    )

    args = parser.parse_args()

    io_place = vpr_io_place.IoPlace()
    io_place.read_io_list_from_eblif(args.blif)
    io_place.load_block_names_from_net_file(args.net)

    # Map of pad names to VPR locations.
    pad_map = {}

    for pin_map_entry in csv.DictReader(args.map):
        pad_map[pin_map_entry['name']] = (
            (
                int(pin_map_entry['x']),
                int(pin_map_entry['y']),
                int(pin_map_entry['z']),
            ),
            pin_map_entry['is_output'],
            pin_map_entry['iob'],
        )

    iostandard_defs = {}

    # Load iostandard constraints. This is a temporary workaround that allows
    # to pass them into fasm2bels. As soon as there is support for XDC this
    # will not be needed anymore.
    # If there is a JSON file with the same name as the PCF file then it is
    # loaded and used as iostandard constraint source NOT for the design but
    # to be used in fasm2bels.
    iostandard_constraints = {}

    fname = args.pcf.name.replace(".pcf", ".json")
    if os.path.isfile(fname):
        with open(fname, "r") as fp:
            iostandard_constraints = json.load(fp)

    # Constrain nets
    for pcf_constraint in parse_simple_pcf(args.pcf):
        if not io_place.is_net(pcf_constraint.net):
            print(
                'PCF constraint "{}" from line {} constraints net {} which is not in available netlist:\n{}'
                .format(
                    pcf_constraint.line_str, pcf_constraint.line_num,
                    pcf_constraint.net, '\n'.join(io_place.get_nets())
                ),
                file=sys.stderr
            )
            sys.exit(1)

        if pcf_constraint.pad not in pad_map:
            print(
                'PCF constraint "{}" from line {} constraints pad {} which is not in available pad map:\n{}'
                .format(
                    pcf_constraint.line_str, pcf_constraint.line_num,
                    pcf_constraint.pad, '\n'.join(sorted(pad_map.keys()))
                ),
                file=sys.stderr
            )
            sys.exit(1)

        loc, is_output, iob = pad_map[pcf_constraint.pad]
        io_place.constrain_net(
            net_name=pcf_constraint.net,
            loc=loc,
            comment=pcf_constraint.line_str
        )

        if pcf_constraint.pad in iostandard_constraints:
            iostandard_defs[iob] = iostandard_constraints[pcf_constraint.pad]
        else:
            if is_output:
                iostandard_defs[iob] = {
                    'DRIVE': args.drive,
                    'IOSTANDARD': args.iostandard,
                }
            else:
                iostandard_defs[iob] = {
                    'IOSTANDARD': args.iostandard,
                }

    io_place.output_io_place(args.output)

    # Write iostandard definitions
    if args.iostandard_defs:
        with open(args.iostandard_defs, 'w') as f:
            json.dump(iostandard_defs, f, indent=2)

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


<commit_before>import os

class InsecurePathError(Exception):
    """
    The path operation is unsafe to perform.

    An insecure operation was requested, for example:

     * a join is performed with an absolute path as input parameter
     * '..' is passed as a parameter to child method
     * Symlinks not passing security validations
    """
    pass

class CrossdeviceRenameError(Exception):
    """
    Rename old and new paths are not on the same filesystem.

    Rename cannot work across different python filesystems backends.
    New path is not on the local filesystem.
    """

class path(object):
    def __init__(self, pathname):
        self._pathname = pathname

    def __str__(self):
        return str(self._pathname)

    def __unicode__(self):
        return unicode(self._pathname)

    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__, self._pathname)

    def join(self, relpath):
        if relpath.startswith(u'/'):
            raise InsecurePathError('path name to join must be relative')
        return self.__class__(os.path.join(self._pathname, relpath))

    def open(self, *args, **kwargs):
        return file(self._pathname, *args, **kwargs)

    def __iter__(self):
        for i in os.listdir(self._pathname):
            yield path(i)

    def child(self, *segments):
        p = self
        for segment in segments:
            if u'/' in segment:
                raise InsecurePathError(
                      'child name contains directory separator')
            # this may be too naive
            if segment == u'..':
                raise InsecurePathError(
                      'child trying to climb out of directory')
            p = p.join(segment)
        return p

    def parent(self):
        head, tail = os.path.split(self._pathname)
        return self.__class__(head)

    def __eq__(self, other):
        if not isinstance(other, path):
            return NotImplemented
        return self._pathname == other._pathname

    def __ne__(self, other):
        if not isinstance(other, path):
            return NotImplemented
        return self._pathname != other._pathname

    def rename(self, newpath):
        """
        Rename this path. Mutates the object.
        """
        if not isinstance(newpath, path):
            raise CrossdeviceRenameError()
        os.rename(self._pathname, newpath._pathname)
        self._pathname = newpath._pathname
<commit_msg>Added stat for path<commit_after>import os

class InsecurePathError(Exception):
    """
    The path operation is unsafe to perform.

    An insecure operation was requested, for example:

     * a join is performed with an absolute path as input parameter
     * '..' is passed as a parameter to child method
     * Symlinks not passing security validations
    """
    pass

class CrossdeviceRenameError(Exception):
    """
    Rename old and new paths are not on the same filesystem.

    Rename cannot work across different python filesystems backends.
    New path is not on the local filesystem.
    """

class path(object):
    def __init__(self, pathname):
        self._pathname = pathname

    def __str__(self):
        return str(self._pathname)

    def __unicode__(self):
        return unicode(self._pathname)

    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__, self._pathname)

    def join(self, relpath):
        if relpath.startswith(u'/'):
            raise InsecurePathError('path name to join must be relative')
        return self.__class__(os.path.join(self._pathname, relpath))

    def open(self, *args, **kwargs):
        return file(self._pathname, *args, **kwargs)

    def __iter__(self):
        for i in os.listdir(self._pathname):
            yield path(i)

    def child(self, *segments):
        p = self
        for segment in segments:
            if u'/' in segment:
                raise InsecurePathError(
                      'child name contains directory separator')
            # this may be too naive
            if segment == u'..':
                raise InsecurePathError(
                      'child trying to climb out of directory')
            p = p.join(segment)
        return p

    def parent(self):
        head, tail = os.path.split(self._pathname)
        return self.__class__(head)

    def __eq__(self, other):
        if not isinstance(other, path):
            return NotImplemented
        return self._pathname == other._pathname

    def __ne__(self, other):
        if not isinstance(other, path):
            return NotImplemented
        return self._pathname != other._pathname

    def rename(self, newpath):
        """
        Rename this path. Mutates the object.
        """
        if not isinstance(newpath, path):
            raise CrossdeviceRenameError()
        os.rename(self._pathname, newpath._pathname)
        self._pathname = newpath._pathname

    def stat(self):
        return os.stat(self._pathname)
<|endoftext|>