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

import re
import json
#from serial import Serial
from time import sleep
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
import stream as streams

class Car(object):

    def __init__(self, *args, **kwargs):
        self.serial = Serial(*args, **kwargs)

    def forward(self):
        self.serial.write(b'f')

    def backward(self):
        self.serial.write(b'b')

    def left(self):
        self.serial.write(b'l')

    def right(self):
        self.serial.write(b'r')

car = Car('/dev/ttyACM0', 115200)

sleep(2)

def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler, port=8000):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()

class StreamController():
    """ Controls the thread for raspivid and netcat """
    stream = None
    running = False

    def control(self, t):
        if t:
            if self.stream != None:
                if self.stream.isAlive():
                    return {"running": True}
                else:
                    self.stream = streams.Stream()
            else:
                self.stream = streams.Stream()
            self.stream.start()
            return {"running": True}
        else:
            self.stream.stop()
            self.stream.join()
            return {"running": False}

class PictureController():
    "Blocks the main thread until the camera has taken an image"

    camera = None

    def control(self):
            self.camera = camera.Camera()
            return self.camera.take_picture()

stream_controller = StreamController()
picture_controller = PictureController()

class RequestHandler(BaseHTTPRequestHandler):

    log_file = open('marsem.log', 'w')
    stream = None

    urls = {
        'control': r'^/$',
        'stream_controller': r'^/stream/$',
        'picture_controller': r'^/picture/$'
    }

    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-Type', 'application/json')
        self.end_headers()

        result = urlparse(self.path)
        path = result.path
        query = {}

        for key, value in parse_qs(result.query).items():
            if len(value) == 1:
                value = value[0]
                try:
                    value = json.loads(value)
                except ValueError:
                    pass
            query[key] = value

        for method, regex in self.urls.items():
            if re.match(regex, path):
                data = getattr(self, method)(query)
                self.wfile.write(json.dumps(data).encode())

    def control(self, query):
        action = query.get('action')
        if action:
            action = getattr(car, action, None)
            if action:
                action()

        return {}

    def stream_controller(self, query):
        param = query.get('stream')
        if param.lower() == 'true':
            return json.dumps(stream_controller.control(True))
        else:
            return json.dumps(stream_controller.control(False))

    """ Takes a picture with the raspberry camera, returns a binary of the image. """
    def picture_controller(self, query):
        if stream_controller.running:
            self.send_error(503,
                            message="Camera is busy",
                            explain="The camera is currently busy with streaming")
            return
        else:
            picture = picture_controller.control()
            self.wfile.write(picture)
            return ""

    def log_message(self, format, *args):
        self.log_file.write("%s - - [%s] %s\n" %
                            (self.client_address[0],
                             self.log_date_time_string(),
                             format%args))

if __name__ == '__main__':
    run(handler_class=RequestHandler)
<commit_msg>Fixed comment<commit_after>#!/usr/bin/python3 -tt
# -*- coding: utf-8 -*-

import re
import json
from serial import Serial
from time import sleep
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
import stream as streams

class Car(object):

    def __init__(self, *args, **kwargs):
        self.serial = Serial(*args, **kwargs)

    def forward(self):
        self.serial.write(b'f')

    def backward(self):
        self.serial.write(b'b')

    def left(self):
        self.serial.write(b'l')

    def right(self):
        self.serial.write(b'r')

car = Car('/dev/ttyACM0', 115200)

sleep(2)

def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler, port=8000):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()

class StreamController():
    """ Controls the thread for raspivid and netcat """
    stream = None
    running = False

    def control(self, t):
        if t:
            if self.stream != None:
                if self.stream.isAlive():
                    return {"running": True}
                else:
                    self.stream = streams.Stream()
            else:
                self.stream = streams.Stream()
            self.stream.start()
            return {"running": True}
        else:
            self.stream.stop()
            self.stream.join()
            return {"running": False}

class PictureController():
    "Blocks the main thread until the camera has taken an image"

    camera = None

    def control(self):
            self.camera = camera.Camera()
            return self.camera.take_picture()

stream_controller = StreamController()
picture_controller = PictureController()

class RequestHandler(BaseHTTPRequestHandler):

    log_file = open('marsem.log', 'w')
    stream = None

    urls = {
        'control': r'^/$',
        'stream_controller': r'^/stream/$',
        'picture_controller': r'^/picture/$'
    }

    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-Type', 'application/json')
        self.end_headers()

        result = urlparse(self.path)
        path = result.path
        query = {}

        for key, value in parse_qs(result.query).items():
            if len(value) == 1:
                value = value[0]
                try:
                    value = json.loads(value)
                except ValueError:
                    pass
            query[key] = value

        for method, regex in self.urls.items():
            if re.match(regex, path):
                data = getattr(self, method)(query)
                self.wfile.write(json.dumps(data).encode())

    def control(self, query):
        action = query.get('action')
        if action:
            action = getattr(car, action, None)
            if action:
                action()

        return {}

    def stream_controller(self, query):
        param = query.get('stream')
        if param.lower() == 'true':
            return json.dumps(stream_controller.control(True))
        else:
            return json.dumps(stream_controller.control(False))

    """ Takes a picture with the raspberry camera, returns a binary of the image. """
    def picture_controller(self, query):
        if stream_controller.running:
            self.send_error(503,
                            message="Camera is busy",
                            explain="The camera is currently busy with streaming")
            return
        else:
            picture = picture_controller.control()
            self.wfile.write(picture)
            return ""

    def log_message(self, format, *args):
        self.log_file.write("%s - - [%s] %s\n" %
                            (self.client_address[0],
                             self.log_date_time_string(),
                             format%args))

if __name__ == '__main__':
    run(handler_class=RequestHandler)
<|endoftext|>


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

from __future__ import (print_function, unicode_literals, division,
                        absolute_import)

import os
import re
import subprocess
import sys

import openstack

from dfs_sdk import scaffold
from dfs_sdk.scaffold import vprint

OS_PREFIX = "OS-"
UNMANAGE_PREFIX = "UNMANAGED-"

# Taken from this SO post :
# http://stackoverflow.com/a/18516125
# Using old-style string formatting because of the nature of the regex
# conflicting with new-style curly braces
UUID4_STR_RE = ("%s[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab]"
                "[a-f0-9]{3}-?[a-f0-9]{12}")
UUID4_RE = re.compile(UUID4_STR_RE % OS_PREFIX)
verbose = False

def usage():
    print("OS_USERNAME, OS_PASSWORD, OS_AUTH_URL and "
          "OS_PROJECT_NAME must be set")

def exe(cmd):
    vprint("Running cmd:", cmd)
    return subprocess.check_output(cmd, shell=True)

def main(args):

    api = scaffold.get_api()

    conn = openstack.connect()
    if args.all_projects_all_tenants:
        ids = exe("openstack volume list --all-projects --format value | "
                  "awk '{print $1}'").split("\n")
        vols = {"OS-{}".format(vid) for vid in ids if vid}

        pids = exe("openstack project list --format value "
                   "| awk '{print $1}'").split("\n")
        imgids = []
        for pid in [p for p in pids if p]:
            try:
                imgids.extend(
                    exe("openstack --os-project-id {} image list --format "
                        "value | awk '{{print $1}}'".format(pid)).split("\n"))
            except subprocess.CalledProcessError:
                pass
        imgids = set(imgids)
        vols = vols.union(
                {"OS-{}".format(imgid) for imgid in imgids if imgid})

    else:
        vols = {"OS-{}".format(vol.id) for vol in conn.block_storage.volumes()}
        vols = vols.union(
                {"OS-{}".format(img.id) for img in conn.image.images()})

    non_os = set()
    ais = api.app_instances.list()
    for ai in ais:
        if ai['name'] not in vols:
            non_os.add(ai['name'])

    pdisplay = "all" if args.all_projects_all_tenants else os.getenv(
        "OS_PROJECT_NAME")
    if args.only_os_orphans:
        for ai in sorted(non_os):
            if UUID4_RE.match(ai):
                print(ai)
    else:
        print("OpenStack Project:", pdisplay)
        print("Datera Tenant: ", scaffold.get_config()["tenant"])
        print()
        print("Datera OpenStack AIs")
        print("--------------------")
        for ai in sorted(vols):
            print(ai)
        print("\nDatera Non-OpenStack AIs")
        print("------------------------")
        for ai in sorted(non_os):
            print(ai)

if __name__ == "__main__":

    parser = scaffold.get_argparser()
    parser.add_argument('--all-projects-all-tenants', action='store_true')
    parser.add_argument('--only-os-orphans', action='store_true')
    parser.add_argument('--only-cached-images', action='store_true')
    args = parser.parse_args()
    sys.exit(main(args))
<commit_msg>Updates to show_os_volumes to account for changes in naming<commit_after>#!/usr/bin/env python

from __future__ import (print_function, unicode_literals, division,
                        absolute_import)

import os
import re
import subprocess
import sys

import openstack

from dfs_sdk import scaffold
from dfs_sdk.scaffold import vprint

# Taken from this SO post :
# http://stackoverflow.com/a/18516125
# Using old-style string formatting because of the nature of the regex
# conflicting with new-style curly braces
UUID4_STR_RE = ("[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab]"
                "[a-f0-9]{3}-?[a-f0-9]{12}")
UUID4_RE = re.compile(UUID4_STR_RE)
verbose = False

def usage():
    print("OS_USERNAME, OS_PASSWORD, OS_AUTH_URL and "
          "OS_PROJECT_NAME must be set")

def exe(cmd):
    vprint("Running cmd:", cmd)
    return subprocess.check_output(cmd, shell=True)

def main(args):

    api = scaffold.get_api()

    conn = openstack.connect()

    # Earlier versions of openstacksdk didn't let us filter by tenant
    # So multi-project accumulation has to be done via openstackclient
    if args.all_projects_all_tenants:
        # Get all openstack volume UUIDs
        ids = exe("openstack volume list --all-projects --format value | "
                  "awk '{print $1}'").split("\n")
        # Filter Nones
        vols = {vid for vid in ids if vid}

        # Get all openstack projects
        pids = exe("openstack project list --format value "
                   "| awk '{print $1}'").split("\n")

        # Iterate through projects and accumulate image ids
        imgids = []
        for pid in [p for p in pids if p]:
            try:
                imgids.extend(
                    exe("openstack --os-project-id {} image list --format "
                        "value | awk '{{print $1}}'".format(pid)).split("\n"))
            except subprocess.CalledProcessError:
                pass
        imgids = set(imgids)
        vols = vols.union(
                {imgid for imgid in imgids if imgid})

    else:
        vols = {str(vol.id) for vol in conn.block_storage.volumes()}
        vols = vols.union(
                {str(img.id) for img in conn.image.images()})

    yes_os, non_os = set(), set()
    ais = api.app_instances.list()
    for ai in ais:
        uid = UUID4_RE.search(ai['name'])
        if not uid or uid.group() not in vols:
            non_os.add(ai['name'])
        else:
            yes_os.add(ai['name'])

    pdisplay = "all" if args.all_projects_all_tenants else os.getenv(
        "OS_PROJECT_NAME")
    if args.only_os_orphans:
        for ai_name in sorted(non_os):
            if ai_name.startswith("OS-"):
                print(ai_name)
    else:
        print("OpenStack Project:", pdisplay)
        print("Datera Tenant: ", scaffold.get_config()["tenant"])
        print()
        print("Datera OpenStack AIs")
        print("--------------------")
        for ai_name in sorted(yes_os):
            print(ai_name)
        print("\nDatera Non-OpenStack AIs")
        print("------------------------")
        for ai in sorted(non_os):
            print(ai_name)

if __name__ == "__main__":

    parser = scaffold.get_argparser()
    parser.add_argument('--all-projects-all-tenants', action='store_true')
    parser.add_argument('--only-os-orphans', action='store_true')
    # parser.add_argument('--only-cached-images', action='store_true')
    args = parser.parse_args()
    sys.exit(main(args))
<|endoftext|>


<commit_before>from __future__ import unicode_literals, division, print_function
import io
import csv
import random
import itertools
import heapq
from recordclass import recordclass
from numpy import array

class Station(recordclass('Station', b'station_id name operator voltages frequencies lines')):
    def __hash__(self):
        return hash(self.station_id)

class Line(recordclass('Line', b'line_id operator left right length frequencies voltages resistance reactance capacitance')):
    def __hash__(self):
        return hash(self.line_id)

    def __repr__(self):
        return "{0}: {1} -> {2}".format(self.line_id, self.left.name, self.right.name).encode('utf-8')

    @property
    def susceptance(self):
        if self.capacitance is None or self.frequency is None:
            return None
        return self.capacitance * self.frequency

class Network(object):
    def __init__(self):
        self.stations = dict()
        self.lines    = dict()
        self._areas   = dict()

    def connected_sets(self):
        # bfs algorithm to find connected sets in the network
        unseen = set(self.stations.values())
        connected = []
        while unseen:
            current = []
            root  = unseen.pop()
            queue = [root]
            while queue:
                node = queue.pop()
                if node in unseen:
                    unseen.remove(node)
                    current.append(node)
                for line in node.lines:
                    if line.left in unseen:
                        queue.append(line.left)
                    if line.right in unseen:
                        queue.append(line.right)
            connected.append(current)
        return connected

    def patch(self):
        # flood algorithm to patch all lines and stations with values from neighbours
        totals = list()
        while True:
            changes = 0
            for station in self.stations.itervalues():
                line_voltages    = set(v for line in station.lines for v in line.voltages)
                line_frequencies = set(f for line in station.lines for f in line.frequencies)
                if line_voltages - station.voltages:
                    station.voltages |= line_voltages
                    changes          += 1
                if line_frequencies - station.frequencies:
                    station.frequencies |= line_frequencies
                    changes             += 1

            for line in self.lines.itervalues():
                shared_frequencies = line.left.frequencies & line.right.frequencies
                if shared_frequencies and not line.frequencies & shared_frequencies:
                    line.frequencies |= shared_frequencies
                    changes += 1
                elif not line.frequencies:
                    if line.left.frequencies:
                        line.frequencies = set(line.left.frequencies)
                        changes += 1
                    elif line.right.frequencies:
                        line.frequencies = set(line.right.frequencies)
                        changes += 1

                shared_voltages = line.left.voltages & line.right.voltages
                if shared_voltages and not line.voltages & shared_voltages:
                    line.voltages |= shared_voltages
                    changes += 1
                elif not line.voltages:
                    if line.left.voltages:
                        line.voltages = set(line.left.voltages)
                        changes += 1
                    elif line.right.voltages:
                        line.voltages = set(line.right.voltages)
                        changes += 1

            if changes == 0:
                break
            totals.append(changes)
            if len(totals) > 1000:
                print(totals)
                raise Exception('dont think ill be stopping soon')
        return totals

    def report(self):
        # calculate missing values statically
        broken_stations = 0
        broken_lines    = 0
        mismatches      = 0
        for station in self.stations.itervalues():
            if not station.voltages or not station.frequencies:
                broken_stations += 1
            for line in station.lines:
                if station.frequencies:
                    if line.frequencies ^ station.frequencies:
                        mismatches += 1
                        continue
                elif line.frequencies:
                    mismatches += 1
                    continue
                if station.voltages:
                    if line.voltages ^ station.voltages:
                        mismatches += 1
                        continue
                elif line.voltages:
                    mismatches += 1
                    continue

        for line in self.lines.itervalues():
            if not line.voltages or not line.frequencies:
                broken_lines += 1
        return broken_stations, broken_lines, mismatches

    def find(self, from_station, to_station):
        lines = []
        pass

    def _area_number(self, area_name):
        if area_name not in self._areas:
            # assign next area number
            self._areas[area_name] = len(self._areas) + 1
        return self._areas[area_name]

    def powercase(self, loads=None):
        # FIXME this needs fixing for the complex-lines case

        # loads is a map of station id -> load, either positive or
        # negative; a negative load is represented by a generator.

        # if no loads map is passed, generate an 'electrified pair' of
        # two random nodes, one of which delivers power, the other
        # consumes it
        if loads is None:
            loads = self._electrified_pair()
        ppc = {
            "version": 2,
            "baseMVA": 100.0
        }
        nodes        = list()
        edges        = list()
        generators   = list()

        station_to_bus = dict()
        bus_id_gen     = itertools.count()

        for station in self.stations.itervalues():
            # because we do a DC PF, we ignore frequencies completely
            minv, maxv = min(station.voltages), max(station.voltages)
            for voltage in station.voltages:
                if station.station_id in loads and voltage == minv:
                    bus_load = loads[station.station_id]
                else:
                    bus_load = 0
                bus_id = next(bus_id_gen)
                station_to_bus[station.station_id, voltage] = bus_id
                if bus_load < 0:
                    # it is a generator instead of a load, insert it
                    generators.append(self._make_generator(bus_id, -bus_load))
                    bus_load = 0
                nodes.append(self._make_bus(station, voltage, bus_load, bus_id))

            for voltage in station.voltages:
                if voltage != maxv:
                    # create a transformer branch from max voltage to this voltage
                    from_bus = station_to_bus[station.station_id, maxv]
                    to_bus   = station_to_bus[station.station_id, voltage]
                    edges.append(self._make_transformer(from_bus, to_bus))

        for line in self.lines.itervalues():
            # create branches between stations
            from_bus = station_to_bus[line.left.station_id, line.voltage]
            to_bus   = station_to_bus[line.right.station_id, line.voltage]
            edges.append(self._make_line(line, from_bus, to_bus))

        ppc['bus']    = array(nodes)
        ppc['gen']    = array(generators)
        ppc['branch'] = array(edges)
        return ppc

    def _electrified_pair(self):
        src, dst = random.sample(self.stations, 2)
        return {
            src: -100, # MW
            dst: 50,  # MW
        }

    def _make_bus(self, station, voltage, load, bus_id):
        # see pypower.caseformat for documentation on how this works
        area_nr  = self._area_number(station.operator)
        base_kv  = voltage // 1000
        return [
            bus_id,
            3,       # slack bus
            load,    # real load in MW
            0,       # reactive load MVAr, zero because DC
            0,       # shunt conductance
            0,       # shunt susceptance
            area_nr, # area number
            1.0,     # voltage magnitude per unit
            0,       # voltage angle
            base_kv, # base voltage (per unit base)
            area_nr, # loss zone nr
            1.1,     # max voltage per unit
            0.9,     # min voltage per unit
        ]

    def _make_transformer(self, from_bus, to_bus):
        return [
            from_bus,
            to_bus,
            0.01,      # resistance
            0.01,      # reactance
            0.01,      # line charging susceptance
            200,       # long term rating (MW)
            200,       # short term rating (MW)
            200,       # emergency rating (MW)
            1,         # off-nominal (correction) taps ratio, 1 for no correction
            0,         # transformer phase shift angle,
            1,         # status (1 = on)
            -360,      # minimum angle
            360,       # maximum angle
        ]

    def _make_line(self, line, from_bus, to_bus):
        return [
            from_bus,
            to_bus,
            line.resistance or 0.01,   # default value if None
            line.reactance or 0.01,
            line.susceptance or 0.01,
            200,
            200,
            200,
            0,                          # not a transformer
            0,                          # not a transformer
            1,                          # status
            -360,
            360
        ]

    def _make_generator(self, bus_id, power_output):
        return [
            bus_id,
            power_output,
            0,             # reactive power output
            0,             # maximum reactive power output
            0,             # minimum reactive power output
            1.0,           # per-unit voltage magnitude setpoint
            100,           # base MVA
            1,             # status (on)
            power_output,  # maximum real power output
            0,             # minimum real power output
            0,             # Pc1, irrelevant
            0,             # Pc2
            0,             # Qc1min
            0,             # Qc1max
            0,             # Qc2min
            0,             # Qc2max
            5,             # ramp rate load-following (MW/min)
            5,             # ramp rate 10-min reserve (MW/min)
            5,             # ramp rate 30-min reserve (MW/min)
            0,             # ramp rate reactive power
            0,             # area participation factor
        ]
        pass

    def dot(self):
        buf = io.StringIO()
        buf.write("graph {\n")
        buf.write("rankdir LR\n")
        for station in self.stations.itervalues():
            buf.write('s_{0} [label="{1}"]\n'.format(station.station_id, station.name.replace('"', "'")))
        for line in self.lines.itervalues():
            buf.write('s_{0} -- s_{1}\n'.format(line.left.station_id, line.right.station_id))
        buf.write("}\n")
        return buf.getvalue()

    def __repr__(self):
        return "Network of {0} stations, {1} lines".format(len(self.stations), len(self.lines)).encode('utf-8')

class ScigridNetwork(Network):
    class _csv_dialect(csv.excel):
        quotechar = b"'"

    def read(self, vertices_csv, links_csv):
        with io.open(vertices_csv, 'rb') as handle:
            for row in csv.DictReader(handle, dialect=self._csv_dialect):
                station_id  = row['v_id']
                name        = row['name'].decode('utf-8')
                operator    = row['operator'].decode('utf-8')
                voltages    = set(map(int, row['voltage'].split(';')) if row['voltage'] else [])
                frequencies = set(map(float, row['frequency'].split(';')) if row['frequency'] else [])
                self.stations[row['v_id']] = Station(station_id=station_id, name=name, operator=operator,
                                                     voltages=voltages, frequencies=frequencies, lines=list())

        with io.open(links_csv, 'rb') as handle:
            for i, row in enumerate(csv.DictReader(handle, dialect=self._csv_dialect)):
                line_id     = row['l_id']
                operator    = row['operator'].decode('utf-8')
                left        = self.stations[row['v_id_1']]
                right       = self.stations[row['v_id_2']]
                length      = float(row['length_m'])
                resistance  = float(row['r_ohmkm']) * int(row['length_m']) / 1000 if row['r_ohmkm'] else None
                reactance   = float(row['x_ohmkm']) * int(row['length_m']) / 1000 if row['x_ohmkm'] else None
                capacitance = float(row['c_nfkm']) * int(row['length_m']) / 1000  if row['c_nfkm'] else  None

                # use complex voltages for lines
                frequencies = set(map(float, row['frequency'].split(';')) if row['frequency'] else [])
                voltages    = set(map(int, row['voltage'].split(';')) if row['voltage'] else [])
                line  = Line(line_id=line_id, operator=operator, left=left, right=right, length=length,
                             voltages=voltages, frequencies=frequencies,
                             resistance=resistance, reactance=reactance, capacitance=capacitance)
                self.lines[row['l_id']] = line
                left.lines.append(line)
                right.lines.append(line)

<commit_msg>Record latitude and longitude of stations<commit_after>from __future__ import unicode_literals, division, print_function
import io
import csv
import random
import itertools
import heapq
from recordclass import recordclass
from numpy import array

class Station(recordclass('Station', b'station_id lat lon name operator voltages frequencies lines')):
    def __hash__(self):
        return hash(self.station_id)

class Line(recordclass('Line', b'line_id operator left right length frequencies voltages resistance reactance capacitance')):
    def __hash__(self):
        return hash(self.line_id)

    def __repr__(self):
        return "{0}: {1} -> {2}".format(self.line_id, self.left.name, self.right.name).encode('utf-8')

    @property
    def susceptance(self):
        if self.capacitance is None or self.frequency is None:
            return None
        return self.capacitance * self.frequency

class Network(object):
    def __init__(self):
        self.stations = dict()
        self.lines    = dict()
        self._areas   = dict()

    def connected_sets(self):
        # bfs algorithm to find connected sets in the network
        unseen = set(self.stations.values())
        connected = []
        while unseen:
            current = []
            root  = unseen.pop()
            queue = [root]
            while queue:
                node = queue.pop()
                if node in unseen:
                    unseen.remove(node)
                    current.append(node)
                for line in node.lines:
                    if line.left in unseen:
                        queue.append(line.left)
                    if line.right in unseen:
                        queue.append(line.right)
            connected.append(current)
        return connected

    def patch(self):
        # flood algorithm to patch all lines and stations with values from neighbours
        totals = list()
        while True:
            changes = 0
            for station in self.stations.itervalues():
                line_voltages    = set(v for line in station.lines for v in line.voltages)
                line_frequencies = set(f for line in station.lines for f in line.frequencies)
                if line_voltages - station.voltages:
                    station.voltages |= line_voltages
                    changes          += 1
                if line_frequencies - station.frequencies:
                    station.frequencies |= line_frequencies
                    changes             += 1

            for line in self.lines.itervalues():
                shared_frequencies = line.left.frequencies & line.right.frequencies
                if shared_frequencies and not line.frequencies & shared_frequencies:
                    line.frequencies |= shared_frequencies
                    changes += 1
                elif not line.frequencies:
                    if line.left.frequencies:
                        line.frequencies = set(line.left.frequencies)
                        changes += 1
                    elif line.right.frequencies:
                        line.frequencies = set(line.right.frequencies)
                        changes += 1

                shared_voltages = line.left.voltages & line.right.voltages
                if shared_voltages and not line.voltages & shared_voltages:
                    line.voltages |= shared_voltages
                    changes += 1
                elif not line.voltages:
                    if line.left.voltages:
                        line.voltages = set(line.left.voltages)
                        changes += 1
                    elif line.right.voltages:
                        line.voltages = set(line.right.voltages)
                        changes += 1

            if changes == 0:
                break
            totals.append(changes)
            if len(totals) > 1000:
                print(totals)
                raise Exception('dont think ill be stopping soon')
        return totals

    def report(self):
        # calculate missing values statically
        broken_stations = 0
        broken_lines    = 0
        mismatches      = 0
        for station in self.stations.itervalues():
            if not station.voltages or not station.frequencies:
                broken_stations += 1
            for line in station.lines:
                if station.frequencies:
                    if line.frequencies - station.frequencies:
                        mismatches += 1
                        continue
                elif line.frequencies:
                    mismatches += 1
                    continue
                if station.voltages:
                    if line.voltages - station.voltages:
                        mismatches += 1
                        continue
                elif line.voltages:
                    mismatches += 1
                    continue

        for line in self.lines.itervalues():
            if not line.voltages or not line.frequencies:
                broken_lines += 1
        return broken_stations, broken_lines, mismatches

    def find(self, from_station, to_station):
        lines = []
        pass

    def _area_number(self, area_name):
        if area_name not in self._areas:
            # assign next area number
            self._areas[area_name] = len(self._areas) + 1
        return self._areas[area_name]

    def powercase(self, loads=None):
        # FIXME this needs fixing for the complex-lines case

        # loads is a map of station id -> load, either positive or
        # negative; a negative load is represented by a generator.

        # if no loads map is passed, generate an 'electrified pair' of
        # two random nodes, one of which delivers power, the other
        # consumes it
        if loads is None:
            loads = self._electrified_pair()
        ppc = {
            "version": 2,
            "baseMVA": 100.0
        }
        nodes        = list()
        edges        = list()
        generators   = list()

        station_to_bus = dict()
        bus_id_gen     = itertools.count()

        for station in self.stations.itervalues():
            # because we do a DC PF, we ignore frequencies completely
            minv, maxv = min(station.voltages), max(station.voltages)
            for voltage in station.voltages:
                if station.station_id in loads and voltage == minv:
                    bus_load = loads[station.station_id]
                else:
                    bus_load = 0
                bus_id = next(bus_id_gen)
                station_to_bus[station.station_id, voltage] = bus_id
                if bus_load < 0:
                    # it is a generator instead of a load, insert it
                    generators.append(self._make_generator(bus_id, -bus_load))
                    bus_load = 0
                nodes.append(self._make_bus(station, voltage, bus_load, bus_id))

            for voltage in station.voltages:
                if voltage != maxv:
                    # create a transformer branch from max voltage to this voltage
                    from_bus = station_to_bus[station.station_id, maxv]
                    to_bus   = station_to_bus[station.station_id, voltage]
                    edges.append(self._make_transformer(from_bus, to_bus))

        for line in self.lines.itervalues():
            # create branches between stations
            from_bus = station_to_bus[line.left.station_id, line.voltage]
            to_bus   = station_to_bus[line.right.station_id, line.voltage]
            edges.append(self._make_line(line, from_bus, to_bus))

        ppc['bus']    = array(nodes)
        ppc['gen']    = array(generators)
        ppc['branch'] = array(edges)
        return ppc

    def _electrified_pair(self):
        src, dst = random.sample(self.stations, 2)
        return {
            src: -100, # MW
            dst: 50,  # MW
        }

    def _make_bus(self, station, voltage, load, bus_id):
        # see pypower.caseformat for documentation on how this works
        area_nr  = self._area_number(station.operator)
        base_kv  = voltage // 1000
        return [
            bus_id,
            3,       # slack bus
            load,    # real load in MW
            0,       # reactive load MVAr, zero because DC
            0,       # shunt conductance
            0,       # shunt susceptance
            area_nr, # area number
            1.0,     # voltage magnitude per unit
            0,       # voltage angle
            base_kv, # base voltage (per unit base)
            area_nr, # loss zone nr
            1.1,     # max voltage per unit
            0.9,     # min voltage per unit
        ]

    def _make_transformer(self, from_bus, to_bus):
        return [
            from_bus,
            to_bus,
            0.01,      # resistance
            0.01,      # reactance
            0.01,      # line charging susceptance
            200,       # long term rating (MW)
            200,       # short term rating (MW)
            200,       # emergency rating (MW)
            1,         # off-nominal (correction) taps ratio, 1 for no correction
            0,         # transformer phase shift angle,
            1,         # status (1 = on)
            -360,      # minimum angle
            360,       # maximum angle
        ]

    def _make_line(self, line, from_bus, to_bus):
        return [
            from_bus,
            to_bus,
            line.resistance or 0.01,   # default value if None
            line.reactance or 0.01,
            line.susceptance or 0.01,
            200,
            200,
            200,
            0,                          # not a transformer
            0,                          # not a transformer
            1,                          # status
            -360,
            360
        ]

    def _make_generator(self, bus_id, power_output):
        return [
            bus_id,
            power_output,
            0,             # reactive power output
            0,             # maximum reactive power output
            0,             # minimum reactive power output
            1.0,           # per-unit voltage magnitude setpoint
            100,           # base MVA
            1,             # status (on)
            power_output,  # maximum real power output
            0,             # minimum real power output
            0,             # Pc1, irrelevant
            0,             # Pc2
            0,             # Qc1min
            0,             # Qc1max
            0,             # Qc2min
            0,             # Qc2max
            5,             # ramp rate load-following (MW/min)
            5,             # ramp rate 10-min reserve (MW/min)
            5,             # ramp rate 30-min reserve (MW/min)
            0,             # ramp rate reactive power
            0,             # area participation factor
        ]
        pass

    def dot(self):
        buf = io.StringIO()
        buf.write("graph {\n")
        buf.write("rankdir LR\n")
        for station in self.stations.itervalues():
            buf.write('s_{0} [label="{1}"]\n'.format(station.station_id, station.name.replace('"', "'")))
        for line in self.lines.itervalues():
            buf.write('s_{0} -- s_{1}\n'.format(line.left.station_id, line.right.station_id))
        buf.write("}\n")
        return buf.getvalue()

    def __repr__(self):
        return "Network of {0} stations, {1} lines".format(len(self.stations), len(self.lines)).encode('utf-8')

class ScigridNetwork(Network):
    class _csv_dialect(csv.excel):
        quotechar = b"'"

    def read(self, vertices_csv, links_csv):
        with io.open(vertices_csv, 'rb') as handle:
            for row in csv.DictReader(handle, dialect=self._csv_dialect):
                station_id  = row['v_id']
                lat         = float(row['lat'])
                lon         = float(row['lon'])
                name        = row['name'].decode('utf-8')
                operator    = row['operator'].decode('utf-8')
                voltages    = set(map(int, row['voltage'].split(';')) if row['voltage'] else [])
                frequencies = set(map(float, row['frequency'].split(';')) if row['frequency'] else [])
                self.stations[row['v_id']] = Station(station_id=station_id, lat=lat, lon=lon, name=name, operator=operator,
                                                     voltages=voltages, frequencies=frequencies, lines=list())

        with io.open(links_csv, 'rb') as handle:
            for i, row in enumerate(csv.DictReader(handle, dialect=self._csv_dialect)):
                line_id     = row['l_id']
                operator    = row['operator'].decode('utf-8')
                left        = self.stations[row['v_id_1']]
                right       = self.stations[row['v_id_2']]
                length      = float(row['length_m'])
                resistance  = float(row['r_ohmkm']) * int(row['length_m']) / 1000 if row['r_ohmkm'] else None
                reactance   = float(row['x_ohmkm']) * int(row['length_m']) / 1000 if row['x_ohmkm'] else None
                capacitance = float(row['c_nfkm']) * int(row['length_m']) / 1000  if row['c_nfkm'] else  None

                # use complex voltages for lines
                frequencies = set(map(float, row['frequency'].split(';')) if row['frequency'] else [])
                voltages    = set(map(int, row['voltage'].split(';')) if row['voltage'] else [])
                line  = Line(line_id=line_id, operator=operator, left=left, right=right, length=length,
                             voltages=voltages, frequencies=frequencies,
                             resistance=resistance, reactance=reactance, capacitance=capacitance)
                self.lines[row['l_id']] = line
                left.lines.append(line)
                right.lines.append(line)

<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2011 by Florian Mounier, Kozea
# This file is part of pystil, licensed under a 3-clause BSD license.

"""Utility functions to help processing data"""

from datetime import datetime, timedelta
from urlparse import urlparse, parse_qs
from pystil.db import Visit, fields
from datetime import date, datetime, time
from time import mktime
from decimal import Decimal
import re
import json

IPV4RE = re.compile(r"(\d{1,3}(\.|$)){4}")
BROWSER_VERSION_NUMBERS = {
    'opera': 1,
    'safari': 1,
    'chrome': 1}

class PystilEncoder(json.JSONEncoder):

    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime("%Y-%m-%d %H:%M:%S")
        if isinstance(obj, timedelta):
            return obj.seconds
        if isinstance(obj, time):
            return obj.strftime("%H:%M:%S")
        if isinstance(obj, date):
            return obj.strftime("%Y-%m-%d")
        if isinstance(obj, str):
            return obj
        if isinstance(obj, Decimal):
            return float(obj)
        return json.JSONEncoder.default(self, obj)

def labelize(string):
    """return the label for a criteria"""
    return {
        'new': 'New visits',
        'unique': 'Visits',
        'all': 'Page hits',
        'spent_time': 'Time spent on site',
        'hour': 'Visits per hour'
    }[string]

def date_to_time(date):
    """Convert a python date to js stamp"""
    return int(1000 * mktime(date.timetuple()))

def time_to_date(time):
    """Convert a js stamp to python date"""
    return datetime.fromtimestamp(time / 1000)

def on(host):
    """Generate a filter on a host """
    if host == 'all':
        return Visit.host == Visit.host
    return Visit.host.like('%' + host)

def between(from_date, to_date):
    """Generate a filter between 2 dates"""
    return ((time_to_date(from_date) <= Visit.date) &
            (Visit.date < time_to_date(to_date) + timedelta(1)))

def transform_for_pie(results, site, from_date, to_date,
                      reparse_referrer=False):
    """Transform result for pie display"""
    visits = [{'label': (parse_referrer(visit.key, host_only=True,
                                        second_pass=True)
                         if reparse_referrer else visit.key),
               'data': visit.count}
              for visit in results]
    all_visits = (Visit.query
                  .filter(on(site))
                  .filter(between(from_date, to_date))
                  .count())
    other = all_visits - sum(visit['data'] for visit in visits)
    if other:
        visits = visits + [{'label': 'Other', 'data': other}]
    return {'list': visits}

def make_time_serie(results, criteria, from_date, to_date):
    """Create a serie with 0 days at 0 for Flot from request"""
    visits = {date_to_time(visit.key): visit.count for visit in results}

    for time in range(from_date, to_date, 1000 * 3600 * 24):
        visits[time] = visits.get(time, 0)
    data = visits.items()
    data.sort()
    return {'label': labelize(criteria),
            'data': data}

def make_serie(results, criteria, is_time=False):
    """Create a serie for Flot from request"""
    return {'label': labelize(criteria),
            'data': [(visit.key, visit.count)
                     for visit in results]}

def parse_referrer(referrer, with_query=False, host_only=False,
                   second_pass=False):
    """Return a pretty format for most search engines"""
    if referrer:
        up = urlparse(referrer)
        netloc = up.netloc
        if not netloc:
            if with_query:
                return "Local: %s" % up.path
            if second_pass:
                return referrer
            return "Local"

        query = up.query
        search = parse_qs(query.encode('ascii')).get(
            'q', parse_qs(query.encode('ascii')).get(
                'p', parse_qs(query.encode('ascii')).get(
                    'rdata', None)))
        if search:
            # TODO Yahoo variable encoding
            if with_query:
                return u"Organic: %s %s" % (netloc, search[0].decode('utf-8'))
            return u"Organic: %s" % netloc
        if host_only:
            return netloc
        return referrer
    return "Direct"

def polish_visit(visit):
    """Transform a visit for nicer display"""
    if visit.last_visit:
        visit.last_visit = date_to_time(visit.last_visit)
    if visit.referrer:
        visit.referrer = parse_referrer(visit.referrer, True)
    if visit.date.date() == date.today():
        visit.date = visit.date.time()

def visit_to_dict(visit):
    visit_dict = {}
    for key in fields(Visit):
        visit_dict[key] = getattr(visit, key)
    return visit_dict
<commit_msg>Use timegm instead of mktime to get UTC time<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2011 by Florian Mounier, Kozea
# This file is part of pystil, licensed under a 3-clause BSD license.

"""Utility functions to help processing data"""

from datetime import datetime, timedelta
from urlparse import urlparse, parse_qs
from pystil.db import Visit, fields
from datetime import date, datetime, time
from time import mktime
from calendar import timegm
from decimal import Decimal
import re
import json

IPV4RE = re.compile(r"(\d{1,3}(\.|$)){4}")
BROWSER_VERSION_NUMBERS = {
    'opera': 1,
    'safari': 1,
    'chrome': 1}

class PystilEncoder(json.JSONEncoder):

    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime("%Y-%m-%d %H:%M:%S")
        if isinstance(obj, timedelta):
            return obj.seconds
        if isinstance(obj, time):
            return obj.strftime("%H:%M:%S")
        if isinstance(obj, date):
            return obj.strftime("%Y-%m-%d")
        if isinstance(obj, str):
            return obj
        if isinstance(obj, Decimal):
            return float(obj)
        return json.JSONEncoder.default(self, obj)

def labelize(string):
    """return the label for a criteria"""
    return {
        'new': 'New visits',
        'unique': 'Visits',
        'all': 'Page hits',
        'spent_time': 'Time spent on site',
        'hour': 'Visits per hour'
    }[string]

def date_to_time(date):
    """Convert a python date to js stamp"""
    return int(1000 * timegm(date.timetuple()))

def time_to_date(time):
    """Convert a js stamp to python date"""
    return datetime.fromtimestamp(time / 1000)

def on(host):
    """Generate a filter on a host """
    if host == 'all':
        return Visit.host == Visit.host
    return Visit.host.like('%' + host)

def between(from_date, to_date):
    """Generate a filter between 2 dates"""
    return ((time_to_date(from_date) <= Visit.date) &
            (Visit.date < time_to_date(to_date) + timedelta(1)))

def transform_for_pie(results, site, from_date, to_date,
                      reparse_referrer=False):
    """Transform result for pie display"""
    visits = [{'label': (parse_referrer(visit.key, host_only=True,
                                        second_pass=True)
                         if reparse_referrer else visit.key),
               'data': visit.count}
              for visit in results]
    all_visits = (Visit.query
                  .filter(on(site))
                  .filter(between(from_date, to_date))
                  .count())
    other = all_visits - sum(visit['data'] for visit in visits)
    if other:
        visits = visits + [{'label': 'Other', 'data': other}]
    return {'list': visits}

def make_time_serie(results, criteria, from_date, to_date):
    """Create a serie with 0 days at 0 for Flot from request"""
    visits = {date_to_time(visit.key): visit.count for visit in results}

    for time in range(from_date, to_date, 1000 * 3600 * 24):
        visits[time] = visits.get(time, 0)
    data = visits.items()
    data.sort()
    return {'label': labelize(criteria),
            'data': data}

def make_serie(results, criteria, is_time=False):
    """Create a serie for Flot from request"""
    return {'label': labelize(criteria),
            'data': [(visit.key, visit.count)
                     for visit in results]}

def parse_referrer(referrer, with_query=False, host_only=False,
                   second_pass=False):
    """Return a pretty format for most search engines"""
    if referrer:
        up = urlparse(referrer)
        netloc = up.netloc
        if not netloc:
            if with_query:
                return "Local: %s" % up.path
            if second_pass:
                return referrer
            return "Local"

        query = up.query
        search = parse_qs(query.encode('ascii')).get(
            'q', parse_qs(query.encode('ascii')).get(
                'p', parse_qs(query.encode('ascii')).get(
                    'rdata', None)))
        if search:
            # TODO Yahoo variable encoding
            if with_query:
                return u"Organic: %s %s" % (netloc, search[0].decode('utf-8'))
            return u"Organic: %s" % netloc
        if host_only:
            return netloc
        return referrer
    return "Direct"

def polish_visit(visit):
    """Transform a visit for nicer display"""
    if visit.last_visit:
        visit.last_visit = date_to_time(visit.last_visit)
    if visit.referrer:
        visit.referrer = parse_referrer(visit.referrer, True)
    if visit.date.date() == date.today():
        visit.date = visit.date.time()

def visit_to_dict(visit):
    visit_dict = {}
    for key in fields(Visit):
        visit_dict[key] = getattr(visit, key)
    return visit_dict
<|endoftext|>


<commit_before>"""
SymPy is a Python library for symbolic mathematics. It aims to become a
full-featured computer algebra system (CAS) while keeping the code as simple as
possible in order to be comprehensible and easily extensible. SymPy is written
entirely in Python and does not require any external libraries, except
optionally for plotting support.

See the webpage for more information and documentation:

    http://code.google.com/p/sympy/
"""

__version__ = "0.5.14-hg"

#put path to pyglet into the search path, so that it can be imported
import os.path
import sys
sys.path.insert(0, os.path.join(os.path.abspath(os.path.dirname(__file__)), "thirdparty", \
        "pyglet"))

import symbol as stdlib_symbol
from sympy.core import *

from series import *
from functions import *
from ntheory import *
from concrete import *
from simplify import *
from solvers import *
from matrices import *
from geometry import *
from polynomials import *
from utilities import *
from integrals import *
from physics import units
from plotting import Plot, textplot
from printing import pretty, pretty_print, pprint, pprint_use_unicode, \
    pprint_try_use_unicode, print_gtk
from printing import latex, preview, view, pngview, pdfview, dviview

import abc

from polys import *
<commit_msg>"from physics import units" commented out for being too slow<commit_after>"""
SymPy is a Python library for symbolic mathematics. It aims to become a
full-featured computer algebra system (CAS) while keeping the code as simple as
possible in order to be comprehensible and easily extensible. SymPy is written
entirely in Python and does not require any external libraries, except
optionally for plotting support.

See the webpage for more information and documentation:

    http://code.google.com/p/sympy/
"""

__version__ = "0.5.14-hg"

#put path to pyglet into the search path, so that it can be imported
import os.path
import sys
sys.path.insert(0, os.path.join(os.path.abspath(os.path.dirname(__file__)), "thirdparty", \
        "pyglet"))

import symbol as stdlib_symbol
from sympy.core import *

from series import *
from functions import *
from ntheory import *
from concrete import *
from simplify import *
from solvers import *
from matrices import *
from geometry import *
from polynomials import *
from utilities import *
from integrals import *
# This module is slow to import:
#from physics import units
from plotting import Plot, textplot
from printing import pretty, pretty_print, pprint, pprint_use_unicode, \
    pprint_try_use_unicode, print_gtk
from printing import latex, preview, view, pngview, pdfview, dviview

import abc

from polys import *
<|endoftext|>


<commit_before>#!/usr/bin/env python
# Copyright 2015 ARM Limited
#
# Licensed under the Apache License, Version 2.0
# See LICENSE file for details.

import subprocess
import logging
import os

from distutils.spawn import find_executable

from project_generator import tool

from gdb import launcher as gdb_launcher
from gdb import arm_none_eabi_launcher as arm_none_eabi_gdb_launcher

# cache the detected IDEs, (map of ide name to a function(projectfiles,
# executable) that will launch it)
IDE_Launchers = {
}

IDEs_Scanned = False

# preferred order of IDEs if multiple are available
IDE_Preference = [
    'uvision', 'arm_none_eabi_gdb', 'gdb'
]

logger = logging.getLogger('ide_detect')

def _read_hklm_reg_value(key_path, value_name):
    ''' read a value from a registry key under HKEY_LOCAL_MACHINE '''
    if os.name != 'nt':
        return None
    import _winreg as winreg

    k = None
    value = None
    try:
        k = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path)
        v = winreg.QueryValueEx(k, value_name)
        return v[0]
    except WindowsError:
        if k:
            winreg.CloseKey(k)
    return value

def _find_uvision():
    found = find_executable('UV4')
    if found: return found
    if os.name == 'nt':
        found_pathdir = _read_hklm_reg_value(r'Software\Keil\Products\MDK', 'Path')
        if not found_pathdir:
            found_pathdir = _read_hklm_reg_value(r'Software\Wow6432Node\Keil\Products\MDK', 'Path')
        if found_pathdir:
            found = os.path.join(found_pathdir, '..', 'UV4', 'UV4.exe')
            if os.path.isfile(found):
                return os.path.normpath(found)
    return None

def _find_generic_gdb():
    return find_executable('gdb')

def _find_arm_none_eabi_gdb():
    return find_executable('arm-none-eabi-gdb')

def _uvision_launcher(uvision_exe):
    def launch_uvision(projectfiles, executable):
        uvproj = [
            x for x in projectfiles if x.endswith('.uvproj') or x.endswith('.uvprojx')
        ]
        if len(uvproj) != 1:
            raise Exception('Exactly one project file must be provided')
        logger.info("launching uvision: %s %s", uvision_exe, uvproj[0])
        child = subprocess.Popen(
            [uvision_exe, uvproj[0]],
        )
        child.wait()
    return launch_uvision

IDE_Scanners = {
              'uvision': (_find_uvision, _uvision_launcher),
                  'gdb': (_find_generic_gdb, gdb_launcher),
    'arm_none_eabi_gdb': (_find_arm_none_eabi_gdb, arm_none_eabi_gdb_launcher),
}

def _ensure_IDEs_scanned():
    global IDEs_Scanned, IDE_Launchers
    if IDEs_Scanned:
        return

    # !!! could scan in parallel, should be worth it as scanners are
    # likely highly io-bound
    for ide, (scanner, launcher) in IDE_Scanners.items():
        logger.debug('scanning for %s: %s', ide, (scanner, launcher))
        detected = scanner()
        logger.debug('scanning for %s... %sfound', ide, ('not ', '')[bool(detected)])
        if detected:
            IDE_Launchers[ide] = launcher(detected)
            # make sure we have a preference order for all detected IDEs, even
            # if the list hasn't been updated:
            if ide not in IDE_Preference:
                IDE_Preference.append(ide)

    IDEs_Scanned = True

def available():
    ''' return a list of available IDEs on this platform '''
    _ensure_IDEs_scanned()
    return [k for k in IDE_Launchers.keys()]

def select(available_ides, target, project_settings):
    ''' select the preferred option out of the available IDEs to debug the
    selected target, or None '''

    possible_ides = [x for x in available_ides if tool.target_supported(target, x, project_settings)]

    if len(possible_ides):
        return sorted(possible_ides, key=lambda x:IDE_Preference.index(x))[0]
    else:
        return None

def get_launcher(ide):
    ''' return a function(projectfiles) that will launch the
    specified IDE when called, or None if that IDE cannot be found '''
    if ide in IDE_Launchers:
        return IDE_Launchers[ide]
    else:
        return None

<commit_msg>ide detection - get exporter for target_supported function<commit_after>#!/usr/bin/env python
# Copyright 2015 ARM Limited
#
# Licensed under the Apache License, Version 2.0
# See LICENSE file for details.

import subprocess
import logging
import os

from distutils.spawn import find_executable

from project_generator import tool

from gdb import launcher as gdb_launcher
from gdb import arm_none_eabi_launcher as arm_none_eabi_gdb_launcher

# cache the detected IDEs, (map of ide name to a function(projectfiles,
# executable) that will launch it)
IDE_Launchers = {
}

IDEs_Scanned = False

# preferred order of IDEs if multiple are available
IDE_Preference = [
    'uvision', 'arm_none_eabi_gdb', 'gdb'
]

logger = logging.getLogger('ide_detect')

def _read_hklm_reg_value(key_path, value_name):
    ''' read a value from a registry key under HKEY_LOCAL_MACHINE '''
    if os.name != 'nt':
        return None
    import _winreg as winreg

    k = None
    value = None
    try:
        k = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path)
        v = winreg.QueryValueEx(k, value_name)
        return v[0]
    except WindowsError:
        if k:
            winreg.CloseKey(k)
    return value

def _find_uvision():
    found = find_executable('UV4')
    if found: return found
    if os.name == 'nt':
        found_pathdir = _read_hklm_reg_value(r'Software\Keil\Products\MDK', 'Path')
        if not found_pathdir:
            found_pathdir = _read_hklm_reg_value(r'Software\Wow6432Node\Keil\Products\MDK', 'Path')
        if found_pathdir:
            found = os.path.join(found_pathdir, '..', 'UV4', 'UV4.exe')
            if os.path.isfile(found):
                return os.path.normpath(found)
    return None

def _find_generic_gdb():
    return find_executable('gdb')

def _find_arm_none_eabi_gdb():
    return find_executable('arm-none-eabi-gdb')

def _uvision_launcher(uvision_exe):
    def launch_uvision(projectfiles, executable):
        uvproj = [
            x for x in projectfiles if x.endswith('.uvproj') or x.endswith('.uvprojx')
        ]
        if len(uvproj) != 1:
            raise Exception('Exactly one project file must be provided')
        logger.info("launching uvision: %s %s", uvision_exe, uvproj[0])
        child = subprocess.Popen(
            [uvision_exe, uvproj[0]],
        )
        child.wait()
    return launch_uvision

IDE_Scanners = {
              'uvision': (_find_uvision, _uvision_launcher),
                  'gdb': (_find_generic_gdb, gdb_launcher),
    'arm_none_eabi_gdb': (_find_arm_none_eabi_gdb, arm_none_eabi_gdb_launcher),
}

def _ensure_IDEs_scanned():
    global IDEs_Scanned, IDE_Launchers
    if IDEs_Scanned:
        return

    # !!! could scan in parallel, should be worth it as scanners are
    # likely highly io-bound
    for ide, (scanner, launcher) in IDE_Scanners.items():
        logger.debug('scanning for %s: %s', ide, (scanner, launcher))
        detected = scanner()
        logger.debug('scanning for %s... %sfound', ide, ('not ', '')[bool(detected)])
        if detected:
            IDE_Launchers[ide] = launcher(detected)
            # make sure we have a preference order for all detected IDEs, even
            # if the list hasn't been updated:
            if ide not in IDE_Preference:
                IDE_Preference.append(ide)

    IDEs_Scanned = True

def available():
    ''' return a list of available IDEs on this platform '''
    _ensure_IDEs_scanned()
    return [k for k in IDE_Launchers.keys()]

def select(available_ides, target, project_settings):
    ''' select the preferred option out of the available IDEs to debug the
    selected target, or None '''

    possible_ides = [x for x in available_ides if tool.target_supported(
        tool.ToolsSupported().get_value(x, 'exporter'), target, x, project_settings)]

    if len(possible_ides):
        return sorted(possible_ides, key=lambda x:IDE_Preference.index(x))[0]
    else:
        return None

def get_launcher(ide):
    ''' return a function(projectfiles) that will launch the
    specified IDE when called, or None if that IDE cannot be found '''
    if ide in IDE_Launchers:
        return IDE_Launchers[ide]
    else:
        return None

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# users.py
#
# Copyright 2014 BitVault, Inc. dba Gem

from .config import *

from .wrappers import *
from .errors import *
from .subscriptions import Subscriptions
from .devices import Devices
from .wallets import generate, Wallet

import applications as apps

class Users(DictWrapper):
    """A collection of round.Users objects."""

    def create(self, email, device_name, passphrase=None,
               api_token=None, redirect_uri=None, **kwargs):
        """Create a new User object and add it to this Users collection.

        In addition to creating a user, this call will create a device for that
        user, whose device_token will be returned from this call. Store the
        device_token, as it's required to complete Gem-Device authentication
        after the user approves the device at the end of their signup flow.

        If you lose the device_token returned from users.create, you'll have to
        create a new device for the user to gain access to their account again.

        Also, after this call, be sure to redirect the user to the location in
        `mfa_uri` (second return value of this function) to complete their
        account.

        If you get a 409 Conflict error, then the user already exists in the Gem
        system and you'll want to do a
        `client.user(email).devices.create(device_name)`

        Args:
          email (str)
          device_name (str): Human-readable name for the device through which
            your Application will be authorized to access the new User's account.
          passphrase (str, optional): A passphrase with which to encrypt a user
            wallet. If not provided, a default_wallet parameter must be passed in
            kwargs.
          api_token (str, optional): Your app's API token. This is optional if
            and only if the Client which will be calling this function already
            has Gem-Application or Gem-Identify authentication.
          redirect_uri (str, optional): A URI to which to redirect the User after
            they confirm their Gem account.
          **kwargs

        Returns: device_token
        """

        if not passphrase and u'default_wallet' not in kwargs:
            raise ValueError("Usage: users.create(email, passphrase, device_name, api_token, redirect_uri)")
        elif passphrase:
            default_wallet = generate(passphrase)[u'primary']
        else:
            default_wallet = kwargs[u'default_wallet']

        default_wallet[u'name'] = 'default'
        default_wallet[u'primary_private_seed'] = default_wallet[u'encrypted_seed']
        default_wallet[u'primary_public_seed'] = default_wallet[u'public_seed']
        del default_wallet[u'encrypted_seed']
        del default_wallet[u'public_seed']
        del default_wallet[u'private_seed']

        # If not supplied, we assume the client already has an api_token param.
        if api_token:
            self.client.authenticate_identify(api_token)

        user_data = dict(email=email,
                         default_wallet=default_wallet,
                         device_name=device_name)

        if redirect_uri:
            user_data[u'redirect_uri'] = redirect_uri
        if u'first_name' in kwargs:
            user_data[u'first_name'] = kwargs[u'first_name']
        if u'last_name' in kwargs:
            user_data[u'last_name'] = kwargs[u'last_name']

        try:
            resource = self.resource.create(user_data)
        except ResponseError as e:
            if "conflict" in e.message:
                raise ConflictError("This user already exists. Use client.user(email).devices.create(name) to request authorization from the user.")
            raise e

        return resource.attributes[u'metadata'][u'device_token']

    def wrap(self, resource):
        return User(resource, self.client)

    def key_for(self, wrapper):
        return wrapper.email

class User(Wrapper, Updatable):
    """A User represents an single *human* end-user.
    A User will have sole access to their backup key, and will need to
    communicate directly with Gem to provide MFA credentials for protected
    actions (updating their User object, publishing transactions, approving
    devices, etc).

    For a custodial model where a Wallet is intended to hold assets of multiple
    individuals or an organization, read the Gem docs regarding Application
    wallets.

    Attributes:
      first_name (str)
      last_name (str)
      email (str)
      phone_number (str)
      default_wallet (round.Wallet)
      wallets (round.Wallets)
      devices (list)
    """

    def update(self, **content):
        resource = self.resource.update(content)
        return User(resource, self.client)

    @property
    def devices(self):
        if not hasattr(self, '_devices'):
            devices_resource = self.client.resources.devices_query(
                dict(email=self.email))
            self._devices = Devices(devices_resource, self.client)
        return self._devices

    @property
    def wallet(self):
        """Fetch and return this user's default (only) Wallet."""
        if not hasattr(self, '_wallet'):
            wallet_resource = self.default_wallet.get()
            self._wallet = Wallet(wallet_resource, self.client)
        return self._wallet

    @property
    def subscriptions(self):
        """Fetch and return Subscriptions associated with this user."""
        if not hasattr(self, '_subscriptions'):
            subscriptions_resource = self.resource.subscriptions
            self._subscriptions = Subscriptions(
                subscriptions_resource, self.client)
        return self._subscriptions

    def send_mfa(self):
        """Send an SMS MFA token to the user."""
        return self.resource.send_mfa({})

    def verify_mfa(self, mfa_token):
        """Verify an SMS or TOTP MFA token for this user.

        Args:
          mfa_token (str): An alphanumeric code from either a User's TOTP
            application or sent to them via SMS.

        Returns:
          True if the mfa_token is valid, False otherwise.
        """
        response = self.resource.verify_mfa({u'mfa_token': mfa_token})
        return (response['valid'] == True or response[u'valid'] == 'true')
<commit_msg> argument typo in generating new user wallet<commit_after># -*- coding: utf-8 -*-
# users.py
#
# Copyright 2014 BitVault, Inc. dba Gem

from .config import *

from .wrappers import *
from .errors import *
from .subscriptions import Subscriptions
from .devices import Devices
from .wallets import generate, Wallet

import applications as apps

class Users(DictWrapper):
    """A collection of round.Users objects."""

    def create(self, email, device_name, passphrase=None,
               api_token=None, redirect_uri=None, **kwargs):
        """Create a new User object and add it to this Users collection.

        In addition to creating a user, this call will create a device for that
        user, whose device_token will be returned from this call. Store the
        device_token, as it's required to complete Gem-Device authentication
        after the user approves the device at the end of their signup flow.

        If you lose the device_token returned from users.create, you'll have to
        create a new device for the user to gain access to their account again.

        Also, after this call, be sure to redirect the user to the location in
        `mfa_uri` (second return value of this function) to complete their
        account.

        If you get a 409 Conflict error, then the user already exists in the Gem
        system and you'll want to do a
        `client.user(email).devices.create(device_name)`

        Args:
          email (str)
          device_name (str): Human-readable name for the device through which
            your Application will be authorized to access the new User's account.
          passphrase (str, optional): A passphrase with which to encrypt a user
            wallet. If not provided, a default_wallet parameter must be passed in
            kwargs.
          api_token (str, optional): Your app's API token. This is optional if
            and only if the Client which will be calling this function already
            has Gem-Application or Gem-Identify authentication.
          redirect_uri (str, optional): A URI to which to redirect the User after
            they confirm their Gem account.
          **kwargs

        Returns: device_token
        """

        if not passphrase and u'default_wallet' not in kwargs:
            raise ValueError("Usage: users.create(email, passphrase, device_name, api_token, redirect_uri)")
        elif passphrase:
            default_wallet = generate(passphrase, [u'primary'])
        else:
            default_wallet = kwargs[u'default_wallet']

        default_wallet[u'name'] = 'default'
        default_wallet[u'primary_private_seed'] = default_wallet[u'encrypted_seed']
        default_wallet[u'primary_public_seed'] = default_wallet[u'public_seed']
        del default_wallet[u'encrypted_seed']
        del default_wallet[u'public_seed']
        del default_wallet[u'private_seed']

        # If not supplied, we assume the client already has an api_token param.
        if api_token:
            self.client.authenticate_identify(api_token)

        user_data = dict(email=email,
                         default_wallet=default_wallet,
                         device_name=device_name)

        if redirect_uri:
            user_data[u'redirect_uri'] = redirect_uri
        if u'first_name' in kwargs:
            user_data[u'first_name'] = kwargs[u'first_name']
        if u'last_name' in kwargs:
            user_data[u'last_name'] = kwargs[u'last_name']

        try:
            resource = self.resource.create(user_data)
        except ResponseError as e:
            if "conflict" in e.message:
                raise ConflictError("This user already exists. Use client.user(email).devices.create(name) to request authorization from the user.")
            raise e

        return resource.attributes[u'metadata'][u'device_token']

    def wrap(self, resource):
        return User(resource, self.client)

    def key_for(self, wrapper):
        return wrapper.email

class User(Wrapper, Updatable):
    """A User represents an single *human* end-user.
    A User will have sole access to their backup key, and will need to
    communicate directly with Gem to provide MFA credentials for protected
    actions (updating their User object, publishing transactions, approving
    devices, etc).

    For a custodial model where a Wallet is intended to hold assets of multiple
    individuals or an organization, read the Gem docs regarding Application
    wallets.

    Attributes:
      first_name (str)
      last_name (str)
      email (str)
      phone_number (str)
      default_wallet (round.Wallet)
      wallets (round.Wallets)
      devices (list)
    """

    def update(self, **content):
        resource = self.resource.update(content)
        return User(resource, self.client)

    @property
    def devices(self):
        if not hasattr(self, '_devices'):
            devices_resource = self.client.resources.devices_query(
                dict(email=self.email))
            self._devices = Devices(devices_resource, self.client)
        return self._devices

    @property
    def wallet(self):
        """Fetch and return this user's default (only) Wallet."""
        if not hasattr(self, '_wallet'):
            wallet_resource = self.default_wallet.get()
            self._wallet = Wallet(wallet_resource, self.client)
        return self._wallet

    @property
    def subscriptions(self):
        """Fetch and return Subscriptions associated with this user."""
        if not hasattr(self, '_subscriptions'):
            subscriptions_resource = self.resource.subscriptions
            self._subscriptions = Subscriptions(
                subscriptions_resource, self.client)
        return self._subscriptions

    def send_mfa(self):
        """Send an SMS MFA token to the user."""
        return self.resource.send_mfa({})

    def verify_mfa(self, mfa_token):
        """Verify an SMS or TOTP MFA token for this user.

        Args:
          mfa_token (str): An alphanumeric code from either a User's TOTP
            application or sent to them via SMS.

        Returns:
          True if the mfa_token is valid, False otherwise.
        """
        response = self.resource.verify_mfa({u'mfa_token': mfa_token})
        return (response['valid'] == True or response[u'valid'] == 'true')
<|endoftext|>


<commit_before>import requests
from lxml import etree

class Refactor:
    def __init__(self, url, payload={}, request='get'):
        self.url = url
        self.payload = payload
        self.request = request

    def return_requests(self, **kwargs):
        if self.request == 'get':
            return requests.get(self.url, params = self.payload, **kwargs)
        else:
            return requests.post(self.url, params = self.payload, **kwargs)

    def xml(self, **kwargs):
        if self.request == 'get':
            out = requests.get(self.url, params = self.payload, **kwargs)
            out.raise_for_status()
            xmlparser = etree.XMLParser()
            tt = etree.fromstring(out.content, xmlparser)
            return tt
        else:
            out = requests.post(self.url, params = self.payload, **kwargs)
            out.raise_for_status()
            xmlparser = etree.XMLParser()
            tt = etree.fromstring(out.content, xmlparser)
            return tt

    def json(self, **kwargs):
        if self.request == 'get':
            out = requests.get(self.url, params = self.payload, **kwargs)
            out.raise_for_status()
            return out.json()
        else:
            out = requests.post(self.url, params = self.payload, **kwargs)
            out.raise_for_status()
            return out.json()

    def raw(self, **kwargs):
        if self.request == 'get':
            out = requests.get(self.url, params = self.payload, **kwargs)
            out.raise_for_status()
            return out.raw()
        else:
            out = requests.post(self.url, params = self.payload, **kwargs)
            out.raise_for_status()
            return out.raw()
<commit_msg>Fix refactor raw method<commit_after>import requests
from lxml import etree

class Refactor:
    def __init__(self, url, payload={}, request='get'):
        self.url = url
        self.payload = payload
        self.request = request

    def return_requests(self, **kwargs):
        if self.request == 'get':
            return requests.get(self.url, params = self.payload, **kwargs)
        else:
            return requests.post(self.url, params = self.payload, **kwargs)

    def xml(self, **kwargs):
        if self.request == 'get':
            out = requests.get(self.url, params = self.payload, **kwargs)
            out.raise_for_status()
            xmlparser = etree.XMLParser()
            tt = etree.fromstring(out.content, xmlparser)
            return tt
        else:
            out = requests.post(self.url, params = self.payload, **kwargs)
            out.raise_for_status()
            xmlparser = etree.XMLParser()
            tt = etree.fromstring(out.content, xmlparser)
            return tt

    def json(self, **kwargs):
        if self.request == 'get':
            out = requests.get(self.url, params = self.payload, **kwargs)
            out.raise_for_status()
            return out.json()
        else:
            out = requests.post(self.url, params = self.payload, **kwargs)
            out.raise_for_status()
            return out.json()

    def raw(self, **kwargs):
        if self.request == 'get':
            out = requests.get(self.url, params = self.payload, **kwargs)
            out.raise_for_status()
            return out.text
        else:
            out = requests.post(self.url, params = self.payload, **kwargs)
            out.raise_for_status()
            return out.text
<|endoftext|>


<commit_before>from __future__ import absolute_import,unicode_literals
'''
A module to isolate python version compatibility filth.
'''
import os
import sys
import time
import base64
import socket
import collections

major = sys.version_info.major
minor = sys.version_info.minor
micro = sys.version_info.micro

majmin = (major,minor)
version = (major,minor,micro)

if version < (3,0,0):
    import select

    import Queue as queue

    from cStringIO import StringIO as BytesIO

    numtypes = (int,long)
    strtypes = (str,unicode)

    sockerrs = (socket.error,)

    def enbase64(s):
        return s.encode('base64')

    def debase64(s):
        return s.decode('base64')

    def isstr(s):
        return type(s) in (str,unicode)

    def iterbytes(byts):
        for c in byts:
            yield(ord(c))

    def makedirs(path,mode=0o777):
        os.makedirs(path,mode=mode)

else:
    import queue
    import builtins

    from io import BytesIO

    numtypes = (int,)
    strtypes = (str,)

    sockerrs = (builtins.ConnectionError,builtins.FileNotFoundError)

    def enbase64(b):
        return base64.b64encode(b).decode('utf8')

    def debase64(b):
        return base64.b64decode( b.encode('utf8') )

    def isstr(s):
        return isinstance(s,str)

    def iterbytes(byts):
        return iter(byts)

    def makedirs(path,mode=0o777):
        os.makedirs(path,mode=mode,exist_ok=True)
<commit_msg>added to_int and to_bytes helpers for py27 py34 compat<commit_after>from __future__ import absolute_import,unicode_literals
'''
A module to isolate python version compatibility filth.
'''
import os
import sys
import time
import base64
import socket
import collections

major = sys.version_info.major
minor = sys.version_info.minor
micro = sys.version_info.micro

majmin = (major,minor)
version = (major,minor,micro)

if version < (3,0,0):
    import select

    import Queue as queue

    from cStringIO import StringIO as BytesIO

    numtypes = (int,long)
    strtypes = (str,unicode)

    fmts = {
        1:'B',
        2:'<H',
        4:'<I',
        8:'<Q',
    }

    sockerrs = (socket.error,)

    def enbase64(s):
        return s.encode('base64')

    def debase64(s):
        return s.decode('base64')

    def isstr(s):
        return type(s) in (str,unicode)

    def iterbytes(byts):
        for c in byts:
            yield(ord(c))

    def makedirs(path,mode=0o777):
        os.makedirs(path,mode=mode)

    def to_bytes(valu, size):
        fmt = fmts.get(size)

        if fmt == None:
            raise Exception('to_bytes size not supported: %d' % (size,))

        return struct.pack(fmt,valu)

    def to_int(byts):
        fmt = fmts.get(len(byts))
        if fmt == None:
            raise Exception('to_int size not supported: %d' % (len(byts),))

        return struct.unpack(fmt,byts)[0]

else:

    import queue
    import builtins

    from io import BytesIO

    numtypes = (int,)
    strtypes = (str,)

    sockerrs = (builtins.ConnectionError,builtins.FileNotFoundError)

    def to_bytes(valu, size):
        return valu.to_bytes(size,byteorder='little')

    def to_int(byts):
        return int.from_bytes(byts,'little')

    def enbase64(b):
        return base64.b64encode(b).decode('utf8')

    def debase64(b):
        return base64.b64decode( b.encode('utf8') )

    def isstr(s):
        return isinstance(s,str)

    def iterbytes(byts):
        return iter(byts)

    def makedirs(path,mode=0o777):
        os.makedirs(path,mode=mode,exist_ok=True)
<|endoftext|>


<commit_before>from .excs import ValidationErrors
from .parsing import parse
from .validation import validate

__version__ = '0.21.0'

__all__ = [
    'ValidationErrors',
    '__version__',
    'parse',
    'validate',
]
<commit_msg>Become 0.21.1<commit_after>from .excs import ValidationErrors
from .parsing import parse
from .validation import validate

__version__ = '0.21.1'

__all__ = [
    'ValidationErrors',
    '__version__',
    'parse',
    'validate',
]
<|endoftext|>


<commit_before>from django.views.generic.detail import SingleObjectTemplateResponseMixin
from django.views.generic.edit import ModelFormMixin, ProcessFormView

from utils.forms import default_crispy_helper

class OrganizationFilterMixin(object):
    """
    Mixin for SingleObjectMixin and MultipleObjectMixin to select only objects
    belonging to the current organization.
    """

    def get_queryset(self):
        return super(OrganizationFilterMixin, self).get_queryset().filter(organization=self.request.organization)

class EventOrganizerFilterMixin(object):
    """
    Mixin for SingleObjectMixin and MultipleObjectMixin to select only objects
    belonging to events organized by the current organization.
    """

    def get_queryset(self):
        return super(EventOrganizerFilterMixin, self).get_queryset().filter(event__organizer=self.request.organization)

class BaseFixedValueCreateView(ModelFormMixin, ProcessFormView):
    """
    Base view for creating an new object instance with some default values.

    Override get_instance to provide defaults.

    Using this base class requires subclassing to provide a response mixin.
    """

    def get_instance(self):
        """
        Override this method to create an instance with default values.
        """
        return self.model()

    def get(self, request, *args, **kwargs):
        self.object = self.get_instance()
        return super(BaseFixedValueCreateView, self).get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.object = self.get_instance()
        return super(BaseFixedValueCreateView, self).post(request, *args, **kwargs)

class FixedValueCreateView(SingleObjectTemplateResponseMixin, BaseFixedValueCreateView):
    """
    View for creating a new object instance with some default values,
    with a response rendered by template.

    Override get_instance to provide defaults.
    """
    template_name_suffix = '_form'

class CreateViewForOrganization(FixedValueCreateView):
    """
    View for creating a new object instance with the current organization as default for the organization field,
    """

    def get_instance(self):
        return self.model(organization=self.request.organization)

class OrganizationFormMixin(object):
    """
    Mixin to create form with current organization as parameter.
    """

    def get_form_kwargs(self):
        kwargs = super(OrganizationFormMixin, self).get_form_kwargs()
        kwargs['organization'] = self.request.organization
        return kwargs

class CrispyFormMixin(object):
    """
    Mixin to add Crispy form helper.
    """

    def get_form(self, form_class):
        form = super(CrispyFormMixin, self).get_form(form_class)

        form.helper = default_crispy_helper()

        return form
<commit_msg>utils: Made get_form parameter of CrispyFormMixin.get_form optional.<commit_after>from django.views.generic.detail import SingleObjectTemplateResponseMixin
from django.views.generic.edit import ModelFormMixin, ProcessFormView

from utils.forms import default_crispy_helper

class OrganizationFilterMixin(object):
    """
    Mixin for SingleObjectMixin and MultipleObjectMixin to select only objects
    belonging to the current organization.
    """

    def get_queryset(self):
        return super(OrganizationFilterMixin, self).get_queryset().filter(organization=self.request.organization)

class EventOrganizerFilterMixin(object):
    """
    Mixin for SingleObjectMixin and MultipleObjectMixin to select only objects
    belonging to events organized by the current organization.
    """

    def get_queryset(self):
        return super(EventOrganizerFilterMixin, self).get_queryset().filter(event__organizer=self.request.organization)

class BaseFixedValueCreateView(ModelFormMixin, ProcessFormView):
    """
    Base view for creating an new object instance with some default values.

    Override get_instance to provide defaults.

    Using this base class requires subclassing to provide a response mixin.
    """

    def get_instance(self):
        """
        Override this method to create an instance with default values.
        """
        return self.model()

    def get(self, request, *args, **kwargs):
        self.object = self.get_instance()
        return super(BaseFixedValueCreateView, self).get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.object = self.get_instance()
        return super(BaseFixedValueCreateView, self).post(request, *args, **kwargs)

class FixedValueCreateView(SingleObjectTemplateResponseMixin, BaseFixedValueCreateView):
    """
    View for creating a new object instance with some default values,
    with a response rendered by template.

    Override get_instance to provide defaults.
    """
    template_name_suffix = '_form'

class CreateViewForOrganization(FixedValueCreateView):
    """
    View for creating a new object instance with the current organization as default for the organization field,
    """

    def get_instance(self):
        return self.model(organization=self.request.organization)

class OrganizationFormMixin(object):
    """
    Mixin to create form with current organization as parameter.
    """

    def get_form_kwargs(self):
        kwargs = super(OrganizationFormMixin, self).get_form_kwargs()
        kwargs['organization'] = self.request.organization
        return kwargs

class CrispyFormMixin(object):
    """
    Mixin to add Crispy form helper.
    """

    def get_form(self, form_class=None):
        form = super(CrispyFormMixin, self).get_form(form_class)

        form.helper = default_crispy_helper()

        return form
<|endoftext|>