<commit_before>from django.contrib import admin
from scoring.models import Event, Score, EggDropScore, VolcanoScore, PreRegistration, DrillingMudScore, GravityCarScore
from registration.models import Team
from django import forms
from django.contrib.admin.widgets import ForeignKeyRawIdWidget
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.admin.views.decorators import staff_member_required
from django.db.models import Max
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.models import User, Group
import simplejson as json
from django.core.mail import send_mass_mail, EmailMultiAlternatives
from django.views.decorators.cache import cache_page
admin.site.register(Event)

admin.site.register(VolcanoScore)

class PreRegistrationAdmin(admin.ModelAdmin):
    list_display = ('teams', 'event',)
    list_filter = ('event',)
    search_fields = ('teams__name', 'event__name',)
    raw_id_fields = ('teams',)
admin.site.register(PreRegistration, PreRegistrationAdmin)

class ScoreForm(forms.ModelForm):
    db_field = Score._meta.get_field_by_name('team')[0]

    team = forms.ModelChoiceField(
        queryset=Team.objects.all(),
        widget=ForeignKeyRawIdWidget(db_field.rel, admin.site),
        required=True
    )
    def __init__(self, *args, **kwargs):

        super(ScoreForm, self).__init__(*args, **kwargs)
        standard_events = Event.objects.filter(event_score_type='STD').distinct()

        standard_events = standard_events.filter(owners__in=self.current_groups)
        # print standard_events
        event_widget = self.fields['event'].widget

        choices = []
        for element in standard_events:
            choices.append((element.id, element.name))
        event_widget.choices = choices

    def clean_team(self):
        selected_event = Event.objects.get(name=self.cleaned_data['event'])
        owners = selected_event.owners.all()

        try:
            team = self.cleaned_data['team']
        except:
             raise forms.ValidationError("Team ID is incorrect or does not exist")
        try:
            event_prereg = PreRegistration.objects.get(event=selected_event, teams=self.cleaned_data['team'])
        except:
            users = User.objects.filter(groups__in=owners)

            emails = [user.email for user in users]
            subject = 'ERROR:Team %s (Number: %s) is not registered for event %s' % (team.name, str(team.id), selected_event.name,)
            message = 'An error has occurred. \r\n  Team %s (Number: %s) is not registered for event %s. \n User:%s' % (team.name, str(team.id), selected_event.name, self.current_user.username,)
            html = '<!DOCTYPE html><title>%s</title>' % (subject,)
            html = html + '
An Error Has Occurred

An error has occurred.
  Team %s (Number: %s) is not registered for event %s .

User: %s
' % (team.name, str(team.id), selected_event.name, self.current_user.username)
            email = EmailMultiAlternatives(subject=subject, body=message,
            bcc=emails,
            headers={'Reply-To': 'hgrimberg01@gmail.com', 'X-Mailer':'ESC EXPO System v1.0'})
            email.attach_alternative(html, "text/html")
            email.send()
            raise forms.ValidationError("Team is not registered for this event")
        return self.cleaned_data['team']

    def clean_score(self):
        selected_event = Event.objects.get(name=self.cleaned_data['event'])

        if(selected_event.max_score > selected_event.min_score):
            if self.cleaned_data['score'] < 0:
                raise forms.ValidationError("Score Cannot Be Negative")
            elif self.cleaned_data['score'] > selected_event.max_score:
                raise forms.ValidationError("Score Cannot Be Greater Than Best Possible Score of " + str(selected_event.max_score))
            elif self.cleaned_data['score'] < selected_event.min_score:
                raise forms.ValidationError("Score Cannot Be Less Than Worst Possible Score of " + str(selected_event.min_score))
            else:
                return self.cleaned_data['score']
        elif selected_event.max_score < selected_event.min_score:
            if self.cleaned_data['score'] < 0:
                raise forms.ValidationError("Score Cannot Be Negative")
            elif self.cleaned_data['score'] < selected_event.max_score:
                raise forms.ValidationError("Score Cannot Be Less Than Best Possible Score of " + str(selected_event.max_score))
            elif self.cleaned_data['score'] > selected_event.min_score:
                raise forms.ValidationError("Score Cannot Be Greater Than Worst Possible Score of " + str(selected_event.min_score))
            else:
                return self.cleaned_data['score']
        class Meta:
            model = Score

class ScoreAdmin(admin.ModelAdmin):
    form = ScoreForm
    list_display = ('event', 'team', 'score')
    list_filter = ('event', 'team__division')
    search_fields = ('team',)

    raw_id_fields = ('team',)
    def queryset(self, request):
        qs = super(ScoreAdmin, self).queryset(request)
        user_groups = request.user.groups.all()
        if request.user.is_superuser:
            return qs
        return qs.filter(event__owners=user_groups)

    def get_form(self, request, obj=None, **kwargs):
        form = super(ScoreAdmin, self).get_form(request, obj, **kwargs)
        form.current_user = request.user
        form.current_groups = request.user.groups.all()

        return form

    pass

class EventAdmin(admin.ModelAdmin):
    list_display = ('name', 'score_report', 'total_scores', 'high_school_scores', 'middle_school_scores', 'elementary_school_scores', 'other_school_scores')
    search_fields = ('name',)

    # def show_link(self, obj):
    #    return 'Click here' % obj.get_absolute_url()

    def score_report(self, obj):
        return 'Click here' % obj.pk
    def total_scores(self, obj):
        return str(Score.objects.filter(event=obj).count())
    def high_school_scores(self, obj):
        return str(Score.objects.filter(event=obj).filter(team__division='HS').count())
    def middle_school_scores(self, obj):
        return str(Score.objects.filter(event=obj).filter(team__division='MS').count())
    def elementary_school_scores(self, obj):
        return str(Score.objects.filter(event=obj).filter(team__division='ES').count())
    def other_school_scores(self, obj):
        return str(Score.objects.filter(event=obj).filter(team__division='OTH').count())

    def queryset(self, request):
        qs = super(EventAdmin, self).queryset(request)
        user_groups = request.user.groups.all()
        if request.user.is_superuser:
            return qs
        return qs.filter(owners=user_groups)

    score_report.allow_tags = True

admin.site.unregister(Event)
admin.site.register(Event, EventAdmin)
admin.site.register(Score, ScoreAdmin)

def TopTenPerEvent(request):
    all_events = Event.objects.all()

    my_values = []
    for event in all_events:
        event_scores = Score.objects.filter(event=event)
        top_five_scores = event_scores.order_by('-normalized_score')[:10]
        my_values.append({'event':event, 'scores':top_five_scores})

    # event_winners = Event.objects.annotate(top_scores = Max('normalized_score')).order_by('-top_scores')[:5]
    # pubs = Publisher.objects.annotate(num_books=Count('book')).order_by('-num_books')[:5]

    return render_to_response(
        "admin/scoring/report.html",
        {'score_list' : my_values},
        RequestContext(request, {}),
    )
TopTenPerEvent = staff_member_required(TopTenPerEvent)

def AllScoresForEvent(request):
    all_events = Event.objects.all()

    my_values = []
    for event in all_events:
        event_scores = Score.objects.filter(event=event)
        top_five_scores = event_scores.order_by('-normalized_score')
        my_values.append({'event':event, 'scores':top_five_scores})

    # event_winners = Event.objects.annotate(top_scores = Max('normalized_score')).order_by('-top_scores')[:5]
    # pubs = Publisher.objects.annotate(num_books=Count('book')).order_by('-num_books')[:5]

    return render_to_response(
        "admin/scoring/report.html",
        {'score_list' : my_values},
        RequestContext(request, {}),
    )
AllScoresForEvent = staff_member_required(AllScoresForEvent)

admin.site.register_view('AllEventScores', AllScoresForEvent)

def AllScoresForEventByDivision(request):
    all_events = Event.objects.all()
    all_divisions = settings.GLOBAL_SETTINGS['SCHOOL_TYPES']

    my_values = []
    for event in all_events:
        div_event = []
        for division in all_divisions:
            div_temp = division[0]
            event_division_score = Score.objects.exclude(disqualified=True).filter(event=event).filter(team__division__exact=div_temp)
            final_scores = event_division_score.order_by('-normalized_score')
            div_event.append({'division':division[1], 'scores':final_scores})

        my_values.append({'event':event, 'div_scores':div_event})
    print my_values
    return render_to_response(
        "admin/scoring/event_division_score.html",
        {'score_list' : my_values},
        RequestContext(request, {}),
    )
    # print my_values

AllScoresForEventByDivision = staff_member_required(AllScoresForEventByDivision)
admin.site.register_view('AllScoresForEventsByDivision', AllScoresForEventByDivision)

def RetabulateEggDropScores(request):
    egg_drop = EggDropScore.objects.all()

    for egg_drop_score in egg_drop:
        egg_drop_score.save()
    return  HttpResponseRedirect('/admin/')
RetabulateEggDropScores = staff_member_required(RetabulateEggDropScores)
admin.site.register_view('retabEggdrop', RetabulateEggDropScores)

def RetabulateVolcanoScores(request):
    volcano_scores = VolcanoScore.objects.all()

    for volcano_score in volcano_scores:
        volcano_score.save()
    return  HttpResponseRedirect('/admin/')
RetabulateEggDropScores = staff_member_required(RetabulateVolcanoScores)
admin.site.register_view('retabVolcanoScores', RetabulateVolcanoScores)

@cache_page(60 * 2)
def AllScoresForSingleEventByDivision(request, event_id):
    event = Event.objects.get(id=event_id)
    all_divisions = settings.GLOBAL_SETTINGS['SCHOOL_TYPES']
    div_event = []
    for division in all_divisions:
        div_temp = division[0]
        event_division_score = Score.objects.filter(event=event).filter(team__division__exact=div_temp)
        final_scores = event_division_score.order_by('-normalized_score')
        div_event.append({'division':division[1], 'scores':final_scores})

    print div_event

    return render_to_response(
        "admin/scoring/event_score.html",
        {'score_list' : div_event, 'event_title':event.name},
        RequestContext(request, {}),
    )
    # print my_values

AllScoresForSingleEventByDivision = staff_member_required(AllScoresForSingleEventByDivision)

def RetabulateDrillingMudScores(request):
    drilling_mud_scores = DrillingMudScore.objects.all()

    for drilling_mud_scores in drilling_mud_scores:
        drilling_mud_scores.save()
    return  HttpResponseRedirect('/admin/')
RetabulateDrillingMudScores = staff_member_required(RetabulateDrillingMudScores)
admin.site.register_view('retabDrillingMudScores', RetabulateDrillingMudScores)

class DrillingMudForm(ScoreForm):
    def __init__(self, *args, **kwargs):

        super(DrillingMudForm, self).__init__(*args, **kwargs)
        standard_events = Event.objects.filter(event_score_type='DMUD').distinct()

        standard_events = standard_events.filter(owners__in=self.current_groups)

        event_widget = self.fields['event'].widget

        choices = []
        for element in standard_events:
            choices.append((element.id, element.name))
        event_widget.choices = choices

class DrillingMudAdmin(ScoreAdmin):
    form = DrillingMudForm
    def get_form(self, request, obj=None, **kwargs):
        form = super(DrillingMudAdmin, self).get_form(request, obj, **kwargs)
        form.current_user = request.user
        form.current_groups = request.user.groups.all()

        return form
    pass

class EggDropScoreForm(ScoreForm):
    def __init__(self, *args, **kwargs):

        super(EggDropScoreForm, self).__init__(*args, **kwargs)
        standard_events = Event.objects.filter(event_score_type='EGD').distinct()

        standard_events = standard_events.filter(owners__in=self.current_groups)

        event_widget = self.fields['event'].widget

        choices = []
        for element in standard_events:
            choices.append((element.id, element.name))
        event_widget.choices = choices

class EggDropScoreAdmin(ScoreAdmin):
    form = EggDropScoreForm
    def get_form(self, request, obj=None, **kwargs):
        form = super(EggDropScoreAdmin, self).get_form(request, obj, **kwargs)
        form.current_user = request.user
        form.current_groups = request.user.groups.all()

        return form
    pass

class GravityCarScoreForm(ScoreForm):
    def __init__(self, *args, **kwargs):

        super(GravityCarScoreForm, self).__init__(*args, **kwargs)
        standard_events = Event.objects.filter(event_score_type='GCAR').distinct()

        standard_events = standard_events.filter(owners__in=self.current_groups)

        event_widget = self.fields['event'].widget

        choices = []
        for element in standard_events:
            choices.append((element.id, element.name))
        event_widget.choices = choices

class GravityCarScoreAdmin(ScoreAdmin):
    form = GravityCarScoreForm
    def get_form(self, request, obj=None, **kwargs):
        form = super(GravityCarScoreAdmin, self).get_form(request, obj, **kwargs)
        form.current_user = request.user
        form.current_groups = request.user.groups.all()

        return form
    pass

admin.site.register(DrillingMudScore, DrillingMudAdmin)
admin.site.register(EggDropScore, EggDropScoreAdmin)
admin.site.register(GravityCarScore, GravityCarScoreAdmin)
<commit_msg>Fix multiple group issue in event/score admin 		<commit_after>from django.contrib import admin
from scoring.models import Event, Score, EggDropScore, VolcanoScore, PreRegistration, DrillingMudScore, GravityCarScore
from registration.models import Team
from django import forms
from django.contrib.admin.widgets import ForeignKeyRawIdWidget
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.admin.views.decorators import staff_member_required
from django.db.models import Max
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.models import User, Group
import simplejson as json
from django.core.mail import send_mass_mail, EmailMultiAlternatives
from django.views.decorators.cache import cache_page
admin.site.register(Event)

admin.site.register(VolcanoScore)

class PreRegistrationAdmin(admin.ModelAdmin):
    list_display = ('teams', 'event',)
    list_filter = ('event',)
    search_fields = ('teams__name', 'event__name',)
    raw_id_fields = ('teams',)
admin.site.register(PreRegistration, PreRegistrationAdmin)

class ScoreForm(forms.ModelForm):
    db_field = Score._meta.get_field_by_name('team')[0]

    team = forms.ModelChoiceField(
        queryset=Team.objects.all(),
        widget=ForeignKeyRawIdWidget(db_field.rel, admin.site),
        required=True
    )
    def __init__(self, *args, **kwargs):

        super(ScoreForm, self).__init__(*args, **kwargs)
        standard_events = Event.objects.filter(event_score_type='STD').distinct()

        standard_events = standard_events.filter(owners__in=self.current_groups)
        # print standard_events
        event_widget = self.fields['event'].widget

        choices = []
        for element in standard_events:
            choices.append((element.id, element.name))
        event_widget.choices = choices

    def clean_team(self):
        selected_event = Event.objects.get(name=self.cleaned_data['event'])
        owners = selected_event.owners.all()

        try:
            team = self.cleaned_data['team']
        except:
             raise forms.ValidationError("Team ID is incorrect or does not exist")
        try:
            event_prereg = PreRegistration.objects.get(event=selected_event, teams=self.cleaned_data['team'])
        except:
            users = User.objects.filter(groups__in=owners)

            emails = [user.email for user in users]
            subject = 'ERROR:Team %s (Number: %s) is not registered for event %s' % (team.name, str(team.id), selected_event.name,)
            message = 'An error has occurred. \r\n  Team %s (Number: %s) is not registered for event %s. \n User:%s' % (team.name, str(team.id), selected_event.name, self.current_user.username,)
            html = '<!DOCTYPE html><title>%s</title>' % (subject,)
            html = html + '
An Error Has Occurred

An error has occurred.
  Team %s (Number: %s) is not registered for event %s .

User: %s
' % (team.name, str(team.id), selected_event.name, self.current_user.username)
            email = EmailMultiAlternatives(subject=subject, body=message,
            bcc=emails,
            headers={'Reply-To': 'hgrimberg01@gmail.com', 'X-Mailer':'ESC EXPO System v1.0'})
            email.attach_alternative(html, "text/html")
            email.send()
            raise forms.ValidationError("Team is not registered for this event")
        return self.cleaned_data['team']

    def clean_score(self):
        selected_event = Event.objects.get(name=self.cleaned_data['event'])

        if(selected_event.max_score > selected_event.min_score):
            if self.cleaned_data['score'] < 0:
                raise forms.ValidationError("Score Cannot Be Negative")
            elif self.cleaned_data['score'] > selected_event.max_score:
                raise forms.ValidationError("Score Cannot Be Greater Than Best Possible Score of " + str(selected_event.max_score))
            elif self.cleaned_data['score'] < selected_event.min_score:
                raise forms.ValidationError("Score Cannot Be Less Than Worst Possible Score of " + str(selected_event.min_score))
            else:
                return self.cleaned_data['score']
        elif selected_event.max_score < selected_event.min_score:
            if self.cleaned_data['score'] < 0:
                raise forms.ValidationError("Score Cannot Be Negative")
            elif self.cleaned_data['score'] < selected_event.max_score:
                raise forms.ValidationError("Score Cannot Be Less Than Best Possible Score of " + str(selected_event.max_score))
            elif self.cleaned_data['score'] > selected_event.min_score:
                raise forms.ValidationError("Score Cannot Be Greater Than Worst Possible Score of " + str(selected_event.min_score))
            else:
                return self.cleaned_data['score']
        class Meta:
            model = Score

class ScoreAdmin(admin.ModelAdmin):
    form = ScoreForm
    list_display = ('event', 'team', 'score')
    list_filter = ('event', 'team__division')
    search_fields = ('team',)

    raw_id_fields = ('team',)
    def queryset(self, request):
        qs = super(ScoreAdmin, self).queryset(request)
        user_groups = request.user.groups.all()
        if request.user.is_superuser:
            return qs
        return qs.filter(event__owners__in=user_groups)

    def get_form(self, request, obj=None, **kwargs):
        form = super(ScoreAdmin, self).get_form(request, obj, **kwargs)
        form.current_user = request.user
        form.current_groups = request.user.groups.all()

        return form

    pass

class EventAdmin(admin.ModelAdmin):
    list_display = ('name', 'score_report', 'total_scores', 'high_school_scores', 'middle_school_scores', 'elementary_school_scores', 'other_school_scores')
    search_fields = ('name',)

    # def show_link(self, obj):
    #    return 'Click here' % obj.get_absolute_url()

    def score_report(self, obj):
        return 'Click here' % obj.pk
    def total_scores(self, obj):
        return str(Score.objects.filter(event=obj).count())
    def high_school_scores(self, obj):
        return str(Score.objects.filter(event=obj).filter(team__division='HS').count())
    def middle_school_scores(self, obj):
        return str(Score.objects.filter(event=obj).filter(team__division='MS').count())
    def elementary_school_scores(self, obj):
        return str(Score.objects.filter(event=obj).filter(team__division='ES').count())
    def other_school_scores(self, obj):
        return str(Score.objects.filter(event=obj).filter(team__division='OTH').count())

    def queryset(self, request):
        qs = super(EventAdmin, self).queryset(request)
        user_groups = request.user.groups.all()
        if request.user.is_superuser:
            return qs
        return qs.filter(owners__in=user_groups)

    score_report.allow_tags = True

admin.site.unregister(Event)
admin.site.register(Event, EventAdmin)
admin.site.register(Score, ScoreAdmin)

def TopTenPerEvent(request):
    all_events = Event.objects.all()

    my_values = []
    for event in all_events:
        event_scores = Score.objects.filter(event=event)
        top_five_scores = event_scores.order_by('-normalized_score')[:10]
        my_values.append({'event':event, 'scores':top_five_scores})

    # event_winners = Event.objects.annotate(top_scores = Max('normalized_score')).order_by('-top_scores')[:5]
    # pubs = Publisher.objects.annotate(num_books=Count('book')).order_by('-num_books')[:5]

    return render_to_response(
        "admin/scoring/report.html",
        {'score_list' : my_values},
        RequestContext(request, {}),
    )
TopTenPerEvent = staff_member_required(TopTenPerEvent)

def AllScoresForEvent(request):
    all_events = Event.objects.all()

    my_values = []
    for event in all_events:
        event_scores = Score.objects.filter(event=event)
        top_five_scores = event_scores.order_by('-normalized_score')
        my_values.append({'event':event, 'scores':top_five_scores})

    # event_winners = Event.objects.annotate(top_scores = Max('normalized_score')).order_by('-top_scores')[:5]
    # pubs = Publisher.objects.annotate(num_books=Count('book')).order_by('-num_books')[:5]

    return render_to_response(
        "admin/scoring/report.html",
        {'score_list' : my_values},
        RequestContext(request, {}),
    )
AllScoresForEvent = staff_member_required(AllScoresForEvent)

admin.site.register_view('AllEventScores', AllScoresForEvent)

def AllScoresForEventByDivision(request):
    all_events = Event.objects.all()
    all_divisions = settings.GLOBAL_SETTINGS['SCHOOL_TYPES']

    my_values = []
    for event in all_events:
        div_event = []
        for division in all_divisions:
            div_temp = division[0]
            event_division_score = Score.objects.exclude(disqualified=True).filter(event=event).filter(team__division__exact=div_temp)
            final_scores = event_division_score.order_by('-normalized_score')
            div_event.append({'division':division[1], 'scores':final_scores})

        my_values.append({'event':event, 'div_scores':div_event})
    print my_values
    return render_to_response(
        "admin/scoring/event_division_score.html",
        {'score_list' : my_values},
        RequestContext(request, {}),
    )
    # print my_values

AllScoresForEventByDivision = staff_member_required(AllScoresForEventByDivision)
admin.site.register_view('AllScoresForEventsByDivision', AllScoresForEventByDivision)

def RetabulateEggDropScores(request):
    egg_drop = EggDropScore.objects.all()

    for egg_drop_score in egg_drop:
        egg_drop_score.save()
    return  HttpResponseRedirect('/admin/')
RetabulateEggDropScores = staff_member_required(RetabulateEggDropScores)
admin.site.register_view('retabEggdrop', RetabulateEggDropScores)

def RetabulateVolcanoScores(request):
    volcano_scores = VolcanoScore.objects.all()

    for volcano_score in volcano_scores:
        volcano_score.save()
    return  HttpResponseRedirect('/admin/')
RetabulateEggDropScores = staff_member_required(RetabulateVolcanoScores)
admin.site.register_view('retabVolcanoScores', RetabulateVolcanoScores)

@cache_page(60 * 2)
def AllScoresForSingleEventByDivision(request, event_id):
    event = Event.objects.get(id=event_id)
    all_divisions = settings.GLOBAL_SETTINGS['SCHOOL_TYPES']
    div_event = []
    for division in all_divisions:
        div_temp = division[0]
        event_division_score = Score.objects.filter(event=event).filter(team__division__exact=div_temp)
        final_scores = event_division_score.order_by('-normalized_score')
        div_event.append({'division':division[1], 'scores':final_scores})

    print div_event

    return render_to_response(
        "admin/scoring/event_score.html",
        {'score_list' : div_event, 'event_title':event.name},
        RequestContext(request, {}),
    )
    # print my_values

AllScoresForSingleEventByDivision = staff_member_required(AllScoresForSingleEventByDivision)

def RetabulateDrillingMudScores(request):
    drilling_mud_scores = DrillingMudScore.objects.all()

    for drilling_mud_scores in drilling_mud_scores:
        drilling_mud_scores.save()
    return  HttpResponseRedirect('/admin/')
RetabulateDrillingMudScores = staff_member_required(RetabulateDrillingMudScores)
admin.site.register_view('retabDrillingMudScores', RetabulateDrillingMudScores)

class DrillingMudForm(ScoreForm):
    def __init__(self, *args, **kwargs):

        super(DrillingMudForm, self).__init__(*args, **kwargs)
        standard_events = Event.objects.filter(event_score_type='DMUD').distinct()

        standard_events = standard_events.filter(owners__in=self.current_groups)

        event_widget = self.fields['event'].widget

        choices = []
        for element in standard_events:
            choices.append((element.id, element.name))
        event_widget.choices = choices

class DrillingMudAdmin(ScoreAdmin):
    form = DrillingMudForm
    def get_form(self, request, obj=None, **kwargs):
        form = super(DrillingMudAdmin, self).get_form(request, obj, **kwargs)
        form.current_user = request.user
        form.current_groups = request.user.groups.all()

        return form
    pass

class EggDropScoreForm(ScoreForm):
    def __init__(self, *args, **kwargs):

        super(EggDropScoreForm, self).__init__(*args, **kwargs)
        standard_events = Event.objects.filter(event_score_type='EGD').distinct()

        standard_events = standard_events.filter(owners__in=self.current_groups)

        event_widget = self.fields['event'].widget

        choices = []
        for element in standard_events:
            choices.append((element.id, element.name))
        event_widget.choices = choices

class EggDropScoreAdmin(ScoreAdmin):
    form = EggDropScoreForm
    def get_form(self, request, obj=None, **kwargs):
        form = super(EggDropScoreAdmin, self).get_form(request, obj, **kwargs)
        form.current_user = request.user
        form.current_groups = request.user.groups.all()

        return form
    pass

class GravityCarScoreForm(ScoreForm):
    def __init__(self, *args, **kwargs):

        super(GravityCarScoreForm, self).__init__(*args, **kwargs)
        standard_events = Event.objects.filter(event_score_type='GCAR').distinct()

        standard_events = standard_events.filter(owners__in=self.current_groups)

        event_widget = self.fields['event'].widget

        choices = []
        for element in standard_events:
            choices.append((element.id, element.name))
        event_widget.choices = choices

class GravityCarScoreAdmin(ScoreAdmin):
    form = GravityCarScoreForm
    def get_form(self, request, obj=None, **kwargs):
        form = super(GravityCarScoreAdmin, self).get_form(request, obj, **kwargs)
        form.current_user = request.user
        form.current_groups = request.user.groups.all()

        return form
    pass

admin.site.register(DrillingMudScore, DrillingMudAdmin)
admin.site.register(EggDropScore, EggDropScoreAdmin)
admin.site.register(GravityCarScore, GravityCarScoreAdmin)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''

Establishes Connection to Square API to allow access to store data for manipulation.

'''

import os
import json
import requests
import squareconnect
from sqware.secrets import get_secrets

class Sq_Connect(object):
	def __init__(self):
		'''constructor for class'''
		self.access_token = get_secrets('ACCESS_TOKEN')
		self.application_id = get_secrets('APPLICATION_ID')
		self.location_id = get_secrets('LOCATION_ID')
		self.request_headers = {
			'Authorization': 'Bearer ' + self.access_token,
			'Accept':        'application/json',
			'Content-Type':  'application/json'
			}

	def connect_api(self, request_path):
		'''
		Connects user to square api.

		User adds endpoint path as per Square API documentation.

		ex: sq_connect.connect_api('/v2/locations')
		'''

		#create connection to Square API using Requests library.
		#Uses custom header parameter to pass  requireed square api headers.
		#'timeout' parameter can be changed to desired time. See Requests docs (http://docs.python-requests.org/en/master/user/quickstart/#custom-headers).
		sq_connection = requests.get('https://connect.squareup.com' + request_path, headers = self.request_headers, timeout=3)
		try:
			sq_connection.raise_for_status()
			return sq_connection
		except requests.exceptions.RequestException as e:
			return str(e)

<commit_msg>[fix] fixed comments on class and tests.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''

Establishes Connection to Square API to allow access to store data for manipulation.

'''

import os
import json
import requests
import squareconnect
from sqware.secrets import get_secrets

class Sq_Connect(object):
	def __init__(self):
		'''constructor for class'''
		self.access_token = get_secrets('ACCESS_TOKEN')
		self.application_id = get_secrets('APPLICATION_ID')
		self.location_id = get_secrets('LOCATION_ID')
		self.request_headers = {
			'Authorization': 'Bearer ' + self.access_token,
			'Accept':        'application/json',
			'Content-Type':  'application/json'
			}

	def connect_api(self, request_path):
		'''
		Connects user to square api.
		User adds endpoint path as per Square API documentation.
		ex: sq_connect.connect_api('/v2/locations')
		'''

		#create connection to Square API using Requests library.
		#Uses custom header parameter to pass  requireed square api headers.
		#'timeout' parameter can be changed to desired time. See Requests docs (http://docs.python-requests.org/en/master/user/quickstart/#custom-headers).
		sq_connection = requests.get('https://connect.squareup.com' + request_path, headers = self.request_headers, timeout=3)
		try:
			sq_connection.raise_for_status()
			return sq_connection
		except requests.exceptions.RequestException as e:
			return str(e)

<|endoftext|>


<commit_before>"""
Atlas class definitions
"""

# Author: John Griffiths
# License: simplified BSD

import os

import numpy as np
import nibabel as nib

from nilearn.image import index_img

from nilearn.plotting import plot_glass_brain

from utils.readers import (load_connectivity,load_vol_file_mappings,load_vol_bboxes,
                           load_stream_file_mappings,load_stream_bboxes,
                           make_nx_graph,dpy_to_trk)

from utils.stats import (compute_vol_hit_stats,compute_vol_scalar_stats,
                         compute_streams_in_roi,#compute_stream_hit_stats,compute_stream_scalar_stats,
                         hit_stats_to_nx)

#from viz.network import plot_network,plot_matrix
#from viz.volume import plot_vol_cnxn,plot_vol_tract
#from viz.streams import plot_stream_cnxn,plot_stream_tract

class _Atlas(): # object):
  """
  Base atlas class
  """

  def __init__(self):

    print 'blah'

class _VolAtlas(_Atlas):
  """
  Volumetric atlas class
  """

  def __init__(self,atlas_name=None,atlas_dir=None):

    self.atlas_name = atlas_name

    # Load volumetric atlas info

    #if atlas_dir:
    #  self.vfms,self.atlas_dir = load_vol_file_mappings(atlas_dir=atlas_dir)
    #  self.bbox = load_vol_bboxes(atlas_dir=atlas_dir)
    #else:
    #  self.vfms,self.atlas_dir = load_vol_file_mappings(atlas_name=atlas_name)
    #  self.bbox = load_vol_bboxes(atlas_name=atlas_name)
    self.vfms,self.atlas_dir = load_vol_file_mappings(atlas_name=atlas_name,atlas_dir=atlas_dir)
    self.bbox = load_vol_bboxes(atlas_name=atlas_name,atlas_dir=atlas_dir)

  def get_vol_from_vfm(self,idx):
    """
    Convenience method to return nifti volume for an entry in the vfm table
    """

    nii_file = self.vfms.ix[idx]['nii_file']
    volnum = self.vfms.ix[idx]['4dvolind']

    if not os.path.isfile(nii_file):
      candidate = os.path.join(self.atlas_dir,nii_file)
      if os.path.isfile(candidate):
        nii_file = candidate
      else:
          Exception('File not found')

    if os.path.isfile(nii_file):
      if (np.isnan(volnum) or volnum == 'nan'):
        print 'getting atlas entry %s: image file %s'  %(idx,nii_file)
        img = nib.load(nii_file)
      else:
        print 'getting atlas entry %s: volume %s from image file %s'  %(idx,volnum,nii_file)
        img = index_img(nii_file,volnum)

    return img

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple',joblib_cache_dir='/tmp'):
    """
    Compute hit stats and store inside this object
    under 'name'
    """

    df_hit_stats = compute_vol_hit_stats(roi,self.vfms,self.bbox,
                                      idxs,n_jobs=n_jobs,
                                      atlas_name=self.atlas_name,
                                      atlas_dir=self.atlas_dir,
                                      run_type=run_type,
                                      joblib_cache_dir=joblib_cache_dir)

    #G_hit_stats = hit_stats_to_nx(df_hit_stats,self.Gnx,self.vfms)
    #return df_hit_stats,G_hit_stats
    return df_hit_stats

  def compute_scalar_stats(self,params,name):
    """
    Compute scalar stats and store inside this
    object under 'name'
    """

    res = compute_vol_scalar_stats(params)

    self.scalar_stats[name] = res

  def get_bbox_dimsizes(self):
    """
    Return lists of lengths between bounding box minima
    and maxima

    Usage:

    xdimsize,ydimsize,zdimsize = cw.get_bbox_dimsizes()
    """

    xdimsize = np.abs(self.bbox['xmax'] - self.bbox['xmin'])
    ydimsize = np.abs(self.bbox['ymax'] - self.bbox['ymin'])
    zdimsize = np.abs(self.bbox['zmax'] - self.bbox['zmin'])

    xdimsize = xdimsize.sort_values(ascending=False)
    ydimsize = ydimsize.sort_values(ascending=False)
    zdimsize = zdimsize.sort_values(ascending=False)

    return xdimsize,ydimsize,zdimsize

class VolTractAtlas(_VolAtlas):
  """
  Volumetric tract-based atlas base class
  """

  def __init__(self,atlas_name=None,atlas_dir=None):

    _VolAtlas.__init__(self,atlas_name=atlas_name,atlas_dir=atlas_dir)

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple',joblib_cache_dir='/tmp'):
    """
    Compute hit stats and store inside this object
      under 'name'
    """

    df_hit_stats = compute_vol_hit_stats(roi,self.vfms,self.bbox,
                                      idxs,n_jobs=n_jobs,
                                      atlas_name=self.atlas_name,
                                      atlas_dir = self.atlas_dir,
                                      run_type=run_type,joblib_cache_dir=joblib_cache_dir)
    return df_hit_stats

  def plot_tract(self,atlas_name):

    # (uses plotting param defaults tuned to JHU atlas)
    plot_tract()

class VolConnAtlas(_VolAtlas):
  """
  Volumetric connectivity-based atlas
  """

  def __init__(self,atlas_dir=None,atlas_name=None):

    _VolAtlas.__init__(self,atlas_dir=atlas_dir,atlas_name=atlas_name)

    # Load connectivity info

    ws,rls,tls,rxyzs,rnii,ctx,hs,rmfslh,rmfsrh = load_connectivity(atlas_name=atlas_name,atlas_dir=atlas_dir)

    self.weights = ws
    self.region_labels = rls

    if tls is not None: self.tract_lengths = tls
    if rxyzs is not None: self.region_xyzs = rxyzs
    if rnii is not None: self.region_nii = rnii
    if ctx is not None: self.cortex = ctx
    if hs is not None: self.hemispheres = hs
    if rmfslh is not None: self.region_mapping_fsav_lh = rmfslh
    if rmfsrh is not None: self.region_mapping_fsav_rh = rmfsrh

    # Compile node and connectivity info into a networkx graph
    self.Gnx = make_nx_graph(self.vfms,self.bbox,ws,rls,hs,ctx)

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple',joblib_cache_dir='/tmp'):
    """
    Compute hit stats and store inside this object
      under 'name'
    """

    df_hit_stats = compute_vol_hit_stats(roi,self.vfms,self.bbox,
                                      idxs,n_jobs=n_jobs,
                                      atlas_name=self.atlas_name,
                                      atlas_dir=self.atlas_dir,
                                      run_type=run_type,
                                      joblib_cache_dir=joblib_cache_dir)

    G_hit_stats = hit_stats_to_nx(df_hit_stats,self.Gnx,self.vfms)

    return df_hit_stats,G_hit_stats

  def get_rois_from_idx(self,idx):
    """
    Note: assumes that 'name' column in vfms is
          of the form 'roi1_to_roi2'
    """
    roi1,roi2 = self.vfms.ix[idx]['name'].split('_to_')
    roi1 = int(roi1)
    roi2 = int(roi2)

    return roi1,roi2

  def get_idx_from_rois(self,roi1,roi2):
    g = self.Gnx[int(roi1)][int(roi2)]
    idx = g['idx']
    return idx

  def get_vol_from_rois(self,roi1,roi2):

    idx = self.get_idx_from_rois(roi1,roi2)

    img = self.get_vol_from_vfm(idx)

    """
    vfm = self.vfms.ix[idx]
    nii_file = vfm['nii_file']
    volnum = vfm['4dvolind']

    if not os.path.isfile(nii_file):
      candidate = os.path.join(self.atlas_dir,nii_file)
      if os.path.isfile(candidate):
        nii_file = candidate
      else:
        Exception('file not found')

    if os.path.isfile(nii_file):
      if (np.isnan(volnum) or volnum == 'nan'):
        img = nib.load(nii_file)
      else:
        img = index_img(nii_file,volnum)

    return img
    """

    return img

  def modify_connectome(self, name='mc1',function=''):
    """
    Modify canonical connectome using hit or scalar
    stats, and store in this obj
    """

    res = modify_vol_connectome()

    self.modcons[name] = res

  def plot_network(self):

    plot_network()

  def plot_matrix(self):

    plot_matrix()

  def get_single_roi_img(self,roinum):
    """
    Return nifti image with just this roi
    """

    roinum = int(roinum)

    allrois_img = self.region_nii
    allrois_dat = allrois_img.get_data()

    thisroi_dat = (allrois_dat==roinum).astype('float32')
    thisroi_img = nib.Nifti1Image(thisroi_dat,allrois_img.affine)

    return thisroi_img

  def nl_plot_connvol(self,idx=None,roi1=None,roi2=None,add_rois=True,
                      nl_params = dict(threshold=0.1,cmap='Reds')):

    if idx:
      cnxn_img = self.get_vol_from_vfm(idx)
      roi1,roi2 = self.get_rois_from_idx(idx)
    else:
      cnxn_img = self.get_vol_from_rois(roi1,roi2)

    """
    if (roi1 != None) and (roi2 != None):

      roi1 = int(roi1)
      roi2 = int(roi2)

      str1 = '%s_to_%s' %(roi1,roi2)
      search1 = np.nonzero(vfms['name'] == str1)[0]

      str2 = '%s_to_%s' %(roi2,roi1)
      search2 = np.nonzero(vfms['name'] == str2)[0]

      if len(search1) > 0:
        idx = search1[0]
        cnxn_name = str1
      elif len(search2) > 0:
        idx = search2[0]
        cnxn_name = str2
      else:
        raise ValueError('connection not found')

    else:

      cnxn_name = vfms.ix[idx]['name']

      roi1 = int(vfms.ix[idx]['name'].split('_to_')[0])
      roi2 = int(vfms.ix[idx]['name'].split('_to_')[1])
    """

    display = plot_glass_brain(cnxn_img,**nl_params)

    if add_rois:

      roi1_img = self.get_single_roi_img(roi1)
      roi2_img = self.get_single_roi_img(roi2)

      display.add_contours(roi1_img, colors='Blue', linewidths=0.5, alpha=0.9)
      display.add_contours(roi2_img, colors='Green', linewidths=0.5, alpha=0.9)

    return display

class _StreamAtlas(_Atlas):
  """
  Streamlinetric atlas base class
  """

  def __init__(self,atlas_name=None,atlas_dir=None):

    self.atlas_name = atlas_name

    # Load streamlinetric atlas info

    self.sfms,self.atlas_dir = load_stream_file_mappings(atlas_name=atlas_name,atlas_dir=atlas_dir)
    self.bbox = load_stream_bboxes(atlas_name=atlas_name,atlas_dir=atlas_dir)

    self.dpy_file = '%s/atlas_streams.dpy' %(self.atlas_dir)

  def write_subset_to_trk(self,ref_file,outfile,stream_inds='all'):

    print 'writing streams to trk file: %s' %outfile
    dpy_to_trk(self.dpy_file,ref_file,outfile,inds=stream_inds)

class StreamTractAtlas(_StreamAtlas):
  """
  Streamlinetric tract-based atlas base class
  """

  def __init__(self,atlas_name=None,atlas_dir=None):

    _StreamAtlas.__init__(self,atlas_name=atlas_name,atlas_dir=atlas_dir)

class StreamConnAtlas(_StreamAtlas):
  """
  Streamlinetric connectome-based atlas base class
  """

  def __init__(self,atlas_name=None,atlas_dir=None):
    # A lot of this is shared between StreamConn atlas and VolConn atlas.
    # modify...

    _StreamAtlas.__init__(self,atlas_name=atlas_name,atlas_dir=atlas_dir)

    # Load connectivity info

    ws,rls,tls,rxyzs,rnii,ctx,hs,rmfslh,rmfsrh = load_connectivity(atlas_name=atlas_name,atlas_dir=atlas_dir)

    self.weights = ws
    self.region_labels = rls

    if tls is not None: self.tract_lengths = tls
    if rxyzs is not None: self.region_xyzs = rxyzs
    if rnii is not None: self.region_nii = rnii
    if ctx is not None: self.cortex = ctx
    if hs is not None: self.hemispheres = hs
    if rmfslh is not None: self.region_mapping_fsav_lh = rmfslh
    if rmfsrh is not None: self.region_mapping_fsav_rh = rmfsrh

    # Compile node and connectivity info into a networkx graph
    self.Gnx = make_nx_graph(self.sfms,self.bbox,ws,rls,hs,ctx)

  def get_rois_from_idx(self,idx):
    """
    Note: assumes that 'name' column in vfms is
          of the form 'roi1_to_roi2'
    """
    roi1,roi2 = self.sfms.ix[idx]['name'].split('_to_')
    roi1 = int(roi1)
    roi2 = int(roi2)

    return roi1,roi2

  def get_idx_from_rois(self,roi1,roi2):
    g = self.Gnx[int(roi1)][int(roi2)]
    idx = g['idx']
    return idx

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple',joblib_cache_dir='/tmp'):
    """
    Compute hit stats and store inside this object
    under 'name'
    """

    df_hit_stats = compute_streams_in_roi(roi,self.dpy_file,self.sfms,self.bbox,
                                          idxs,n_jobs=n_jobs,
                                          atlas_name=self.atlas_name,
                                          joblib_cache_dir=joblib_cache_dir)

    G_hit_stats = hit_stats_to_nx(df_hit_stats,self.Gnx,self.sfms)

    return df_hit_stats,G_hit_stats

  def modify_connectome(self, name='mc1',function=''):
    """
    Modify canonical connectome using hit or scalar
    stats, and store in this obj
    """

    res = modify_stream_connectome()

    self.modcons[name] = res

  def write_cnxn_to_trk(self,ref_file,outfile,idx=None,roi1=None,roi2=None):

    if not idx:
      idx = self.get_idx_from_rois(roi1,roi2)

    idxlist = self.sfms.ix[idx]['idxlist']

    self.write_subset_to_trk(ref_file,outfile,stream_inds=idxlist)

  def plot_network(self):

    plot_network()

  def plot_matrix(self):

    plot_matrix()

  def plot_cnxns(self):

    plot_stream_cnxns()

<commit_msg>no changes<commit_after>"""
Atlas class definitions
"""

# Author: John Griffiths
# License: simplified BSD

import os

import numpy as np
import nibabel as nib

from nilearn.image import index_img

from nilearn.plotting import plot_glass_brain

from utils.readers import (load_connectivity,load_vol_file_mappings,load_vol_bboxes,
                           load_stream_file_mappings,load_stream_bboxes,
                           make_nx_graph,dpy_to_trk)

from utils.stats import (compute_vol_hit_stats,compute_vol_scalar_stats,
                         compute_streams_in_roi,#compute_stream_hit_stats,compute_stream_scalar_stats,
                         hit_stats_to_nx)

#from viz.network import plot_network,plot_matrix
#from viz.volume import plot_vol_cnxn,plot_vol_tract
#from viz.streams import plot_stream_cnxn,plot_stream_tract

class _Atlas(): # object):
  """
  Base atlas class
  """

  def __init__(self):

    print 'blah'

class _VolAtlas(_Atlas):
  """
  Volumetric atlas class
  """

  def __init__(self,atlas_name=None,atlas_dir=None):

    self.atlas_name = atlas_name

    # Load volumetric atlas info

    #if atlas_dir:
    #  self.vfms,self.atlas_dir = load_vol_file_mappings(atlas_dir=atlas_dir)
    #  self.bbox = load_vol_bboxes(atlas_dir=atlas_dir)
    #else:
    #  self.vfms,self.atlas_dir = load_vol_file_mappings(atlas_name=atlas_name)
    #  self.bbox = load_vol_bboxes(atlas_name=atlas_name)
    self.vfms,self.atlas_dir = load_vol_file_mappings(atlas_name=atlas_name,atlas_dir=atlas_dir)
    self.bbox = load_vol_bboxes(atlas_name=atlas_name,atlas_dir=atlas_dir)

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple',joblib_cache_dir='/tmp'):
    """
    Compute hit stats and store inside this object
    under 'name'
    """

    df_hit_stats = compute_vol_hit_stats(roi,self.vfms,self.bbox,
                                      idxs,n_jobs=n_jobs,
                                      atlas_name=self.atlas_name,
                                      atlas_dir=self.atlas_dir,
                                      run_type=run_type,
                                      joblib_cache_dir=joblib_cache_dir)

    #G_hit_stats = hit_stats_to_nx(df_hit_stats,self.Gnx,self.vfms)
    #return df_hit_stats,G_hit_stats
    return df_hit_stats

  def compute_scalar_stats(self,params,name):
    """
    Compute scalar stats and store inside this
    object under 'name'
    """

    res = compute_vol_scalar_stats(params)

    self.scalar_stats[name] = res

  def get_bbox_dimsizes(self):
    """
    Return lists of lengths between bounding box minima
    and maxima

    Usage:

    xdimsize,ydimsize,zdimsize = cw.get_bbox_dimsizes()
    """

    xdimsize = np.abs(self.bbox['xmax'] - self.bbox['xmin'])
    ydimsize = np.abs(self.bbox['ymax'] - self.bbox['ymin'])
    zdimsize = np.abs(self.bbox['zmax'] - self.bbox['zmin'])

    xdimsize = xdimsize.sort_values(ascending=False)
    ydimsize = ydimsize.sort_values(ascending=False)
    zdimsize = zdimsize.sort_values(ascending=False)

    return xdimsize,ydimsize,zdimsize

class VolTractAtlas(_VolAtlas):
  """
  Volumetric tract-based atlas base class
  """

  def __init__(self,atlas_name=None,atlas_dir=None):

    _VolAtlas.__init__(self,atlas_name=atlas_name,atlas_dir=atlas_dir)

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple',joblib_cache_dir='/tmp'):
    """
    Compute hit stats and store inside this object
      under 'name'
    """

    df_hit_stats = compute_vol_hit_stats(roi,self.vfms,self.bbox,
                                      idxs,n_jobs=n_jobs,
                                      atlas_name=self.atlas_name,
                                      atlas_dir = self.atlas_dir,
                                      run_type=run_type,joblib_cache_dir=joblib_cache_dir)
    return df_hit_stats

  def plot_tract(self,atlas_name):

    # (uses plotting param defaults tuned to JHU atlas)
    plot_tract()

class VolConnAtlas(_VolAtlas):
  """
  Volumetric connectivity-based atlas
  """

  def __init__(self,atlas_dir=None,atlas_name=None):

    _VolAtlas.__init__(self,atlas_dir=atlas_dir,atlas_name=atlas_name)

    # Load connectivity info

    ws,rls,tls,rxyzs,rnii,ctx,hs,rmfslh,rmfsrh = load_connectivity(atlas_name=atlas_name,atlas_dir=atlas_dir)

    self.weights = ws
    self.region_labels = rls

    if tls is not None: self.tract_lengths = tls
    if rxyzs is not None: self.region_xyzs = rxyzs
    if rnii is not None: self.region_nii = rnii
    if ctx is not None: self.cortex = ctx
    if hs is not None: self.hemispheres = hs
    if rmfslh is not None: self.region_mapping_fsav_lh = rmfslh
    if rmfsrh is not None: self.region_mapping_fsav_rh = rmfsrh

    # Compile node and connectivity info into a networkx graph
    self.Gnx = make_nx_graph(self.vfms,self.bbox,ws,rls,hs,ctx)

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple',joblib_cache_dir='/tmp'):
    """
    Compute hit stats and store inside this object
      under 'name'
    """

    df_hit_stats = compute_vol_hit_stats(roi,self.vfms,self.bbox,
                                      idxs,n_jobs=n_jobs,
                                      atlas_name=self.atlas_name,
                                      atlas_dir=self.atlas_dir,
                                      run_type=run_type,
                                      joblib_cache_dir=joblib_cache_dir)

    G_hit_stats = hit_stats_to_nx(df_hit_stats,self.Gnx,self.vfms)

    return df_hit_stats,G_hit_stats

  def get_rois_from_idx(self,idx):
    """
    Note: assumes that 'name' column in vfms is
          of the form 'roi1_to_roi2'
    """
    roi1,roi2 = self.vfms.ix[idx]['name'].split('_to_')
    roi1 = int(roi1)
    roi2 = int(roi2)

    return roi1,roi2

  def get_idx_from_rois(self,roi1,roi2):
    g = self.Gnx[int(roi1)][int(roi2)]
    idx = g['idx']
    return idx

  def get_vol_from_rois(self,roi1,roi2):

    idx = self.get_idx_from_rois(roi1,roi2)

    img = self.get_vol_from_vfm(idx)

    """
    vfm = self.vfms.ix[idx]
    nii_file = vfm['nii_file']
    volnum = vfm['4dvolind']

    if not os.path.isfile(nii_file):
      candidate = os.path.join(self.atlas_dir,nii_file)
      if os.path.isfile(candidate):
        nii_file = candidate
      else:
        Exception('file not found')

    if os.path.isfile(nii_file):
      if (np.isnan(volnum) or volnum == 'nan'):
        img = nib.load(nii_file)
      else:
        img = index_img(nii_file,volnum)

    return img
    """

    return img

  def modify_connectome(self, name='mc1',function=''):
    """
    Modify canonical connectome using hit or scalar
    stats, and store in this obj
    """

    res = modify_vol_connectome()

    self.modcons[name] = res

  def plot_network(self):

    plot_network()

  def plot_matrix(self):

    plot_matrix()

  def get_single_roi_img(self,roinum):
    """
    Return nifti image with just this roi
    """

    roinum = int(roinum)

    allrois_img = self.region_nii
    allrois_dat = allrois_img.get_data()

    thisroi_dat = (allrois_dat==roinum).astype('float32')
    thisroi_img = nib.Nifti1Image(thisroi_dat,allrois_img.affine)

    return thisroi_img

  def nl_plot_connvol(self,idx=None,roi1=None,roi2=None,add_rois=True,
                      nl_params = dict(threshold=0.1,cmap='Reds')):

    if idx:
      cnxn_img = self.get_vol_from_vfm(idx)
      roi1,roi2 = self.get_rois_from_idx(idx)
    else:
      cnxn_img = self.get_vol_from_rois(roi1,roi2)

    """
    if (roi1 != None) and (roi2 != None):

      roi1 = int(roi1)
      roi2 = int(roi2)

      str1 = '%s_to_%s' %(roi1,roi2)
      search1 = np.nonzero(vfms['name'] == str1)[0]

      str2 = '%s_to_%s' %(roi2,roi1)
      search2 = np.nonzero(vfms['name'] == str2)[0]

      if len(search1) > 0:
        idx = search1[0]
        cnxn_name = str1
      elif len(search2) > 0:
        idx = search2[0]
        cnxn_name = str2
      else:
        raise ValueError('connection not found')

    else:

      cnxn_name = vfms.ix[idx]['name']

      roi1 = int(vfms.ix[idx]['name'].split('_to_')[0])
      roi2 = int(vfms.ix[idx]['name'].split('_to_')[1])
    """

    display = plot_glass_brain(cnxn_img,**nl_params)

    if add_rois:

      roi1_img = self.get_single_roi_img(roi1)
      roi2_img = self.get_single_roi_img(roi2)

      display.add_contours(roi1_img, colors='Blue', linewidths=0.5, alpha=0.9)
      display.add_contours(roi2_img, colors='Green', linewidths=0.5, alpha=0.9)

    return display

class _StreamAtlas(_Atlas):
  """
  Streamlinetric atlas base class
  """

  def __init__(self,atlas_name=None,atlas_dir=None):

    self.atlas_name = atlas_name

    # Load streamlinetric atlas info

    self.sfms,self.atlas_dir = load_stream_file_mappings(atlas_name=atlas_name,atlas_dir=atlas_dir)
    self.bbox = load_stream_bboxes(atlas_name=atlas_name,atlas_dir=atlas_dir)

    self.dpy_file = '%s/atlas_streams.dpy' %(self.atlas_dir)

  def write_subset_to_trk(self,ref_file,outfile,stream_inds='all'):

    print 'writing streams to trk file: %s' %outfile
    dpy_to_trk(self.dpy_file,ref_file,outfile,inds=stream_inds)

class StreamTractAtlas(_StreamAtlas):
  """
  Streamlinetric tract-based atlas base class
  """

  def __init__(self,atlas_name=None,atlas_dir=None):

    _StreamAtlas.__init__(self,atlas_name=atlas_name,atlas_dir=atlas_dir)

class StreamConnAtlas(_StreamAtlas):
  """
  Streamlinetric connectome-based atlas base class
  """

  def __init__(self,atlas_name=None,atlas_dir=None):
    # A lot of this is shared between StreamConn atlas and VolConn atlas.
    # modify...

    _StreamAtlas.__init__(self,atlas_name=atlas_name,atlas_dir=atlas_dir)

    # Load connectivity info

    ws,rls,tls,rxyzs,rnii,ctx,hs,rmfslh,rmfsrh = load_connectivity(atlas_name=atlas_name,atlas_dir=atlas_dir)

    self.weights = ws
    self.region_labels = rls

    if tls is not None: self.tract_lengths = tls
    if rxyzs is not None: self.region_xyzs = rxyzs
    if rnii is not None: self.region_nii = rnii
    if ctx is not None: self.cortex = ctx
    if hs is not None: self.hemispheres = hs
    if rmfslh is not None: self.region_mapping_fsav_lh = rmfslh
    if rmfsrh is not None: self.region_mapping_fsav_rh = rmfsrh

    # Compile node and connectivity info into a networkx graph
    self.Gnx = make_nx_graph(self.sfms,self.bbox,ws,rls,hs,ctx)

  def get_rois_from_idx(self,idx):
    """
    Note: assumes that 'name' column in vfms is
          of the form 'roi1_to_roi2'
    """
    roi1,roi2 = self.sfms.ix[idx]['name'].split('_to_')
    roi1 = int(roi1)
    roi2 = int(roi2)

    return roi1,roi2

  def get_idx_from_rois(self,roi1,roi2):
    g = self.Gnx[int(roi1)][int(roi2)]
    idx = g['idx']
    return idx

  def compute_hit_stats(self,roi,idxs,n_jobs=1,run_type='simple',joblib_cache_dir='/tmp'):
    """
    Compute hit stats and store inside this object
    under 'name'
    """

    df_hit_stats = compute_streams_in_roi(roi,self.dpy_file,self.sfms,self.bbox,
                                          idxs,n_jobs=n_jobs,
                                          atlas_name=self.atlas_name,
                                          joblib_cache_dir=joblib_cache_dir)

    G_hit_stats = hit_stats_to_nx(df_hit_stats,self.Gnx,self.sfms)

    return df_hit_stats,G_hit_stats

  def modify_connectome(self, name='mc1',function=''):
    """
    Modify canonical connectome using hit or scalar
    stats, and store in this obj
    """

    res = modify_stream_connectome()

    self.modcons[name] = res

  def write_cnxn_to_trk(self,ref_file,outfile,idx=None,roi1=None,roi2=None):

    if not idx:
      idx = self.get_idx_from_rois(roi1,roi2)

    idxlist = self.sfms.ix[idx]['idxlist']

    self.write_subset_to_trk(ref_file,outfile,stream_inds=idxlist)

  def plot_network(self):

    plot_network()

  def plot_matrix(self):

    plot_matrix()

  def plot_cnxns(self):

    plot_stream_cnxns()

<|endoftext|>


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

###
# Copyright (c) 2002, Jeremiah Fincher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions, and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions, and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the author of this software nor the name of
#     contributors to this software may be used to endorse or promote products
#     derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###

"""
Add the module docstring here.  This will be used by the setup.py script.
"""

import string

import conf
import ircdb
import ircmsgs
import privmsgs
import callbacks

class AdminCommands(privmsgs.CapabilityCheckingPrivmsg):
    capability = 'admin'
    def join(self, irc, msg, args):
        """<channel> [<channel> ...]

        Tell the bot to join the whitespace-separated list of channels
        you give it.  If a channel requires a key, attach it behind the
        channel name via a comma.  I.e., if you need to join both #a and #b,
        and #a requires a key of 'aRocks', then you'd call 'join #a,aRocks #b'
        """
        keys = []
        channels = []
        for s in args:
            if ',' in s:
                (channel, key) = s.split(',', 1)
                channels.insert(0, channel)
                keys.insert(0, key)
            else:
                channels.append(channel)
        irc.queueMsg(ircmsgs.joins(channels, keys))
        for channel in channels:
            irc.queueMsg(ircmsgs.who(channel))

    def nick(self, irc, msg, args):
        """<nick>

        Changes the bot's nick to <nick>."""
        nick = privmsgs.getArgs(args)
        irc.queueMsg(ircmsgs.nick(nick))

    def part(self, irc, msg, args):
        """<channel> [<channel> ...]

        Tells the bot to part the whitespace-separated list of channels
        you give it.
        """
        irc.queueMsg(ircmsgs.parts(args, msg.nick))

    def disable(self, irc, msg, args):
        """<command>

        Disables the command <command> for all non-owner users.
        """
        command = privmsgs.getArgs(args)
        if command in ('enable', 'identify'):
            irc.error(msg, 'You can\'t disable %s!' % command)
        else:
            # This has to know that defaultCapabilties gets turned into a
            # dictionary.
            if command in conf.defaultCapabilities:
                conf.defaultCapabilities.remove(command)
            capability = ircdb.makeAntiCapability(command)
            conf.defaultCapabilities.add(capability)
            irc.reply(msg, conf.replySuccess)

    def enable(self, irc, msg, args):
        """<command>

        Re-enables the command <command> for all non-owner users.
        """
        command = privmsgs.getArgs(args)
        anticapability = ircdb.makeAntiCapability(command)
        if anticapability in conf.defaultCapabilities:
            conf.defaultCapabilities.remove(anticapability)
            irc.reply(msg, conf.replySuccess)
        else:
            irc.error(msg, 'That command wasn\'t disabled.')

    def addcapability(self, irc, msg, args):
        """<name|hostmask> <capability>

        Gives the user specified by <name> (or the user to whom <hostmask>
        currently maps) the specified capability <capability>
        """
        (name, capability) = privmsgs.getArgs(args, 2)
        # This next check to make sure 'admin's can't hand out 'owner'.
        if ircdb.checkCapability(msg.prefix, capability) or \
           '!' in capability:
            try:
                u = ircdb.users.getUser(name)
                u.addCapability(capability)
                ircdb.users.setUser(name, u)
                irc.reply(msg, conf.replySuccess)
            except KeyError:
                irc.error(msg, conf.replyNoUser)
        else:
            s = 'You can\'t add capabilities you don\'t have.'
            irc.error(msg, s)

    def removecapability(self, irc, msg, args):
        """<name|hostmask> <capability>

        Takes from the user specified by <name> (or the uswer to whom
        <hostmask> currently maps) the specified capability <capability>
        """
        (name, capability) = privmsgs.getArgs(args, 2)
        if ircdb.checkCapability(msg.prefix, capability) or \
           '!' in capability:
            try:
                u = ircdb.users.getUser(name)
                u.addCapability(capability)
                ircdb.users.setUser(name, u)
                irc.reply(msg, conf.replySuccess)
            except KeyError:
                irc.error(msg, conf.replyNoUser)
        else:
            s = 'You can\'t remove capabilities you don\'t have.'
            irc.error(msg, s)

    def setprefixchar(self, irc, msg, args):
        """<prefixchars>

        Sets the prefix chars by which the bot can be addressed.
        """
        s = privmsgs.getArgs(args)
        if s.translate(string.ascii, string.ascii_letters) == '':
            irc.error(msg, 'Prefixes cannot contain letters.')
        else:
            conf.prefixChars = s
            irc.reply(msg, conf.replySuccess)

Class = AdminCommands

# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
<commit_msg>Wrong variable in join command.<commit_after>#!/usr/bin/env python

###
# Copyright (c) 2002, Jeremiah Fincher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions, and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions, and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the author of this software nor the name of
#     contributors to this software may be used to endorse or promote products
#     derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###

"""
Add the module docstring here.  This will be used by the setup.py script.
"""

import string

import conf
import ircdb
import ircmsgs
import privmsgs
import callbacks

class AdminCommands(privmsgs.CapabilityCheckingPrivmsg):
    capability = 'admin'
    def join(self, irc, msg, args):
        """<channel> [<channel> ...]

        Tell the bot to join the whitespace-separated list of channels
        you give it.  If a channel requires a key, attach it behind the
        channel name via a comma.  I.e., if you need to join both #a and #b,
        and #a requires a key of 'aRocks', then you'd call 'join #a,aRocks #b'
        """
        keys = []
        channels = []
        for channel in args:
            if ',' in s:
                (channel, key) = s.split(',', 1)
                channels.insert(0, channel)
                keys.insert(0, key)
            else:
                channels.append(channel)
        irc.queueMsg(ircmsgs.joins(channels, keys))
        for channel in channels:
            irc.queueMsg(ircmsgs.who(channel))

    def nick(self, irc, msg, args):
        """<nick>

        Changes the bot's nick to <nick>."""
        nick = privmsgs.getArgs(args)
        irc.queueMsg(ircmsgs.nick(nick))

    def part(self, irc, msg, args):
        """<channel> [<channel> ...]

        Tells the bot to part the whitespace-separated list of channels
        you give it.
        """
        irc.queueMsg(ircmsgs.parts(args, msg.nick))

    def disable(self, irc, msg, args):
        """<command>

        Disables the command <command> for all non-owner users.
        """
        command = privmsgs.getArgs(args)
        if command in ('enable', 'identify'):
            irc.error(msg, 'You can\'t disable %s!' % command)
        else:
            # This has to know that defaultCapabilties gets turned into a
            # dictionary.
            if command in conf.defaultCapabilities:
                conf.defaultCapabilities.remove(command)
            capability = ircdb.makeAntiCapability(command)
            conf.defaultCapabilities.add(capability)
            irc.reply(msg, conf.replySuccess)

    def enable(self, irc, msg, args):
        """<command>

        Re-enables the command <command> for all non-owner users.
        """
        command = privmsgs.getArgs(args)
        anticapability = ircdb.makeAntiCapability(command)
        if anticapability in conf.defaultCapabilities:
            conf.defaultCapabilities.remove(anticapability)
            irc.reply(msg, conf.replySuccess)
        else:
            irc.error(msg, 'That command wasn\'t disabled.')

    def addcapability(self, irc, msg, args):
        """<name|hostmask> <capability>

        Gives the user specified by <name> (or the user to whom <hostmask>
        currently maps) the specified capability <capability>
        """
        (name, capability) = privmsgs.getArgs(args, 2)
        # This next check to make sure 'admin's can't hand out 'owner'.
        if ircdb.checkCapability(msg.prefix, capability) or \
           '!' in capability:
            try:
                u = ircdb.users.getUser(name)
                u.addCapability(capability)
                ircdb.users.setUser(name, u)
                irc.reply(msg, conf.replySuccess)
            except KeyError:
                irc.error(msg, conf.replyNoUser)
        else:
            s = 'You can\'t add capabilities you don\'t have.'
            irc.error(msg, s)

    def removecapability(self, irc, msg, args):
        """<name|hostmask> <capability>

        Takes from the user specified by <name> (or the uswer to whom
        <hostmask> currently maps) the specified capability <capability>
        """
        (name, capability) = privmsgs.getArgs(args, 2)
        if ircdb.checkCapability(msg.prefix, capability) or \
           '!' in capability:
            try:
                u = ircdb.users.getUser(name)
                u.addCapability(capability)
                ircdb.users.setUser(name, u)
                irc.reply(msg, conf.replySuccess)
            except KeyError:
                irc.error(msg, conf.replyNoUser)
        else:
            s = 'You can\'t remove capabilities you don\'t have.'
            irc.error(msg, s)

    def setprefixchar(self, irc, msg, args):
        """<prefixchars>

        Sets the prefix chars by which the bot can be addressed.
        """
        s = privmsgs.getArgs(args)
        if s.translate(string.ascii, string.ascii_letters) == '':
            irc.error(msg, 'Prefixes cannot contain letters.')
        else:
            conf.prefixChars = s
            irc.reply(msg, conf.replySuccess)

Class = AdminCommands

# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
<|endoftext|>


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

import argparse
import datetime
import http.server
import json
import os
import os.path
import shutil
import socketserver
import subprocess
import tempfile
import urllib.parse

import lxml.etree
import lxml.html

import paths

HTMLCOMPRESSOR_URL = "https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/htmlcompressor/htmlcompressor-1.5.3.jar"
YUICOMPRESSOR_URL = "https://github.com/yui/yuicompressor/releases/download/v2.4.8/yuicompressor-2.4.8.jar"

SCRIPTS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))

class Chdir():

  def __init__(self, path):
    self._path = os.path.abspath(path)

  def __enter__(self):
    self._previous = os.getcwd()
    os.chdir(self._path)

  def __exit__(self, exc_type, exc_val, exc_tb):
    os.chdir(self._previous)

def git_sha(repository):
  with Chdir(repository):
    return subprocess.check_output(["git", "rev-parse", "HEAD"]).strip()

def extract_javascript(html, root):
  scripts = ""
  transactions = html.xpath("//script[@type='text/javascript']")
  for transaction in transactions:
    src = os.path.join(root, transaction.get("src"))
    with open(src) as script:
      scripts = scripts + "\n" + script.read()
    transaction.drop_tree()

  filtered = ""
  for line in scripts.split("\n"):
    if line.find('"use strict";') != 0:
      filtered = filtered + "\n" + line

  return filtered

def extract_tags(html, tag, key, root):
  scripts = ""
  transactions = html.xpath(tag)
  for transaction in transactions:
    src = os.path.join(root, transaction.get(key))
    with open(src) as script:
      scripts = scripts + "\n" + script.read()
    transaction.drop_tree()
  return scripts

def append_javascript(html, script):
  body = html.find('body')
  source = "<script type='text/javascript' type='text/css'>" + script + "</script>"
  link = lxml.html.fromstring(source).find('.//script')
  body.append(link)

def append_style(html, style):
  head = html.find('head')
  tag = "<style>" + style + "</style>"
  style = lxml.html.fromstring(tag).find('.//style')
  head.append(style)

def env(filename):
  for path in os.environ["PATH"].split(":"):
    try:
      if filename in os.listdir(path):
        return os.path.join(path, filename)
    except FileNotFoundError:
      pass
  raise KeyError("Unable to find '%s' in path." % filename)

def first_command(commands):
  for command in commands:
    try:
      return env(command)
    except KeyError:
      pass
  quoted_commands = ["'%s'" % command for command in commands]
  summary = quoted_commands[0]
  if len(quoted_commands) > 1:
    summary = "%s or %s" % (", ".join(quoted_commands[:-1]), quoted_commands[-1])
  raise KeyError("Unable to find %s in path." % summary)

def yuicompressor(contents, suffix):
  temp = tempfile.mktemp(suffix=suffix)
  with open(temp, 'w') as f:
    f.write(contents)
  yuicompressor_path = None
  command = []
  try:
    yuicompressor_path = first_command(["yuicompressor", "yui-compressor"])
    command = [yuicompressor_path]
  except KeyError as e:
    yuicompressor_path = download(YUICOMPRESSOR_URL, SCRIPTS_DIRECTORY)
    command = ["java", "-jar", yuicompressor_path]
  output = subprocess.check_output(command + [temp]).decode('utf-8')
  os.unlink(temp)
  return output

def download(url, directory):
  basename = os.path.basename(urllib.parse.urlparse(url).path)
  path = os.path.join(directory, basename)
  if not os.path.exists(path):
    print("Downloading '%s'..." % basename)
    subprocess.check_call(["curl", "-L", "-o", path, url])
  return path

def htmlcompressor(contents):
  htmlcompressor_path = download(HTMLCOMPRESSOR_URL, SCRIPTS_DIRECTORY)
  command = ['java', '-jar', htmlcompressor_path]
  p = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
  output = p.communicate(input=contents)[0].decode('utf-8')
  return output

def pngcrush(source_file, destination_file):
  print("Crushing '%s'..." % source_file)
  subprocess.check_call(['pngcrush', source_file, destination_file])

def find_files(directory):
  result = []
  for root, subdirs, files in os.walk(directory):
    for file in files:
      if file.find('.') != 0:
        result.append(os.path.relpath(os.path.join(root, file), directory))
  return result

def copy_diretory(source_dir, output_dir, task=shutil.copy):
  name = os.path.basename(os.path.abspath(source_dir))
  destination_dir = os.path.join(output_dir, name)
  os.mkdir(destination_dir)
  files = find_files(source_dir)
  for file in files:
    image_name = os.path.basename(os.path.abspath(file))
    destination_file = os.path.join(destination_dir, image_name)
    task(os.path.join(source_dir, file), destination_file)

def copy_files(source, destination, files):
  for file in files:
    source_file = os.path.join(source, file)
    destination_file = os.path.join(destination, file)
    shutil.copy(source_file, destination_file)

def load_settings(path):
  version = None
  with open(paths.VERSION_FILE, 'r') as f:
    version = f.read().strip()
  settings = None
  with open(path, 'r') as f:
    settings = json.load(f)
  settings["version"] = version
  return settings

def build(options):
  paths.BUILD_DIR = os.path.join(paths.ROOT_DIR, "build")
  archives_dir = os.path.join(paths.ROOT_DIR, "archives")
  sha_file = os.path.join(paths.BUILD_DIR, "sha.txt")
  input_file = os.path.join(paths.SOURCE_DIR, "index.html")
  output_file = os.path.join(paths.BUILD_DIR, "index.html")
  images_dir = os.path.join(paths.SOURCE_DIR, "images")
  assets_dir = os.path.join(paths.SOURCE_DIR, "assets")
  defaults_dir = os.path.join(paths.SOURCE_DIR, "defaults")
  manifest_file = os.path.join(paths.BUILD_DIR, "cache.manifest")

  settings = load_settings(path=os.path.abspath(options.settings))

  # Create/empty the build directory.
  # We do not simply delete the directory so as not to break the development server which might be serving from here.
  if os.path.exists(paths.BUILD_DIR):
    for path in os.listdir(paths.BUILD_DIR):
      path = os.path.join(paths.BUILD_DIR, path)
      if os.path.isfile(path):
        os.unlink(path)
      else:
        shutil.rmtree(path)
  else:
    os.mkdir(paths.BUILD_DIR)

  # git sha
  sha = git_sha(repository=paths.ROOT_DIR)
  with open(sha_file, "w") as f:
    f.write("%s\n" % sha)

  # index.html
  contents = None
  with open(input_file) as f:
    contents = f.read()
  html = lxml.html.fromstring(contents)

  print("Extracting JavaScript...")
  script = "window.config = %s;\n" % json.dumps(settings)
  script += extract_tags(html, "//script[@type='text/javascript']", "src", paths.SOURCE_DIR)
  if not settings["debug"]:
    print("Minifying JavaScript...")
    script = yuicompressor(script, '.js')
  append_javascript(html, script)

  print("Exctracting CSS...")
  style = extract_tags(html, "//link[@type='text/css']", "href", paths.SOURCE_DIR)
  if not settings["debug"]:
    print("Minifying CSS...")
    style = yuicompressor(style, '.css')
  append_style(html, style)

  contents = lxml.html.tostring(html)
  if not settings["debug"]:
    print("Compressing HTML...")
    contents = htmlcompressor(contents)

  print("Writing HTML...")
  with open(output_file, 'w') as f:
    f.write("<!DOCTYPE html>\n")
    f.write(contents)

  # images
  print("Copying images...")
  copy_diretory(images_dir, paths.BUILD_DIR, pngcrush)

  # images
  print("Copying assets...")
  copy_diretory(assets_dir, paths.BUILD_DIR)

  # defaults
  print("Copying defaults...")
  copy_diretory(defaults_dir, paths.BUILD_DIR, pngcrush)

  # icon
  icon_file = os.path.join(paths.ROOT_DIR, settings['icon'])
  pngcrush(icon_file, os.path.join(paths.BUILD_DIR, "images", "icon.png"))

  # manifest
  manifest = find_files(paths.BUILD_DIR)
  with open(manifest_file, 'w') as f:
    f.write("CACHE MANIFEST\n")
    f.write("# %s\n" % str(datetime.datetime.now()))
    f.write("CACHE:\n")
    f.write("\n".join(map(lambda x: x, manifest)))
    f.write("\n")
    f.write("NETWORK:\n")
    f.write("*\n")

  # We don't want the following files to be added to the manifest.
  copy_files(paths.SOURCE_DIR, paths.BUILD_DIR, ["version.txt", "release.txt", "sizes.html"])

  # Archive the build.
  if not os.path.exists(archives_dir):
    os.makedirs(archives_dir)
  settings_name = os.path.splitext(os.path.basename(options.settings))[0]
  with Chdir(paths.ROOT_DIR):
    subprocess.check_call(["tar", "-zcf", os.path.join(archives_dir, "build-%s-%s.tar.gz" % (sha, settings_name)), "build"])

def command_build(parser):
  parser.add_argument("settings", help="settings file")
  return build;

def command_serve(parser):
    parser.add_argument("--port", default=8000, type=int, help="Listening port.")

    def inner(options):
      httpd = SocketServer.TCPServer(("", options.port), SimpleHTTPServer.SimpleHTTPRequestHandler)
      print("Serving on http://127.0.0.1:%d..." % options.port)
      os.chdir(paths.BUILD_DIR)
      httpd.serve_forever()

    return inner

def command_deploy(parser):
  parser.add_argument("settings", help="settings file")

  def inner(options):
    build(options)
    settings = load_settings(path=os.path.abspath(options.settings))

    if (settings["deploy"]["confirm"]):
      print("Deploy to '%s'? [y/N]" % settings["deploy"]["domain"])
      choice = raw_input().lower()
      if choice != "y":
        exit("Abort.")

    with Chdir(paths.ANSIBLE_DIR):
      subprocess.check_call(["ansible-playbook", "gameplay.yml",
                             "--extra-vars", json.dumps(settings["deploy"])])

  return inner

def add_command(subparsers, name, command, help=""):
  parser = subparsers.add_parser(name, help=help)
  fn = command(parser)
  parser.set_defaults(fn=fn)

def main():
  parser = argparse.ArgumentParser(description="Build script for Game Play.")
  subparsers = parser.add_subparsers(help="Command to run.")
  add_command(subparsers, name="build", command=command_build, help="Build the project.")
  add_command(subparsers, name="serve", command=command_serve, help="Run a local server.")
  add_command(subparsers, name="deploy", command=command_deploy, help="Deploy the project.")
  options = parser.parse_args()
  options.fn(options)

if __name__ == '__main__':
  main()
<commit_msg>Don't bother to use pngcrush as it doesn't make a significant difference to file sizes when considering modern connection speeds.<commit_after>#!/usr/bin/env python3

import argparse
import datetime
import http.server
import json
import os
import os.path
import shutil
import socketserver
import subprocess
import tempfile
import urllib.parse

import lxml.etree
import lxml.html

import paths

HTMLCOMPRESSOR_URL = "https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/htmlcompressor/htmlcompressor-1.5.3.jar"
YUICOMPRESSOR_URL = "https://github.com/yui/yuicompressor/releases/download/v2.4.8/yuicompressor-2.4.8.jar"

SCRIPTS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))

class Chdir():

  def __init__(self, path):
    self._path = os.path.abspath(path)

  def __enter__(self):
    self._previous = os.getcwd()
    os.chdir(self._path)

  def __exit__(self, exc_type, exc_val, exc_tb):
    os.chdir(self._previous)

def git_sha(repository):
  with Chdir(repository):
    return subprocess.check_output(["git", "rev-parse", "HEAD"]).strip()

def extract_javascript(html, root):
  scripts = ""
  transactions = html.xpath("//script[@type='text/javascript']")
  for transaction in transactions:
    src = os.path.join(root, transaction.get("src"))
    with open(src) as script:
      scripts = scripts + "\n" + script.read()
    transaction.drop_tree()

  filtered = ""
  for line in scripts.split("\n"):
    if line.find('"use strict";') != 0:
      filtered = filtered + "\n" + line

  return filtered

def extract_tags(html, tag, key, root):
  scripts = ""
  transactions = html.xpath(tag)
  for transaction in transactions:
    src = os.path.join(root, transaction.get(key))
    with open(src) as script:
      scripts = scripts + "\n" + script.read()
    transaction.drop_tree()
  return scripts

def append_javascript(html, script):
  body = html.find('body')
  source = "<script type='text/javascript' type='text/css'>" + script + "</script>"
  link = lxml.html.fromstring(source).find('.//script')
  body.append(link)

def append_style(html, style):
  head = html.find('head')
  tag = "<style>" + style + "</style>"
  style = lxml.html.fromstring(tag).find('.//style')
  head.append(style)

def env(filename):
  for path in os.environ["PATH"].split(":"):
    try:
      if filename in os.listdir(path):
        return os.path.join(path, filename)
    except FileNotFoundError:
      pass
  raise KeyError("Unable to find '%s' in path." % filename)

def first_command(commands):
  for command in commands:
    try:
      return env(command)
    except KeyError:
      pass
  quoted_commands = ["'%s'" % command for command in commands]
  summary = quoted_commands[0]
  if len(quoted_commands) > 1:
    summary = "%s or %s" % (", ".join(quoted_commands[:-1]), quoted_commands[-1])
  raise KeyError("Unable to find %s in path." % summary)

def yuicompressor(contents, suffix):
  temp = tempfile.mktemp(suffix=suffix)
  with open(temp, 'w') as f:
    f.write(contents)
  yuicompressor_path = None
  command = []
  try:
    yuicompressor_path = first_command(["yuicompressor", "yui-compressor"])
    command = [yuicompressor_path]
  except KeyError as e:
    yuicompressor_path = download(YUICOMPRESSOR_URL, SCRIPTS_DIRECTORY)
    command = ["java", "-jar", yuicompressor_path]
  output = subprocess.check_output(command + [temp]).decode('utf-8')
  os.unlink(temp)
  return output

def download(url, directory):
  basename = os.path.basename(urllib.parse.urlparse(url).path)
  path = os.path.join(directory, basename)
  if not os.path.exists(path):
    print("Downloading '%s'..." % basename)
    subprocess.check_call(["curl", "-L", "-o", path, url])
  return path

def htmlcompressor(contents):
  htmlcompressor_path = download(HTMLCOMPRESSOR_URL, SCRIPTS_DIRECTORY)
  command = ['java', '-jar', htmlcompressor_path]
  p = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
  output = p.communicate(input=contents)[0].decode('utf-8')
  return output

def find_files(directory):
  result = []
  for root, subdirs, files in os.walk(directory):
    for file in files:
      if file.find('.') != 0:
        result.append(os.path.relpath(os.path.join(root, file), directory))
  return result

def copy_diretory(source_dir, output_dir, task=shutil.copy):
  name = os.path.basename(os.path.abspath(source_dir))
  destination_dir = os.path.join(output_dir, name)
  os.mkdir(destination_dir)
  files = find_files(source_dir)
  for file in files:
    image_name = os.path.basename(os.path.abspath(file))
    destination_file = os.path.join(destination_dir, image_name)
    task(os.path.join(source_dir, file), destination_file)

def copy_files(source, destination, files):
  for file in files:
    source_file = os.path.join(source, file)
    destination_file = os.path.join(destination, file)
    shutil.copy(source_file, destination_file)

def load_settings(path):
  version = None
  with open(paths.VERSION_FILE, 'r') as f:
    version = f.read().strip()
  settings = None
  with open(path, 'r') as f:
    settings = json.load(f)
  settings["version"] = version
  return settings

def build(options):
  paths.BUILD_DIR = os.path.join(paths.ROOT_DIR, "build")
  archives_dir = os.path.join(paths.ROOT_DIR, "archives")
  sha_file = os.path.join(paths.BUILD_DIR, "sha.txt")
  input_file = os.path.join(paths.SOURCE_DIR, "index.html")
  output_file = os.path.join(paths.BUILD_DIR, "index.html")
  images_dir = os.path.join(paths.SOURCE_DIR, "images")
  assets_dir = os.path.join(paths.SOURCE_DIR, "assets")
  defaults_dir = os.path.join(paths.SOURCE_DIR, "defaults")
  manifest_file = os.path.join(paths.BUILD_DIR, "cache.manifest")

  settings = load_settings(path=os.path.abspath(options.settings))

  # Create/empty the build directory.
  # We do not simply delete the directory so as not to break the development server which might be serving from here.
  if os.path.exists(paths.BUILD_DIR):
    for path in os.listdir(paths.BUILD_DIR):
      path = os.path.join(paths.BUILD_DIR, path)
      if os.path.isfile(path):
        os.unlink(path)
      else:
        shutil.rmtree(path)
  else:
    os.mkdir(paths.BUILD_DIR)

  # git sha
  sha = git_sha(repository=paths.ROOT_DIR)
  with open(sha_file, "w") as f:
    f.write("%s\n" % sha)

  # index.html
  contents = None
  with open(input_file) as f:
    contents = f.read()
  html = lxml.html.fromstring(contents)

  print("Extracting JavaScript...")
  script = "window.config = %s;\n" % json.dumps(settings)
  script += extract_tags(html, "//script[@type='text/javascript']", "src", paths.SOURCE_DIR)
  if not settings["debug"]:
    print("Minifying JavaScript...")
    script = yuicompressor(script, '.js')
  append_javascript(html, script)

  print("Exctracting CSS...")
  style = extract_tags(html, "//link[@type='text/css']", "href", paths.SOURCE_DIR)
  if not settings["debug"]:
    print("Minifying CSS...")
    style = yuicompressor(style, '.css')
  append_style(html, style)

  contents = lxml.html.tostring(html)
  if not settings["debug"]:
    print("Compressing HTML...")
    contents = htmlcompressor(contents)

  print("Writing HTML...")
  with open(output_file, 'w') as f:
    f.write("<!DOCTYPE html>\n")
    f.write(contents)

  # images
  print("Copying images...")
  copy_diretory(images_dir, paths.BUILD_DIR)

  # images
  print("Copying assets...")
  copy_diretory(assets_dir, paths.BUILD_DIR)

  # defaults
  print("Copying defaults...")
  copy_diretory(defaults_dir, paths.BUILD_DIR)

  # icon
  icon_file = os.path.join(paths.ROOT_DIR, settings['icon'])
  shutil.copy(icon_file, os.path.join(paths.BUILD_DIR, "images", "icon.png"))

  # manifest
  manifest = find_files(paths.BUILD_DIR)
  with open(manifest_file, 'w') as f:
    f.write("CACHE MANIFEST\n")
    f.write("# %s\n" % str(datetime.datetime.now()))
    f.write("CACHE:\n")
    f.write("\n".join(map(lambda x: x, manifest)))
    f.write("\n")
    f.write("NETWORK:\n")
    f.write("*\n")

  # We don't want the following files to be added to the manifest.
  copy_files(paths.SOURCE_DIR, paths.BUILD_DIR, ["version.txt", "release.txt", "sizes.html"])

  # Archive the build.
  if not os.path.exists(archives_dir):
    os.makedirs(archives_dir)
  settings_name = os.path.splitext(os.path.basename(options.settings))[0]
  with Chdir(paths.ROOT_DIR):
    subprocess.check_call(["tar", "-zcf", os.path.join(archives_dir, "build-%s-%s.tar.gz" % (sha, settings_name)), "build"])

def command_build(parser):
  parser.add_argument("settings", help="settings file")
  return build;

def command_serve(parser):
    parser.add_argument("--port", default=8000, type=int, help="Listening port.")

    def inner(options):
      httpd = SocketServer.TCPServer(("", options.port), SimpleHTTPServer.SimpleHTTPRequestHandler)
      print("Serving on http://127.0.0.1:%d..." % options.port)
      os.chdir(paths.BUILD_DIR)
      httpd.serve_forever()

    return inner

def command_deploy(parser):
  parser.add_argument("settings", help="settings file")

  def inner(options):
    build(options)
    settings = load_settings(path=os.path.abspath(options.settings))

    if (settings["deploy"]["confirm"]):
      print("Deploy to '%s'? [y/N]" % settings["deploy"]["domain"])
      choice = raw_input().lower()
      if choice != "y":
        exit("Abort.")

    with Chdir(paths.ANSIBLE_DIR):
      subprocess.check_call(["ansible-playbook", "gameplay.yml",
                             "--extra-vars", json.dumps(settings["deploy"])])

  return inner

def add_command(subparsers, name, command, help=""):
  parser = subparsers.add_parser(name, help=help)
  fn = command(parser)
  parser.set_defaults(fn=fn)

def main():
  parser = argparse.ArgumentParser(description="Build script for Game Play.")
  subparsers = parser.add_subparsers(help="Command to run.")
  add_command(subparsers, name="build", command=command_build, help="Build the project.")
  add_command(subparsers, name="serve", command=command_serve, help="Run a local server.")
  add_command(subparsers, name="deploy", command=command_deploy, help="Deploy the project.")
  options = parser.parse_args()
  options.fn(options)

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