<commit_before># -*- coding: utf-8 -*-
# admin.py

from django.conf import settings  # needed if we use the GOOGLE_MAPS_API_KEY from settings

# Import the admin site reference from django.contrib.admin
from django.contrib import admin
from django.contrib.admin import SimpleListFilter
from import_export.admin import ImportExportModelAdmin
from django.utils.translation import ugettext_lazy as _
from adminsortable2.admin import SortableInlineAdminMixin, SortableAdminMixin
from django.contrib.auth.admin import UserAdmin, User
from django.contrib.gis.shortcuts import render_to_kml
from django.db.models import Q, Count
from constance.admin import config
import fgp
from django.core.urlresolvers import reverse

# Grab the Admin Manager that automaticall initializes an OpenLayers map
# for any geometry field using the in Google Mercator projection with OpenStreetMap basedata
from django.contrib.gis.admin import OSMGeoAdmin
from django.contrib.gis.geos import Point

# Note, another simplier manager that does not reproject the data on OpenStreetMap is available
# with from `django.contrib.gis.admin import GeoModelAdmin`

# Finally, import our model from the working project
# the geographic_admin folder must be on your python path
# for this import to work correctly
from models import Poi, Photo, PhotoAdminForm, Marker, Property, LegendAdminForm, Legend, Sector, Status, License, BaseLayer, OverlayLayer, MapPreset

USE_GOOGLE_TERRAIN_TILES = False

class UserAdmin(UserAdmin):
    list_display = ('__unicode__', 'email', 'first_name', 'last_name', 'is_staff', 'is_superuser', 'is_active', 'last_login', 'get_groups', 'get_user_permissions')

    def get_groups(self, obj):
        if obj:
            return ", ".join([group.name for group in obj.groups.all()])

    def get_user_permissions(self, obj):
        if obj:
            return ", ".join([user_permission.name for user_permission in obj.user_permissions.all()])

class SectorFilter(SimpleListFilter):
    title = _(u"sector")
    parameter_name = u"sector"

    def lookups(self, request, model_admin):
        self.model_admin = model_admin
        return [("outer", _(u"Out of sectors"))] + [(sector.slug, sector.name) for sector in Sector.objects.all()]

    def queryset(self, request, queryset):
        prefix = ""
        if isinstance(self.model_admin, PhotoAdmin):
            prefix = "poi__"

        if not self.value():
            return queryset
        if self.value() == "outer":
            for sector in Sector.objects.all():
                qfilter = {}
                qfilter[prefix + 'geom__intersects'] = sector.geom
                queryset = queryset.exclude(**qfilter)
            return queryset
        qfilter = {}
        qfilter[prefix + 'geom__intersects'] = Sector.objects.get(slug=self.value()).geom
        return queryset.filter(**qfilter)

class PoiStatusFilter(SimpleListFilter):
    title = _(u"all statuses")
    parameter_name = u"statuses"

    def lookups(self, request, model_admin):
        return ((None, _(u"Visible")),
                ('all', _('All')),
                ("unvisible", _(u"Unvisible")))

    def choices(self, cl):
        for lookup, title in self.lookup_choices:
            yield {
                'selected': self.value() == lookup,
                'query_string': cl.get_query_string({
                    self.parameter_name: lookup,
                }, []),
                'display': title,
            }

    def queryset(self, request, queryset):
        if self.value() == 'all':
            return queryset
        if not self.value() or self.value() == "visible":
            return queryset.filter(Q(status__show_to_mapper=True) & Q(marker__status__show_to_mapper=True) & Q(marker__layer__status__show_to_mapper=True))
        if self.value() == "unvisible":
            return queryset.exclude(Q(status__show_to_mapper=True) & Q(marker__status__show_to_mapper=True) & Q(marker__layer__status__show_to_mapper=True))

class PhotoInline(SortableInlineAdminMixin, admin.TabularInline):
    model = Photo
    form = PhotoAdminForm
    extra = 0
    readonly_fields = ('author', 'updated_by', 'created_at', 'last_modification')

def export_kml(modeladmin, request, queryset):
    points = queryset.kml()
    return render_to_kml("webmap/gis/kml/layer.kml", {'places': points})

@fgp.enforce
class PoiAdmin(OSMGeoAdmin, ImportExportModelAdmin):
    model = Poi
    list_display = ['__unicode__', 'status', 'marker', 'properties_list', 'last_modification', 'address', 'url', 'desc', 'id', 'photo__count']
    list_filter = (PoiStatusFilter, 'status', SectorFilter, 'marker__layer', 'marker', 'properties')
    exclude = ('properties_cache', )
    readonly_fields = ("created_at", "last_modification", "author", "updated_by")
    raw_id_fields = ('marker',)
    search_fields = ('name',)
    ordering = ('name',)
    save_as = True
    search_fields = ['name']
    list_select_related = True
    filter_horizontal = ('properties',)
    inlines = [PhotoInline]
    list_max_show_all = 10000
    actions = [export_kml, ]
    openlayers_url = 'django_webmap_corpus/js/OpenLayers.js'

    if USE_GOOGLE_TERRAIN_TILES:
        map_template = 'gis/admin/google.html'
        extra_js = ['http://openstreetmap.org/openlayers/OpenStreetMap.js', 'http://maps.google.com/maps?file=api&v=2&key=%s' % settings.GOOGLE_MAPS_API_KEY]
    else:
        pass  # defaults to OSMGeoAdmin presets of OpenStreetMap tiles

    # Default GeoDjango OpenLayers map options
    # Uncomment and modify as desired
    # To learn more about this jargon visit:
    # www.openlayers.org

    def get_form(self, request, obj=None, **kwargs):
        pnt = Point(config.MAP_BASELON, config.MAP_BASELAT, srid=4326)
        pnt.transform(3857)
        self.default_lon, self.default_lat = pnt.coords

        if not request.user.is_superuser and request.user.has_perm(u'mapa.can_only_own_data_only') and obj and obj.author != request.user:
            self.fields = ('name', )
            self.readonly_fields = ('name', )
        else:
            self.fields = PoiAdmin.fields
            self.readonly_fields = PoiAdmin.readonly_fields
        return super(PoiAdmin, self).get_form(request, obj, **kwargs)

    def queryset(self, request):
        qs = super(PoiAdmin, self).queryset(request)
        qs = qs.annotate(Count('photos'))
        return qs

    def photo__count(self, obj):
        return obj.photos.count()
    photo__count.admin_order_field = 'photos__count'

    default_zoom = 12
    scrollable = True
    map_width = 700
    map_height = 500
    map_srid = 3857

class SectorAdmin(OSMGeoAdmin):
    list_display = ('name',)
    prepopulated_fields = {'slug': ('name',)}  # automatically make slug from name
    if USE_GOOGLE_TERRAIN_TILES:
        map_template = 'gis/admin/google.html'
        extra_js = ['http://openstreetmap.org/openlayers/OpenStreetMap.js', 'http://maps.google.com/maps?file=api&v=2&key=%s' % settings.GOOGLE_MAPS_API_KEY]
    else:
        pass  # defaults to OSMGeoAdmin presets of OpenStreetMap tiles

    def get_form(self, request, obj=None, **kwargs):
        pnt = Point(config.MAP_BASELON, config.MAP_BASELAT, srid=4326)
        pnt.transform(3857)
        self.default_lon, self.default_lat = pnt.coords
        return super(SectorAdmin, self).get_form(request, obj, **kwargs)

    default_zoom = 12
    scrollable = True
    map_width = 700
    map_height = 500
    map_srid = 3857

class MarkerInline(admin.TabularInline):
    model = Marker
    extra = 0

class OverlayLayerAdmin(SortableAdminMixin, ImportExportModelAdmin, admin.ModelAdmin):
    prepopulated_fields = {'slug': ('name',)}  # automatically make slug from name
    list_display = ['name', 'slug', 'status', 'order', 'enabled']
    inlines = [MarkerInline]

class MapaAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('name',)}  # automatically make slug from name

class PropertyAdmin(SortableAdminMixin, admin.ModelAdmin):
    list_display = ('name', 'as_filter', 'icon_tag', 'status', 'order')
    prepopulated_fields = {'slug': ('name',)}  # automatically make slug from name
    model = Property

class MarkerStatusFilter(SimpleListFilter):
    title = _(u"all statuses")
    parameter_name = u"statuses"

    def lookups(self, request, model_admin):
        return ((None, _(u"Visible")),
                ('all', _('All')),
                ("unvisible", _(u"Unvisible")))

    def choices(self, cl):
        for lookup, title in self.lookup_choices:
            yield {
                'selected': self.value() == lookup,
                'query_string': cl.get_query_string({
                    self.parameter_name: lookup,
                }, []),
                'display': title,
            }

    def queryset(self, request, queryset):
        if self.value() == 'all':
            return queryset
        if not self.value() or self.value() == "visible":
            return queryset.filter(Q(status__show_to_mapper=True) & Q(layer__status__show_to_mapper=True))
        if self.value() == "unvisible":
            return queryset.exclude(Q(status__show_to_mapper=True) & Q(layer__status__show_to_mapper=True))

class MarkerAdmin(SortableAdminMixin, admin.ModelAdmin):
    list_display = ('name', 'desc', 'layer', 'minzoom', 'status', 'default_icon_image', 'id', 'poi_count', 'created_at', 'last_modification')
    list_filter = (MarkerStatusFilter, 'layer', 'status',)
    search_fields = ('name', 'desc',)
    readonly_fields = ('poi_count', 'created_at', 'last_modification', )
    prepopulated_fields = {'slug': ('name',)}

    def default_icon_image(self, obj):
        if obj.default_icon:
            return '' % obj.default_icon.url
    default_icon_image.short_description = _("icon")
    default_icon_image.allow_tags = True

    def has_change_permission(self, request, obj=None):
        if obj is None and request.user.has_perm(u'webmap.can_only_view'):
            return True
        return super(MarkerAdmin, self).has_change_permission(request, obj)

    def poi_count(self, obj):
        url = reverse('admin:webmap_poi_changelist')
        return '{2}'.format(url, obj.id, obj.pois.count())
    poi_count.short_description = _("count")
    poi_count.allow_tags = True

class StatusAdmin(admin.ModelAdmin):
    list_display = ('name', 'desc', 'show', 'show_to_mapper')

class LicenseAdmin(admin.ModelAdmin):
    list_display = ('name', 'desc')

class MapPresetAdmin(SortableAdminMixin, admin.ModelAdmin):
    list_display = ('name', 'base_layer', 'order')
    filter_horizontal = ('overlay_layers',)

class LegendAdmin(admin.ModelAdmin):
    form = LegendAdminForm
    list_display = ('name', 'image_tag', 'desc',)
    prepopulated_fields = {'slug': ('name',)}

class PhotoAdmin(ImportExportModelAdmin, admin. ModelAdmin):
    form = PhotoAdminForm
    list_display = ('__unicode__', 'poi', 'image_tag', 'author', 'photographer', 'created_at', 'last_modification', 'order', 'license', 'desc')
    readonly_fields = ('author', 'updated_by', 'created_at', 'last_modification')
    search_fields = ('name', 'poi__name', )
    raw_id_fields = ('poi', )
    list_filter = ('license', SectorFilter, 'author', 'poi__properties')
    list_per_page = 20

    def has_change_permission(self, request, obj=None):
        if request.user.has_perm(u'webmap.can_view_photo_list'):
            return super(PhotoAdmin, self).has_change_permission(request, obj)
        return False

    def has_add_permission(self, request):
        if request.user.has_perm(u'webmap.can_view_photo_list'):
            return super(PhotoAdmin, self).has_add_permission(request)
        return False

class BaseLayerAdmin(SortableAdminMixin, admin.ModelAdmin):
    list_display = ('name', 'slug', 'url', 'order')
    prepopulated_fields = {'slug': ('name',)}  # automatically make slug from name

admin.site.register(Poi, PoiAdmin)
admin.site.register(OverlayLayer, OverlayLayerAdmin)
admin.site.register(Sector, SectorAdmin)
admin.site.register(Marker, MarkerAdmin)
admin.site.register(Status, StatusAdmin)
admin.site.register(Property, PropertyAdmin)
admin.site.register(License, LicenseAdmin)
admin.site.register(Legend, LegendAdmin)
admin.site.register(Photo, PhotoAdmin)
admin.site.register(BaseLayer, BaseLayerAdmin)
admin.site.register(MapPreset, MapPresetAdmin)

admin.site.unregister(User)
admin.site.register(User, UserAdmin)
<commit_msg>fix typo<commit_after># -*- coding: utf-8 -*-
# admin.py

from django.conf import settings  # needed if we use the GOOGLE_MAPS_API_KEY from settings

# Import the admin site reference from django.contrib.admin
from django.contrib import admin
from django.contrib.admin import SimpleListFilter
from import_export.admin import ImportExportModelAdmin
from django.utils.translation import ugettext_lazy as _
from adminsortable2.admin import SortableInlineAdminMixin, SortableAdminMixin
from django.contrib.auth.admin import UserAdmin, User
from django.contrib.gis.shortcuts import render_to_kml
from django.db.models import Q, Count
from constance.admin import config
import fgp
from django.core.urlresolvers import reverse

# Grab the Admin Manager that automaticall initializes an OpenLayers map
# for any geometry field using the in Google Mercator projection with OpenStreetMap basedata
from django.contrib.gis.admin import OSMGeoAdmin
from django.contrib.gis.geos import Point

# Note, another simplier manager that does not reproject the data on OpenStreetMap is available
# with from `django.contrib.gis.admin import GeoModelAdmin`

# Finally, import our model from the working project
# the geographic_admin folder must be on your python path
# for this import to work correctly
from models import Poi, Photo, PhotoAdminForm, Marker, Property, LegendAdminForm, Legend, Sector, Status, License, BaseLayer, OverlayLayer, MapPreset

USE_GOOGLE_TERRAIN_TILES = False

class UserAdmin(UserAdmin):
    list_display = ('__unicode__', 'email', 'first_name', 'last_name', 'is_staff', 'is_superuser', 'is_active', 'last_login', 'get_groups', 'get_user_permissions')

    def get_groups(self, obj):
        if obj:
            return ", ".join([group.name for group in obj.groups.all()])

    def get_user_permissions(self, obj):
        if obj:
            return ", ".join([user_permission.name for user_permission in obj.user_permissions.all()])

class SectorFilter(SimpleListFilter):
    title = _(u"sector")
    parameter_name = u"sector"

    def lookups(self, request, model_admin):
        self.model_admin = model_admin
        return [("outer", _(u"Out of sectors"))] + [(sector.slug, sector.name) for sector in Sector.objects.all()]

    def queryset(self, request, queryset):
        prefix = ""
        if isinstance(self.model_admin, PhotoAdmin):
            prefix = "poi__"

        if not self.value():
            return queryset
        if self.value() == "outer":
            for sector in Sector.objects.all():
                qfilter = {}
                qfilter[prefix + 'geom__intersects'] = sector.geom
                queryset = queryset.exclude(**qfilter)
            return queryset
        qfilter = {}
        qfilter[prefix + 'geom__intersects'] = Sector.objects.get(slug=self.value()).geom
        return queryset.filter(**qfilter)

class PoiStatusFilter(SimpleListFilter):
    title = _(u"all statuses")
    parameter_name = u"statuses"

    def lookups(self, request, model_admin):
        return ((None, _(u"Visible")),
                ('all', _('All')),
                ("unvisible", _(u"Invisible")))

    def choices(self, cl):
        for lookup, title in self.lookup_choices:
            yield {
                'selected': self.value() == lookup,
                'query_string': cl.get_query_string({
                    self.parameter_name: lookup,
                }, []),
                'display': title,
            }

    def queryset(self, request, queryset):
        if self.value() == 'all':
            return queryset
        if not self.value() or self.value() == "visible":
            return queryset.filter(Q(status__show_to_mapper=True) & Q(marker__status__show_to_mapper=True) & Q(marker__layer__status__show_to_mapper=True))
        if self.value() == "unvisible":
            return queryset.exclude(Q(status__show_to_mapper=True) & Q(marker__status__show_to_mapper=True) & Q(marker__layer__status__show_to_mapper=True))

class PhotoInline(SortableInlineAdminMixin, admin.TabularInline):
    model = Photo
    form = PhotoAdminForm
    extra = 0
    readonly_fields = ('author', 'updated_by', 'created_at', 'last_modification')

def export_kml(modeladmin, request, queryset):
    points = queryset.kml()
    return render_to_kml("webmap/gis/kml/layer.kml", {'places': points})

@fgp.enforce
class PoiAdmin(OSMGeoAdmin, ImportExportModelAdmin):
    model = Poi
    list_display = ['__unicode__', 'status', 'marker', 'properties_list', 'last_modification', 'address', 'url', 'desc', 'id', 'photo__count']
    list_filter = (PoiStatusFilter, 'status', SectorFilter, 'marker__layer', 'marker', 'properties')
    exclude = ('properties_cache', )
    readonly_fields = ("created_at", "last_modification", "author", "updated_by")
    raw_id_fields = ('marker',)
    search_fields = ('name',)
    ordering = ('name',)
    save_as = True
    search_fields = ['name']
    list_select_related = True
    filter_horizontal = ('properties',)
    inlines = [PhotoInline]
    list_max_show_all = 10000
    actions = [export_kml, ]
    openlayers_url = 'django_webmap_corpus/js/OpenLayers.js'

    if USE_GOOGLE_TERRAIN_TILES:
        map_template = 'gis/admin/google.html'
        extra_js = ['http://openstreetmap.org/openlayers/OpenStreetMap.js', 'http://maps.google.com/maps?file=api&v=2&key=%s' % settings.GOOGLE_MAPS_API_KEY]
    else:
        pass  # defaults to OSMGeoAdmin presets of OpenStreetMap tiles

    # Default GeoDjango OpenLayers map options
    # Uncomment and modify as desired
    # To learn more about this jargon visit:
    # www.openlayers.org

    def get_form(self, request, obj=None, **kwargs):
        pnt = Point(config.MAP_BASELON, config.MAP_BASELAT, srid=4326)
        pnt.transform(3857)
        self.default_lon, self.default_lat = pnt.coords

        if not request.user.is_superuser and request.user.has_perm(u'mapa.can_only_own_data_only') and obj and obj.author != request.user:
            self.fields = ('name', )
            self.readonly_fields = ('name', )
        else:
            self.fields = PoiAdmin.fields
            self.readonly_fields = PoiAdmin.readonly_fields
        return super(PoiAdmin, self).get_form(request, obj, **kwargs)

    def queryset(self, request):
        qs = super(PoiAdmin, self).queryset(request)
        qs = qs.annotate(Count('photos'))
        return qs

    def photo__count(self, obj):
        return obj.photos.count()
    photo__count.admin_order_field = 'photos__count'

    default_zoom = 12
    scrollable = True
    map_width = 700
    map_height = 500
    map_srid = 3857

class SectorAdmin(OSMGeoAdmin):
    list_display = ('name',)
    prepopulated_fields = {'slug': ('name',)}  # automatically make slug from name
    if USE_GOOGLE_TERRAIN_TILES:
        map_template = 'gis/admin/google.html'
        extra_js = ['http://openstreetmap.org/openlayers/OpenStreetMap.js', 'http://maps.google.com/maps?file=api&v=2&key=%s' % settings.GOOGLE_MAPS_API_KEY]
    else:
        pass  # defaults to OSMGeoAdmin presets of OpenStreetMap tiles

    def get_form(self, request, obj=None, **kwargs):
        pnt = Point(config.MAP_BASELON, config.MAP_BASELAT, srid=4326)
        pnt.transform(3857)
        self.default_lon, self.default_lat = pnt.coords
        return super(SectorAdmin, self).get_form(request, obj, **kwargs)

    default_zoom = 12
    scrollable = True
    map_width = 700
    map_height = 500
    map_srid = 3857

class MarkerInline(admin.TabularInline):
    model = Marker
    extra = 0

class OverlayLayerAdmin(SortableAdminMixin, ImportExportModelAdmin, admin.ModelAdmin):
    prepopulated_fields = {'slug': ('name',)}  # automatically make slug from name
    list_display = ['name', 'slug', 'status', 'order', 'enabled']
    inlines = [MarkerInline]

class MapaAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('name',)}  # automatically make slug from name

class PropertyAdmin(SortableAdminMixin, admin.ModelAdmin):
    list_display = ('name', 'as_filter', 'icon_tag', 'status', 'order')
    prepopulated_fields = {'slug': ('name',)}  # automatically make slug from name
    model = Property

class MarkerStatusFilter(SimpleListFilter):
    title = _(u"all statuses")
    parameter_name = u"statuses"

    def lookups(self, request, model_admin):
        return ((None, _(u"Visible")),
                ('all', _('All')),
                ("unvisible", _(u"Invisible")))

    def choices(self, cl):
        for lookup, title in self.lookup_choices:
            yield {
                'selected': self.value() == lookup,
                'query_string': cl.get_query_string({
                    self.parameter_name: lookup,
                }, []),
                'display': title,
            }

    def queryset(self, request, queryset):
        if self.value() == 'all':
            return queryset
        if not self.value() or self.value() == "visible":
            return queryset.filter(Q(status__show_to_mapper=True) & Q(layer__status__show_to_mapper=True))
        if self.value() == "unvisible":
            return queryset.exclude(Q(status__show_to_mapper=True) & Q(layer__status__show_to_mapper=True))

class MarkerAdmin(SortableAdminMixin, admin.ModelAdmin):
    list_display = ('name', 'desc', 'layer', 'minzoom', 'status', 'default_icon_image', 'id', 'poi_count', 'created_at', 'last_modification')
    list_filter = (MarkerStatusFilter, 'layer', 'status',)
    search_fields = ('name', 'desc',)
    readonly_fields = ('poi_count', 'created_at', 'last_modification', )
    prepopulated_fields = {'slug': ('name',)}

    def default_icon_image(self, obj):
        if obj.default_icon:
            return '' % obj.default_icon.url
    default_icon_image.short_description = _("icon")
    default_icon_image.allow_tags = True

    def has_change_permission(self, request, obj=None):
        if obj is None and request.user.has_perm(u'webmap.can_only_view'):
            return True
        return super(MarkerAdmin, self).has_change_permission(request, obj)

    def poi_count(self, obj):
        url = reverse('admin:webmap_poi_changelist')
        return '{2}'.format(url, obj.id, obj.pois.count())
    poi_count.short_description = _("count")
    poi_count.allow_tags = True

class StatusAdmin(admin.ModelAdmin):
    list_display = ('name', 'desc', 'show', 'show_to_mapper')

class LicenseAdmin(admin.ModelAdmin):
    list_display = ('name', 'desc')

class MapPresetAdmin(SortableAdminMixin, admin.ModelAdmin):
    list_display = ('name', 'base_layer', 'order')
    filter_horizontal = ('overlay_layers',)

class LegendAdmin(admin.ModelAdmin):
    form = LegendAdminForm
    list_display = ('name', 'image_tag', 'desc',)
    prepopulated_fields = {'slug': ('name',)}

class PhotoAdmin(ImportExportModelAdmin, admin. ModelAdmin):
    form = PhotoAdminForm
    list_display = ('__unicode__', 'poi', 'image_tag', 'author', 'photographer', 'created_at', 'last_modification', 'order', 'license', 'desc')
    readonly_fields = ('author', 'updated_by', 'created_at', 'last_modification')
    search_fields = ('name', 'poi__name', )
    raw_id_fields = ('poi', )
    list_filter = ('license', SectorFilter, 'author', 'poi__properties')
    list_per_page = 20

    def has_change_permission(self, request, obj=None):
        if request.user.has_perm(u'webmap.can_view_photo_list'):
            return super(PhotoAdmin, self).has_change_permission(request, obj)
        return False

    def has_add_permission(self, request):
        if request.user.has_perm(u'webmap.can_view_photo_list'):
            return super(PhotoAdmin, self).has_add_permission(request)
        return False

class BaseLayerAdmin(SortableAdminMixin, admin.ModelAdmin):
    list_display = ('name', 'slug', 'url', 'order')
    prepopulated_fields = {'slug': ('name',)}  # automatically make slug from name

admin.site.register(Poi, PoiAdmin)
admin.site.register(OverlayLayer, OverlayLayerAdmin)
admin.site.register(Sector, SectorAdmin)
admin.site.register(Marker, MarkerAdmin)
admin.site.register(Status, StatusAdmin)
admin.site.register(Property, PropertyAdmin)
admin.site.register(License, LicenseAdmin)
admin.site.register(Legend, LegendAdmin)
admin.site.register(Photo, PhotoAdmin)
admin.site.register(BaseLayer, BaseLayerAdmin)
admin.site.register(MapPreset, MapPresetAdmin)

admin.site.unregister(User)
admin.site.register(User, UserAdmin)
<|endoftext|>


<commit_before># Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from extension_helpers import ExtensionHelper

_log = logging.getLogger('varnish')

DEFAULTS = {
    'VARNISH_HOST': 'raw.githubusercontent.com',
    'VARNISH_VERSION': '3.0.6',
    'VARNISH_PACKAGE': 'varnish-{VARNISH_VERSION}.tar.gz',
    'VARNISH_DOWNLOAD_URL': '/varnish/{VARNISH_PACKAGE}',
}

class VarnishExtension(ExtensionHelper):

    def __init__(self, ctx):
        self._log = _log
        self._ctx = ctx
        for key, val in DEFAULTS.iteritems():
            if key not in self._ctx:
                self._ctx[key] = val

    def _should_compile(self):
        return self._ctx['CACHE_SERVER'] == 'varnish'

    def _preprocess_commands(self):
        return ((
            '$HOME/.bp/bin/rewrite',
            '"$HOME/varnish/etc/varnish"'),)

    def _service_commands(self):
        return {
            'varnish': (
                '$HOME/varnish/sbin/varnishd',
                '-n $TMPDIR/varnish/',
                '-F',
                '-f $HOME/varnish/etc/varnish/default.vcl',
                '-a 0.0.0.0:$VCAP_APP_PORT',
                '-t 120',
                '-w 50,1000,120',
                '-s malloc,128m',
                '-T 127.0.0.1:6082',
                '-p http_resp_hdr_len=32768'
                )
        }

    def _service_environment(self):
        return {}

    def _compile(self, install):
        print 'Installing varnish'
        (install
            .package('VARNISH')
            .config()
                .from_application('.bp-config/varnish')  # noqa
                .or_from_build_pack('defaults/config/varnish/{VARNISH_VERSION}')
                .to('varnish/etc/varnish')
                .rewrite()
                .done())
        return 0

VarnishExtension.register(__name__)
<commit_msg>install...<commit_after># Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from extension_helpers import ExtensionHelper

_log = logging.getLogger('varnish')

DEFAULTS = {
    'VARNISH_HOST': 'raw.githubusercontent.com',
    'VARNISH_VERSION': '3.0.6',
    'VARNISH_PACKAGE': 'varnish-{VARNISH_VERSION}.tar.gz',
    'VARNISH_DOWNLOAD_URL': '/varnish/{VARNISH_PACKAGE}',
}

class VarnishExtension(ExtensionHelper):

    def __init__(self, ctx):
        self._log = _log
        self._ctx = ctx
        for key, val in DEFAULTS.iteritems():
            if key not in self._ctx:
                self._ctx[key] = val

    def _should_compile(self):
        return self._ctx['CACHE_SERVER'] == 'varnish'

    def _preprocess_commands(self):
        return ((
            '$HOME/.bp/bin/rewrite',
            '"$HOME/varnish/etc/varnish"'),)

    def _service_commands(self):
        return {
            'varnish': (
                '$HOME/varnish/sbin/varnishd',
                '-n $TMPDIR/varnish/',
                '-F',
                '-f $HOME/varnish/etc/varnish/default.vcl',
                '-a 0.0.0.0:$VCAP_APP_PORT',
                '-t 120',
                '-w 50,1000,120',
                '-s malloc,128m',
                '-T 127.0.0.1:6082',
                '-p http_resp_hdr_len=32768'
                )
        }

    def _service_environment(self):
        return {}

    def _compile(self, install):
        print 'Installing varnish'
        (install
            .package('VARNISH')
            .config()
                .from_application('.bp-config/varnish')  # noqa
                .or_from_build_pack('defaults/config/varnish/{VARNISH_VERSION}')
                .to('varnish/etc/varnish')
                .rewrite()
                .done())
        return 0

    def install(self):
        self._builder.install()._installer._install_binary_from_manifest(
            self._ctx['VARNISH_DOWNLOAD_URL'],
            os.path.join(self._ctx['BUILD_DIR']),
            extract=True)

VarnishExtension.register(__name__)
<|endoftext|>


<commit_before>import numpy as np
from scipy.constants import mu_0, pi
from scipy import special

def DCAnalyticHalf(txloc, rxlocs, sigma, earth_type="wholespace"):
    """
        Analytic solution for electric potential from a postive pole

        Input variables:

            txloc =  a xyz location of A (+) electrode (np.r_[xa, ya, za])

            rxlocs = [M, N]
                M: xyz locations of M (+) electrode (np.c_[xmlocs, ymlocs, zmlocs])
                N: xyz locations of N (-) electrode (np.c_[xnlocs, ynlocs, znlocs])

            sigma = conductivity (either float or complex)
            earth_type = "wholsespace" or "halfspace"

    """
    M = rxlocs[0]
    N = rxlocs[1]

    rM = np.sqrt( (M[:,0]-txloc[0])**2 + (M[:,1]-txloc[1])**2 + (M[:,2]-txloc[1])**2 )
    rN = np.sqrt( (N[:,0]-txloc[0])**2 + (N[:,1]-txloc[1])**2 + (N[:,2]-txloc[1])**2 )

    phiM = 1./(4*np.pi*rM*sigma)
    phiN = 1./(4*np.pi*rN*sigma)
    phi = phiM - phiN

    if earth_type == "halfspace":
        phi *= 2

    return phi

deg2rad  = lambda deg: deg/180.*np.pi
rad2deg  = lambda rad: rad*180./np.pi

def DCAnalyticSphere(txloc, rxloc, xc, radius, sigma, sigma1, \
                 field_type = "secondary", order=12, halfspace=False):
# def DCSpherePointCurrent(txloc, rxloc, xc, radius, rho, rho1, \
#                  field_type = "secondary", order=12):
    """

        Parameters:

            txloc (array) : current electrode location (x,y,z)
            xc (float)    : x center of depressed sphere
            rxloc (array) : electrode locations
                              (Nx3 array, # of electrodes)
            radius (float): radius of the sphere (m)
            rho (float)   : resistivity of the background (ohm-m)
            rho1 (float)  : resistivity of the sphere
            field_type (string) : "secondary", "total", "primary"
                              (default="secondary")
                              "secondary": secondary potential only due to sphere
                              "primary": primary potential from the point source
                              "total": "secondary"+"primary"
            order (float) : maximum order of Legendre polynomial
                              (default=12)

        Written by Seogi Kang (skang@eos.ubc.ca)
        Ph.D. Candidate of University of British Columbia, Canada

    """

    Pleg = []
    # Compute Legendre Polynomial
    for i in range(order):
        Pleg.append(special.legendre(i, monic=0))

    rho = 1./sigma
    rho1 = 1./sigma1

    # Center of the sphere should be aligned in txloc in y-direction
    yc = txloc[1]
    xyz = np.c_[rxloc[:,0]-xc, rxloc[:,1]-yc, rxloc[:,2]]
    r = np.sqrt( (xyz**2).sum(axis=1) )

    x0 = abs(txloc[0]-xc)

    costheta = xyz[:,0]/r * (txloc[0]-xc)/x0
    phi = np.zeros_like(r)
    R = (r**2+x0**2.-2.*r*x0*costheta)**0.5
    # primary potential in a whole space
    prim = rho*1./(4*np.pi*R)

    if field_type =="primary":
        return prim

    sphind = r < radius
    out = np.zeros_like(r)
    for n in range(order):
        An, Bn = AnBnfun(n, radius, x0, rho, rho1)
        dumout = An*r[~sphind]**(-n-1.)*Pleg[n](costheta[~sphind])
        out[~sphind] += dumout
        dumin = Bn*r[sphind]**(n)*Pleg[n](costheta[sphind])
        out[sphind] += dumin

    out[~sphind] += prim[~sphind]

    if halfspace:
        scale = 2
    else:
        scale = 1

    if field_type == "secondary":
        return scale*(out-prim)
    elif field_type == "total":
        return scale*out

def AnBnfun(n, radius, x0, rho, rho1, I=1.):
    const = I*rho/(4*np.pi)
    bunmo = n*rho + (n+1)*rho1
    An = const * radius**(2*n+1) / x0 ** (n+1.) * n * \
        (rho1-rho) / bunmo
    Bn = const * 1. / x0 ** (n+1.) * (2*n+1) * (rho1) / bunmo
    return An, Bn
<commit_msg>Incorporate Lindsey's comments on documenting codes<commit_after>import numpy as np
from scipy.constants import mu_0, pi
from scipy import special

def DCAnalyticHalf(txloc, rxlocs, sigma, earth_type="wholespace"):
    """
        Analytic solution for electric potential from a postive pole

        :param array txloc: a xyz location of A (+) electrode (np.r_[xa, ya, za])
        :param list rxlocs: xyz locations of M (+) and N (-) electrodes [M, N]

            e.g.
            rxlocs = [M, N]
            M: xyz locations of M (+) electrode (np.c_[xmlocs, ymlocs, zmlocs])
            N: xyz locations of N (-) electrode (np.c_[xnlocs, ynlocs, znlocs])

        :param float or complex sigma: values of conductivity
        :param string earth_type: values of conductivity ("wholsespace" or "halfspace")

    """
    M = rxlocs[0]
    N = rxlocs[1]

    rM = np.sqrt( (M[:,0]-txloc[0])**2 + (M[:,1]-txloc[1])**2 + (M[:,2]-txloc[1])**2 )
    rN = np.sqrt( (N[:,0]-txloc[0])**2 + (N[:,1]-txloc[1])**2 + (N[:,2]-txloc[1])**2 )

    phiM = 1./(4*np.pi*rM*sigma)
    phiN = 1./(4*np.pi*rN*sigma)
    phi = phiM - phiN

    if earth_type == "halfspace":
        phi *= 2

    return phi

deg2rad  = lambda deg: deg/180.*np.pi
rad2deg  = lambda rad: rad*180./np.pi

def DCAnalyticSphere(txloc, rxloc, xc, radius, sigma, sigma1, \
                 field_type = "secondary", order=12, halfspace=False):
# def DCSpherePointCurrent(txloc, rxloc, xc, radius, rho, rho1, \
#                  field_type = "secondary", order=12):
    """

        Parameters:

            :param array txloc: A (+) current electrode location (x,y,z)
            :param array xc: x center of depressed sphere
            :param array rxloc: M(+) electrode locations / (Nx3 array, # of electrodes)

            :param float radius: radius (float): radius of the sphere (m)
            :param float rho: resistivity of the background (ohm-m)
            :param float rho1: resistivity of the sphere
            :param string field_type: : "secondary", "total", "primary"
                  (default="secondary")
                  "secondary": secondary potential only due to sphere
                  "primary": primary potential from the point source
                  "total": "secondary"+"primary"
            :param float order: maximum order of Legendre polynomial (default=12)

        Written by Seogi Kang (skang@eos.ubc.ca)
        Ph.D. Candidate of University of British Columbia, Canada

    """

    Pleg = []
    # Compute Legendre Polynomial
    for i in range(order):
        Pleg.append(special.legendre(i, monic=0))

    rho = 1./sigma
    rho1 = 1./sigma1

    # Center of the sphere should be aligned in txloc in y-direction
    yc = txloc[1]
    xyz = np.c_[rxloc[:,0]-xc, rxloc[:,1]-yc, rxloc[:,2]]
    r = np.sqrt( (xyz**2).sum(axis=1) )

    x0 = abs(txloc[0]-xc)

    costheta = xyz[:,0]/r * (txloc[0]-xc)/x0
    phi = np.zeros_like(r)
    R = (r**2+x0**2.-2.*r*x0*costheta)**0.5
    # primary potential in a whole space
    prim = rho*1./(4*np.pi*R)

    if field_type =="primary":
        return prim

    sphind = r < radius
    out = np.zeros_like(r)
    for n in range(order):
        An, Bn = AnBnfun(n, radius, x0, rho, rho1)
        dumout = An*r[~sphind]**(-n-1.)*Pleg[n](costheta[~sphind])
        out[~sphind] += dumout
        dumin = Bn*r[sphind]**(n)*Pleg[n](costheta[sphind])
        out[sphind] += dumin

    out[~sphind] += prim[~sphind]

    if halfspace:
        scale = 2
    else:
        scale = 1

    if field_type == "secondary":
        return scale*(out-prim)
    elif field_type == "total":
        return scale*out

def AnBnfun(n, radius, x0, rho, rho1, I=1.):
    const = I*rho/(4*np.pi)
    bunmo = n*rho + (n+1)*rho1
    An = const * radius**(2*n+1) / x0 ** (n+1.) * n * \
        (rho1-rho) / bunmo
    Bn = const * 1. / x0 ** (n+1.) * (2*n+1) * (rho1) / bunmo
    return An, Bn
<|endoftext|>


<commit_before><commit_msg>Ende der ersten Übung<commit_after>from random import randint

def guess():
	x = randint(0,100)
	n = int(raw_input("Rate die Zahl zwischen 0 und 100: "))
	y = 0
	while x != n:
		y = y+1
		if n < x:
			print 'Die gesuchte Zahl ist groesser.'
			n = int(raw_input("Please enter an integer: "))
		elif n > x:
			print 'Die gesuchte Zahl ist kleiner.'
			n = int(raw_input("Please enter an integer: "))
	else:
		print('Treffer, das war die Zahl. \nDu hast {} Versuche gebraucht' .format(y))

guess()
<|endoftext|>


<commit_before>import os
import sys
import textwrap

import requests

__version__ = '1.0'

class AkismetError(Exception):
    """
    Base exception class for Akismet errors.

    """
    pass

class ProtocolError(AkismetError):
    """
    Indicates an unexpected or non-standard response was received from
    Akismet.

    """
    pass

class ConfigurationError(AkismetError):

    """
    Indicates an Akismet configuration error (config missing or invalid).

    """
    pass

class APIKeyError(ConfigurationError):
    """
    Indicates the supplied Akismet API key/URL are invalid.

    """
    pass

class Akismet:
    """
    A Python implementation of the Akismet web API.

    Two configuration parameters -- your Akismet API key and
    registered URL -- are required; they can be passed when
    instantiating, or set in the environment variables
    PYTHON_AKISMET_API_KEY and PYTHON_AKISMET_BLOG_URL.

    All the operations of the Akismet API are exposed here:

    * verify_key

    * comment_check

    * submit_spam

    * submit_ham

    For full details of the Akismet API, see the Akismet documentation:

    https://akismet.com/development/api/#detailed-docs

    The verify_key operation will be automatically called for you as
    this class is instantiated; ConfigurationError will be raised if
    the configuration cannot be found or if the supplied key/URL are
    invalid.

    """
    COMMENT_CHECK_URL = 'https://{}.rest.akismet.com/1.1/comment-check'
    SUBMIT_HAM_URL = 'https://{}.rest.akismet.com/1.1/submit-ham'
    SUBMIT_SPAM_URL = 'https://{}.rest.akismet.com/1.1/submit-spam'
    VERIFY_KEY_URL = 'https://rest.akismet.com/1.1/verify-key'

    SUBMIT_SUCCESS_RESPONSE = 'Thanks for making the web a better place.'

    OPTIONAL_KEYS = (
        'referrer', 'permalink', 'comment_type', 'comment_author',
        'comment_author_email', 'comment_author_url', 'comment_content',
        'comment_date_gmt', 'comment_post_modified_gmt', 'blog_lang',
        'blog_charset', 'user_role', 'is_test'
    )

    user_agent_header = {
        'User-Agent': 'Python/{} | akismet.py/{}'.format(
            '{}.{}'.format(*sys.version_info[:2]),
            __version__
        )
    }

    def __init__(self, key=None, blog_url=None):
        maybe_key = (key if key is not None
                     else os.getenv('PYTHON_AKISMET_API_KEY'))
        maybe_url = (blog_url if blog_url is not None
                     else os.getenv('PYTHON_AKISMET_BLOG_URL'))
        if maybe_key in (None, '') or maybe_url in (None, ''):
            raise ConfigurationError(textwrap.dedent('''
                Could not find full Akismet configuration.

                Found API key: {}
                Found blog URL: {}
            '''.format(maybe_key, maybe_url)))
        if not self.verify_key(maybe_key, maybe_url):
            raise APIKeyError(
                'Akismet configuration ({}, {}) is invalid.'.format(
                    key, blog_url
                )
            )
        self.api_key = maybe_key
        self.blog_url = maybe_url

    def _api_request(self, endpoint, user_ip, user_agent, **kwargs):
        """
        Make a request to the Akismet API.

        This method is used for all API calls except key verification,
        since all endpoints other than key verification must
        interpolate the API key into the URL and supply certain basic
        data.

        """
        data = {'blog': self.blog_url,
                'user_ip': user_ip,
                'user_agent': user_agent}
        for key in self.OPTIONAL_KEYS:
            if key in kwargs:
                data[key] = kwargs[key]
        response = requests.post(
            endpoint.format(self.api_key),
            data=data,
            headers=self.user_agent_header)
        return response.text

    def _submit_call(self, operation, user_ip, user_agent, **kwargs):
        """
        Submit spam or ham to the Akismet API.

        """
        endpoint = {'submit_spam': self.SUBMIT_SPAM_URL,
                    'submit_ham': self.SUBMIT_HAM_URL}[operation]
        response_text = self._api_request(
            endpoint, user_ip, user_agent, **kwargs
        )
        if response_text == self.SUBMIT_SUCCESS_RESPONSE:
            return True
        else:
            self._protocol_error(operation, response_text)

    @classmethod
    def _protocol_error(cls, operation, message):
        """
        Raise an appropriate exception for unexpected API responses.

        """
        raise ProtocolError(textwrap.dedent('''
        Received unexpected or non-standard response from Akismet API.

        API operation was: {}
        API response received was: {}
        ''').format(operation, message))

    @classmethod
    def verify_key(cls, key, blog_url):
        """
        Verify an Akismet API key and URL.

        Returns True if the key and URL are valid, False otherwise.

        """
        response = requests.post(
            cls.VERIFY_KEY_URL,
            data={'key': key, 'blog': blog_url},
            headers=cls.user_agent_header
        )
        if response.text == 'valid':
            return True
        elif response.text == 'invalid':
            return False
        else:
            cls._protocol_error('verify_key', response.text)

    def comment_check(self, user_ip, user_agent, **kwargs):
        """
        Check a comment to determine whether it is spam.

        The IP address and user-agent string of the remote user are
        required. All other arguments documented by Akismet (other
        than the PHP server information) are also optionally accepted;
        see the Akismet API documentation for a full list:

        https://akismet.com/development/api/#comment-check

        Like the Akismet web API, returns True for a comment that is
        spam, and False for a comment that is not spam.

        """
        response_text = self._api_request(
            self.COMMENT_CHECK_URL, user_ip, user_agent, **kwargs
        )
        if response_text == 'true':
            return True
        elif response_text == 'false':
            return False
        else:
            self._protocol_error('comment_check', response_text)

    def submit_spam(self, user_ip, user_agent, **kwargs):
        """
        Inform Akismet that a comment is spam.

        The IP address and user-agent string of the remote user are
        required. All other arguments documented by Akismet (other
        than the PHP server information) are also optionally accepted;
        see the Akismet API documentation for a full list:

        https://akismet.com/development/api/#submit-spam

        Returns True on success (the only expected response).

        """
        return self._submit_call('submit_spam', user_ip, user_agent, **kwargs)

    def submit_ham(self, user_ip, user_agent, **kwargs):
        """
        Inform Akismet that a comment is not spam.

        The IP address and user-agent string of the remote user are
        required. All other arguments documented by Akismet (other
        than the PHP server information) are also optionally accepted;
        see the Akismet API documentation for a full list:

        https://akismet.com/development/api/#submit-ham

        Returns True on success (the only expected response).

        """
        return self._submit_call('submit_ham', user_ip, user_agent, **kwargs)
<commit_msg>Missed a variable rename.<commit_after>import os
import sys
import textwrap

import requests

__version__ = '1.0'

class AkismetError(Exception):
    """
    Base exception class for Akismet errors.

    """
    pass

class ProtocolError(AkismetError):
    """
    Indicates an unexpected or non-standard response was received from
    Akismet.

    """
    pass

class ConfigurationError(AkismetError):

    """
    Indicates an Akismet configuration error (config missing or invalid).

    """
    pass

class APIKeyError(ConfigurationError):
    """
    Indicates the supplied Akismet API key/URL are invalid.

    """
    pass

class Akismet:
    """
    A Python implementation of the Akismet web API.

    Two configuration parameters -- your Akismet API key and
    registered URL -- are required; they can be passed when
    instantiating, or set in the environment variables
    PYTHON_AKISMET_API_KEY and PYTHON_AKISMET_BLOG_URL.

    All the operations of the Akismet API are exposed here:

    * verify_key

    * comment_check

    * submit_spam

    * submit_ham

    For full details of the Akismet API, see the Akismet documentation:

    https://akismet.com/development/api/#detailed-docs

    The verify_key operation will be automatically called for you as
    this class is instantiated; ConfigurationError will be raised if
    the configuration cannot be found or if the supplied key/URL are
    invalid.

    """
    COMMENT_CHECK_URL = 'https://{}.rest.akismet.com/1.1/comment-check'
    SUBMIT_HAM_URL = 'https://{}.rest.akismet.com/1.1/submit-ham'
    SUBMIT_SPAM_URL = 'https://{}.rest.akismet.com/1.1/submit-spam'
    VERIFY_KEY_URL = 'https://rest.akismet.com/1.1/verify-key'

    SUBMIT_SUCCESS_RESPONSE = 'Thanks for making the web a better place.'

    OPTIONAL_KEYS = (
        'referrer', 'permalink', 'comment_type', 'comment_author',
        'comment_author_email', 'comment_author_url', 'comment_content',
        'comment_date_gmt', 'comment_post_modified_gmt', 'blog_lang',
        'blog_charset', 'user_role', 'is_test'
    )

    user_agent_header = {
        'User-Agent': 'Python/{} | akismet.py/{}'.format(
            '{}.{}'.format(*sys.version_info[:2]),
            __version__
        )
    }

    def __init__(self, key=None, blog_url=None):
        maybe_key = (key if key is not None
                     else os.getenv('PYTHON_AKISMET_API_KEY'))
        maybe_url = (blog_url if blog_url is not None
                     else os.getenv('PYTHON_AKISMET_BLOG_URL'))
        if maybe_key in (None, '') or maybe_url in (None, ''):
            raise ConfigurationError(textwrap.dedent('''
                Could not find full Akismet configuration.

                Found API key: {}
                Found blog URL: {}
            '''.format(maybe_key, maybe_url)))
        if not self.verify_key(maybe_key, maybe_url):
            raise APIKeyError(
                'Akismet configuration ({}, {}) is invalid.'.format(
                    maybe_key, maybe_url
                )
            )
        self.api_key = maybe_key
        self.blog_url = maybe_url

    def _api_request(self, endpoint, user_ip, user_agent, **kwargs):
        """
        Make a request to the Akismet API.

        This method is used for all API calls except key verification,
        since all endpoints other than key verification must
        interpolate the API key into the URL and supply certain basic
        data.

        """
        data = {'blog': self.blog_url,
                'user_ip': user_ip,
                'user_agent': user_agent}
        for key in self.OPTIONAL_KEYS:
            if key in kwargs:
                data[key] = kwargs[key]
        response = requests.post(
            endpoint.format(self.api_key),
            data=data,
            headers=self.user_agent_header)
        return response.text

    def _submit_call(self, operation, user_ip, user_agent, **kwargs):
        """
        Submit spam or ham to the Akismet API.

        """
        endpoint = {'submit_spam': self.SUBMIT_SPAM_URL,
                    'submit_ham': self.SUBMIT_HAM_URL}[operation]
        response_text = self._api_request(
            endpoint, user_ip, user_agent, **kwargs
        )
        if response_text == self.SUBMIT_SUCCESS_RESPONSE:
            return True
        else:
            self._protocol_error(operation, response_text)

    @classmethod
    def _protocol_error(cls, operation, message):
        """
        Raise an appropriate exception for unexpected API responses.

        """
        raise ProtocolError(textwrap.dedent('''
        Received unexpected or non-standard response from Akismet API.

        API operation was: {}
        API response received was: {}
        ''').format(operation, message))

    @classmethod
    def verify_key(cls, key, blog_url):
        """
        Verify an Akismet API key and URL.

        Returns True if the key and URL are valid, False otherwise.

        """
        response = requests.post(
            cls.VERIFY_KEY_URL,
            data={'key': key, 'blog': blog_url},
            headers=cls.user_agent_header
        )
        if response.text == 'valid':
            return True
        elif response.text == 'invalid':
            return False
        else:
            cls._protocol_error('verify_key', response.text)

    def comment_check(self, user_ip, user_agent, **kwargs):
        """
        Check a comment to determine whether it is spam.

        The IP address and user-agent string of the remote user are
        required. All other arguments documented by Akismet (other
        than the PHP server information) are also optionally accepted;
        see the Akismet API documentation for a full list:

        https://akismet.com/development/api/#comment-check

        Like the Akismet web API, returns True for a comment that is
        spam, and False for a comment that is not spam.

        """
        response_text = self._api_request(
            self.COMMENT_CHECK_URL, user_ip, user_agent, **kwargs
        )
        if response_text == 'true':
            return True
        elif response_text == 'false':
            return False
        else:
            self._protocol_error('comment_check', response_text)

    def submit_spam(self, user_ip, user_agent, **kwargs):
        """
        Inform Akismet that a comment is spam.

        The IP address and user-agent string of the remote user are
        required. All other arguments documented by Akismet (other
        than the PHP server information) are also optionally accepted;
        see the Akismet API documentation for a full list:

        https://akismet.com/development/api/#submit-spam

        Returns True on success (the only expected response).

        """
        return self._submit_call('submit_spam', user_ip, user_agent, **kwargs)

    def submit_ham(self, user_ip, user_agent, **kwargs):
        """
        Inform Akismet that a comment is not spam.

        The IP address and user-agent string of the remote user are
        required. All other arguments documented by Akismet (other
        than the PHP server information) are also optionally accepted;
        see the Akismet API documentation for a full list:

        https://akismet.com/development/api/#submit-ham

        Returns True on success (the only expected response).

        """
        return self._submit_call('submit_ham', user_ip, user_agent, **kwargs)
<|endoftext|>


<commit_before>
'''
Given the location of outlier(s), check each field in a mosaic for coverage of
that location, image it, subtract out of the UV plane
'''

import sys
import os
import warnings
import re
import glob
from astropy.extern import six

from taskinit import tb
from tasks import split, uvsub, concat, clean, rmtables

from .graceful_error_catch import catch_fail

def subtract_outlier(vis, outlier_coords, field='M33*', split_fields=True,
                     stokes='I', interactive=True, weighting='natural',
                     threshold='5mJy/beam', cell='3arcsec', cleanup=False,
                     datacolumn="CORRECTED", imsize=64, save_space=False,
                     masks=None):
    '''
    Subtract an outlier at the given coordinates. Splits out each field,
    tries to image at that coordinate, then subracts of the model in the UV
    plane. The fields are concatenated into a final ms.
    '''

    vis = vis.rstrip("/")

    # Get some info from the image
    tb.open(os.path.join(vis, 'FIELD'))
    all_fields = tb.getcol('NAME')
    tb.close()

    regex = re.compile(field)
    fields = [f for f in all_fields if re.match(regex, f)]

    # If only one position given, convert to list for iteration.
    if isinstance(outlier_coords, six.string_types):
        outlier_coords = [outlier_coords]

    if masks is not None:
        assert len(masks) == len(outlier_coords)

    try:
        os.mkdir('temp_files')
    except OSError:
        warnings.warn("temp_files already exists. "
                      "Going to remove all ms files from it.")
        catch_fail(rmtables, tablenames='temp_files/*')

    # Loop through the fields
    for f in fields:

        fieldvis = os.path.join('temp_files', f+".ms")
        fieldimg = os.path.join('temp_files', f)

        # Split the field off
        catch_fail(split, vis=vis, outputvis=fieldvis, field=f,
                   datacolumn=datacolumn)

        # Now image the data, keeping the phasecenter at the outlier

        # Image each outlier at its phasecenter, then uvsub
        for i, coord in enumerate(outlier_coords):

            outfield_img = fieldimg + "_" + str(i)

            if masks is not None:
                mask = masks[i]
            else:
                mask = None

            catch_fail(clean, vis=fieldvis, imagename=outfield_img, mode='mfs',
                       phasecenter=coord, niter=10000, usescratch=True,
                       interactive=interactive, cell='3arcsec',
                       imsize=imsize, threshold=threshold, weighting=weighting,
                       minpb=0.0, mask=mask)

            # Subtract out the model from the imaging
            catch_fail(uvsub, vis=fieldvis)

            # Remove the individual images
            if cleanup:
                catch_fail(rmtables, tablenames=outfield_img+"*")

            if save_space:

                fieldvis_corronly = \
                    os.path.join('temp_files', f+"_corrected.ms")

                catch_fail(split, vis=fieldvis, outputvis=fieldvis_corronly,
                           field=f, datacolumn="CORRECTED")

                catch_fail(rmtables, tablenames=fieldvis)

    # Now append the uvsub fields back together
    individ_ms = glob.glob("temp_files/*.ms")
    catch_fail(concat, vis=individ_ms,
               concatvis=vis.rstrip(".ms")+"_outsub.ms",
               respectname=True)

    if cleanup:
        catch_fail(rmtables, tablenames='temp_files/*')
<commit_msg>Give a better error<commit_after>
'''
Given the location of outlier(s), check each field in a mosaic for coverage of
that location, image it, subtract out of the UV plane
'''

import sys
import os
import warnings
import re
import glob
from astropy.extern import six

from taskinit import tb
from tasks import split, uvsub, concat, clean, rmtables

from .graceful_error_catch import catch_fail

def subtract_outlier(vis, outlier_coords, field='M33*', split_fields=True,
                     stokes='I', interactive=True, weighting='natural',
                     threshold='5mJy/beam', cell='3arcsec', cleanup=False,
                     datacolumn="CORRECTED", imsize=64, save_space=False,
                     masks=None):
    '''
    Subtract an outlier at the given coordinates. Splits out each field,
    tries to image at that coordinate, then subracts of the model in the UV
    plane. The fields are concatenated into a final ms.
    '''

    vis = vis.rstrip("/")

    # Get some info from the image
    tb.open(os.path.join(vis, 'FIELD'))
    all_fields = tb.getcol('NAME')
    tb.close()

    regex = re.compile(field)
    fields = [f for f in all_fields if re.match(regex, f)]

    # If only one position given, convert to list for iteration.
    if isinstance(outlier_coords, six.string_types):
        outlier_coords = [outlier_coords]

    if masks is not None:
        if len(masks) != len(outlier_coords):
            raise Warning("The number of specified masks must match the"
                          " number of coordinates.")

    try:
        os.mkdir('temp_files')
    except OSError:
        warnings.warn("temp_files already exists. "
                      "Going to remove all ms files from it.")
        catch_fail(rmtables, tablenames='temp_files/*')

    # Loop through the fields
    for f in fields:

        fieldvis = os.path.join('temp_files', f+".ms")
        fieldimg = os.path.join('temp_files', f)

        # Split the field off
        catch_fail(split, vis=vis, outputvis=fieldvis, field=f,
                   datacolumn=datacolumn)

        # Now image the data, keeping the phasecenter at the outlier

        # Image each outlier at its phasecenter, then uvsub
        for i, coord in enumerate(outlier_coords):

            outfield_img = fieldimg + "_" + str(i)

            if masks is not None:
                mask = masks[i]
            else:
                mask = None

            catch_fail(clean, vis=fieldvis, imagename=outfield_img, mode='mfs',
                       phasecenter=coord, niter=10000, usescratch=True,
                       interactive=interactive, cell='3arcsec',
                       imsize=imsize, threshold=threshold, weighting=weighting,
                       minpb=0.0, mask=mask)

            # Subtract out the model from the imaging
            catch_fail(uvsub, vis=fieldvis)

            # Remove the individual images
            if cleanup:
                catch_fail(rmtables, tablenames=outfield_img+"*")

            if save_space:

                fieldvis_corronly = \
                    os.path.join('temp_files', f+"_corrected.ms")

                catch_fail(split, vis=fieldvis, outputvis=fieldvis_corronly,
                           field=f, datacolumn="CORRECTED")

                catch_fail(rmtables, tablenames=fieldvis)

    # Now append the uvsub fields back together
    individ_ms = glob.glob("temp_files/*.ms")
    catch_fail(concat, vis=individ_ms,
               concatvis=vis.rstrip(".ms")+"_outsub.ms",
               respectname=True)

    if cleanup:
        catch_fail(rmtables, tablenames='temp_files/*')
<|endoftext|>


<commit_before>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4

import rapidsms
import re
import __builtin__

class App(rapidsms.app.App):
    class Session (dict):
        def __init__ (self, *args, **kwargs):
            self.cleanup()
            dict.__init__(self, *args, **kwargs)

        def prepare (self, temps):
            self.temps = temps

        def cleanup (self):
            self.temps = {}

        def __getitem__ (self, key):
            if key in self.temps:
                return self.temps[key]
            if hasattr(__builtin__, key):
                return getattr(__builtin__, key)
            if key not in self:
                self[key] = None
            return dict.__getitem__(self,key)

    def __init__ (self, *args, cfg="apps/ezsms/ezsms.cfg"):
        App.__init__(self, *args)
        self.file = cfg

    def start(self):
        self.sessions = {}

    def parse(self, msg):
        if msg.caller not in self.sessions:
            self.sessions[msg.caller] = self.Session()

    def handle(self, msg):
        sections = self.load()
        self.debug("available: %r", sections.keys())
        cmd = text = args = None
        for section in sections.keys():
            regex = re.compile(section, re.I)
            match = re.search(regex, msg.text)
            if match:
                cmd = section
                text = re.sub(regex, "", msg.text).strip()
                args = match.groups()
                break
        if not cmd: return
        if not args: args = filter(None, text.split())
        self.debug("cmd: %s, args: %r", cmd, args)
        session = self.sessions[msg.caller]
        session.prepare({
            'caller': msg.caller,
            'text': text,
            'args': args,
            'respond': msg.respond,
            'info': self.info,
            'debug': self.debug,
        })
        self.debug("session before: %r", sorted(session.keys()))
        try:
            code = compile(sections[cmd], cmd, 'exec')
            eval(code, globals(), session)
        except Exception, e:
            self.error("%s section raised %r", cmd, e)
        finally:
            session.cleanup()
        self.debug("session after: %r", sorted(session.keys()))

    def load (self):
        cfg = file(self.file)
        sections = {}
        current = None
        indent = 0
        for line in cfg.readlines():
            if re.match("\s*#",line): continue #comment
            heading = re.match(r"^(\S[^:]+):$", line)
            if heading:
                current = heading.group(1).strip()
                sections[current] = ""
                indent = 0
                continue
            if current:
                if indent == 0 and line.strip() != "":
                    while line[indent].isspace():
                        indent += 1
                sections[current] += line[indent:]
        return sections
<commit_msg>Fix ezsms constructor syntax.<commit_after>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4

import rapidsms
import re
import __builtin__

class App(rapidsms.app.App):
    class Session (dict):
        def __init__ (self, *args, **kwargs):
            self.cleanup()
            dict.__init__(self, *args, **kwargs)

        def prepare (self, temps):
            self.temps = temps

        def cleanup (self):
            self.temps = {}

        def __getitem__ (self, key):
            if key in self.temps:
                return self.temps[key]
            if hasattr(__builtin__, key):
                return getattr(__builtin__, key)
            if key not in self:
                self[key] = None
            return dict.__getitem__(self,key)

    def __init__ (self, title, router, cfg="apps/ezsms/ezsms.cfg"):
        rapidsms.app.App.__init__(self, title, router)
        self.file = cfg

    def start(self):
        self.sessions = {}

    def parse(self, msg):
        if msg.caller not in self.sessions:
            self.sessions[msg.caller] = self.Session()

    def handle(self, msg):
        sections = self.load()
        self.debug("available: %r", sections.keys())
        cmd = text = args = None
        for section in sections.keys():
            regex = re.compile(section, re.I)
            match = re.search(regex, msg.text)
            if match:
                cmd = section
                text = re.sub(regex, "", msg.text).strip()
                args = match.groups()
                break
        if not cmd: return
        if not args: args = filter(None, text.split())
        self.debug("cmd: %s, args: %r", cmd, args)
        session = self.sessions[msg.caller]
        session.prepare({
            'caller': msg.caller,
            'text': text,
            'args': args,
            'respond': msg.respond,
            'info': self.info,
            'debug': self.debug,
        })
        self.debug("session before: %r", sorted(session.keys()))
        try:
            code = compile(sections[cmd], cmd, 'exec')
            eval(code, globals(), session)
        except Exception, e:
            self.error("%s section raised %r", cmd, e)
        finally:
            session.cleanup()
        self.debug("session after: %r", sorted(session.keys()))

    def load (self):
        cfg = file(self.file)
        sections = {}
        current = None
        indent = 0
        for line in cfg.readlines():
            if re.match("\s*#",line): continue #comment
            heading = re.match(r"^(\S[^:]+):$", line)
            if heading:
                current = heading.group(1).strip()
                sections[current] = ""
                indent = 0
                continue
            if current:
                if indent == 0 and line.strip() != "":
                    while line[indent].isspace():
                        indent += 1
                sections[current] += line[indent:]
        return sections
<|endoftext|>


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

"""
Copyright(C) 2010  Romain Bignon

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.

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

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

"""

from __future__ import with_statement

from logging import debug
from copy import copy
from threading import Thread, Event, RLock, Timer
from .tools.misc import getBacktrace

__all__ = ['BackendsCall', 'CallErrors']

class CallErrors(Exception):
    def __init__(self, errors):
        Exception.__init__(self, "Several errors have been raised:\n%s" % ('\n'.join(['%s: %s' % (b, e) for b, e in errors])))
        self.errors = copy(errors)

    def __iter__(self):
        return self.errors.__iter__()

class Result(object):
    def __init__(self, backend, result):
        self.backend = backend
        self.result = result

    def __iter__(self):
        """
        To allow unpack.

        For example:
        >>> for backend, result in self.weboob.do(blabla)
        """
        yield self.backend
        yield self.result

class BackendsCall(object):
    def __init__(self, backends, function, *args, **kwargs):
        """
        @param backends  list of backends to call
        @param function  backends' method name, or callable object
        @param args, kwargs  arguments given to called functions
        """
        # Store if a backend is finished
        self.backends = {}
        for backend in backends:
            self.backends[backend.name] = False
        # Global mutex on object
        self.mutex = RLock()
        # Event set when every backends have give their data
        self.finish_event = Event()
        # Event set when there are new responses
        self.response_event = Event()
        # Waiting responses
        self.responses = []
        # Errors
        self.errors = []
        # Threads
        self.threads = []

        # Create jobs for each backend
        with self.mutex:
            for b in backends:
                debug('New timer for %s' % b)
                self.threads.append(Timer(0, self._caller, (b, function, args, kwargs)).start())

    def _store_error(self, b, e):
        with self.mutex:
            # TODO save backtrace and/or print it here (with debug)
            self.errors.append((b, e))
            debug(getBacktrace(e))

    def _store_result(self, b, r):
        with self.mutex:
            self.responses.append((b,r))
            self.response_event.set()

    def _caller(self, b, function, args, kwargs):
        debug('Hello from timer %s' % b)
        with b:
            try:
                # Call method on backend
                try:
                    if callable(function):
                        r = function(b, *args, **kwargs)
                    else:
                        r = getattr(b, function)(*args, **kwargs)
                except Exception, e:
                    self._store_error(b, e)
                else:
                    debug('%s: Got answer! %s' % (b, r))

                    if hasattr(r, '__iter__'):
                        # Loop on iterator
                        try:
                            for e in r:
                                # Lock mutex only in loop in case the iterator is slow
                                # (for example if backend do some parsing operations)
                                self._store_result(b, e)
                        except Exception, e:
                            self._store_error(b, e)
                    else:
                        self._store_result(b, r)
            finally:
                with self.mutex:
                    # This backend is now finished
                    self.backends[b.name] = True
                    for finished in self.backends.itervalues():
                        if not finished:
                            return
                    self.finish_event.set()
                    self.response_event.set()

    def _callback_thread_run(self, callback, errback):
        responses = []
        while not self.finish_event.isSet():
            self.response_event.wait()
            with self.mutex:
                responses = self.responses
                self.responses = []

                # Reset event
                self.response_event.clear()

            # Consume responses
            while responses:
                callback(*responses.pop())

        if errback:
            with self.mutex:
                while self.errors:
                    errback(*self.errors.pop())

        callback(None, None)

    def callback_thread(self, callback, errback=None):
        """
        Call this method to create a thread which will callback a
        specified function everytimes a new result comes.

        When the process is over, the function will be called with
        both arguments set to None.

        The functions prototypes:
            def callback(backend, result)
            def errback(backend, error)

        """
        thread = Thread(target=self._callback_thread_run, args=(callback, errback))
        thread.start()
        return thread

    def __iter__(self):
        # Don't know how to factorize with _callback_thread_run
        responses = []
        while not self.finish_event.isSet():
            self.response_event.wait()
            with self.mutex:
                responses = self.responses
                self.responses = []

                # Reset event
                self.response_event.clear()

            # Consume responses
            while responses:
                yield Result(*responses.pop())

        # Raise errors
        with self.mutex:
            if self.errors:
                raise CallErrors(self.errors)
<commit_msg>do not freeze when a call is made with no backends<commit_after># -*- coding: utf-8 -*-

"""
Copyright(C) 2010  Romain Bignon

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.

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

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

"""

from __future__ import with_statement

from logging import debug
from copy import copy
from threading import Thread, Event, RLock, Timer
from .tools.misc import getBacktrace

__all__ = ['BackendsCall', 'CallErrors']

class CallErrors(Exception):
    def __init__(self, errors):
        Exception.__init__(self, "Several errors have been raised:\n%s" % ('\n'.join(['%s: %s' % (b, e) for b, e in errors])))
        self.errors = copy(errors)

    def __iter__(self):
        return self.errors.__iter__()

class Result(object):
    def __init__(self, backend, result):
        self.backend = backend
        self.result = result

    def __iter__(self):
        """
        To allow unpack.

        For example:
        >>> for backend, result in self.weboob.do(blabla)
        """
        yield self.backend
        yield self.result

class BackendsCall(object):
    def __init__(self, backends, function, *args, **kwargs):
        """
        @param backends  list of backends to call
        @param function  backends' method name, or callable object
        @param args, kwargs  arguments given to called functions
        """
        # Store if a backend is finished
        self.backends = {}
        for backend in backends:
            self.backends[backend.name] = False
        # Global mutex on object
        self.mutex = RLock()
        # Event set when every backends have give their data
        self.finish_event = Event()
        # Event set when there are new responses
        self.response_event = Event()
        # Waiting responses
        self.responses = []
        # Errors
        self.errors = []
        # Threads
        self.threads = []

        # Create jobs for each backend
        with self.mutex:
            for b in backends:
                debug('New timer for %s' % b)
                self.threads.append(Timer(0, self._caller, (b, function, args, kwargs)).start())
            if not backends:
                self.finish_event.set()

    def _store_error(self, b, e):
        with self.mutex:
            # TODO save backtrace and/or print it here (with debug)
            self.errors.append((b, e))
            debug(getBacktrace(e))

    def _store_result(self, b, r):
        with self.mutex:
            self.responses.append((b,r))
            self.response_event.set()

    def _caller(self, b, function, args, kwargs):
        debug('Hello from timer %s' % b)
        with b:
            try:
                # Call method on backend
                try:
                    if callable(function):
                        r = function(b, *args, **kwargs)
                    else:
                        r = getattr(b, function)(*args, **kwargs)
                except Exception, e:
                    self._store_error(b, e)
                else:
                    debug('%s: Got answer! %s' % (b, r))

                    if hasattr(r, '__iter__'):
                        # Loop on iterator
                        try:
                            for e in r:
                                # Lock mutex only in loop in case the iterator is slow
                                # (for example if backend do some parsing operations)
                                self._store_result(b, e)
                        except Exception, e:
                            self._store_error(b, e)
                    else:
                        self._store_result(b, r)
            finally:
                with self.mutex:
                    # This backend is now finished
                    self.backends[b.name] = True
                    for finished in self.backends.itervalues():
                        if not finished:
                            return
                    self.finish_event.set()
                    self.response_event.set()

    def _callback_thread_run(self, callback, errback):
        responses = []
        while not self.finish_event.isSet():
            self.response_event.wait()
            with self.mutex:
                responses = self.responses
                self.responses = []

                # Reset event
                self.response_event.clear()

            # Consume responses
            while responses:
                callback(*responses.pop())

        if errback:
            with self.mutex:
                while self.errors:
                    errback(*self.errors.pop())

        callback(None, None)

    def callback_thread(self, callback, errback=None):
        """
        Call this method to create a thread which will callback a
        specified function everytimes a new result comes.

        When the process is over, the function will be called with
        both arguments set to None.

        The functions prototypes:
            def callback(backend, result)
            def errback(backend, error)

        """
        thread = Thread(target=self._callback_thread_run, args=(callback, errback))
        thread.start()
        return thread

    def __iter__(self):
        # Don't know how to factorize with _callback_thread_run
        responses = []
        while not self.finish_event.isSet():
            self.response_event.wait()
            with self.mutex:
                responses = self.responses
                self.responses = []

                # Reset event
                self.response_event.clear()

            # Consume responses
            while responses:
                yield Result(*responses.pop())

        # Raise errors
        with self.mutex:
            if self.errors:
                raise CallErrors(self.errors)
<|endoftext|>