<commit_before>#coding=utf-8
from flask import Flask, request, url_for
from flask.ext.restful import fields, reqparse, marshal_with
from instance_manager import NavitiaManager, RegionNotFound
from protobuf_to_dict import protobuf_to_dict
from fields import stop_point, stop_area, route, line, physical_mode,\
                   commercial_mode, company, network, pagination, place,\
                   PbField, stop_date_time, enum_type, NonNullList, NonNullNested,\
                   display_informations_vj,additional_informations_vj,error

from interfaces.parsers import option_value
from ResourceUri import ResourceUri, add_notes
import datetime
from functools import wraps
from make_links import add_id_links, clean_links
from errors import ManageError
from interfaces.argument import ArgumentDoc
from interfaces.parsers import depth_argument
from operator import itemgetter
from datetime import datetime, timedelta
import sys
from copy import copy
from datetime import datetime

class SectionLinks(fields.Raw):
    def __init__(self, **kwargs):
        super(SectionLinks, self).__init__(**kwargs)

    def output(self, key, obj):
        links = None
        try:
            if obj.HasField("uris"):
                links = obj.uris.ListFields()
        except ValueError:
            return None
        response = []
        if links:
            for type_, value in links:
                response.append({"type": type_.name, "id": value})
        return response

class GeoJson(fields.Raw):
    def __init__(self, **kwargs):
        super(GeoJson, self).__init__(**kwargs)

    def output(self, key, obj):
        coords = []
        length = 0
        enum = obj.DESCRIPTOR.fields_by_name['type'].enum_type.values_by_name
        if obj.type == enum['STREET_NETWORK'].number:
            try:
                if obj.HasField("street_network"):
                    coords = obj.street_network.coordinates
                    length = obj.street_network.length
                else:
                    return None
            except ValueError:
                return None
        elif obj.type == enum['PUBLIC_TRANSPORT'].number:
            coords = [sdt.stop_point.coord for sdt in obj.stop_date_times]
        elif obj.type == enum['TRANSFER'].number:
            coords.append(obj.origin.stop_point.coord)
            coords.append(obj.destination.stop_point.coord)
        else:
            return None

        response = {
                "type" : "LineString",
                "coordinates" : [],
                "properties" : [{
                    "length" : length
                    }]
        }
        for coord in coords:
            response["coordinates"].append([coord.lon, coord.lat])
        return response

class section_type(enum_type):
    def output(self, key, obj):
        try:
            if obj.HasField("pt_display_informations"):
                infos = obj.pt_display_informations
                if infos.HasField("vehicle_journey_type"):
                    infos_desc = infos.DESCRIPTOR
                    odt_enum = infos_desc.fields_by_name['vehicle_journey_type'].enum_type
                    if infos.vehicle_journey_type != odt_enum.values_by_name['regular'].number:
                        return "on_demand_transport"
        except ValueError:
            pass
        return super(section_type, self).output(key, obj)

class section_place(PbField):
    def output(self, key, obj):
        enum_t = obj.DESCRIPTOR.fields_by_name['type'].enum_type.values_by_name
        if obj.type == enum_t['WAITING'].number:
            return None
        else:
            return super(PbField, self).output(key, obj)

section = {
    "type": section_type(attribute="type"),
    "mode" : enum_type(attribute="street_network.mode"),
    "duration": fields.Integer(),
    "from" : section_place(place, attribute="origin"),
    "to": section_place(place, attribute="destination"),
    "links" : SectionLinks(attribute="uris"),
    "display_informations" : display_informations_vj(),
    "additional_informations" : additional_informations_vj(),
    "geojson" : GeoJson(),
    "path" : NonNullList(NonNullNested({"length":fields.Integer(),
                                        "name":fields.String()}),
                         attribute="street_network.path_items"),
    "transfer_type" : enum_type(),
    "stop_date_times" : NonNullList(NonNullNested(stop_date_time)),
    "departure_date_time" : fields.String(attribute="begin_date_time"),
    "arrival_date_time" : fields.String(attribute="end_date_time")
}

journey = {
    'duration' : fields.Integer(),
    'nb_transfers' : fields.Integer(),
    'departure_date_time' : fields.String(),
    'arrival_date_time' : fields.String(),
    'requested_date_time' : fields.String(),
    'sections' : NonNullList(NonNullNested(section)),
    'from' : PbField(place, attribute='origin'),
    'to' : PbField(place, attribute='destination'),
    'type' : fields.String()
}

journeys = {
    "journeys" : NonNullList(NonNullNested(journey)),
    "error": PbField(error,attribute='error')
}

def dt_represents(value):
    if value == "arrival":
        return False
    elif value == "departure":
        return True
    else:
        raise ValueError("Unable to parse datetime_represents")

class add_journey_href(object):
    def __call__(self, f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            objects = f(*args, **kwargs)
            if objects[1] != 200:
                return objects
            if not "journeys" in objects[0].keys():
                return objects
            if "region" in kwargs.keys():
                del kwargs["region"]
            if "uri" in kwargs.keys():
                kwargs["from"] = kwargs["uri"].split("/")[-1]
                del kwargs["uri"]
            if "lon" in kwargs.keys() and "lat" in kwargs.keys():
                if not "from" in kwargs.keys():
                    kwargs["from"] = kwargs["lon"] + ';' + kwargs["lat"]
                del kwargs["lon"]
                del kwargs["lat"]
            kwargs["_external"] = True
            for journey in objects[0]['journeys']:
                if not "sections" in journey.keys():
                    kwargs["datetime"] = journey["requested_date_time"]
                    kwargs["to"] = journey["to"]["id"]
                    journey['links'] = [
                            {
                                "type" : "journeys",
                                "href" : url_for("v1.journeys", **kwargs),
                                "templated" : False
                                }
                    ]
            return objects
        return wrapper

class add_journey_pagination(object):
    def __call__(self, f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            objects = f(*args, **kwargs)
            if objects[1] != 200:
                return objects
            datetime_before, datetime_after = self.extremes(objects[0])
            if not datetime_before is None and not datetime_after is None:
                if not "links" in objects[0]:
                    objects[0]["links"] = []

                args = request.args.copy()
                args["datetime"] = datetime_before.strftime("%Y%m%dT%H%M%S")
                args["datetime_represents"] = "arrival"
                objects[0]["links"].append({
                    "href" : url_for("v1.journeys", _external=True, **args),
                    "templated" : False,
                    "type" : "prev"
                    })
                args["datetime"] = datetime_after.strftime("%Y%m%dT%H%M%S")
                args["datetime_represents"] = "departure"
                objects[0]["links"].append({
                    "href" : url_for("v1.journeys", _external=True, **args),
                    "templated" : False,
                    "type" : "next"
                    })
            return objects
        return wrapper

    def extremes(self, resp):
        datetime_before = None
        datetime_after = None
        try:
            list_journeys = [journey for journey in resp['journeys']\
                                if 'arrival_date_time' in journey.keys() and\
                                    journey['arrival_date_time'] != '']
            asap_journey = min(list_journeys, key=itemgetter('arrival_date_time'))
        except:
            return (None, None)
        if asap_journey['arrival_date_time'] and ['asap_journey.departure_date_time']:
            minute = timedelta(minutes = 1)
            datetime_after = datetime.strptime(asap_journey['departure_date_time'], "%Y%m%dT%H%M%S") + minute
            datetime_before = datetime.strptime(asap_journey['arrival_date_time'], "%Y%m%dT%H%M%S") - minute

        return (datetime_before, datetime_after)

class Journeys(ResourceUri):
    def __init__(self):
        ResourceUri.__init__(self)
        modes = ["walking", "car", "bike", "br"]
        types = ["all", "rapid"]
        self.parsers = {}
        self.parsers["get"] = reqparse.RequestParser(argument_class=ArgumentDoc)
        parser_get = self.parsers["get"]
        parser_get.add_argument("from", type=str, dest="origin")
        parser_get.add_argument("to", type=str, dest="destination")
        parser_get.add_argument("datetime", type=str)
        parser_get.add_argument("datetime_represents", dest="clockwise",
                           type=dt_represents, default=True)
        parser_get.add_argument("max_nb_transfers", type=int, default=10,
                           dest="max_transfers")
        parser_get.add_argument("first_section_mode",
                           type=option_value(modes),
                           default="walking",
                           dest="origin_mode")
        parser_get.add_argument("last_section_mode",
                           type=option_value(modes),
                           default="walking",
                           dest="destination_mode")
        parser_get.add_argument("walking_speed", type=float, default=1.68)
        parser_get.add_argument("walking_distance", type=int, default=1000)
        parser_get.add_argument("bike_speed", type=float, default=8.8)
        parser_get.add_argument("bike_distance", type=int, default=5000)
        parser_get.add_argument("br_speed", type=float, default=8.8,)
        parser_get.add_argument("br_distance", type=int, default=5000)
        parser_get.add_argument("car_speed", type=float, default=16.8)
        parser_get.add_argument("car_distance", type=int, default=15000)
        parser_get.add_argument("forbidden_uris[]", type=str, action="append")
        parser_get.add_argument("count", type=int)
        parser_get.add_argument("type", type=option_value(types), default="all")
#a supprimer
        parser_get.add_argument("max_duration", type=int, default=36000)
        self.method_decorators.append(add_notes(self))

    @clean_links()
    @add_id_links()
    @add_journey_pagination()
    @add_journey_href()
    @marshal_with(journeys)
    @ManageError()
    def get(self, region=None, lon=None, lat=None, uri=None):
        args = self.parsers["get"].parse_args()
        #TODO : Changer le protobuff pour que ce soit propre
        args["destination_mode"] = "vls" if args["destination_mode"] == "br" else args["destination_mode"]
        args["origin_mode"] = "vls" if args["origin_mode"] == "br" else args["origin_mode"]
        if not region is None or (not lon is None and not lat is None):
            self.region = NavitiaManager().get_region(region, lon, lat)
            if uri:
                objects = uri.split("/")
                if objects and len(objects) % 2 == 0:
                    args["origin"] =  self.transform_id(objects[-1])
                else:
                    return {"error" : "Unable to compute journeys from this \
                                       object"}, 503
        else:
            if "origin" in args.keys():
                self.region = NavitiaManager().key_of_id(args["origin"])
                args["origin"] = self.transform_id(args["origin"])
            elif "destination" in args.keys():
                self.region = NavitiaManager().key_of_id(args["destination"])
            if "destination" in args.keys():
                args["destination"] = self.transform_id(args["destination"])
            else:
                raise RegionNotFound("")
        if not args["datetime"]:
            args["datetime"] = datetime.now().strftime("%Y%m%dT1337")
        api = None
        if "destination" in args.keys() and args["destination"]:
            api = "journeys"
        else:
            api = "isochrone"
        response = NavitiaManager().dispatch(args, self.region, api)
        return response

    def transform_id(self, id):
        splitted_coord = id.split(";")
        splitted_address = id.split(":")
        if len(splitted_coord) == 2:
            return "coord:"+id.replace(";", ":")
        if len(splitted_address) >=3 and splitted_address[0] == 'address':
            del splitted_address[1]
            return ':'.join(splitted_address)
        return id
<commit_msg>Journeys: Reformatter uri de Admin dans Interface V1<commit_after>#coding=utf-8
from flask import Flask, request, url_for
from flask.ext.restful import fields, reqparse, marshal_with
from instance_manager import NavitiaManager, RegionNotFound
from protobuf_to_dict import protobuf_to_dict
from fields import stop_point, stop_area, route, line, physical_mode,\
                   commercial_mode, company, network, pagination, place,\
                   PbField, stop_date_time, enum_type, NonNullList, NonNullNested,\
                   display_informations_vj,additional_informations_vj,error

from interfaces.parsers import option_value
from ResourceUri import ResourceUri, add_notes
import datetime
from functools import wraps
from make_links import add_id_links, clean_links
from errors import ManageError
from interfaces.argument import ArgumentDoc
from interfaces.parsers import depth_argument
from operator import itemgetter
from datetime import datetime, timedelta
import sys
from copy import copy
from datetime import datetime

class SectionLinks(fields.Raw):
    def __init__(self, **kwargs):
        super(SectionLinks, self).__init__(**kwargs)

    def output(self, key, obj):
        links = None
        try:
            if obj.HasField("uris"):
                links = obj.uris.ListFields()
        except ValueError:
            return None
        response = []
        if links:
            for type_, value in links:
                response.append({"type": type_.name, "id": value})
        return response

class GeoJson(fields.Raw):
    def __init__(self, **kwargs):
        super(GeoJson, self).__init__(**kwargs)

    def output(self, key, obj):
        coords = []
        length = 0
        enum = obj.DESCRIPTOR.fields_by_name['type'].enum_type.values_by_name
        if obj.type == enum['STREET_NETWORK'].number:
            try:
                if obj.HasField("street_network"):
                    coords = obj.street_network.coordinates
                    length = obj.street_network.length
                else:
                    return None
            except ValueError:
                return None
        elif obj.type == enum['PUBLIC_TRANSPORT'].number:
            coords = [sdt.stop_point.coord for sdt in obj.stop_date_times]
        elif obj.type == enum['TRANSFER'].number:
            coords.append(obj.origin.stop_point.coord)
            coords.append(obj.destination.stop_point.coord)
        else:
            return None

        response = {
                "type" : "LineString",
                "coordinates" : [],
                "properties" : [{
                    "length" : length
                    }]
        }
        for coord in coords:
            response["coordinates"].append([coord.lon, coord.lat])
        return response

class section_type(enum_type):
    def output(self, key, obj):
        try:
            if obj.HasField("pt_display_informations"):
                infos = obj.pt_display_informations
                if infos.HasField("vehicle_journey_type"):
                    infos_desc = infos.DESCRIPTOR
                    odt_enum = infos_desc.fields_by_name['vehicle_journey_type'].enum_type
                    if infos.vehicle_journey_type != odt_enum.values_by_name['regular'].number:
                        return "on_demand_transport"
        except ValueError:
            pass
        return super(section_type, self).output(key, obj)

class section_place(PbField):
    def output(self, key, obj):
        enum_t = obj.DESCRIPTOR.fields_by_name['type'].enum_type.values_by_name
        if obj.type == enum_t['WAITING'].number:
            return None
        else:
            return super(PbField, self).output(key, obj)

section = {
    "type": section_type(attribute="type"),
    "mode" : enum_type(attribute="street_network.mode"),
    "duration": fields.Integer(),
    "from" : section_place(place, attribute="origin"),
    "to": section_place(place, attribute="destination"),
    "links" : SectionLinks(attribute="uris"),
    "display_informations" : display_informations_vj(),
    "additional_informations" : additional_informations_vj(),
    "geojson" : GeoJson(),
    "path" : NonNullList(NonNullNested({"length":fields.Integer(),
                                        "name":fields.String()}),
                         attribute="street_network.path_items"),
    "transfer_type" : enum_type(),
    "stop_date_times" : NonNullList(NonNullNested(stop_date_time)),
    "departure_date_time" : fields.String(attribute="begin_date_time"),
    "arrival_date_time" : fields.String(attribute="end_date_time")
}

journey = {
    'duration' : fields.Integer(),
    'nb_transfers' : fields.Integer(),
    'departure_date_time' : fields.String(),
    'arrival_date_time' : fields.String(),
    'requested_date_time' : fields.String(),
    'sections' : NonNullList(NonNullNested(section)),
    'from' : PbField(place, attribute='origin'),
    'to' : PbField(place, attribute='destination'),
    'type' : fields.String()
}

journeys = {
    "journeys" : NonNullList(NonNullNested(journey)),
    "error": PbField(error,attribute='error')
}

def dt_represents(value):
    if value == "arrival":
        return False
    elif value == "departure":
        return True
    else:
        raise ValueError("Unable to parse datetime_represents")

class add_journey_href(object):
    def __call__(self, f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            objects = f(*args, **kwargs)
            if objects[1] != 200:
                return objects
            if not "journeys" in objects[0].keys():
                return objects
            if "region" in kwargs.keys():
                del kwargs["region"]
            if "uri" in kwargs.keys():
                kwargs["from"] = kwargs["uri"].split("/")[-1]
                del kwargs["uri"]
            if "lon" in kwargs.keys() and "lat" in kwargs.keys():
                if not "from" in kwargs.keys():
                    kwargs["from"] = kwargs["lon"] + ';' + kwargs["lat"]
                del kwargs["lon"]
                del kwargs["lat"]
            kwargs["_external"] = True
            for journey in objects[0]['journeys']:
                if not "sections" in journey.keys():
                    kwargs["datetime"] = journey["requested_date_time"]
                    kwargs["to"] = journey["to"]["id"]
                    journey['links'] = [
                            {
                                "type" : "journeys",
                                "href" : url_for("v1.journeys", **kwargs),
                                "templated" : False
                                }
                    ]
            return objects
        return wrapper

class add_journey_pagination(object):
    def __call__(self, f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            objects = f(*args, **kwargs)
            if objects[1] != 200:
                return objects
            datetime_before, datetime_after = self.extremes(objects[0])
            if not datetime_before is None and not datetime_after is None:
                if not "links" in objects[0]:
                    objects[0]["links"] = []

                args = request.args.copy()
                args["datetime"] = datetime_before.strftime("%Y%m%dT%H%M%S")
                args["datetime_represents"] = "arrival"
                objects[0]["links"].append({
                    "href" : url_for("v1.journeys", _external=True, **args),
                    "templated" : False,
                    "type" : "prev"
                    })
                args["datetime"] = datetime_after.strftime("%Y%m%dT%H%M%S")
                args["datetime_represents"] = "departure"
                objects[0]["links"].append({
                    "href" : url_for("v1.journeys", _external=True, **args),
                    "templated" : False,
                    "type" : "next"
                    })
            return objects
        return wrapper

    def extremes(self, resp):
        datetime_before = None
        datetime_after = None
        try:
            list_journeys = [journey for journey in resp['journeys']\
                                if 'arrival_date_time' in journey.keys() and\
                                    journey['arrival_date_time'] != '']
            asap_journey = min(list_journeys, key=itemgetter('arrival_date_time'))
        except:
            return (None, None)
        if asap_journey['arrival_date_time'] and ['asap_journey.departure_date_time']:
            minute = timedelta(minutes = 1)
            datetime_after = datetime.strptime(asap_journey['departure_date_time'], "%Y%m%dT%H%M%S") + minute
            datetime_before = datetime.strptime(asap_journey['arrival_date_time'], "%Y%m%dT%H%M%S") - minute

        return (datetime_before, datetime_after)

class Journeys(ResourceUri):
    def __init__(self):
        ResourceUri.__init__(self)
        modes = ["walking", "car", "bike", "br"]
        types = ["all", "rapid"]
        self.parsers = {}
        self.parsers["get"] = reqparse.RequestParser(argument_class=ArgumentDoc)
        parser_get = self.parsers["get"]
        parser_get.add_argument("from", type=str, dest="origin")
        parser_get.add_argument("to", type=str, dest="destination")
        parser_get.add_argument("datetime", type=str)
        parser_get.add_argument("datetime_represents", dest="clockwise",
                           type=dt_represents, default=True)
        parser_get.add_argument("max_nb_transfers", type=int, default=10,
                           dest="max_transfers")
        parser_get.add_argument("first_section_mode",
                           type=option_value(modes),
                           default="walking",
                           dest="origin_mode")
        parser_get.add_argument("last_section_mode",
                           type=option_value(modes),
                           default="walking",
                           dest="destination_mode")
        parser_get.add_argument("walking_speed", type=float, default=1.68)
        parser_get.add_argument("walking_distance", type=int, default=1000)
        parser_get.add_argument("bike_speed", type=float, default=8.8)
        parser_get.add_argument("bike_distance", type=int, default=5000)
        parser_get.add_argument("br_speed", type=float, default=8.8,)
        parser_get.add_argument("br_distance", type=int, default=5000)
        parser_get.add_argument("car_speed", type=float, default=16.8)
        parser_get.add_argument("car_distance", type=int, default=15000)
        parser_get.add_argument("forbidden_uris[]", type=str, action="append")
        parser_get.add_argument("count", type=int)
        parser_get.add_argument("type", type=option_value(types), default="all")
#a supprimer
        parser_get.add_argument("max_duration", type=int, default=36000)
        self.method_decorators.append(add_notes(self))

    @clean_links()
    @add_id_links()
    @add_journey_pagination()
    @add_journey_href()
    @marshal_with(journeys)
    @ManageError()
    def get(self, region=None, lon=None, lat=None, uri=None):
        args = self.parsers["get"].parse_args()
        #TODO : Changer le protobuff pour que ce soit propre
        args["destination_mode"] = "vls" if args["destination_mode"] == "br" else args["destination_mode"]
        args["origin_mode"] = "vls" if args["origin_mode"] == "br" else args["origin_mode"]
        if not region is None or (not lon is None and not lat is None):
            self.region = NavitiaManager().get_region(region, lon, lat)
            if uri:
                objects = uri.split("/")
                if objects and len(objects) % 2 == 0:
                    args["origin"] =  self.transform_id(objects[-1])
                else:
                    return {"error" : "Unable to compute journeys from this \
                                       object"}, 503
        else:
            if "origin" in args.keys():
                self.region = NavitiaManager().key_of_id(args["origin"])
                args["origin"] = self.transform_id(args["origin"])
            elif "destination" in args.keys():
                self.region = NavitiaManager().key_of_id(args["destination"])
            if "destination" in args.keys():
                args["destination"] = self.transform_id(args["destination"])
            else:
                raise RegionNotFound("")
        if not args["datetime"]:
            args["datetime"] = datetime.now().strftime("%Y%m%dT1337")
        api = None
        if "destination" in args.keys() and args["destination"]:
            api = "journeys"
        else:
            api = "isochrone"
        response = NavitiaManager().dispatch(args, self.region, api)
        return response

    def transform_id(self, id):
        splitted_coord = id.split(";")
        splitted_address = id.split(":")
        if len(splitted_coord) == 2:
            return "coord:"+id.replace(";", ":")
        if len(splitted_address) >=3 and splitted_address[0] == 'address':
            del splitted_address[1]
            return ':'.join(splitted_address)
        if len(splitted_address) >=3 and splitted_address[0] == 'admin':
            del splitted_address[1]
            return ':'.join(splitted_address)
        return id
<|endoftext|>


<commit_before>
from parenthetics import Parenthentic
# import pytest

def test_isone():
    text = Parenthentic()
    text.isequal("(")
    assert text.count == "1"

def test_isneg():
    text = Parenthentic()
    text.isequal(")")
    assert text.count == "-1"

def test_iszero():
    text = Parenthentic()
    text.isequal("()")
    assert text.count == "0"
<commit_msg>added tests to test all possibilties<commit_after>
from parenthetics import Parenthentic
import pytest

def test_isone():
    text = Parenthentic()
    assert text.isequal("(") == 1

def test_isneg():
    text = Parenthentic()
    assert text.isequal(")") == -1

def test_iszero():
    text = Parenthentic()
    assert text.isequal("( )") == 0

def test_withstring():
    text = Parenthentic()
    assert text.isequal("this is a (string) with open and close") == 0

def test_mismatched():
    text = Parenthentic()
    assert text.isequal("This is a string with )))))(((((( wrong order") == -1
<|endoftext|>


<commit_before>from django.contrib import admin
from django.contrib.contenttypes import generic
from django.db import models

from pennyblack import settings
from pennyblack.options import NewsletterReceiverMixin, JobUnitMixin, JobUnitAdmin

import datetime

class NewsletterSubscriberManager(models.Manager):
    """
    Custom manager for NewsletterSubscriber to provide extra functionality
    """
    use_for_related_fields = True

    def get_or_add(self,email, **kwargs):
        """
        Gets a subscriber, if he doesn't exist it creates him.
        """
        try:
            return self.get(email__iexact=email)
        except self.model.DoesNotExist:
            return self.create(email=email.lower(), **kwargs)

    def active(self):
        """
        Gives only the active subscribers
        """
        return self.filter(is_active=True)

newsletter_subscriber_manager = NewsletterSubscriberManager()

class NewsletterSubscriber(models.Model, NewsletterReceiverMixin):
    """
    A generic newsletter subscriber
    """
    # from pennyblack.models.mail import Mail
    email = models.EmailField(verbose_name="Email address")
    groups = models.ManyToManyField('subscriber.SubscriberGroup',
        verbose_name="Groups", related_name='subscribers')
    date_subscribed = models.DateTimeField(verbose_name="Subscribe Date",
        default=datetime.datetime.now())
    mails = generic.GenericRelation('pennyblack.Mail')
    is_active = models.BooleanField(verbose_name="Active", default=True)

    objects = newsletter_subscriber_manager
    default_manager = newsletter_subscriber_manager

    class Meta:
        verbose_name = "Subscriber"
        verbose_name_plural = "Subscribers"

    def __unicode__(self):
        return self.email

    def on_bounce(self,mail):
        """
        A mail got bounced, consider deactivating this subscriber.
        """
        bounce_count = 0
        for mail in self.mails.order_by('pk'):
            bounce_count += mail.bounced
            if mail.viewed:
                bounce_count = 0
        if bounce_count >= settings.SUBSCRIBER_BOUNCES_UNTIL_DEACTIVATION:
            self.is_active = False
            self.save()

    def unsubscribe(self):
        self.is_active = False
        self.save()

    @classmethod
    def register_extension(cls, register_fn):
        """
        Call the register function of an extension. You must override this
        if you provide a custom ModelAdmin class and want your extensions to
        be able to patch stuff in.
        """
        register_fn(cls, NewsletterSubscriberAdmin)

class NewsletterSubscriberAdmin(admin.ModelAdmin):
    search_fields = ('email',)
    list_filter = ('groups', 'is_active')
    list_display = ('__unicode__','is_active')
    filter_horizontal = ('groups',)

class SubscriberGroupManager(models.Manager):
    """
    Custom manager for SubscriberGroup to provide extra functionality
    """
    def get_or_add(self, name, **kwargs):
        """
        Gets a group, if she doesn't exist it creates her.
        """
        try:
            return self.get(name__iexact=name)
        except self.model.DoesNotExist:
            return self.create(name=name, **kwargs)

class SubscriberGroup(models.Model, JobUnitMixin):
    """
    Groups to add newsletter subscribers
    """
    name = models.CharField(max_length=50, verbose_name="Name", unique=True)

    objects = SubscriberGroupManager()

    class Meta:
        verbose_name = "Subscriber Group"
        verbose_name_plural = "Subscriber Groups"

    def __unicode__(self):
        return self.name

    @property
    def member_count(self):
        return self.subscribers.active().count()

    def get_member_count(self):
        return self.member_count
    get_member_count.short_description = "Member Count"

    def get_newsletter_receiver_collections(self):
        """
        Every Group has only one collection
        """
        return (('all',{}),)

    def get_receiver_queryset(self):
        """
        Return all group members
        """
        return self.subscribers.active()

    def get_extra_links(self):
        """
        Define extra links for unsubscribing
        """
        from pennyblack.module.subscriber.views import unsubscribe
        return {
            'unsubscribe':unsubscribe,
        }

class SubscriberGroupAdmin(JobUnitAdmin):
    list_display = ('__unicode__', 'get_member_count')
    <commit_msg>make emailaddress in subscriber module unique<commit_after>from django.contrib import admin
from django.contrib.contenttypes import generic
from django.db import models

from pennyblack import settings
from pennyblack.options import NewsletterReceiverMixin, JobUnitMixin, JobUnitAdmin

import datetime

class NewsletterSubscriberManager(models.Manager):
    """
    Custom manager for NewsletterSubscriber to provide extra functionality
    """
    use_for_related_fields = True

    def get_or_add(self,email, **kwargs):
        """
        Gets a subscriber, if he doesn't exist it creates him.
        """
        try:
            return self.get(email__iexact=email)
        except self.model.DoesNotExist:
            return self.create(email=email.lower(), **kwargs)

    def active(self):
        """
        Gives only the active subscribers
        """
        return self.filter(is_active=True)

newsletter_subscriber_manager = NewsletterSubscriberManager()

class NewsletterSubscriber(models.Model, NewsletterReceiverMixin):
    """
    A generic newsletter subscriber
    """
    # from pennyblack.models.mail import Mail
    email = models.EmailField(verbose_name="Email address", unique=True)
    groups = models.ManyToManyField('subscriber.SubscriberGroup',
        verbose_name="Groups", related_name='subscribers')
    date_subscribed = models.DateTimeField(verbose_name="Subscribe Date",
        default=datetime.datetime.now())
    mails = generic.GenericRelation('pennyblack.Mail')
    is_active = models.BooleanField(verbose_name="Active", default=True)

    objects = newsletter_subscriber_manager
    default_manager = newsletter_subscriber_manager

    class Meta:
        verbose_name = "Subscriber"
        verbose_name_plural = "Subscribers"

    def __unicode__(self):
        return self.email

    def on_bounce(self,mail):
        """
        A mail got bounced, consider deactivating this subscriber.
        """
        bounce_count = 0
        for mail in self.mails.order_by('pk'):
            bounce_count += mail.bounced
            if mail.viewed:
                bounce_count = 0
        if bounce_count >= settings.SUBSCRIBER_BOUNCES_UNTIL_DEACTIVATION:
            self.is_active = False
            self.save()

    def unsubscribe(self):
        self.is_active = False
        self.save()

    @classmethod
    def register_extension(cls, register_fn):
        """
        Call the register function of an extension. You must override this
        if you provide a custom ModelAdmin class and want your extensions to
        be able to patch stuff in.
        """
        register_fn(cls, NewsletterSubscriberAdmin)

class NewsletterSubscriberAdmin(admin.ModelAdmin):
    search_fields = ('email',)
    list_filter = ('groups', 'is_active')
    list_display = ('__unicode__','is_active')
    filter_horizontal = ('groups',)

class SubscriberGroupManager(models.Manager):
    """
    Custom manager for SubscriberGroup to provide extra functionality
    """
    def get_or_add(self, name, **kwargs):
        """
        Gets a group, if she doesn't exist it creates her.
        """
        try:
            return self.get(name__iexact=name)
        except self.model.DoesNotExist:
            return self.create(name=name, **kwargs)

class SubscriberGroup(models.Model, JobUnitMixin):
    """
    Groups to add newsletter subscribers
    """
    name = models.CharField(max_length=50, verbose_name="Name", unique=True)

    objects = SubscriberGroupManager()

    class Meta:
        verbose_name = "Subscriber Group"
        verbose_name_plural = "Subscriber Groups"

    def __unicode__(self):
        return self.name

    @property
    def member_count(self):
        return self.subscribers.active().count()

    def get_member_count(self):
        return self.member_count
    get_member_count.short_description = "Member Count"

    def get_newsletter_receiver_collections(self):
        """
        Every Group has only one collection
        """
        return (('all',{}),)

    def get_receiver_queryset(self):
        """
        Return all group members
        """
        return self.subscribers.active()

    def get_extra_links(self):
        """
        Define extra links for unsubscribing
        """
        from pennyblack.module.subscriber.views import unsubscribe
        return {
            'unsubscribe':unsubscribe,
        }

class SubscriberGroupAdmin(JobUnitAdmin):
    list_display = ('__unicode__', 'get_member_count')
    <|endoftext|>


<commit_before># Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
Tests for the epsf_stars module.
"""

from astropy.modeling.models import Moffat2D
from astropy.nddata import NDData
from astropy.table import Table
import numpy as np
import pytest

from ..epsf_stars import extract_stars, EPSFStar, EPSFStars

try:
    import scipy  # noqa
    HAS_SCIPY = True
except ImportError:
    HAS_SCIPY = False

@pytest.mark.skipif('not HAS_SCIPY')
class TestExtractStars:
    def setup_class(self):
        stars_tbl = Table()
        stars_tbl['x'] = [15, 15, 35, 35]
        stars_tbl['y'] = [15, 35, 40, 10]
        self.stars_tbl = stars_tbl

        yy, xx = np.mgrid[0:51, 0:55]
        self.data = np.zeros(xx.shape)
        for (xi, yi) in zip(stars_tbl['x'], stars_tbl['y']):
            m = Moffat2D(100, xi, yi, 3, 3)
            self.data += m(xx, yy)

        self.nddata = NDData(data=self.data)

    def test_extract_stars(self):
        size = 11
        stars = extract_stars(self.nddata, self.stars_tbl, size=size)
        assert len(stars) == 4
        assert isinstance(stars, EPSFStars)
        assert isinstance(stars[0], EPSFStar)
        assert stars[0].data.shape == (size, size)
        assert stars.n_stars == stars.n_all_stars
        assert stars.n_stars == stars.n_good_stars
        assert stars.center.shape == (len(stars), 2)

    def test_extract_stars_inputs(self):
        with pytest.raises(ValueError):
            extract_stars(np.ones(3), self.stars_tbl)

        with pytest.raises(ValueError):
            extract_stars(self.nddata, [(1, 1), (2, 2), (3, 3)])

        with pytest.raises(ValueError):
            extract_stars(self.nddata, [self.stars_tbl, self.stars_tbl])

        with pytest.raises(ValueError):
            extract_stars([self.nddata, self.nddata], self.stars_tbl)
<commit_msg>Add test for EPSFStar residual image<commit_after># Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
Tests for the epsf_stars module.
"""

from astropy.modeling.models import Moffat2D, Gaussian2D
from astropy.nddata import NDData
from astropy.table import Table
import numpy as np
from numpy.testing import assert_allclose
import pytest

from ..epsf_stars import extract_stars, EPSFStar, EPSFStars
from ..models import EPSFModel

try:
    import scipy  # noqa
    HAS_SCIPY = True
except ImportError:
    HAS_SCIPY = False

@pytest.mark.skipif('not HAS_SCIPY')
class TestExtractStars:
    def setup_class(self):
        stars_tbl = Table()
        stars_tbl['x'] = [15, 15, 35, 35]
        stars_tbl['y'] = [15, 35, 40, 10]
        self.stars_tbl = stars_tbl

        yy, xx = np.mgrid[0:51, 0:55]
        self.data = np.zeros(xx.shape)
        for (xi, yi) in zip(stars_tbl['x'], stars_tbl['y']):
            m = Moffat2D(100, xi, yi, 3, 3)
            self.data += m(xx, yy)

        self.nddata = NDData(data=self.data)

    def test_extract_stars(self):
        size = 11
        stars = extract_stars(self.nddata, self.stars_tbl, size=size)
        assert len(stars) == 4
        assert isinstance(stars, EPSFStars)
        assert isinstance(stars[0], EPSFStar)
        assert stars[0].data.shape == (size, size)
        assert stars.n_stars == stars.n_all_stars
        assert stars.n_stars == stars.n_good_stars
        assert stars.center.shape == (len(stars), 2)

    def test_extract_stars_inputs(self):
        with pytest.raises(ValueError):
            extract_stars(np.ones(3), self.stars_tbl)

        with pytest.raises(ValueError):
            extract_stars(self.nddata, [(1, 1), (2, 2), (3, 3)])

        with pytest.raises(ValueError):
            extract_stars(self.nddata, [self.stars_tbl, self.stars_tbl])

        with pytest.raises(ValueError):
            extract_stars([self.nddata, self.nddata], self.stars_tbl)

def test_epsf_star_residual_image():
    """
    Test to ensure ``compute_residual_image`` gives correct residuals.
    """

    size = 100
    yy, xx, = np.mgrid[0:size, 0:size]
    gmodel = Gaussian2D(100, 50, 50, 10, 10)(xx, yy)
    epsf = EPSFModel(gmodel, oversampling=4)
    data = np.zeros((size, size))
    data += epsf.evaluate(x=xx, y=yy, flux=16, x_0=50, y_0=50)
    tbl = Table()
    tbl['x'] = [50.]
    tbl['y'] = [50.]
    stars = extract_stars(NDData(data), tbl, size=25)
    residual = stars[0].compute_residual_image(epsf)

    assert_allclose(np.sum(residual), 0., atol=1.e-6)
<|endoftext|>


<commit_before># coding: utf-8
"""JupyterLab entry points"""

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import errno
import json
import pipes
import os
from os import path as osp
from os.path import join as pjoin
from subprocess import check_output
import shutil
import sys
import tarfile
from jupyter_core.paths import ENV_JUPYTER_PATH, ENV_CONFIG_PATH

if sys.platform == 'win32':
    from subprocess import list2cmdline
else:
    def list2cmdline(cmd_list):
        return ' '.join(map(pipes.quote, cmd_list))

here = osp.dirname(osp.abspath(__file__))
CONFIG_PATH = os.environ.get('JUPYTER_CONFIG_DIR', ENV_CONFIG_PATH[0])
BUILD_PATH = ENV_JUPYTER_PATH[0]

def run(cmd, **kwargs):
    """Run a command in the given working directory.
    """
    print('> ' + list2cmdline(cmd))
    kwargs.setdefault('shell', sys.platform == 'win32')
    kwargs.setdefault('env', os.environ)
    return check_output(cmd, **kwargs)

def install_extension(extension):
    """Install an extension package into JupyterLab.

    Follows the semantics of https://docs.npmjs.com/cli/install.

    The extension is first validated.

    If link is true, the source directory is linked using `npm link`.
    """
    tar_name, pkg_name = validate_extension(extension)
    config = _get_config()
    path = pjoin(_get_cache_dir(config), tar_name)
    run(['npm', 'install', '--save', path], cwd=_get_root_dir(config))
    config['installed_extensions'][pkg_name] = path
    if pkg_name in config['linked_extensions']:
        del config['linked_extensions'][pkg_name]
    _write_config(config)

def link_extension(extension):
    """Link an extension against the JupyterLab build.
    """
    path = _normalize_path(extension)

    # Verify the package.json data.
    pkg_path = osp.join(path, 'package.json')
    if not osp.exists(pkg_path):
        msg = 'Linked package must point to a directory with package.json'
        raise ValueError(msg)

    with open(pkg_path) as fid:
        data = json.load(fid)

    _validate_package(data, path)

    # Update JupyterLab metadata.
    config = _get_config()
    name = data['name']
    config['linked_extensions'][name] = path
    if name in config['installed_extensions']:
        del config['installed_extensions'][name]
    _write_config(config)

def unlink_extension(extension):
    """Unlink an extension from JupyterLab by path or name.
    """
    extension = _normalize_path(extension)
    config = _get_config()

    name = None
    for (key, value) in config['linked_extensions'].items():
        if value == extension or key == extension:
            name = key
            break

    if name:
        del config['linked_extensions'][name]
        _write_config(config)
        return True

    print('No labextension matching "%s" is linked' % extension)
    return False

def uninstall_extension(name):
    """Uninstall an extension by name.
    """
    config = _get_config()

    if name in config['installed_extensions']:
        del config['installed_extensions'][name]
        _write_config(config)
        return True

    print('No labextension named "%s" installed' % name)
    return False

def list_extensions():
    """List installed extensions.
    """
    config = _get_config()
    installed = list(config['installed_extensions'])
    linked = list(config['linked_extensions'])
    return sorted(installed + linked)

def validate_extension(extension):
    """Verify that a JupyterLab extension is valid.
    """
    config = _get_config()
    extension = _normalize_path(extension)
    _ensure_package(config)
    cache_dir = _get_cache_dir(config)
    # npm pack the extension
    output = run(['npm', 'pack', extension], cwd=cache_dir)
    name = output.decode('utf8').splitlines()[-1]
    # read the package.json data from the file
    tar = tarfile.open(pjoin(cache_dir, name), "r:gz")
    f = tar.extractfile('package/package.json')
    data = json.loads(f.read().decode('utf8'))
    _validate_package(data, extension)
    return name, data['name']

def clean():
    """Clean the JupyterLab application directory."""
    config = _get_config()
    for name in ['node_modules', 'build']:
        target = pjoin(_get_root_dir(config), name)
        if osp.exists(target):
            shutil.rmtree(target)

def build():
    """Build the JupyterLab application."""
    # Set up the build directory.
    config = _get_config()
    _ensure_package(config)
    root = _get_root_dir(config)

    # Make sure packages are installed.
    run(['npm', 'install'], cwd=root)

    # Install the linked extensions.
    for value in config['linked_extensions'].values():
        run(['npm', 'install', value], cwd=root)

    # Build the app.
    run(['npm', 'run', 'build'], cwd=root)

def describe():
    """Get the git description of the JupyterLab application.
    """
    description = 'unknown'
    try:
        cwd = os.path.dirname(os.path.dirname(__file__))
        description = run(['git', 'describe'], cwd=cwd)
        description = description.decode('utf8').strip()
    except Exception:
        pass
    return description

def _ensure_package(config):
    """Make sure the build dir is set up.
    """
    cache_dir = _get_cache_dir(config)
    root_dir = _get_root_dir(config)
    if not osp.exists(cache_dir):
        os.makedirs(cache_dir)
    for name in ['package.json', 'index.template.js', 'webpack.config.js']:
        dest = pjoin(root_dir, name)
        shutil.copy2(pjoin(here, name), dest)

    # Template the package.json file.
    pkg_path = pjoin(root_dir, 'package.json')
    with open(pkg_path) as fid:
        data = json.load(fid)
    for (key, value) in config['installed_extensions'].items():
        data['dependencies'][key] = value
        data['jupyterlab']['extensions'].append(key)
    for key in config['linked_extensions']:
        data['jupyterlab']['extensions'].append(key)
    with open(pkg_path, 'w') as fid:
        json.dump(data, fid, indent=4)

def _validate_package(data, extension):
    """Validate package.json data.
    """
    msg = '%s is not a valid JupyterLab extension' % extension
    if 'jupyterlab' not in data:
        raise ValueError(msg)
    if not isinstance(data['jupyterlab'], dict):
        raise ValueError(msg)
    if not data['jupyterlab'].get('extension', False):
        raise ValueError(msg)

def _get_config(config_dir=None):
    """Get the JupyterLab config data.
    """
    config_dir = config_dir or _get_config_dir()
    file = pjoin(config_dir, 'build_config.json')
    if not osp.exists(file):
        data = {}
    else:
        with open(file) as fid:
            data = json.load(fid)
    data.setdefault('location', pjoin(BUILD_PATH, 'lab'))
    data.setdefault('installed_extensions', dict())
    data.setdefault('linked_extensions', dict())
    return data

def _write_config(data, config_dir=None):
    """Write the JupyterLab config data.
    """
    config_dir = config_dir or _get_config_dir()
    if not osp.exists(config_dir):
        try:
            os.makedirs(config_dir)
        except OSError as e:
            if e.errno != errno.EEXIST:
                pass
    with open(pjoin(config_dir, 'build_config.json'), 'w') as fid:
        json.dump(data, fid, indent=4)

def _normalize_path(extension):
    extension = osp.expanduser(extension)
    if osp.exists(extension):
        extension = osp.abspath(extension)
    return extension

def _get_config_dir():
    return pjoin(CONFIG_PATH, 'labconfig')

def _get_root_dir(config=None):
    config = config or _get_config()
    return config['location']

def _get_build_dir(config=None):
    config = config or _get_config()
    return pjoin(_get_root_dir(config), 'build')

def _get_pkg_path(config=None):
    config = config or _get_config()
    return pjoin(_get_root_dir(config), 'package.json')

def _get_cache_dir(config=None):
    config = config or _get_config()
    return pjoin(_get_root_dir(config), 'cache')
<commit_msg>Fix env variable name<commit_after># coding: utf-8
"""JupyterLab entry points"""

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import errno
import json
import pipes
import os
from os import path as osp
from os.path import join as pjoin
from subprocess import check_output
import shutil
import sys
import tarfile
from jupyter_core.paths import ENV_JUPYTER_PATH, ENV_CONFIG_PATH

if sys.platform == 'win32':
    from subprocess import list2cmdline
else:
    def list2cmdline(cmd_list):
        return ' '.join(map(pipes.quote, cmd_list))

here = osp.dirname(osp.abspath(__file__))
CONFIG_PATH = os.environ.get('JUPYTERLAB_CONFIG_DIR', ENV_CONFIG_PATH[0])
BUILD_PATH = ENV_JUPYTER_PATH[0]

def run(cmd, **kwargs):
    """Run a command in the given working directory.
    """
    print('> ' + list2cmdline(cmd))
    kwargs.setdefault('shell', sys.platform == 'win32')
    kwargs.setdefault('env', os.environ)
    return check_output(cmd, **kwargs)

def install_extension(extension):
    """Install an extension package into JupyterLab.

    Follows the semantics of https://docs.npmjs.com/cli/install.

    The extension is first validated.

    If link is true, the source directory is linked using `npm link`.
    """
    tar_name, pkg_name = validate_extension(extension)
    config = _get_config()
    path = pjoin(_get_cache_dir(config), tar_name)
    run(['npm', 'install', '--save', path], cwd=_get_root_dir(config))
    config['installed_extensions'][pkg_name] = path
    if pkg_name in config['linked_extensions']:
        del config['linked_extensions'][pkg_name]
    _write_config(config)

def link_extension(extension):
    """Link an extension against the JupyterLab build.
    """
    path = _normalize_path(extension)

    # Verify the package.json data.
    pkg_path = osp.join(path, 'package.json')
    if not osp.exists(pkg_path):
        msg = 'Linked package must point to a directory with package.json'
        raise ValueError(msg)

    with open(pkg_path) as fid:
        data = json.load(fid)

    _validate_package(data, path)

    # Update JupyterLab metadata.
    config = _get_config()
    name = data['name']
    config['linked_extensions'][name] = path
    if name in config['installed_extensions']:
        del config['installed_extensions'][name]
    _write_config(config)

def unlink_extension(extension):
    """Unlink an extension from JupyterLab by path or name.
    """
    extension = _normalize_path(extension)
    config = _get_config()

    name = None
    for (key, value) in config['linked_extensions'].items():
        if value == extension or key == extension:
            name = key
            break

    if name:
        del config['linked_extensions'][name]
        _write_config(config)
        return True

    print('No labextension matching "%s" is linked' % extension)
    return False

def uninstall_extension(name):
    """Uninstall an extension by name.
    """
    config = _get_config()

    if name in config['installed_extensions']:
        del config['installed_extensions'][name]
        _write_config(config)
        return True

    print('No labextension named "%s" installed' % name)
    return False

def list_extensions():
    """List installed extensions.
    """
    config = _get_config()
    installed = list(config['installed_extensions'])
    linked = list(config['linked_extensions'])
    return sorted(installed + linked)

def validate_extension(extension):
    """Verify that a JupyterLab extension is valid.
    """
    config = _get_config()
    extension = _normalize_path(extension)
    _ensure_package(config)
    cache_dir = _get_cache_dir(config)
    # npm pack the extension
    output = run(['npm', 'pack', extension], cwd=cache_dir)
    name = output.decode('utf8').splitlines()[-1]
    # read the package.json data from the file
    tar = tarfile.open(pjoin(cache_dir, name), "r:gz")
    f = tar.extractfile('package/package.json')
    data = json.loads(f.read().decode('utf8'))
    _validate_package(data, extension)
    return name, data['name']

def clean():
    """Clean the JupyterLab application directory."""
    config = _get_config()
    for name in ['node_modules', 'build']:
        target = pjoin(_get_root_dir(config), name)
        if osp.exists(target):
            shutil.rmtree(target)

def build():
    """Build the JupyterLab application."""
    # Set up the build directory.
    config = _get_config()
    _ensure_package(config)
    root = _get_root_dir(config)

    # Make sure packages are installed.
    run(['npm', 'install'], cwd=root)

    # Install the linked extensions.
    for value in config['linked_extensions'].values():
        run(['npm', 'install', value], cwd=root)

    # Build the app.
    run(['npm', 'run', 'build'], cwd=root)

def describe():
    """Get the git description of the JupyterLab application.
    """
    description = 'unknown'
    try:
        cwd = os.path.dirname(os.path.dirname(__file__))
        description = run(['git', 'describe'], cwd=cwd)
        description = description.decode('utf8').strip()
    except Exception:
        pass
    return description

def _ensure_package(config):
    """Make sure the build dir is set up.
    """
    cache_dir = _get_cache_dir(config)
    root_dir = _get_root_dir(config)
    if not osp.exists(cache_dir):
        os.makedirs(cache_dir)
    for name in ['package.json', 'index.template.js', 'webpack.config.js']:
        dest = pjoin(root_dir, name)
        shutil.copy2(pjoin(here, name), dest)

    # Template the package.json file.
    pkg_path = pjoin(root_dir, 'package.json')
    with open(pkg_path) as fid:
        data = json.load(fid)
    for (key, value) in config['installed_extensions'].items():
        data['dependencies'][key] = value
        data['jupyterlab']['extensions'].append(key)
    for key in config['linked_extensions']:
        data['jupyterlab']['extensions'].append(key)
    with open(pkg_path, 'w') as fid:
        json.dump(data, fid, indent=4)

def _validate_package(data, extension):
    """Validate package.json data.
    """
    msg = '%s is not a valid JupyterLab extension' % extension
    if 'jupyterlab' not in data:
        raise ValueError(msg)
    if not isinstance(data['jupyterlab'], dict):
        raise ValueError(msg)
    if not data['jupyterlab'].get('extension', False):
        raise ValueError(msg)

def _get_config(config_dir=None):
    """Get the JupyterLab config data.
    """
    config_dir = config_dir or _get_config_dir()
    file = pjoin(config_dir, 'build_config.json')
    if not osp.exists(file):
        data = {}
    else:
        with open(file) as fid:
            data = json.load(fid)
    data.setdefault('location', pjoin(BUILD_PATH, 'lab'))
    data.setdefault('installed_extensions', dict())
    data.setdefault('linked_extensions', dict())
    return data

def _write_config(data, config_dir=None):
    """Write the JupyterLab config data.
    """
    config_dir = config_dir or _get_config_dir()
    if not osp.exists(config_dir):
        try:
            os.makedirs(config_dir)
        except OSError as e:
            if e.errno != errno.EEXIST:
                pass
    with open(pjoin(config_dir, 'build_config.json'), 'w') as fid:
        json.dump(data, fid, indent=4)

def _normalize_path(extension):
    extension = osp.expanduser(extension)
    if osp.exists(extension):
        extension = osp.abspath(extension)
    return extension

def _get_config_dir():
    return pjoin(CONFIG_PATH, 'labconfig')

def _get_root_dir(config=None):
    config = config or _get_config()
    return config['location']

def _get_build_dir(config=None):
    config = config or _get_config()
    return pjoin(_get_root_dir(config), 'build')

def _get_pkg_path(config=None):
    config = config or _get_config()
    return pjoin(_get_root_dir(config), 'package.json')

def _get_cache_dir(config=None):
    config = config or _get_config()
    return pjoin(_get_root_dir(config), 'cache')
<|endoftext|>


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

import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Joy
from sensor_msgs.msg import JointState

import brics_actuator.msg

#/JointPositions

class Teleop(object):

    def __init__(self, linearAxisIndex = 1, angularAxisIndex = 0):
        rospy.init_node('picksie_teleop')
        self._joints=[]
        self.joint_names=['arm_joint_1', 'arm_joint_2', 'arm_joint_3', 'arm_joint_4', 'arm_joint_5']#, 'gripper_finger_joint_l', 'gripper_finger_joint_r']
        self.def_pos=[3.14,0.058,-0.4834,0.0222,2.88, 0.5, 0.5]
        self.drop_pos= [5.84, 0.49, -1.0, 0.083, 2.88]

        self._xymode=False
        self.turbo = 1
        self.arm_disabled=True

        self._LinearAxisIndex = rospy.get_param("~linearAxisIndex", linearAxisIndex)
        self._AngularAxisIndex = rospy.get_param("~angularAxisIndex", angularAxisIndex)
        self._LinearScalingFactor = rospy.get_param("~linearScalingFactor", 0.5)
        self._AngularScalingFactor = rospy.get_param("~angularScalingFactor", 0.5)

        rospy.loginfo("Starting teleop node with linear axis %d and angular axis %d" % (self._LinearAxisIndex, self._AngularAxisIndex))

        for i in self.joint_names:
            joint={}
            joint['name']=i
            self._joints.append(joint)

        joint={}
        joint['name']= 'gripper_finger_joint_r'
        self._joints.append(joint)

        #print self._joints

        # subscriptions
        rospy.Subscriber("/joint_states", JointState, self._HandleJointStateMessage)
        rospy.Subscriber("/joy", Joy, self._HandleJoystickMessage)

        self._BricsCmdPublisher = rospy.Publisher("/arm_1/arm_controller/position_command", brics_actuator.msg.JointPositions, queue_size=1)
        self._GripperCmdPublisher = rospy.Publisher("/arm_1/gripper_controller/position_command", brics_actuator.msg.JointPositions, queue_size=1)

        self._VelocityCommandPublisher = rospy.Publisher("/cmd_vel", Twist, queue_size=1)
        rospy.sleep(2)
        self.go_to_search_pos()

    def go_to_search_pos(self):
        print "going to search pos"
        for i in range(0,len(self.joint_names)):
            arm_cmd = brics_actuator.msg.JointPositions()
            j_cmd= brics_actuator.msg.JointValue()
            j_cmd.joint_uri = self.joint_names[i]
            j_cmd.unit = 'rad'
            j_cmd.value = self.def_pos[i]
            arm_cmd.positions.append(j_cmd)
            #print arm_cmd
            self._BricsCmdPublisher.publish(arm_cmd)
            rospy.sleep(1)
        self.arm_disabled=False

    def go_to_drop_pos(self):
        print "going to drop pos"
        for i in range(0,len(self.joint_names)):
            arm_cmd = brics_actuator.msg.JointPositions()
            j_cmd= brics_actuator.msg.JointValue()
            j_cmd.joint_uri = self.joint_names[i]
            j_cmd.unit = 'rad'
            j_cmd.value = self.drop_pos[i]
            arm_cmd.positions.append(j_cmd)
            #print arm_cmd
            self._BricsCmdPublisher.publish(arm_cmd)
            rospy.sleep(1)
        self.arm_disabled=False

    def toggle_gripper(self):
        print self._joints[5]
        grp_cmd = brics_actuator.msg.JointPositions()
        j_cmd= brics_actuator.msg.JointValue()
        j_cmd.joint_uri = self._joints[5]['name']
        j_cmd.unit = 'm'
        if self._joints[5]['pos'] < 0.005:
            j_cmd.value = 0.0115
        else:
            j_cmd.value = 0.0
        grp_cmd.positions.append(j_cmd)
        self._GripperCmdPublisher.publish(grp_cmd)

    def _HandleJointStateMessage(self, jointMessage):
        for i in self._joints:
            if i['name'] in jointMessage.name:
                i['pos'] = jointMessage.position[jointMessage.name.index(i['name'])]

        #print self._joints

    def _HandleJoystickMessage(self, joyMessage):
        turbo = self.turbo
        if joyMessage.buttons[9]:
            self._xymode = not self._xymode

        if joyMessage.buttons[6]:
            self.turbo = 1.0
            print "Turbo = %d" %turbo
        if joyMessage.buttons[7]:
            self.turbo += 1.0
            print "Turbo = %d" %turbo

        if joyMessage.buttons[4]:
            if not self._xymode:
                velocityCommand = Twist()
                velocityCommand.linear.x = self._LinearScalingFactor * joyMessage.axes[self._LinearAxisIndex]
                velocityCommand.linear.y = self._LinearScalingFactor * joyMessage.axes[3]
                velocityCommand.angular.z = self._AngularScalingFactor * joyMessage.axes[self._AngularAxisIndex]
            else:
                velocityCommand = Twist()
                velocityCommand.linear.x = self._LinearScalingFactor * joyMessage.axes[self._LinearAxisIndex]
                velocityCommand.linear.y = self._LinearScalingFactor * joyMessage.axes[self._AngularAxisIndex]
                velocityCommand.angular.z = self._AngularScalingFactor * joyMessage.axes[3]
        else:
            velocityCommand = Twist()
            velocityCommand.linear.x = 0.0
            velocityCommand.linear.y = 0.0
            velocityCommand.angular.z = 0.0
        self._VelocityCommandPublisher.publish(velocityCommand)

        if joyMessage.buttons[3]:
            self.go_to_search_pos()
            self.arm_disabled=True

        if joyMessage.buttons[0]:
            self.go_to_drop_pos()
            self.arm_disabled=True

        if joyMessage.buttons[5]:
            print self._joints
            self.toggle_gripper()

        if not self.arm_disabled:
            arm_cmd = brics_actuator.msg.JointPositions()

            if abs(joyMessage.axes[6])>0.5:
                j_cmd= brics_actuator.msg.JointValue()
                if joyMessage.axes[6] > 0:
                    j_cmd.joint_uri = self._joints[0]['name']
                    j_cmd.unit = 'rad'
                    j_cmd.value = self._joints[0]['pos'] + (0.0174533*turbo)
                    if j_cmd.value > 5.84 :
                       j_cmd.value = 5.84
                else:
                    j_cmd.joint_uri = self._joints[0]['name']
                    j_cmd.unit = 'rad'
                    j_cmd.value = self._joints[0]['pos'] - (0.0174533*turbo)
                    if j_cmd.value < 0.011 :
                       j_cmd.value = 0.011
                arm_cmd.positions.append(j_cmd)

            if abs(joyMessage.axes[7])>0.5:
                j_cmd= brics_actuator.msg.JointValue()
                if joyMessage.axes[7] > 0:
                    j_cmd.joint_uri = self._joints[1]['name']
                    j_cmd.unit = 'rad'
                    j_cmd.value = self._joints[1]['pos'] + (0.0174533*turbo)
                    if j_cmd.value > 2.617 :
                       j_cmd.value = 2.617
                else:
                    j_cmd.joint_uri = self._joints[1]['name']
                    j_cmd.unit = 'rad'
                    j_cmd.value = self._joints[1]['pos'] - (0.0174533*turbo)
                    if j_cmd.value < 0.011 :
                       j_cmd.value = 0.011
                arm_cmd.positions.append(j_cmd)

            if joyMessage.buttons[0]:
                j_cmd= brics_actuator.msg.JointValue()
                j_cmd.joint_uri = self._joints[4]['name']
                j_cmd.unit = 'rad'
                j_cmd.value = 2.90
                arm_cmd.positions.append(j_cmd)

#            if abs(joyMessage.axes[3])>0.5:
#                j_cmd= brics_actuator.msg.JointValue()
#                if joyMessage.axes[3] > 0:
#                    j_cmd.joint_uri = self._joints[4]['name']
#                    j_cmd.unit = 'rad'
#                    j_cmd.value = self._joints[4]['pos'] + 0.174533
#                    if j_cmd.value > 5.64 :
#                       j_cmd.value = 5.64
#                else:
#                    j_cmd.joint_uri = self._joints[4]['name']
#                    j_cmd.unit = 'rad'
#                    j_cmd.value = self._joints[4]['pos'] - 0.174533
#                    if j_cmd.value < 0.111 :
#                       j_cmd.value = 0.111
#                arm_cmd.positions.append(j_cmd)

            if abs(joyMessage.axes[4])>0.5:
                j_cmd= brics_actuator.msg.JointValue()
                if joyMessage.axes[4] > 0:
                    j_cmd.joint_uri = self._joints[3]['name']
                    j_cmd.unit = 'rad'
                    j_cmd.value = self._joints[3]['pos'] + (0.0174533 * abs(joyMessage.axes[4]))
                    if j_cmd.value > 3.42 :
                       j_cmd.value = 3.42
                else:
                    j_cmd.joint_uri = self._joints[3]['name']
                    j_cmd.unit = 'rad'
                    j_cmd.value = self._joints[3]['pos'] - (0.0174533 * abs(joyMessage.axes[4]))
                    if j_cmd.value < 0.0222 :
                       j_cmd.value = 0.0222
                arm_cmd.positions.append(j_cmd)

            if joyMessage.axes[5] < 0.0:
                j_cmd= brics_actuator.msg.JointValue()
                j_cmd.joint_uri = self._joints[2]['name']
                j_cmd.unit = 'rad'
                j_cmd.value = self._joints[2]['pos'] + 0.0174533
                if j_cmd.value > -0.0158 :
                   j_cmd.value = -0.0158
                arm_cmd.positions.append(j_cmd)

            if joyMessage.axes[2] < 0.0:
                j_cmd= brics_actuator.msg.JointValue()
                j_cmd.joint_uri = self._joints[2]['name']
                j_cmd.unit = 'rad'
                j_cmd.value = self._joints[2]['pos'] - 0.0174533
                if j_cmd.value < -5.0655 :
                   j_cmd.value = -5.0655
                arm_cmd.positions.append(j_cmd)

            if len(arm_cmd.positions) >0:
                self._BricsCmdPublisher.publish(arm_cmd)

if __name__ == '__main__':
    teleop = Teleop()
    rospy.spin()<commit_msg>new search and drop positions<commit_after>#!/usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Joy
from sensor_msgs.msg import JointState

import brics_actuator.msg

#/JointPositions

class Teleop(object):

    def __init__(self, linearAxisIndex = 1, angularAxisIndex = 0):
        rospy.init_node('picksie_teleop')
        self._joints=[]
        self.joint_names=['arm_joint_1', 'arm_joint_2', 'arm_joint_3', 'arm_joint_4', 'arm_joint_5']#, 'gripper_finger_joint_l', 'gripper_finger_joint_r']
        #self.def_pos=[3.14,0.058,-0.4834,2.4,2.88, 0.5, 0.5]

        self.def_pos=[3.0, 2.61, -0.91, 0.25, 2.88, 0.0115, 0.0]
#{'name': 'gripper_finger_joint_r', 'pos': 0.0115}
#        self.drop_pos= [5.84, 0.49, -1.0, 0.083, 2.88]
        self.drop_pos= [3.0, 0.011, -2.25, 0.25, 0.111, 0.0]
#{'name': 'gripper_finger_joint_r', 'pos': 0.0}

        self._xymode=False
        self.turbo = 1
        self.arm_disabled=True

        self._LinearAxisIndex = rospy.get_param("~linearAxisIndex", linearAxisIndex)
        self._AngularAxisIndex = rospy.get_param("~angularAxisIndex", angularAxisIndex)
        self._LinearScalingFactor = rospy.get_param("~linearScalingFactor", 0.5)
        self._AngularScalingFactor = rospy.get_param("~angularScalingFactor", 0.5)

        rospy.loginfo("Starting teleop node with linear axis %d and angular axis %d" % (self._LinearAxisIndex, self._AngularAxisIndex))

        for i in self.joint_names:
            joint={}
            joint['name']=i
            self._joints.append(joint)

        joint={}
        joint['name']= 'gripper_finger_joint_r'
        self._joints.append(joint)

        #print self._joints

        # subscriptions
        rospy.Subscriber("/joint_states", JointState, self._HandleJointStateMessage)
        rospy.Subscriber("/joy", Joy, self._HandleJoystickMessage)

        self._BricsCmdPublisher = rospy.Publisher("/arm_1/arm_controller/position_command", brics_actuator.msg.JointPositions, queue_size=1)
        self._GripperCmdPublisher = rospy.Publisher("/arm_1/gripper_controller/position_command", brics_actuator.msg.JointPositions, queue_size=1)

        self._VelocityCommandPublisher = rospy.Publisher("/cmd_vel", Twist, queue_size=1)
        rospy.sleep(2)
        self.go_to_search_pos()

    def go_to_search_pos(self):
        print "going to search pos"
        for i in range(0,len(self.joint_names)):
            arm_cmd = brics_actuator.msg.JointPositions()
            j_cmd= brics_actuator.msg.JointValue()
            j_cmd.joint_uri = self.joint_names[i]
            j_cmd.unit = 'rad'
            j_cmd.value = self.def_pos[i]
            arm_cmd.positions.append(j_cmd)
            print arm_cmd
            self._BricsCmdPublisher.publish(arm_cmd)
            rospy.sleep(1)
        self.arm_disabled=False
        print "Done"

    def go_to_drop_pos(self):
        print "going to drop pos"
        for i in range(0,len(self.joint_names)):
            arm_cmd = brics_actuator.msg.JointPositions()
            j_cmd= brics_actuator.msg.JointValue()
            j_cmd.joint_uri = self.joint_names[i]
            j_cmd.unit = 'rad'
            j_cmd.value = self.drop_pos[i]
            arm_cmd.positions.append(j_cmd)
            #print arm_cmd
            self._BricsCmdPublisher.publish(arm_cmd)
            rospy.sleep(1)
        self.arm_disabled=False

    def toggle_gripper(self):
        print self._joints[5]
        grp_cmd = brics_actuator.msg.JointPositions()
        j_cmd= brics_actuator.msg.JointValue()
        j_cmd.joint_uri = self._joints[5]['name']
        j_cmd.unit = 'm'
        if self._joints[5]['pos'] < 0.005:
            j_cmd.value = 0.0115
        else:
            j_cmd.value = 0.0
        grp_cmd.positions.append(j_cmd)
        self._GripperCmdPublisher.publish(grp_cmd)

    def _HandleJointStateMessage(self, jointMessage):
        for i in self._joints:
            if i['name'] in jointMessage.name:
                i['pos'] = jointMessage.position[jointMessage.name.index(i['name'])]

        #print self._joints

    def _HandleJoystickMessage(self, joyMessage):
        turbo = self.turbo
        if joyMessage.buttons[9]:
            self._xymode = not self._xymode

        if joyMessage.buttons[6]:
            self.turbo = 1.0
            print "Turbo = %d" %turbo
        if joyMessage.buttons[7]:
            self.turbo += 1.0
            print "Turbo = %d" %turbo

        if joyMessage.buttons[4]:
            if not self._xymode:
                velocityCommand = Twist()
                velocityCommand.linear.x = self._LinearScalingFactor * joyMessage.axes[self._LinearAxisIndex]
                velocityCommand.linear.y = self._LinearScalingFactor * joyMessage.axes[3]
                velocityCommand.angular.z = self._AngularScalingFactor * joyMessage.axes[self._AngularAxisIndex]
            else:
                velocityCommand = Twist()
                velocityCommand.linear.x = self._LinearScalingFactor * joyMessage.axes[self._LinearAxisIndex]
                velocityCommand.linear.y = self._LinearScalingFactor * joyMessage.axes[self._AngularAxisIndex]
                velocityCommand.angular.z = self._AngularScalingFactor * joyMessage.axes[3]
        else:
            velocityCommand = Twist()
            velocityCommand.linear.x = 0.0
            velocityCommand.linear.y = 0.0
            velocityCommand.angular.z = 0.0
        self._VelocityCommandPublisher.publish(velocityCommand)

        if joyMessage.buttons[3]:
            self.go_to_search_pos()
            self.arm_disabled=True

        if joyMessage.buttons[0]:
            self.go_to_drop_pos()
            self.arm_disabled=True

        if joyMessage.buttons[5]:
            print self._joints
            self.toggle_gripper()

        if not self.arm_disabled:
            arm_cmd = brics_actuator.msg.JointPositions()

            if abs(joyMessage.axes[6])>0.5:
                j_cmd= brics_actuator.msg.JointValue()
                if joyMessage.axes[6] > 0:
                    j_cmd.joint_uri = self._joints[0]['name']
                    j_cmd.unit = 'rad'
                    j_cmd.value = self._joints[0]['pos'] + (0.0174533*turbo)
                    if j_cmd.value > 5.84 :
                       j_cmd.value = 5.84
                else:
                    j_cmd.joint_uri = self._joints[0]['name']
                    j_cmd.unit = 'rad'
                    j_cmd.value = self._joints[0]['pos'] - (0.0174533*turbo)
                    if j_cmd.value < 0.011 :
                       j_cmd.value = 0.011
                arm_cmd.positions.append(j_cmd)

            if abs(joyMessage.axes[7])>0.5:
                j_cmd= brics_actuator.msg.JointValue()
                if joyMessage.axes[7] > 0:
                    j_cmd.joint_uri = self._joints[1]['name']
                    j_cmd.unit = 'rad'
                    j_cmd.value = self._joints[1]['pos'] + (0.0174533*turbo)
                    if j_cmd.value > 2.617 :
                       j_cmd.value = 2.617
                else:
                    j_cmd.joint_uri = self._joints[1]['name']
                    j_cmd.unit = 'rad'
                    j_cmd.value = self._joints[1]['pos'] - (0.0174533*turbo)
                    if j_cmd.value < 0.011 :
                       j_cmd.value = 0.011
                arm_cmd.positions.append(j_cmd)

            if joyMessage.buttons[0]:
                j_cmd= brics_actuator.msg.JointValue()
                j_cmd.joint_uri = self._joints[4]['name']
                j_cmd.unit = 'rad'
                j_cmd.value = 2.90
                arm_cmd.positions.append(j_cmd)

#            if abs(joyMessage.axes[3])>0.5:
#                j_cmd= brics_actuator.msg.JointValue()
#                if joyMessage.axes[3] > 0:
#                    j_cmd.joint_uri = self._joints[4]['name']
#                    j_cmd.unit = 'rad'
#                    j_cmd.value = self._joints[4]['pos'] + 0.174533
#                    if j_cmd.value > 5.64 :
#                       j_cmd.value = 5.64
#                else:
#                    j_cmd.joint_uri = self._joints[4]['name']
#                    j_cmd.unit = 'rad'
#                    j_cmd.value = self._joints[4]['pos'] - 0.174533
#                    if j_cmd.value < 0.111 :
#                       j_cmd.value = 0.111
#                arm_cmd.positions.append(j_cmd)

            if abs(joyMessage.axes[4])>0.5:
                j_cmd= brics_actuator.msg.JointValue()
                if joyMessage.axes[4] > 0:
                    j_cmd.joint_uri = self._joints[3]['name']
                    j_cmd.unit = 'rad'
                    j_cmd.value = self._joints[3]['pos'] + (0.0174533 * abs(joyMessage.axes[4]))
                    if j_cmd.value > 3.42 :
                       j_cmd.value = 3.42
                else:
                    j_cmd.joint_uri = self._joints[3]['name']
                    j_cmd.unit = 'rad'
                    j_cmd.value = self._joints[3]['pos'] - (0.0174533 * abs(joyMessage.axes[4]))
                    if j_cmd.value < 0.0222 :
                       j_cmd.value = 0.0222
                arm_cmd.positions.append(j_cmd)

            if joyMessage.axes[5] < 0.0:
                j_cmd= brics_actuator.msg.JointValue()
                j_cmd.joint_uri = self._joints[2]['name']
                j_cmd.unit = 'rad'
                j_cmd.value = self._joints[2]['pos'] + 0.0174533
                if j_cmd.value > -0.0158 :
                   j_cmd.value = -0.0158
                arm_cmd.positions.append(j_cmd)

            if joyMessage.axes[2] < 0.0:
                j_cmd= brics_actuator.msg.JointValue()
                j_cmd.joint_uri = self._joints[2]['name']
                j_cmd.unit = 'rad'
                j_cmd.value = self._joints[2]['pos'] - 0.0174533
                if j_cmd.value < -5.0655 :
                   j_cmd.value = -5.0655
                arm_cmd.positions.append(j_cmd)

            if len(arm_cmd.positions) >0:
                self._BricsCmdPublisher.publish(arm_cmd)

if __name__ == '__main__':
    teleop = Teleop()
    rospy.spin()<|endoftext|>


<commit_before>"""
Packaging operations for the shell manager.
"""

import json, re, gzip
import spur

from os import makedirs, listdir
from os.path import join, isdir

from shell_manager import problem_repo
from shell_manager.util import full_copy, move

def problem_to_control(problem, control_path):
    """
    Convert problem.json to a deb control file.

    Args:
        problem: deserialized problem.json (dict)
        control_path: path to the DEBIAN directory
    """

    #a-z, digits 0-9, plus + and minus - signs, and periods
    sanitized_name = re.sub(r"[^a-z0-9\+-\.]", "-", problem["name"].lower())

    control = {
        "Package": sanitized_name,
        "Version": problem.get("version", "1.0-0"),
        "Section": "ctf",
        "Priority": "optional",
        "Architecture": "all",
        "Maintainer": problem.get("author", "None"),
        "Description": problem.get("description", "No provided package description.")
    }

    contents = ""
    for option, value in control.items():
        contents += "{}: {}\n".format(option, value)

    control_file = open(join(control_path, "control"), "w")
    control_file.write(contents)
    control_file.close()

def get_problem(problem_path):
    """
    Retrieve a problem spec from a given problem directory.

    Args:
        problem_path: path to the root of the problem directory.

    Returns:
        A problem object.
    """

    json_path = join(problem_path, "problem.json")
    problem = json.loads(open(json_path, "r").read())

    return problem

def problem_builder(args):
    """
    Main entrypoint for package building operations.
    """

    problem_path = args.problem_paths.pop()
    problem = get_problem(problem_path)

    paths = {}
    paths["staging"] = join(problem_path, "staging")

    paths["control"] = join(paths["staging"], "DEBIAN")
    paths["data"] = join(paths["staging"], "problems", problem["name"])

    #Make all of the directories, order does not matter with makedirs
    [makedirs(staging_path) for _, staging_path in paths.items() if not isdir(staging_path)]

    full_copy(problem_path, paths["data"], ignore=["staging"])

    problem_to_control(problem, paths["control"])

    deb_directory = args.out if args.out is not None else paths["staging"]
    deb_path = join(deb_directory, problem["name"] + ".deb")

    shell = spur.LocalShell()
    result = shell.run(["fakeroot", "dpkg-deb", "--build", paths["staging"], deb_path])

    if result.return_code != 0:
        print("Error building problem deb for '{}'".format(problem["name"]))
        print(result.output)
    else:
        print("Problem '{}' packaged successfully.".format(problem["name"]))

    #Ensure repo exists
    if not isdir(args.repository):
        makedirs(args.repository)

    deb_package_path = join(paths["staging"], deb_path)

    if len(args.problem_paths) >= 1:
        #Copy the deb and process the rest of the problems
        move(deb_package_path, args.repository)
        return problem_builder(args)

    problem_repo.update(args.repository, [deb_package_path])
<commit_msg>Generating control files with reasonable defaults.<commit_after>"""
Packaging operations for the shell manager.
"""

import json, re, gzip
import spur

from os import makedirs, listdir
from os.path import join, isdir

from copy import deepcopy

from shell_manager import problem_repo
from shell_manager.util import full_copy, move
from shell_manager.problem import get_problem

DEB_DEFAULTS = {
    "Section": "ctf",
    "Priority": "optional",
}

def problem_to_control(problem, control_path):
    """
    Convert problem.json to a deb control file.

    Args:
        problem: deserialized problem.json (dict)
        control_path: path to the DEBIAN directory
    """

    #a-z, digits 0-9, plus + and minus - signs, and periods
    package_name = problem.get("pkg_name", problem["name"])
    sanitized_name = re.sub(r"[^a-z0-9\+-\.]", "-", package_name.lower())

    control = deepcopy(DEB_DEFAULTS)
    control.update(**{
        "Package": sanitized_name,
        "Version": problem.get("version", "1.0-0"),
        "Architecture": problem.get("architecture", "all"),
        "Maintainer": problem["author"],
        "Description": problem.get("pkg_description", problem["description"])
    })

    contents = ""
    for option, value in control.items():
        contents += "{}: {}\n".format(option, value)

    control_file = open(join(control_path, "control"), "w")
    control_file.write(contents)
    control_file.close()

def problem_builder(args):
    """
    Main entrypoint for package building operations.
    """

    problem_path = args.problem_paths.pop()
    problem = get_problem(problem_path)

    paths = {}
    paths["staging"] = join(problem_path, "staging")

    paths["control"] = join(paths["staging"], "DEBIAN")
    paths["data"] = join(paths["staging"], "problems", problem["name"])

    #Make all of the directories, order does not matter with makedirs
    [makedirs(staging_path) for _, staging_path in paths.items() if not isdir(staging_path)]

    full_copy(problem_path, paths["data"], ignore=["staging"])

    problem_to_control(problem, paths["control"])

    deb_directory = args.out if args.out is not None else paths["staging"]
    deb_path = join(deb_directory, problem["name"] + ".deb")

    shell = spur.LocalShell()
    result = shell.run(["fakeroot", "dpkg-deb", "--build", paths["staging"], deb_path])

    if result.return_code != 0:
        print("Error building problem deb for '{}'".format(problem["name"]))
        print(result.output)
    else:
        print("Problem '{}' packaged successfully.".format(problem["name"]))

    #Ensure repo exists
    if not isdir(args.repository):
        makedirs(args.repository)

    deb_package_path = join(paths["staging"], deb_path)

    if len(args.problem_paths) >= 1:
        #Copy the deb and process the rest of the problems
        move(deb_package_path, args.repository)
        return problem_builder(args)

    problem_repo.update(args.repository, [deb_package_path])
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

"""
kay.ext.gaema.views

:Copyright: (c) 2009 Takashi Matsuo <tmatsuo@candit.jp> All rights reserved.
:license: BSD, see LICENSE for more details.

"""

from werkzeug import (
  redirect, Response
)

from kay.utils import set_cookie
from kay.utils import render_to_response
from kay.ext.gaema import (
  GoogleAuth, TwitterAuth, FacebookAuth, GAEMA_USER_KEY_FORMAT,
  NEXT_URL_KEY_FORMAT
)
from kay.ext.gaema.utils import (
  set_gaema_user, get_gaema_user
)

# Create your views here.

auth_modules = {
  'goog_openid': GoogleAuth,
  'goog_hybrid': GoogleAuth,
  'twitter': TwitterAuth,
  'facebook': FacebookAuth,
}

def create_login_view(name, oauth_scope=None):
  auth_module = auth_modules[name]
  next_url_key =  NEXT_URL_KEY_FORMAT % name
  def login_view(request, *args, **kwargs):
    def auth_callback(user):
      set_gaema_user(name, user)
    next_url = request.cookies.get(next_url_key, None)
    if next_url is None:
      next_url = "/"
    if get_gaema_user(name):
      return redirect(next_url)
    auth_instance = auth_module(request)
    if auth_instance.is_callback():
      auth_instance.get_authenticated_user(auth_callback)
      return redirect(next_url)
    if oauth_scope:
      auth_instance.authorize_redirect(oauth_scope)
    else:
      auth_instance.authenticate_redirect()
  return login_view

def create_logout_view(name):
  next_url_key = NEXT_URL_KEY_FORMAT % name
  def logout_view(request, *args, **kwargs):
    set_gaema_user(name, None)
    next_url = request.cookies.get(next_url_key, None)
    if next_url is None:
      next_url = "/"
    return redirect(next_url)
  return logout_view

goog_hybrid_login = create_login_view(
  "goog_hybrid",
  oauth_scope = "http://www.google.com/m8/feeds/ "
  "http://finance.google.com/finance/feeds/")
goog_openid_login = create_login_view("goog_openid")
twitter_login = create_login_view("twitter")
facebook_login = create_login_view("facebook")

goog_hybrid_logout = create_logout_view("goog_hybrid")
goog_openid_logout = create_logout_view("goog_openid")
twitter_logout = create_logout_view("twitter")
facebook_logout = create_logout_view("facebook")
<commit_msg>Import stuff.<commit_after># -*- coding: utf-8 -*-

"""
kay.ext.gaema.views

:Copyright: (c) 2009 Takashi Matsuo <tmatsuo@candit.jp> All rights reserved.
:license: BSD, see LICENSE for more details.

"""

from werkzeug import redirect

from kay.utils import set_cookie
from kay.utils import render_to_response
from kay.ext.gaema import (
  GoogleAuth, TwitterAuth, FacebookAuth, GAEMA_USER_KEY_FORMAT,
  NEXT_URL_KEY_FORMAT
)
from kay.ext.gaema.utils import (
  set_gaema_user, get_gaema_user
)

# Create your views here.

auth_modules = {
  'goog_openid': GoogleAuth,
  'goog_hybrid': GoogleAuth,
  'twitter': TwitterAuth,
  'facebook': FacebookAuth,
}

def create_login_view(name, oauth_scope=None):
  auth_module = auth_modules[name]
  next_url_key =  NEXT_URL_KEY_FORMAT % name
  def login_view(request, *args, **kwargs):
    def auth_callback(user):
      set_gaema_user(name, user)
    next_url = request.cookies.get(next_url_key, None)
    if next_url is None:
      next_url = "/"
    if get_gaema_user(name):
      return redirect(next_url)
    auth_instance = auth_module(request)
    if auth_instance.is_callback():
      auth_instance.get_authenticated_user(auth_callback)
      return redirect(next_url)
    if oauth_scope:
      auth_instance.authorize_redirect(oauth_scope)
    else:
      auth_instance.authenticate_redirect()
  return login_view

def create_logout_view(name):
  next_url_key = NEXT_URL_KEY_FORMAT % name
  def logout_view(request, *args, **kwargs):
    set_gaema_user(name, None)
    next_url = request.cookies.get(next_url_key, None)
    if next_url is None:
      next_url = "/"
    return redirect(next_url)
  return logout_view

goog_hybrid_login = create_login_view(
  "goog_hybrid",
  oauth_scope = "http://www.google.com/m8/feeds/ "
  "http://finance.google.com/finance/feeds/")
goog_openid_login = create_login_view("goog_openid")
twitter_login = create_login_view("twitter")
facebook_login = create_login_view("facebook")

goog_hybrid_logout = create_logout_view("goog_hybrid")
goog_openid_logout = create_logout_view("goog_openid")
twitter_logout = create_logout_view("twitter")
facebook_logout = create_logout_view("facebook")
<|endoftext|>


<commit_before>#
# This file is part of khmer, http://github.com/ged-lab/khmer/, and is
# Copyright (C) Michigan State University, 2009-2013. It is licensed under
# the three-clause BSD license; see doc/LICENSE.txt. Contact: ctb@msu.edu
#
import os
import argparse
import khmer

DEFAULT_K = 32
DEFAULT_N_HT = 4
DEFAULT_MIN_HASHSIZE = 1e6

def build_construct_args(descr=None):
    """ Build common options for counting hash scripts.

    Returns an argparse.ArgumentParser.

    """

    if descr is None:
        descr = 'Build & load a counting Bloom filter.'

    parser = argparse.ArgumentParser(
        description=descr,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    env_ksize = os.environ.get('KHMER_KSIZE', DEFAULT_K)
    env_n_hashes = os.environ.get('KHMER_N_HASHES', DEFAULT_N_HT)
    env_hashsize = os.environ.get('KHMER_MIN_HASHSIZE', DEFAULT_MIN_HASHSIZE)

    parser.add_argument('-q', '--quiet', dest='quiet', default=False,
                        action='store_true')

    parser.add_argument('--ksize', '-k', type=int, dest='ksize',
                        default=env_ksize,
                        help='k-mer size to use')
    parser.add_argument('--n_hashes', '-N', type=int, dest='n_hashes',
                        default=env_n_hashes,
                        help='number of hash tables to use')
    parser.add_argument('--hashsize', '-x', type=float, dest='min_hashsize',
                        default=env_hashsize,
                        help='lower bound on hashsize to use')
    parser.add_argument('--version', action='version', version='%(prog)s '
                        + khmer.__version__)

    return parser

def build_counting_multifile_args():
    """Build common options for multi-file counting hash scripts.

    Returns an argparse.ArgumentParser.

    """
    parser = argparse.ArgumentParser(
        description='Use a counting Bloom filter.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--version', action='version', version='%(prog)s ' +
                        khmer.__version__)

    parser.add_argument('input_table')
    parser.add_argument('input_filenames', nargs='+')

    return parser

def report_on_config(args):
    """
        Summarizes the configuration produced by the command-line arguments
        made available by this module.
    """

    from khmer.utils import print_error

    if args.quiet:
        return

    print_error("\nPARAMETERS:")
    print_error(" - kmer size =    {0} \t\t(-k)".format(args.ksize))
    print_error(" - n hashes =     {0} \t\t(-N)".format(args.n_hashes))
    print_error(
        " - min hashsize = {0:5.2g} \t(-x)".format(args.min_hashsize)
    )
    print_error("")
    print_error(
        "Estimated memory usage is {0:.2g} bytes "
        "(n_hashes x min_hashsize)".format(args.n_hashes * args.min_hashsize)
    )
    print_error("-" * 8)

    if DEFAULT_MIN_HASHSIZE == args.min_hashsize:
        print_error(
            "** WARNING: hashsize is default!  "
            "You absodefly want to increase this!\n** "
            "Please read the docs!"
        )

# vim: set ft=python ts=4 sts=4 sw=4 et tw=79:
<commit_msg>you touch it, you fix it. Docstrings and formatting fixes only<commit_after>'''Shared ArgumentParser methods.

This file is part of khmer, http://github.com/ged-lab/khmer/, and is
Copyright (C) Michigan State University, 2009-2014. It is licensed under
the three-clause BSD license; see doc/LICENSE.txt.
Contact: khmer-project@idyll.org
'''
import os
import argparse
import khmer

DEFAULT_K = 32
DEFAULT_N_HT = 4
DEFAULT_MIN_HASHSIZE = 1e6

def build_construct_args(descr=None):
    """Build common options for counting hash scripts.

    Returns an argparse.ArgumentParser.
    """

    if descr is None:
        descr = 'Build & load a counting Bloom filter.'

    parser = argparse.ArgumentParser(
        description=descr,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    env_ksize = os.environ.get('KHMER_KSIZE', DEFAULT_K)
    env_n_hashes = os.environ.get('KHMER_N_HASHES', DEFAULT_N_HT)
    env_hashsize = os.environ.get('KHMER_MIN_HASHSIZE', DEFAULT_MIN_HASHSIZE)

    parser.add_argument('-q', '--quiet', dest='quiet', default=False,
                        action='store_true')

    parser.add_argument('--ksize', '-k', type=int, dest='ksize',
                        default=env_ksize,
                        help='k-mer size to use')
    parser.add_argument('--n_hashes', '-N', type=int, dest='n_hashes',
                        default=env_n_hashes,
                        help='number of hash tables to use')
    parser.add_argument('--hashsize', '-x', type=float, dest='min_hashsize',
                        default=env_hashsize,
                        help='lower bound on hashsize to use')
    parser.add_argument('--version', action='version', version='%(prog)s '
                        + khmer.__version__)

    return parser

def build_counting_multifile_args():
    """Build common options for multi-file counting hash scripts.

    Returns an argparse.ArgumentParser.
    """
    parser = argparse.ArgumentParser(
        description='Use a counting Bloom filter.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--version', action='version', version='%(prog)s ' +
                        khmer.__version__)

    parser.add_argument('input_table')
    parser.add_argument('input_filenames', nargs='+')

    return parser

def report_on_config(args):
    """Summarize the configuration produced by the command-line arguments
    made available by this module.
    """

    from khmer.utils import print_error

    if args.quiet:
        return

    print_error("\nPARAMETERS:")
    print_error(" - kmer size =    {0} \t\t(-k)".format(args.ksize))
    print_error(" - n hashes =     {0} \t\t(-N)".format(args.n_hashes))
    print_error(
        " - min hashsize = {0:5.2g} \t(-x)".format(args.min_hashsize)
    )
    print_error("")
    print_error(
        "Estimated memory usage is {0:.2g} bytes "
        "(n_hashes x min_hashsize)".format(args.n_hashes * args.min_hashsize)
    )
    print_error("-" * 8)

    if DEFAULT_MIN_HASHSIZE == args.min_hashsize:
        print_error(
            "** WARNING: hashsize is default!  "
            "You absodefly want to increase this!\n** "
            "Please read the docs!"
        )

# vim: set ft=python ts=4 sts=4 sw=4 et tw=79:
<|endoftext|>


<commit_before># coding=utf-8
# Filename: __init__.py
# pylint: disable=locally-disabled
"""
A collection of commonly used modules.

"""
from __future__ import division, absolute_import, print_function

import datetime

from km3pipe import Module

class HitCounter(Module):
    """Prints the number of hits and raw hits in an Evt file"""
    def process(self, blob):
        print("Number of hits: {0}".format(len(blob['hit'])))
        print("Number of raw hits: {0}".format(len(blob['hit_raw'])))
        return blob

class StatusBar(Module):
    """Displays the current blob number"""
    def __init__(self, **context):
        super(self.__class__, self).__init__(**context)
        self.blob_index = 0

        self.start = datetime.datetime.now()

    def process(self, blob):
        print("----[Blob {0:>5}]----".format(self.blob_index))
        self.blob_index += 1
        return blob

    def finish(self):
        elapsed_time = self.start - datetime.datetime.now()
        print("\n" + '='*42)
        print("Processed {0} blobs in {1} s."
              .format(self.blob_index, elapsed_time.microseconds / 1e6))
<commit_msg>Docstring for StatusBar.finish()<commit_after># coding=utf-8
# Filename: __init__.py
# pylint: disable=locally-disabled
"""
A collection of commonly used modules.

"""
from __future__ import division, absolute_import, print_function

import datetime

from km3pipe import Module

class HitCounter(Module):
    """Prints the number of hits and raw hits in an Evt file"""
    def process(self, blob):
        print("Number of hits: {0}".format(len(blob['hit'])))
        print("Number of raw hits: {0}".format(len(blob['hit_raw'])))
        return blob

class StatusBar(Module):
    """Displays the current blob number"""
    def __init__(self, **context):
        super(self.__class__, self).__init__(**context)
        self.blob_index = 0

        self.start = datetime.datetime.now()

    def process(self, blob):
        print("----[Blob {0:>5}]----".format(self.blob_index))
        self.blob_index += 1
        return blob

    def finish(self):
        """Display some basic statistics like elapsed time"""
        elapsed_time = self.start - datetime.datetime.now()
        print("\n" + '='*42)
        print("Processed {0} blobs in {1} s."
              .format(self.blob_index, elapsed_time.microseconds / 1e6))
<|endoftext|>