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

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

from cyber_py import cyber

from plot_data import Plotter

from modules.canbus.proto import chassis_pb2
from modules.control.proto import control_cmd_pb2
from modules.localization.proto import localization_pb2

class DataCollector(object):
    """
    DataCollector Class
    """

    def __init__(self, node):
        self.sequence_num = 0
        self.control_pub = node.create_writer('/apollo/control',
                            control_cmd_pb2.DESCRIPTOR.
                            message_types_by_name['ControlCommand'].full_name)
        time.sleep(0.3)
        self.controlcmd = control_cmd_pb2.ControlCommand()

        self.canmsg_received = False
        self.localization_received = False

        self.case = 'a'
        self.in_session = False

        self.outfile = ""

    def run(self, cmd):
        signal.signal(signal.SIGINT, self.signal_handler)

        self.in_session = True
        self.cmd = map(float, cmd)
        out = ''
        if self.cmd[0] > 0:
            out = out + 't'
        else:
            out = out + 'b'
        out = out + str(int(self.cmd[0]))
        if self.cmd[2] > 0:
            out = out + 't'
        else:
            out = out + 'b'
        out = out + str(int(self.cmd[2])) + 'r'
        i = 0
        self.outfile = out + str(i) + '.csv'
        while os.path.exists(self.outfile):
            i += 1
            self.outfile = out + str(i) + '.csv'
        self.file = open(self.outfile, 'w')
        self.file.write(
            "time,io,ctlmode,ctlbrake,ctlthrottle,ctlgear_location,vehicle_speed,"
            +
            "engine_rpm,driving_mode,throttle_percentage,brake_percentage,gear_location, imu\n"
        )

        print "Send Reset Command"
        self.controlcmd.header.module_name = "control"
        self.controlcmd.header.sequence_num = self.sequence_num
        self.sequence_num = self.sequence_num + 1
        self.controlcmd.header.timestamp_sec = time.time()
        self.controlcmd.pad_msg.action = 2
        self.control_pub.publish(self.controlcmd)

        time.sleep(0.2)
        # Set Default Message
        print "Send Default Command"
        self.controlcmd.pad_msg.action = 1
        self.controlcmd.throttle = 0
        self.controlcmd.brake = 0
        self.controlcmd.steering_rate = 100
        self.controlcmd.steering_target = 0
        self.controlcmd.gear_location = chassis_pb2.Chassis.GEAR_DRIVE

        self.canmsg_received = False

        while self.in_session:
            now = time.time()
            self.publish_control()
            sleep_time = 0.01 - (time.time() - now)
            if sleep_time > 0:
                time.sleep(sleep_time)

    def signal_handler(self, signal, frame):
        self.in_session = False

    def callback_localization(self, data):
        """
        New Localization
        """
        self.acceleration = data.pose.linear_acceleration_vrf.y
        self.localization_received = True

    def callback_canbus(self, data):
        """
        New CANBUS
        """
        if not self.localization_received:
            print "No Localization Message Yet"
            return
        timenow = data.header.timestamp_sec
        self.vehicle_speed = data.speed_mps
        self.engine_rpm = data.engine_rpm
        self.throttle_percentage = data.throttle_percentage
        self.brake_percentage = data.brake_percentage
        self.gear_location = data.gear_location
        self.driving_mode = data.driving_mode

        self.canmsg_received = True
        if self.in_session:
            self.write_file(timenow, 0)

    def publish_control(self):
        """
        New Control Command
        """
        if not self.canmsg_received:
            print "No CAN Message Yet"
            return

        self.controlcmd.header.sequence_num = self.sequence_num
        self.sequence_num = self.sequence_num + 1

        if self.case == 'a':
            if self.cmd[0] > 0:
                self.controlcmd.throttle = self.cmd[0]
                self.controlcmd.brake = 0
            else:
                self.controlcmd.throttle = 0
                self.controlcmd.brake = -self.cmd[0]
            if self.vehicle_speed >= self.cmd[1]:
                self.case = 'd'
        elif self.case == 'd':
            if self.cmd[2] > 0:
                self.controlcmd.throttle = self.cmd[0]
                self.controlcmd.brake = 0
            else:
                self.controlcmd.throttle = 0
                self.controlcmd.brake = -self.cmd[0]
            if self.vehicle_speed == 0:
                self.in_session = False

        self.controlcmd.header.timestamp_sec = time.time()
        self.control_pub.publish(self.controlcmd)
        self.write_file(self.controlcmd.header.timestamp_sec, 1)
        if self.in_session == False:
            self.file.close()

    def write_file(self, time, io):
        """
        Write Message to File
        """
        self.file.write(
            "%.4f,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" %
            (time, io, 1, self.controlcmd.brake, self.controlcmd.throttle,
             self.controlcmd.gear_location, self.vehicle_speed, self.engine_rpm,
             self.driving_mode, self.throttle_percentage, self.brake_percentage,
             self.gear_location, self.acceleration))

def main():
    """
    Main function
    """
    node = cyber.Node("data_collector")

    data_collector = DataCollector(node)
    plotter = Plotter()
    node.create_reader('/apollo/localization/pose',
                                       localization_pb2.LocalizationEstimate,
                                       data_collector.callback_localization)
    node.create_reader('/apollo/canbus/chassis', chassis_pb2.Chassis,
                                 data_collector.callback_canbus)

    print "Enter q to quit"
    print "Enter p to plot result from last run"
    print "Enter x to remove result from last run"
    print "Enter x y z, where x is acceleration command, y is speed limit, z is decceleration command"
    print "Positive number for throttle and negative number for brake"

    while True:
        cmd = raw_input("Enter commands: ").split()
        if len(cmd) == 0:
            print "Quiting"
            break
        elif len(cmd) == 1:
            if cmd[0] == "q":
                break
            elif cmd[0] == "p":
                print "Plotting result"
                if os.path.exists(data_collector.outfile):
                    plotter.process_data(data_collector.outfile)
                    plotter.plot_result()
                else:
                    print "File does not exist"
            elif cmd[0] == "x":
                print "Removing last result"
                if os.path.exists(data_collector.outfile):
                    os.remove(data_collector.outfile)
                else:
                    print "File does not exist"
        elif len(cmd) == 3:
            data_collector.run(cmd)

if __name__ == '__main__':
    main()
<commit_msg>control: fix calibration tool.<commit_after>#!/usr/bin/env python

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

from cyber_py import cyber

from plot_data import Plotter

from modules.canbus.proto import chassis_pb2
from modules.control.proto import control_cmd_pb2
from modules.localization.proto import localization_pb2

class DataCollector(object):
    """
    DataCollector Class
    """

    def __init__(self, node):
        self.sequence_num = 0
        self.control_pub = node.create_writer('/apollo/control',
                            control_cmd_pb2.ControlCommand)
        time.sleep(0.3)
        self.controlcmd = control_cmd_pb2.ControlCommand()

        self.canmsg_received = False
        self.localization_received = False

        self.case = 'a'
        self.in_session = False

        self.outfile = ""

    def run(self, cmd):
        signal.signal(signal.SIGINT, self.signal_handler)

        self.in_session = True
        self.cmd = map(float, cmd)
        out = ''
        if self.cmd[0] > 0:
            out = out + 't'
        else:
            out = out + 'b'
        out = out + str(int(self.cmd[0]))
        if self.cmd[2] > 0:
            out = out + 't'
        else:
            out = out + 'b'
        out = out + str(int(self.cmd[2])) + 'r'
        i = 0
        self.outfile = out + str(i) + '.csv'
        while os.path.exists(self.outfile):
            i += 1
            self.outfile = out + str(i) + '.csv'
        self.file = open(self.outfile, 'w')
        self.file.write(
            "time,io,ctlmode,ctlbrake,ctlthrottle,ctlgear_location,vehicle_speed,"
            +
            "engine_rpm,driving_mode,throttle_percentage,brake_percentage,gear_location, imu\n"
        )

        print "Send Reset Command"
        self.controlcmd.header.module_name = "control"
        self.controlcmd.header.sequence_num = self.sequence_num
        self.sequence_num = self.sequence_num + 1
        self.controlcmd.header.timestamp_sec = time.time()
        self.controlcmd.pad_msg.action = 2
        self.control_pub.publish(self.controlcmd)

        time.sleep(0.2)
        # Set Default Message
        print "Send Default Command"
        self.controlcmd.pad_msg.action = 1
        self.controlcmd.throttle = 0
        self.controlcmd.brake = 0
        self.controlcmd.steering_rate = 100
        self.controlcmd.steering_target = 0
        self.controlcmd.gear_location = chassis_pb2.Chassis.GEAR_DRIVE

        self.canmsg_received = False

        while self.in_session:
            now = time.time()
            self.publish_control()
            sleep_time = 0.01 - (time.time() - now)
            if sleep_time > 0:
                time.sleep(sleep_time)

    def signal_handler(self, signal, frame):
        self.in_session = False

    def callback_localization(self, data):
        """
        New Localization
        """
        self.acceleration = data.pose.linear_acceleration_vrf.y
        self.localization_received = True

    def callback_canbus(self, data):
        """
        New CANBUS
        """
        if not self.localization_received:
            print "No Localization Message Yet"
            return
        timenow = data.header.timestamp_sec
        self.vehicle_speed = data.speed_mps
        self.engine_rpm = data.engine_rpm
        self.throttle_percentage = data.throttle_percentage
        self.brake_percentage = data.brake_percentage
        self.gear_location = data.gear_location
        self.driving_mode = data.driving_mode

        self.canmsg_received = True
        if self.in_session:
            self.write_file(timenow, 0)

    def publish_control(self):
        """
        New Control Command
        """
        if not self.canmsg_received:
            print "No CAN Message Yet"
            return

        self.controlcmd.header.sequence_num = self.sequence_num
        self.sequence_num = self.sequence_num + 1

        if self.case == 'a':
            if self.cmd[0] > 0:
                self.controlcmd.throttle = self.cmd[0]
                self.controlcmd.brake = 0
            else:
                self.controlcmd.throttle = 0
                self.controlcmd.brake = -self.cmd[0]
            if self.vehicle_speed >= self.cmd[1]:
                self.case = 'd'
        elif self.case == 'd':
            if self.cmd[2] > 0:
                self.controlcmd.throttle = self.cmd[0]
                self.controlcmd.brake = 0
            else:
                self.controlcmd.throttle = 0
                self.controlcmd.brake = -self.cmd[0]
            if self.vehicle_speed == 0:
                self.in_session = False

        self.controlcmd.header.timestamp_sec = time.time()
        self.control_pub.publish(self.controlcmd)
        self.write_file(self.controlcmd.header.timestamp_sec, 1)
        if self.in_session == False:
            self.file.close()

    def write_file(self, time, io):
        """
        Write Message to File
        """
        self.file.write(
            "%.4f,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" %
            (time, io, 1, self.controlcmd.brake, self.controlcmd.throttle,
             self.controlcmd.gear_location, self.vehicle_speed, self.engine_rpm,
             self.driving_mode, self.throttle_percentage, self.brake_percentage,
             self.gear_location, self.acceleration))

def main():
    """
    Main function
    """
    node = cyber.Node("data_collector")

    data_collector = DataCollector(node)
    plotter = Plotter()
    node.create_reader('/apollo/localization/pose',
                                       localization_pb2.LocalizationEstimate,
                                       data_collector.callback_localization)
    node.create_reader('/apollo/canbus/chassis', chassis_pb2.Chassis,
                                 data_collector.callback_canbus)

    print "Enter q to quit"
    print "Enter p to plot result from last run"
    print "Enter x to remove result from last run"
    print "Enter x y z, where x is acceleration command, y is speed limit, z is decceleration command"
    print "Positive number for throttle and negative number for brake"

    while True:
        cmd = raw_input("Enter commands: ").split()
        if len(cmd) == 0:
            print "Quiting"
            break
        elif len(cmd) == 1:
            if cmd[0] == "q":
                break
            elif cmd[0] == "p":
                print "Plotting result"
                if os.path.exists(data_collector.outfile):
                    plotter.process_data(data_collector.outfile)
                    plotter.plot_result()
                else:
                    print "File does not exist"
            elif cmd[0] == "x":
                print "Removing last result"
                if os.path.exists(data_collector.outfile):
                    os.remove(data_collector.outfile)
                else:
                    print "File does not exist"
        elif len(cmd) == 3:
            data_collector.run(cmd)

if __name__ == '__main__':
    cyber.init()
    main()
    cyber.shutdown()
<|endoftext|>


<commit_before>__author__ = 'Bieliaievskyi Sergey'
__credits__ = ["Bieliaievskyi Sergey"]
__license__ = "Apache License"
__version__ = "1.0.0"
__maintainer__ = "Bieliaievskyi Sergey"
__email__ = "magelan09@gmail.com"
__status__ = "Release"

import urllib.parse
import mimetypes
import base64
import pycurl
import json
import io

class PushBullet:
    def __init__(self, api_id):
        self.auth_id = api_id
        self.post_data = {}

    def pb_request(self, url, req_method=None):
        curl_obj = pycurl.Curl()
        response = io.BytesIO()
        encoded_string = base64.encodebytes(('%s:' % self.auth_id).encode())[:-1]
        auth = "Basic %s" % encoded_string.decode('utf-8')
        if self.post_data:
            req_data = json.dumps(self.post_data)
            curl_obj.setopt(pycurl.POST, 1)
            curl_obj.setopt(curl_obj.POSTFIELDS, req_data)
        if req_method:
            curl_obj.setopt(pycurl.CUSTOMREQUEST, req_method)
        curl_obj.setopt(pycurl.URL, url)
        curl_obj.setopt(curl_obj.WRITEFUNCTION, response.write)
        curl_obj.setopt(curl_obj.HTTPHEADER, ["Authorization: " + auth,
                                              "Accept: application/json",
                                              "Content-Type: application/json",
                                              "User-Agent: PushBullet-agent"])
        curl_obj.perform()
        return curl_obj.getinfo(pycurl.HTTP_CODE), \
               curl_obj.getinfo(pycurl.EFFECTIVE_URL), \
               json.loads(response.getvalue().decode())

class PBPushes(PushBullet):
    def __init__(self, api_key_id):
        PushBullet.__init__(self, api_key_id)

    def get_push_history(self):
        return self.pb_request('https://api.pushbullet.com/v2/pushes?modified_after=0')

    def del_pushes(self, push_id):
        return self.pb_request('https://api.pushbullet.com/v2/pushes/%s' % push_id, req_method='DELETE')

    def pushes(self, **pbargs):
        self.post_data = pbargs
        return self.pb_request('https://api.pushbullet.com/v2/pushes')

class PBFileUpload(PushBullet):
    def __init__(self, api_key_id):
        PushBullet.__init__(self, api_key_id)
        self.file_upload_param = {}

    def upload_request(self, file_name):
        my_mime = mimetypes.guess_type(file_name)[0]
        url_param = urllib.parse.urlencode({'file_name': file_name, 'file_type': my_mime})
        response = self.pb_request('https://api.pushbullet.com/v2/upload-request?' + url_param)[2]
        self.file_upload_param = response
        return response

    def pb_upload(self, file_path):
        buffer = io.BytesIO()
        data = [(elm, str(val)) for (elm, val) in self.file_upload_param['data'].items()]
        data.append(('file', (pycurl.FORM_FILE, file_path)))
        my_curl = pycurl.Curl()
        my_curl.setopt(pycurl.URL, self.file_upload_param['upload_url'])
        my_curl.setopt(pycurl.POST, 1)
        my_curl.setopt(pycurl.HTTPPOST, data)
        my_curl.setopt(pycurl.WRITEFUNCTION, buffer.write)
        my_curl.perform()
        return my_curl.getinfo(pycurl.HTTP_CODE), \
               my_curl.getinfo(pycurl.EFFECTIVE_URL)

class PBUsers(PushBullet):
    def __init__(self, api_key_id):
        PushBullet.__init__(self, api_key_id)

    def get_me(self):
        return self.pb_request('https://api.pushbullet.com/v2/users/me')

class PBContacts(PushBullet):
    def __init__(self, api_key_id):
        PushBullet.__init__(self, api_key_id)

    def get_contacts(self):
        return self.pb_request('https://api.pushbullet.com/v2/contacts')

    def del_contact(self, contact_iden):
        return self.pb_request('https://api.pushbullet.com/v2/contacts/%s' % contact_iden, req_method='DELETE')

class PBDevices(PushBullet):
    def __init__(self, api_key_id):
        PushBullet.__init__(self, api_key_id)

    def get_connected_devices(self):
        return self.pb_request('https://api.pushbullet.com/v2/devices')

    def del_connected_device(self, dev_id):
        return self.pb_request('https://api.pushbullet.com/v2/devices/%s' % dev_id, req_method='DELETE')<commit_msg>reduce number of lines. optimization has been done<commit_after>__author__ = 'Bieliaievskyi Sergey'
__credits__ = ["Bieliaievskyi Sergey"]
__license__ = "Apache License"
__version__ = "1.0.0"
__maintainer__ = "Bieliaievskyi Sergey"
__email__ = "magelan09@gmail.com"
__status__ = "Release"

import urllib.parse
import mimetypes
import base64
import pycurl
import json
import io

class PushBullet:
    def __init__(self, api_id):
        self.auth_id = api_id
        self.post_data = {}
        self.main_url = 'https://api.pushbullet.com/v2/'

    def pb_request(self, url, req_method=None):
        curl_obj = pycurl.Curl()
        response = io.BytesIO()
        encoded_string = base64.encodebytes(('%s:' % self.auth_id).encode())[:-1]
        auth = "Basic %s" % encoded_string.decode('utf-8')
        if self.post_data:
            req_data = json.dumps(self.post_data)
            curl_obj.setopt(curl_obj.POSTFIELDS, req_data)
        if req_method:
            curl_obj.setopt(pycurl.CUSTOMREQUEST, req_method)
        curl_obj.setopt(pycurl.URL, url)
        curl_obj.setopt(curl_obj.WRITEFUNCTION, response.write)
        curl_obj.setopt(curl_obj.HTTPHEADER, ["Authorization: %s" % auth,
                                              "Accept: application/json",
                                              "Content-Type: application/json",
                                              "User-Agent: PushBullet-agent"])
        curl_obj.perform()
        return curl_obj.getinfo(pycurl.HTTP_CODE), \
               curl_obj.getinfo(pycurl.EFFECTIVE_URL), \
               json.loads(response.getvalue().decode())

class PBFileUpload(PushBullet):
    def __init__(self, api_key_id):
        PushBullet.__init__(self, api_key_id)
        self.file_upload_param = {}

    def upload_request(self, file_name):
        url_param = urllib.parse.urlencode({'file_name': file_name, 'file_type': mimetypes.guess_type(file_name)[0]})
        self.file_upload_param = self.pb_request('%supload-request?%s' % (self.main_url, url_param))[2]

    def pb_upload(self, file_path):
        buffer = io.BytesIO()
        data = [(elm, str(val)) for (elm, val) in self.file_upload_param['data'].items()]
        data.append(('file', (pycurl.FORM_FILE, file_path)))
        my_curl = pycurl.Curl()
        my_curl.setopt(pycurl.URL, self.file_upload_param['upload_url'])
        my_curl.setopt(pycurl.HTTPPOST, data)
        my_curl.setopt(pycurl.WRITEFUNCTION, buffer.write)
        my_curl.perform()
        return my_curl.getinfo(pycurl.HTTP_CODE), \
               my_curl.getinfo(pycurl.EFFECTIVE_URL)

class PBUsers(PushBullet):
    def __init__(self, api_key_id):
        PushBullet.__init__(self, api_key_id)

    def get_me(self):
        return self.pb_request('%susers/me' % self.main_url)

class PBContacts(PushBullet):
    def __init__(self, api_key_id):
        PushBullet.__init__(self, api_key_id)

    def get_contacts(self):
        return self.pb_request('%scontacts' % self.main_url)

    def del_contact(self, contact_iden):
        return self.pb_request('%scontacts/%s' % (self.main_url, contact_iden), req_method='DELETE')

class PBDevices(PushBullet):
    def __init__(self, api_key_id):
        PushBullet.__init__(self, api_key_id)

    def get_connected_devices(self):
        return self.pb_request('%sdevices' % self.main_url)

    def del_connected_device(self, dev_id):
        return self.pb_request('%sdevices/%s' % (self.main_url, dev_id), req_method='DELETE')

class PBPushes(PushBullet):
    def __init__(self, api_key_id):
        PushBullet.__init__(self, api_key_id)

    def get_push_history(self):
        return self.pb_request('%spushes?modified_after=0' % self.main_url)

    def del_pushes(self, push_id):
        return self.pb_request('%spushes/%s' % (self.main_url, push_id), req_method='DELETE')

    def pushes(self, **pbargs):
        self.post_data = pbargs
        return self.pb_request('%spushes' % self.main_url)<|endoftext|>


<commit_before>from django import template
from django.template.defaultfilters import stringfilter

from django.utils.translation import check_for_language
from django.utils.translation.trans_real import translation

from multilang.translators import NoTranslationError

register = template.Library()

def token_splitter(token, unquote=False):
    """
    Splits a template tag token and returns a dict containing:
    * tag_name  - The template tag name (ie. first piece in the token)
    * args      - List of arguments to the template tag
    * context_var - Context variable name if the "as" keyword is used,
                    or None otherwise.
    """
    pieces = token.split_contents()

    tag_name = pieces[0]
    args = []
    context_var = None
    if len(pieces) > 2 and pieces[-2] == 'as':
        args = pieces[1:-2]
        context_var = pieces[-1]
    elif len(pieces) > 1:
        args = pieces[1:]

    if unquote:
        args = [strip_quotes(arg) for arg in args]

    return {
        'tag_name':tag_name,
        'args':args,
        'context_var':context_var,
    }

def strip_quotes(string):
    """
    Strips quotes off the ends of `string` if it is quoted
    and returns it.
    """
    if len(string) >= 2:
        if string[0] == string[-1]:
            if string[0] in ('"', "'"):
                string = string[1:-1]
    return string

@register.tag
def this_page_in_lang(parser, token):
    """
    Returns the URL for the equivalent of the current page in the requested language.
    If no URL can be generated, returns nothing.

    Usage:

        {% this_page_in_lang "fr" %}
        {% this_page_in_lang "fr" as var_name %}

    """
    bits = token_splitter(token)
    fallback = None
    if len(bits['args']) < 1:
        raise template.TemplateSyntaxError, "%s tag requires at least one argument" % bits['tag_name']
    elif len(bits['args']) == 2:
        fallback = bits['args'][1]
    elif len(bits['args']) > 2:
        raise template.TemplateSyntaxError, "%s tag takes at most two arguments" % bits['tag_name']

    return ThisPageInLangNode(bits['args'][0], fallback, bits['context_var'])

class ThisPageInLangNode(template.Node):
    def __init__(self, lang, fallback=None, context_var=None):
        self.context_var = context_var
        self.lang = template.Variable(lang)
        if fallback:
            self.fallback = template.Variable(fallback)
        else:
            self.fallback = None

    def render(self, context):
        try:
            output = context['_url_translator'].get_url(
                self.lang.resolve(context), context
            )
        except (KeyError, NoTranslationError), e:
            output = ''

        if (not output) and self.fallback:
            output = self.fallback.resolve(context)

        if self.context_var:
            context[self.context_var] = output
            return ''
        else:
            return output

@register.filter
@stringfilter
def trans_in_lang(string, lang):
    """
    Translate a string into a specific language (which can be different
    than the set language).

    Usage:

        {{ var|trans_in_lang:"fr" }}

    """
    if check_for_language(lang):
        return translation(lang).ugettext(string)
    return string
<commit_msg>Removed trailing whitespace.<commit_after>from django import template
from django.template.defaultfilters import stringfilter

from django.utils.translation import check_for_language
from django.utils.translation.trans_real import translation

from multilang.translators import NoTranslationError

register = template.Library()

def token_splitter(token, unquote=False):
    """
    Splits a template tag token and returns a dict containing:
    * tag_name  - The template tag name (ie. first piece in the token)
    * args      - List of arguments to the template tag
    * context_var - Context variable name if the "as" keyword is used,
                    or None otherwise.
    """
    pieces = token.split_contents()

    tag_name = pieces[0]
    args = []
    context_var = None
    if len(pieces) > 2 and pieces[-2] == 'as':
        args = pieces[1:-2]
        context_var = pieces[-1]
    elif len(pieces) > 1:
        args = pieces[1:]

    if unquote:
        args = [strip_quotes(arg) for arg in args]

    return {
        'tag_name':tag_name,
        'args':args,
        'context_var':context_var,
    }

def strip_quotes(string):
    """
    Strips quotes off the ends of `string` if it is quoted
    and returns it.
    """
    if len(string) >= 2:
        if string[0] == string[-1]:
            if string[0] in ('"', "'"):
                string = string[1:-1]
    return string

@register.tag
def this_page_in_lang(parser, token):
    """
    Returns the URL for the equivalent of the current page in the requested language.
    If no URL can be generated, returns nothing.

    Usage:

        {% this_page_in_lang "fr" %}
        {% this_page_in_lang "fr" as var_name %}

    """
    bits = token_splitter(token)
    fallback = None
    if len(bits['args']) < 1:
        raise template.TemplateSyntaxError, "%s tag requires at least one argument" % bits['tag_name']
    elif len(bits['args']) == 2:
        fallback = bits['args'][1]
    elif len(bits['args']) > 2:
        raise template.TemplateSyntaxError, "%s tag takes at most two arguments" % bits['tag_name']

    return ThisPageInLangNode(bits['args'][0], fallback, bits['context_var'])

class ThisPageInLangNode(template.Node):
    def __init__(self, lang, fallback=None, context_var=None):
        self.context_var = context_var
        self.lang = template.Variable(lang)
        if fallback:
            self.fallback = template.Variable(fallback)
        else:
            self.fallback = None

    def render(self, context):
        try:
            output = context['_url_translator'].get_url(
                self.lang.resolve(context), context
            )
        except (KeyError, NoTranslationError), e:
            output = ''

        if (not output) and self.fallback:
            output = self.fallback.resolve(context)

        if self.context_var:
            context[self.context_var] = output
            return ''
        else:
            return output

@register.filter
@stringfilter
def trans_in_lang(string, lang):
    """
    Translate a string into a specific language (which can be different
    than the set language).

    Usage:

        {{ var|trans_in_lang:"fr" }}

    """
    if check_for_language(lang):
        return translation(lang).ugettext(string)
    return string
<|endoftext|>


<commit_before>"""
These URLs are used by the test suite to exercise the various
views. You should not use these URLs in any sort of real deployment
situation.

"""

from django.conf.urls import patterns
from django.conf.urls import url

from .. import policies

def make_test_policy():
    policy = policies.Policy()
    policy.allow_domain('media.example.com')
    policy.allow_headers('media.example.com', ['SomeHeader'])
    return policy

urlpatterns = patterns('',
                       url(r'^crossdomain-serve.xml$',
                           'flashpolicies.views.serve',
                           {'policy': make_test_policy()}),
                       url(r'^crossdomain-simple.xml$',
                           'flashpolicies.views.simple',
                           {'domains': ['media.example.com',
                                        'api.example.com']}),
                       url(r'^crossdomain-no-access.xml$',
                           'flashpolicies.views.no_access'),
                       url(r'^crossdomain-metapolicy.xml$',
                           'flashpolicies.views.metapolicy',
                           {'permitted': policies.SITE_CONTROL_ALL}),
                       )
<commit_msg>Modernize testing URLconf.<commit_after>"""
These URLs are used by the test suite to exercise the various
views. You should not use these URLs in any sort of real deployment
situation.

"""

from django.conf.urls import url

from .. import policies

def make_test_policy():
    policy = policies.Policy()
    policy.allow_domain('media.example.com')
    policy.allow_headers('media.example.com', ['SomeHeader'])
    return policy

urlpatterns = [
    url(r'^crossdomain-serve.xml$',
        'flashpolicies.views.serve',
        {'policy': make_test_policy()}),
    url(r'^crossdomain-simple.xml$',
        'flashpolicies.views.simple',
        {'domains': ['media.example.com',
                     'api.example.com']}),
    url(r'^crossdomain-no-access.xml$',
        'flashpolicies.views.no_access'),
    url(r'^crossdomain-metapolicy.xml$',
        'flashpolicies.views.metapolicy',
        {'permitted': policies.SITE_CONTROL_ALL}),
]
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# LICENSE
#
# Copyright (c) 2015 GEM Foundation
#
# The Catalogue Toolkit is free software: you can redistribute
# it and/or modify it under the terms of the GNU Affero General Public
# License as published by the Free Software Foundation, either version
# 3 of the License, or (at your option) any later version.
#
# You should have received a copy of the GNU Affero General Public License
# with this download. If not, see <http://www.gnu.org/licenses/>

#!/usr/bin/env/python

'''
Reader for the catalogue in a reduced ISF Format considering only
headers
'''
import os
import re
import datetime
import numpy as np
from math import floor, ceil, fabs
from eqcat.parsers.base import (BaseCatalogueDatabaseReader,
                                _to_int, _to_str, _to_float)
from eqcat.isf_catalogue import (Magnitude,
                                 Location,
                                 Origin,
                                 Event,
                                 ISFCatalogue)

origin_header = '   Date       Time        Err   RMS Latitude Longitude  '\
    'Smaj  Smin  Az Depth   Err Ndef Nsta Gap  mdist  Mdist Qual   '\
    'Author      OrigID'

magnitude_header = 'Magnitude  Err Nsta Author      OrigID'

# Default to 'Global Catalogue' agency bulletins
GLOBAL_SELECTED_AGENCIES = ['ISC', 'EHB', 'GCMT', 'HRVD',
                            'GUTE', 'PAS', 'NIED']

def get_event_header_row(row):
    """
    Parses a header row from ISF format and returns an instance of the
    isf_catalogue.Event class
    """
    header_dat = row.split()
    return Event(header_dat[1], origins=[], magnitudes=[],
                 description=" ".join(header_dat[2:]))

def get_time_from_str(row):
    """
    Parses the time data from the origin line and returns an instance of the
    datetime.time class
    """
    # Get time hh:mm:ss.ss
    hms = row[11:22].split(':')
    #print "hh:mm:ss:", hms
    hours = int(hms[0])
    minutes = int(hms[1])
    secs = float(hms[2])
    seconds = int(floor(secs))
    microseconds = int((secs - seconds) * 1000000.)
    # Get time error
    time_error = _to_float(row[24:29])
    time_rms = _to_float(row[30:35])
    return datetime.time(hours, minutes, seconds, microseconds), time_error, \
        time_rms

def get_origin_metadata(row):
    """
    Returns the dictionary of metadata from the origins
    """
    metadata = {
        'Nphases': _to_int(row[83:87]),
        'Nstations': _to_int(row[88:92]),
        'AzimuthGap': _to_float(row[93:96]),
        'minDist': _to_float(row[97:103]),
        'maxDist': _to_float(row[104:110]),
        'FixedTime': _to_str(row[22]),
        'DepthSolution': _to_str(row[54]),
        'AnalysisType': _to_str(row[111]),
        'LocationMethod': _to_str(row[113]),
        'EventType': _to_str(row[115:117])}
    return metadata

def get_event_origin_row(row, selected_agencies=[]):
    '''
    Parses the Origin row from ISF format to an instance of an
    isf_catalogue.Origin class. If the author is not one of the selected
    agencies then None is returns
    '''
    origin_id = _to_str(row[128:])
    author = _to_str(row[118:127])
    if len(selected_agencies) and not author in selected_agencies:
        # Origin not an instance of ISC Reviewed, Engdahl, HRVD or GCMT -
        # origin not saved!
        return None
    # Get date yyyy/mm/dd
    ymd = map(int, row[:10].split('/'))
    #print "YMD",ymd
    date = datetime.date(ymd[0], ymd[1], ymd[2])
    # Get time
    time, time_error, time_rms = get_time_from_str(row)
    # Get longitude and latitude
    latitude = float(row[36:44])
    longitude = float(row[45:54])
    # Get error ellipse
    semimajor90 = _to_float(row[55:60])
    semiminor90 = _to_float(row[61:66])
    error_strike = _to_float(row[67:70])
    # Get depths
    #depth = _to_float(row[71:75])
    depth = _to_float(row[71:75])
    depthSolution = _to_str(row[76:78])
    #if depthSolution == "f":
    #    print "depth= ", depth, "DepthSolution =", depthSolution
    depth_error = _to_float(row[78:82])
    # Create location class
    location = Location(origin_id, longitude, latitude, depth,depthSolution,
                        semimajor90,semiminor90, error_strike, depth_error)
    # Get the metadata
    metadata = get_origin_metadata(row)
    return Origin(origin_id, date, time, location, author,
                  time_error=time_error, time_rms=time_rms, metadata=metadata)

def get_event_magnitude(row, event_id,
                        selected_agencies=[],
                        selected_types=[]):
    """
    Creates an instance of an isf_catalogue.Magnitude object from the row
    string, or returns None if the author is not one of the selected agencies
    or magnitude types
    """
    origin_id = _to_str(row[30:])
    author = row[20:29].strip(' ')
    scale=row[:5].strip(' ')
    if ((len(selected_agencies) and not author in selected_agencies)\
        or (len(selected_agencies) and not scale in selected_types)):
        # Magnitude does not correspond to a selected agency - ignore
        return None
    sigma = _to_float(row[11:14])
    nstations = _to_int(row[15:19])
    return Magnitude(event_id, origin_id, _to_float(row[6:10]), author,
                     scale=scale, sigma=sigma, stations=nstations)

class ISFReader(BaseCatalogueDatabaseReader):
    '''
    Class to read an ISF formatted earthquake catalogue considering only
    the origin agencies, the magnitude agencies and the magnitude types
    defined by the user
    '''

    def __init__(self, filename,
                 selected_origin_agencies=[],
                 selected_magnitude_agencies=[],
                 selected_magnitude_types =[],
                 rejection_keywords=[],
                 lower_magnitude=None, upper_magnitude=None):

        super(ISFReader, self).__init__(filename,
                                        selected_origin_agencies,
                                        selected_magnitude_agencies,
                                        selected_magnitude_types)

        self.rejected_catalogue = []
        self.rejection_keywords = rejection_keywords
        if lower_magnitude and upper_magnitude:
            assert upper_magnitude > lower_magnitude
        if lower_magnitude:
            self.lower_mag = lower_magnitude
        else:
            self.lower_mag = -np.inf
        if upper_magnitude:
            self.upper_mag = upper_magnitude
        else:
            self.upper_mag = np.inf

    def read_file(self, identifier, name):
        """
        Reads the catalogue from the file and assigns the identifier and name
        """
        self.catalogue = ISFCatalogue(identifier, name)
        f = open(self.filename, 'rt')
        counter = 0
        is_origin = False
        is_magnitude = False
        comment_str = ""
        for row in f.readlines():
            if not row.rstrip('\n'):
                # Ignore empty rows
                continue
            elif "DATA_TYPE EVENT IMS1.0" in row:
                # Ignore header row
                continue
            elif "ISC Bulletin" in row:
                # Yet anothet header row
                continue
            elif "STOP" in row:
                # Footer row
                continue
            else:
                pass

            if '(#PRIME)' in row:
                # Previous origin block was the prime origin
                if len(origins) > 0:
                    origins[-1].is_prime = True
                continue

            if '(#CENTROID)' in row:
                # Previous origin block is a centroid
                if len(origins) > 0:
                    origins[-1].is_centroid = True
                continue

            comment_find = re.search("\((.*?)\)", row)
            if comment_find:
                comment_find.group(1)
                comment_str += "{:s}\n".format(comment_find.group(1))
                continue

            if 'Event' in row[:5]:
                # Is an event header row
                if counter > 0:
                    # Add magnitudes and origins to previous event
                    # and previous event to catalogue
                    Event.origins = origins
                    Event.magnitudes = magnitudes
                    if len(Event.origins) and len(Event.magnitudes):
                        Event.assign_magnitudes_to_origins()
                        Event.comment = comment_str
                        #print "%s - %s" % (Event.id, Event.description)
                        #print Event.comment
                        if self._acceptance(Event):
                            Event.comment = ""
                            self.catalogue.events.append(Event)

                # Get a new event
                Event = get_event_header_row(row.rstrip('\n'))
                comment_str = ""
                origins = []
                magnitudes = []
                counter += 1
                continue

            if row.rstrip('\n') == origin_header:
                is_origin = True
                is_magnitude = False
                continue
            elif row.strip('\n') == magnitude_header:
                is_origin = False
                is_magnitude = True
                continue
            else:
                pass

            if is_magnitude and len(row.strip('\n')) == 38:
                # Is a magnitude row
                mag = get_event_magnitude(row.strip('\n'),
                                          Event.id,
                                          self.selected_magnitude_agencies,
                                          self.selected_magnitude_types)

                if mag:
                    magnitudes.append(mag)
                continue

            if is_origin and len(row.strip('\n')) == 136:
                # Is an origin row
                orig = get_event_origin_row(row.strip('\n'),
                                            self.selected_origin_agencies)
                if orig:
                    origins.append(orig)

        if len(self.rejected_catalogue):
            # Turn list of rejected events into its own instance of
            # ISFCatalogue
            self.rejected_catalogue = ISFCatalogue(
                identifier + "-R",
                name + " - Rejected",
                events=self.rejected_catalogue)
        return self.catalogue

    def _acceptance(self, event):
        """
        Determines whether to accept the event according to the magnitude
        and keyword criteria
        :param event:
            Event as instance of Event class
        :returns:
            True (if event is accepted), False otherwise
        """
        # Magnitude rejection - based on an "any" criterion
        valid_magnitude = False
        for mag in event.magnitudes:
            if (mag.value >= self.lower_mag) and (mag.value <= self.upper_mag):
                valid_magnitude = True
                break
        if not valid_magnitude:
            return False
        for keyword in self.rejection_keywords:
            if keyword.lower() in event.comment.lower():
                self.rejected_catalogue.append(event)
                return False
        return True

<commit_msg>removing mag_type_selection from reader<commit_after># -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# LICENSE
#
# Copyright (c) 2015 GEM Foundation
#
# The Catalogue Toolkit is free software: you can redistribute
# it and/or modify it under the terms of the GNU Affero General Public
# License as published by the Free Software Foundation, either version
# 3 of the License, or (at your option) any later version.
#
# You should have received a copy of the GNU Affero General Public License
# with this download. If not, see <http://www.gnu.org/licenses/>

#!/usr/bin/env/python

'''
Reader for the catalogue in a reduced ISF Format considering only
headers
'''
import os
import re
import datetime
import numpy as np
from math import floor, ceil, fabs
from eqcat.parsers.base import (BaseCatalogueDatabaseReader,
                                _to_int, _to_str, _to_float)
from eqcat.isf_catalogue import (Magnitude,
                                 Location,
                                 Origin,
                                 Event,
                                 ISFCatalogue)

origin_header = '   Date       Time        Err   RMS Latitude Longitude  '\
    'Smaj  Smin  Az Depth   Err Ndef Nsta Gap  mdist  Mdist Qual   '\
    'Author      OrigID'

magnitude_header = 'Magnitude  Err Nsta Author      OrigID'

# Default to 'Global Catalogue' agency bulletins
GLOBAL_SELECTED_AGENCIES = ['ISC', 'EHB', 'GCMT', 'HRVD',
                            'GUTE', 'PAS', 'NIED']

def get_event_header_row(row):
    """
    Parses a header row from ISF format and returns an instance of the
    isf_catalogue.Event class
    """
    header_dat = row.split()
    return Event(header_dat[1], origins=[], magnitudes=[],
                 description=" ".join(header_dat[2:]))

def get_time_from_str(row):
    """
    Parses the time data from the origin line and returns an instance of the
    datetime.time class
    """
    # Get time hh:mm:ss.ss
    hms = row[11:22].split(':')
    #print "hh:mm:ss:", hms
    hours = int(hms[0])
    minutes = int(hms[1])
    secs = float(hms[2])
    seconds = int(floor(secs))
    microseconds = int((secs - seconds) * 1000000.)
    # Get time error
    time_error = _to_float(row[24:29])
    time_rms = _to_float(row[30:35])
    return datetime.time(hours, minutes, seconds, microseconds), time_error, \
        time_rms

def get_origin_metadata(row):
    """
    Returns the dictionary of metadata from the origins
    """
    metadata = {
        'Nphases': _to_int(row[83:87]),
        'Nstations': _to_int(row[88:92]),
        'AzimuthGap': _to_float(row[93:96]),
        'minDist': _to_float(row[97:103]),
        'maxDist': _to_float(row[104:110]),
        'FixedTime': _to_str(row[22]),
        'DepthSolution': _to_str(row[54]),
        'AnalysisType': _to_str(row[111]),
        'LocationMethod': _to_str(row[113]),
        'EventType': _to_str(row[115:117])}
    return metadata

def get_event_origin_row(row, selected_agencies=[]):
    '''
    Parses the Origin row from ISF format to an instance of an
    isf_catalogue.Origin class. If the author is not one of the selected
    agencies then None is returns
    '''
    origin_id = _to_str(row[128:])
    author = _to_str(row[118:127])
    if len(selected_agencies) and not author in selected_agencies:
        # Origin not an instance of ISC Reviewed, Engdahl, HRVD or GCMT -
        # origin not saved!
        return None
    # Get date yyyy/mm/dd
    ymd = map(int, row[:10].split('/'))
    #print "YMD",ymd
    date = datetime.date(ymd[0], ymd[1], ymd[2])
    # Get time
    time, time_error, time_rms = get_time_from_str(row)
    # Get longitude and latitude
    latitude = float(row[36:44])
    longitude = float(row[45:54])
    # Get error ellipse
    semimajor90 = _to_float(row[55:60])
    semiminor90 = _to_float(row[61:66])
    error_strike = _to_float(row[67:70])
    # Get depths
    #depth = _to_float(row[71:75])
    depth = _to_float(row[71:75])
    depthSolution = _to_str(row[76:78])
    #if depthSolution == "f":
    #    print "depth= ", depth, "DepthSolution =", depthSolution
    depth_error = _to_float(row[78:82])
    # Create location class
    location = Location(origin_id, longitude, latitude, depth,depthSolution,
                        semimajor90,semiminor90, error_strike, depth_error)
    # Get the metadata
    metadata = get_origin_metadata(row)
    return Origin(origin_id, date, time, location, author,
                  time_error=time_error, time_rms=time_rms,
                  metadata=metadata)

def get_event_magnitude(row, event_id,
                        selected_agencies=[]):
                        #,selected_types=[]):
    """
    Creates an instance of an isf_catalogue.Magnitude object from the row
    string, or returns None if the author is not one of the selected agencies
    or magnitude types
    """
    origin_id = _to_str(row[30:])
    author = row[20:29].strip(' ')
    scale=row[:5].strip(' ')
    if (len(selected_agencies) and not author in selected_agencies):
        #\
        #or (len(selected_agencies) and not scale in selected_types)):
        # Magnitude does not correspond to a selected agency - ignore
        return None
    sigma = _to_float(row[11:14])
    nstations = _to_int(row[15:19])
    return Magnitude(event_id, origin_id, _to_float(row[6:10]), author,
                     scale=scale, sigma=sigma, stations=nstations)

class ISFReader(BaseCatalogueDatabaseReader):
    '''
    Class to read an ISF formatted earthquake catalogue considering only
    the origin agencies, the magnitude agencies and the magnitude types
    defined by the user
    '''

    def __init__(self, filename,
                 selected_origin_agencies=[],
                 selected_magnitude_agencies=[],
                 #selected_magnitude_types =[],
                 rejection_keywords=[],
                 lower_magnitude=None, upper_magnitude=None):

        super(ISFReader, self).__init__(filename,
                                        selected_origin_agencies,
                                        selected_magnitude_agencies)
#                                        ,selected_magnitude_types)

        self.rejected_catalogue = []
        self.rejection_keywords = rejection_keywords
        if lower_magnitude and upper_magnitude:
            assert upper_magnitude > lower_magnitude
        if lower_magnitude:
            self.lower_mag = lower_magnitude
        else:
            self.lower_mag = -np.inf
        if upper_magnitude:
            self.upper_mag = upper_magnitude
        else:
            self.upper_mag = np.inf

    def read_file(self, identifier, name):
        """
        Reads the catalogue from the file and assigns the identifier and name
        """
        self.catalogue = ISFCatalogue(identifier, name)
        f = open(self.filename, 'rt')
        counter = 0
        is_origin = False
        is_magnitude = False
        comment_str = ""
        for row in f.readlines():
            if not row.rstrip('\n'):
                # Ignore empty rows
                continue
            elif "DATA_TYPE EVENT IMS1.0" in row:
                # Ignore header row
                continue
            elif "ISC Bulletin" in row:
                # Yet anothet header row
                continue
            elif "STOP" in row:
                # Footer row
                continue
            else:
                pass

            if '(#PRIME)' in row:
                # Previous origin block was the prime origin
                if len(origins) > 0:
                    origins[-1].is_prime = True
                continue

            if '(#CENTROID)' in row:
                # Previous origin block is a centroid
                if len(origins) > 0:
                    origins[-1].is_centroid = True
                continue

            comment_find = re.search("\((.*?)\)", row)
            if comment_find:
                comment_find.group(1)
                comment_str += "{:s}\n".format(comment_find.group(1))
                continue

            if 'Event' in row[:5]:
                # Is an event header row
                if counter > 0:
                    # Add magnitudes and origins to previous event
                    # and previous event to catalogue
                    Event.origins = origins
                    Event.magnitudes = magnitudes
                    if len(Event.origins) and len(Event.magnitudes):
                        Event.assign_magnitudes_to_origins()
                        Event.comment = comment_str
                        #print "%s - %s" % (Event.id, Event.description)
                        #print Event.comment
                        if self._acceptance(Event):
                            Event.comment = ""
                            self.catalogue.events.append(Event)

                # Get a new event
                Event = get_event_header_row(row.rstrip('\n'))
                comment_str = ""
                origins = []
                magnitudes = []
                counter += 1
                continue

            if row.rstrip('\n') == origin_header:
                is_origin = True
                is_magnitude = False
                continue
            elif row.strip('\n') == magnitude_header:
                is_origin = False
                is_magnitude = True
                continue
            else:
                pass

            if is_magnitude and len(row.strip('\n')) == 38:
                # Is a magnitude row
                mag = get_event_magnitude(row.strip('\n'),
                                          Event.id,
                                          self.selected_magnitude_agencies)
#                                          ,self.selected_magnitude_types)

                if mag:
                    magnitudes.append(mag)
                continue

            if is_origin and len(row.strip('\n')) == 136:
                # Is an origin row
                orig = get_event_origin_row(row.strip('\n'),
                                            self.selected_origin_agencies)
                if orig:
                    origins.append(orig)

        if len(self.rejected_catalogue):
            # Turn list of rejected events into its own instance of
            # ISFCatalogue
            self.rejected_catalogue = ISFCatalogue(
                identifier + "-R",
                name + " - Rejected",
                events=self.rejected_catalogue)
        return self.catalogue

    def _acceptance(self, event):
        """
        Determines whether to accept the event according to the magnitude
        and keyword criteria
        :param event:
            Event as instance of Event class
        :returns:
            True (if event is accepted), False otherwise
        """
        # Magnitude rejection - based on an "any" criterion
        valid_magnitude = False
        for mag in event.magnitudes:
            if (mag.value >= self.lower_mag) and (mag.value <= self.upper_mag):
                valid_magnitude = True
                break
        if not valid_magnitude:
            return False
        for keyword in self.rejection_keywords:
            if keyword.lower() in event.comment.lower():
                self.rejected_catalogue.append(event)
                return False
        return True

<|endoftext|>


<commit_before>

import re

def assign_and_return_positions(alphabet, rotation):
    """

    """
    i = 0
    rotation_dict = dict()

    while i < len(alphabet):

        i_plus_rotation = (i + rotation) % len(alphabet)

        rotation_dict[alphabet[i]] = alphabet[i_plus_rotation]
        i += 1

    return rotation_dict

def apply_substitution(subst_dict, cleaned_string):
    """ Apply a substitution dictionary to a string.

    """
    encoded_string = ''

    # Slightly confusing, the get function will get the value of the
    # key named letter or will return letter.
    for letter in cleaned_string:
        encoded_string += subst_dict.get(letter, letter)

    return encoded_string

def clean_string(input_string):
    """ Reduce the input string to a-z.

    This is text munging. See the wikipedia article and python re docs.

    """
    allowed_chars = r'[^a-z .,;:\-_]'

    repl_char = '' # delete... will this work with None?

    return re.sub(allowed_chars,repl_char,input_string)

if __name__ == '__main__':
    """ Perform rotational encryption on an input.

    """

    alphabet = r'abcdefghijklmnopqrstuvwxyz'
    rotation = 13

    rotation_dict = assign_and_return_positions(alphabet, rotation)

    input_string = "Alphabet Soup; Acceptable Inputs: a-z, comma, period, colon, space, hyphen, underscore, and semicolon. Uppercase letters will be converted to lowercase."

    cleaned_input = clean_string(input_string.lower())

    print rotation_dict
    print cleaned_input

    encoded_input = apply_substitution(rotation_dict, cleaned_input)

    print encoded_input

<commit_msg>Reformatted code and added notes in rot13.py.<commit_after>"""
A sample encryption library using rot13.

"""

# File encoding PEP, see: http://legacy.python.org/dev/peps/pep-0263/
# -*- coding: utf-8 -*-

import re

def assign_and_return_positions(alphabet, rotation):
    """ This is an arbitrary rotation on a 26 character alphabet.

    """
    i = 0
    rotation_dict = dict()

    while i < len(alphabet):

        i_plus_rotation = (i + rotation) % len(alphabet)

        rotation_dict[alphabet[i]] = alphabet[i_plus_rotation]
        i += 1

    return rotation_dict

def apply_substitution(subst_dict, cleaned_string):
    """ Apply a substitution dictionary to a string.

    """
    encoded_string = ''

    # Slightly confusing, the get function will get the value of the
    # key named letter or will return letter.
    for letter in cleaned_string:
        encoded_string += subst_dict.get(letter, letter)

    return encoded_string

def clean_string(input_string):
    """ Reduce the input string to a-z.

    This is text munging. See the wikipedia article and python re docs.
    """
    allowed_chars = r'[^a-z .,;:\-_]'

    repl_char = '' # delete... will this work with None?

    return re.sub(allowed_chars,repl_char,input_string)

if __name__ == '__main__':
    """ Perform rotational encryption on an input.

    """

    alphabet = r'abcdefghijklmnopqrstuvwxyz'
    rotation = 13

    rotation_dict = assign_and_return_positions(alphabet, rotation)

    input_string = "Alphabet Soup; Acceptable Inputs: a-z, comma, period, colon, space, hyphen, underscore, and semicolon. Uppercase letters will be converted to lowercase."

    cleaned_input = clean_string(input_string.lower())

    print rotation_dict
    print cleaned_input

    encoded_input = apply_substitution(rotation_dict, cleaned_input)

    print encoded_input

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright (c) 2016-2017 Ircam
# Copyright (c) 2016-2017 Guillaume Pellerin
# Copyright (c) 2016-2017 Emilie Zawadzki

# This file is part of mezzanine-organization.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import requests
from optparse import make_option
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from organization.network.models import Person
from django.utils.text import slugify

class Command(BaseCommand):
    help = """Import figgo id from api
    python manage.py import-figo-id
    """
    number_of_person = 0
    def handle(self, *args, **options):
        # process active person
        r_p_active = requests.get('https://ircam.ilucca.net/api/users?fields=id,lastname,firstname',
        headers={'Authorization': 'Lucca application=bd6d5481-40eb-414b-9135-434e12749223'})
        self.update_external_id(r_p_active.json())

        # process INactive person
        r_p_inactive = requests.get('https://ircam.ilucca.net/api/users?dtContractEnd=until,2016-12-31,null&fields=id,lastname,firstname',
        headers={'Authorization': 'Lucca application=bd6d5481-40eb-414b-9135-434e12749223'})
        self.update_external_id(r_p_inactive.json())

        print('***************************************************')
        print("Number of person processed : "+str(self.number_of_person))
        print('***************************************************')

    def update_external_id(self, figgo_users):
        for figgo_user in figgo_users['data']:
            slug = slugify(figgo_user['firstName'].lower()+'-'+figgo_user['lastName'].lower()) #).replace( ' ', '-')
            person = Person.objects.filter(slug__contains=slug)
            if person:
                self.number_of_person += 1
                # persons have sometimes two ids
                for p in person:
                    p.external_id = figgo_user['id']
                    p.save()
            else :
                print("Person not found: "+figgo_user['lastName']+' '+figgo_user['firstName']+' | manual slug : '+ slug)
<commit_msg>[Timesheet] : url to api<commit_after># -*- coding: utf-8 -*-
#
# Copyright (c) 2016-2017 Ircam
# Copyright (c) 2016-2017 Guillaume Pellerin
# Copyright (c) 2016-2017 Emilie Zawadzki

# This file is part of mezzanine-organization.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import requests
from optparse import make_option
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from organization.network.models import Person
from django.utils.text import slugify

class Command(BaseCommand):
    help = """Import figgo id from api
    python manage.py import-figo-id
    """
    number_of_person = 0
    def handle(self, *args, **options):
        # process active person
        r_p_active = requests.get(settings.FIGGO_API_URL_PROD+'api/users?fields=id,lastname,firstname',
        headers={'Authorization': 'Lucca application=bd6d5481-40eb-414b-9135-434e12749223'})
        self.update_external_id(r_p_active.json())

        # process INactive person
        r_p_inactive = requests.get(settings.FIGGO_API_URL_PROD+'api/users?dtContractEnd=until,2016-12-31,null&fields=id,lastname,firstname',
        headers={'Authorization': 'Lucca application=bd6d5481-40eb-414b-9135-434e12749223'})
        self.update_external_id(r_p_inactive.json())

        print('***************************************************')
        print("Number of person processed : "+str(self.number_of_person))
        print('***************************************************')

    def update_external_id(self, figgo_users):
        for figgo_user in figgo_users['data']:
            slug = slugify(figgo_user['firstName'].lower()+'-'+figgo_user['lastName'].lower()) #).replace( ' ', '-')
            person = Person.objects.filter(slug__contains=slug)
            if person:
                self.number_of_person += 1
                # persons have sometimes two ids
                for p in person:
                    p.external_id = figgo_user['id']
                    p.save()
            else :
                print("Person not found: "+figgo_user['lastName']+' '+figgo_user['firstName']+' | manual slug : '+ slug)
<|endoftext|>


<commit_before>from myuw_mobile.views.rest_dispatch import RESTDispatch
from restclients.trumba import get_calendar_by_name
from django.http import HttpResponse
import json
import re

class AcademicEvents(RESTDispatch):
    """
    Performs actions on /api/v1/academic_events
    """
    def GET(self, request):
        events = []

        cal_names = ['sea_acad-comm', 'sea_acad-inst', 'sea_acad-holidays']

        calendars = []
        for cal in cal_names:
            calendars.append(get_calendar_by_name(cal))

        for calendar in calendars:
            for event in calendar.walk('vevent'):
                events.append(self.json_for_event(event))

        return HttpResponse(json.dumps(events))

    def json_for_event(self, event):
        year, quarter = self.parse_year_quarter(event)
        start, end = self.parse_dates(event)
        category = self.parse_category(event)
        event_url = self.parse_event_url(event)

        is_all_day = self.parse_event_is_all_day(event)

        return {
            "summary": event.get('summary'),
            "start": start,
            "end": end,
            "year": year,
            "quarter": quarter,
            "category": category,
            "event_url": event_url,
            "is_all_day": is_all_day,
        }

    def parse_event_is_all_day(self, event):
        start = event.get('dtstart')
        end = event.get('dtend')

        diff = end.dt - start.dt
        if diff.days == 1 and diff.seconds == 0:
            return True

        return False

    def parse_category(self, event):
        return event.get('categories')

    def parse_event_url(self, event):
        uid = event.get('uid')

        matches = re.match('.*?(\d+)$', uid)
        if not matches:
            return

        event_id = matches.group(1)

        url = ("http://www.washington.edu/calendar/academic/"
               "?trumbaEmbed=view%%3Devent%%26eventid%%3D%s" % (event_id))

        return url

    def parse_dates(self, event):
        return (self.format_datetime(event.get('dtstart')),
                self.format_datetime(event.get('dtend')))

    def parse_year_quarter(self, event):
        desc = event.get('description')

        if not desc:
            return None, None

        matches = re.match(".*Year: (\d{4})\s+Quarter: (\w+).*", desc)
        if matches:
            return matches.group(1), matches.group(2)

        else:
            matches = re.match(".*Year: (\d{4}).*", desc)
            if matches:
                return matches.group(1), None

        return None, None

    def format_datetime(self, dt):
        return str(dt.dt)
<commit_msg>Work on MUWM-2395<commit_after>from myuw_mobile.views.rest_dispatch import RESTDispatch
from myuw_mobile.dao.term import get_comparison_date, get_current_quarter
from restclients.trumba import get_calendar_by_name
from restclients.sws.term import get_term_after
from django.http import HttpResponse
import json
import re

class AcademicEvents(RESTDispatch):
    """
    Performs actions on /api/v1/academic_events
    """
    def GET(self, request):
        events = []

        cal_names = ['sea_acad-comm', 'sea_acad-inst', 'sea_acad-holidays']

        calendars = []
        for cal in cal_names:
            calendars.append(get_calendar_by_name(cal))

        raw_events = []
        for calendar in calendars:
            for event in calendar.walk('vevent'):
                raw_events.append(event)

        raw_events = self.sort_events(raw_events)

        raw_events = self.filter_past_events(request, raw_events)
        raw_events = self.filter_too_future_events(request, raw_events)

        for event in raw_events:
            events.append(self.json_for_event(event))

        return HttpResponse(json.dumps(events))

    def json_for_event(self, event):
        year, quarter = self.parse_year_quarter(event)
        start, end = self.parse_dates(event)
        category = self.parse_category(event)
        event_url = self.parse_event_url(event)

        is_all_day = self.parse_event_is_all_day(event)

        return {
            "summary": event.get('summary'),
            "start": start,
            "end": end,
            "year": year,
            "quarter": quarter,
            "category": category,
            "event_url": event_url,
            "is_all_day": is_all_day,
        }

    def parse_event_is_all_day(self, event):
        start = event.get('dtstart')
        end = event.get('dtend')

        diff = end.dt - start.dt
        if diff.days == 1 and diff.seconds == 0:
            return True

        return False

    def parse_category(self, event):
        return event.get('categories')

    def parse_event_url(self, event):
        uid = event.get('uid')

        matches = re.match('.*?(\d+)$', uid)
        if not matches:
            return

        event_id = matches.group(1)

        url = ("http://www.washington.edu/calendar/academic/"
               "?trumbaEmbed=view%%3Devent%%26eventid%%3D%s" % (event_id))

        return url

    def parse_dates(self, event):
        return (self.format_datetime(event.get('dtstart')),
                self.format_datetime(event.get('dtend')))

    def parse_year_quarter(self, event):
        desc = event.get('description')

        if not desc:
            return None, None

        matches = re.match(".*Year: (\d{4})\s+Quarter: (\w+).*", desc)
        if matches:
            return matches.group(1), matches.group(2)

        else:
            matches = re.match(".*Year: (\d{4}).*", desc)
            if matches:
                return matches.group(1), None

        return None, None

    def format_datetime(self, dt):
        return str(dt.dt)

    def sort_events(self, events):
        return sorted(events,
                      key=lambda e: e.get('dtstart').dt
                      )

    def filter_past_events(self, request, events):
        comparison_date = get_comparison_date(request)

        non_past = []
        for event in events:
            if event.get('dtend').dt >= comparison_date:
                non_past.append(event)

        return non_past

    def filter_too_future_events(self, request, events):
        current = get_current_quarter(request)
        last = get_term_after(get_term_after(get_term_after(get_term_after(current))))

        not_too_future = []
        for event in events:
            if event.get('dtstart').dt <= last.grade_submission_deadline.date():
                not_too_future.append(event)

        return not_too_future
<|endoftext|>


<commit_before>from bottle import get, request, post
import bigchain_client
import json

@get('/keys')
def keys():
    user_priv, user_pub = bigchain_client.generate_key_pair()
    return json.dumps({'private': user_priv, 'public': user_pub})

@post('/store')
def store():
    public_key = request.forms.get('pub', '').strip()
    if not public_key:
        public_key = request.GET.get('pub', '').strip()

    asset = request.forms.get('asset', '').strip()
    if not asset:
        asset = request.GET.get('asset', '').strip()

    digital_asset_payload = get_asset_payload(asset)

    store_result = bigchain_client.store_asset(public_key, digital_asset_payload)

    return json.dumps({'pub': public_key, 'asset': digital_asset_payload, 'result': store_result})

@get('/exists')
def exists():
    asset = request.GET.get('asset', '').strip()

    digital_asset_payload = get_asset_payload(asset)
    result = bigchain_client.exists(digital_asset_payload)

    return json.dumps({'result': result})

def get_asset_payload(asset):
    return {'asset': asset}

# run(host='localhost', port=8777, debug=True)
<commit_msg>get_request_param method<commit_after>from bottle import get, request, post
import bigchain_client
import json

@get('/keys')
def keys():
    user_priv, user_pub = bigchain_client.generate_key_pair()
    return json.dumps({'private': user_priv, 'public': user_pub})

@post('/store')
def store():
    public_key = get_request_param('pub')
    asset = get_request_param('asset')

    digital_asset_payload = get_asset_payload(asset)

    store_result = bigchain_client.store_asset(public_key, digital_asset_payload)

    return json.dumps({'pub': public_key, 'asset': digital_asset_payload, 'result': store_result})

@get('/exists')
def exists():
    asset = request.GET.get('asset', '').strip()

    digital_asset_payload = get_asset_payload(asset)
    result = bigchain_client.exists(digital_asset_payload)

    return json.dumps({'result': result})

def get_asset_payload(asset):
    return {'asset': asset}

def get_request_param(param_name):
    result = request.forms.get(param_name, '').strip()
    if not result:
        result = request.GET.get(param_name, '').strip()
    return result

# run(host='localhost', port=8777, debug=True)
<|endoftext|>