<commit_before>from django.core.management.base import BaseCommand

from django_bundles.conf.bundles_settings import bundles_settings
from django_bundles.core import get_bundles, set_bundle_versions
from django_bundles.processors import processor_pipeline, processor_library
from django_bundles.utils.files import FileChunkGenerator
from django_bundles.utils.processes import run_process

import os
from hashlib import md5
from tempfile import NamedTemporaryFile

def iter_bundle_files(bundle, debug=False):
    for bundle_file in bundle.files:
        for chunk in processor_pipeline(bundle_file.processors, FileChunkGenerator(open(bundle_file.file_path, 'rb')), debug=debug):
            yield chunk
        yield '\n'

def make_bundle(bundle, debug=False):
    """
    Does all of the processing required to create a bundle and write it to disk, returning its hash version
    """
    tmp_output_file_name = '%s.%s.%s' % (os.path.join(bundle.bundle_file_root, bundle.bundle_filename), 'temp', bundle.bundle_type)
    tmp_source_map_url = bundle.get_url()
    tmp_source_map_processed_files = []

    try:
        if bundle.uglify_command:
            infile_list = []

            for bundle_file in bundle.files:
                if bundle_file.processors:
                    # for now preprocessed files are written to temp files and therefore won't be available in the source map
                    output_pipeline = processor_pipeline(bundle_file.processors, FileChunkGenerator(open(bundle_file.file_path, 'rb')), debug=debug)
                    tmp_input_file = NamedTemporaryFile()
                    tmp_source_map_processed_files.append(tmp_input_file)
                    for chunk in output_pipeline:
                        tmp_input_file.write(chunk)
                    tmp_input_file.seek(0)
                    infile_list.append(tmp_input_file.name)
                else:
                    infile_list.append(bundle_file.file_path)

            source_map_options = [
                '--source-map %s.map' % tmp_output_file_name,
                '--source-map-root %s' % bundle.files_url_root,
                '--source-map-url %s.map' % tmp_source_map_url,
                '-p %s' % os.path.realpath(bundle.files_root).count('/')
            ]
            output_pipeline = run_process(bundle.uglify_command.format(infile_list=' '.join(infile_list), source_map_options=' '.join(source_map_options)))
        else:
            iter_input = iter_bundle_files(bundle, debug=debug)

            output_pipeline = processor_pipeline(bundle.processors, iter_input, debug=debug)

        m = md5()

        with open(tmp_output_file_name, 'wb') as output_file:
            for chunk in output_pipeline:
                m.update(chunk)
                output_file.write(chunk)
    finally:
        for tmp_input_file in tmp_source_map_processed_files:
            tmp_input_file.close()

    hash_version = m.hexdigest()

    if debug:
        output_file_name = '%s.debug.%s.%s' % (os.path.join(bundle.bundle_file_root, bundle.bundle_filename), hash_version, bundle.bundle_type)
    else:
        output_file_name = '%s.%s.%s' % (os.path.join(bundle.bundle_file_root, bundle.bundle_filename), hash_version, bundle.bundle_type)

    if bundle.uglify_command:
        source_map_line = '//# sourceMappingURL=%s.map' % tmp_source_map_url
        with open(tmp_output_file_name, 'rb') as input_file:
            with open(output_file_name, 'wb') as output_file:
                for l in input_file:
                    if l == source_map_line:
                        output_file.write('//# sourceMappingURL=%s.map' % bundle.get_url(version=hash_version))
                    else:
                        output_file.write(l)
        os.remove(tmp_output_file_name)
        os.rename(tmp_output_file_name + '.map', output_file_name + '.map')
    else:
        os.rename(tmp_output_file_name, output_file_name)

    return hash_version

class Command(BaseCommand):
    help = "Bundles up the media"
    requires_model_validation = False

    def handle(self, *args, **options):
        self.stdout.write("Bundling...\n")

        _bundle_versions = {}
        set_bundle_versions(_bundle_versions)

        for bundle in get_bundles():
            self.stdout.write("Writing bundle: %s\n" % bundle.name)

            hash_version = make_bundle(bundle)

            # Build bundle versions as we're going along in case they're used in templated bundles
            _bundle_versions[bundle.name] = hash_version

            if bundle.create_debug:
                _bundle_versions['debug:' + bundle.name] = make_bundle(bundle, debug=True)

            self.stdout.write("\t%s\n" % bundle.get_version())

        version_info = '\n'.join(['    "%s": "%s",' % version for version in _bundle_versions.iteritems()])

        with open(bundles_settings.BUNDLES_VERSION_FILE, 'wb') as bundles_versions:
            bundles_versions.write("""\
#!/usr/bin/env python

BUNDLES_VERSIONS = {
%s
}
""" % version_info)

        for single_file_input, single_file_output in bundles_settings.BUNDLES_SINGLE_FILES:
            self.stdout.write("Writing: %s\n" % single_file_output)
            file_type = os.path.splitext(single_file_input)[1][1:]
            processors = processor_library.get_default_preprocessors_for(file_type) + processor_library.get_default_postprocessors_for(file_type)

            with open(single_file_output, 'wb') as output_file:
                for chunk in processor_pipeline(processors, FileChunkGenerator(open(single_file_input, 'rb'))):
                    output_file.write(chunk)

        self.stdout.write("Done.\n")
<commit_msg>Tidying up the create function<commit_after>from django.core.management.base import BaseCommand

from django_bundles.conf.bundles_settings import bundles_settings
from django_bundles.core import get_bundles, set_bundle_versions
from django_bundles.processors import processor_pipeline, processor_library
from django_bundles.utils.files import FileChunkGenerator
from django_bundles.utils.processes import run_process

import os
import collections
from hashlib import md5
from tempfile import NamedTemporaryFile

def iter_bundle_files(bundle, debug=False):
    for bundle_file in bundle.files:
        for chunk in processor_pipeline(bundle_file.processors, FileChunkGenerator(open(bundle_file.file_path, 'rb')), debug=debug):
            yield chunk
        yield '\n'

def make_uglify_bundle(bundle, debug=False):
    m = md5()

    infile_list = []
    source_map_processed_input_files = []

    try:
        for bundle_file in bundle.files:
            if bundle_file.processors:
                # for now preprocessed files are written to temp files and therefore won't be available in the source map
                output_pipeline = processor_pipeline(bundle_file.processors, FileChunkGenerator(open(bundle_file.file_path, 'rb')), debug=debug)
                tmp_input_file = NamedTemporaryFile()
                source_map_processed_input_files.append(tmp_input_file)
                for chunk in output_pipeline:
                    m.update(chunk)
                    tmp_input_file.write(chunk)
                tmp_input_file.seek(0)
                infile_list.append(tmp_input_file.name)
            else:
                for chunk in FileChunkGenerator(open(bundle_file.file_path, 'rb')):
                    m.update(chunk)
                infile_list.append(bundle_file.file_path)

        hash_version = m.hexdigest()

        if debug:
            output_file_name = '%s.debug.%s.%s' % (os.path.join(bundle.bundle_file_root, bundle.bundle_filename), hash_version, bundle.bundle_type)
        else:
            output_file_name = '%s.%s.%s' % (os.path.join(bundle.bundle_file_root, bundle.bundle_filename), hash_version, bundle.bundle_type)

        source_map_options = [
            '--source-map %s.map' % output_file_name,
            '--source-map-root %s' % bundle.files_url_root,
            '--source-map-url %s.map' % bundle.get_url(version=hash_version),
            '-p %s' % os.path.realpath(bundle.files_root).count('/'),
            '-o %s' % output_file_name,
        ]

        # Consume the iterator into a zero length deque
        collections.deque(run_process(bundle.uglify_command.format(infile_list=' '.join(infile_list), source_map_options=' '.join(source_map_options))), maxlen=0)
    finally:
        for tmp_input_file in source_map_processed_input_files:
            tmp_input_file.close()

    return hash_version

def make_bundle(bundle, debug=False):
    """
    Does all of the processing required to create a bundle and write it to disk, returning its hash version
    """
    tmp_output_file_name = '%s.%s.%s' % (os.path.join(bundle.bundle_file_root, bundle.bundle_filename), 'temp', bundle.bundle_type)

    iter_input = iter_bundle_files(bundle, debug=debug)

    output_pipeline = processor_pipeline(bundle.processors, iter_input, debug=debug)

    m = md5()

    with open(tmp_output_file_name, 'wb') as output_file:
        for chunk in output_pipeline:
            m.update(chunk)
            output_file.write(chunk)

    hash_version = m.hexdigest()

    if debug:
        output_file_name = '%s.debug.%s.%s' % (os.path.join(bundle.bundle_file_root, bundle.bundle_filename), hash_version, bundle.bundle_type)
    else:
        output_file_name = '%s.%s.%s' % (os.path.join(bundle.bundle_file_root, bundle.bundle_filename), hash_version, bundle.bundle_type)

    os.rename(tmp_output_file_name, output_file_name)

    return hash_version

class Command(BaseCommand):
    help = "Bundles up the media"
    requires_model_validation = False

    def handle(self, *args, **options):
        self.stdout.write("Bundling...\n")

        _bundle_versions = {}
        set_bundle_versions(_bundle_versions)

        for bundle in get_bundles():
            self.stdout.write("Writing bundle: %s\n" % bundle.name)

            if bundle.uglify_command:
                hash_version = make_uglify_bundle(bundle)
            else:
                hash_version = make_bundle(bundle)

            # Build bundle versions as we're going along in case they're used in templated bundles
            _bundle_versions[bundle.name] = hash_version

            if bundle.create_debug:
                if bundle.uglify_command:
                    _bundle_versions['debug:' + bundle.name] = make_uglify_bundle(bundle, debug=True)
                else:
                    _bundle_versions['debug:' + bundle.name] = make_bundle(bundle, debug=True)

            self.stdout.write("\t%s\n" % bundle.get_version())

        version_info = '\n'.join(['    "%s": "%s",' % version for version in _bundle_versions.iteritems()])

        with open(bundles_settings.BUNDLES_VERSION_FILE, 'wb') as bundles_versions:
            bundles_versions.write("""\
#!/usr/bin/env python

BUNDLES_VERSIONS = {
%s
}
""" % version_info)

        for single_file_input, single_file_output in bundles_settings.BUNDLES_SINGLE_FILES:
            self.stdout.write("Writing: %s\n" % single_file_output)
            file_type = os.path.splitext(single_file_input)[1][1:]
            processors = processor_library.get_default_preprocessors_for(file_type) + processor_library.get_default_postprocessors_for(file_type)

            with open(single_file_output, 'wb') as output_file:
                for chunk in processor_pipeline(processors, FileChunkGenerator(open(single_file_input, 'rb'))):
                    output_file.write(chunk)

        self.stdout.write("Done.\n")
<|endoftext|>


<commit_before>
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, Http404
from django.forms.models import inlineformset_factory
from django.template import RequestContext
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.core.urlresolvers import reverse
from django.utils.translation import get_language
from django.conf import settings
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage

from django_fixmystreet.fixmystreet.models import Report, ReportFile, ReportSubscription, OrganisationEntity, ZipCode, ReportMainCategoryClass
from django_fixmystreet.fixmystreet.forms import CitizenReportForm, CitizenForm, ReportCommentForm, ReportFileForm, MarkAsDoneForm
from django_fixmystreet.fixmystreet.utils import dict_to_point, RequestFingerprint

import logging
logger = logging.getLogger(__name__)

def new(request):
    ReportFileFormSet = inlineformset_factory(Report, ReportFile, form=ReportFileForm, extra=0)

    pnt = dict_to_point(request.REQUEST)
    report=None
    file_formset = ReportFileFormSet(prefix='files', queryset=ReportFile.objects.none())

    if request.method == "POST":
        report_form = CitizenReportForm(request.POST, request.FILES, prefix='report')
        comment_form = ReportCommentForm(request.POST, request.FILES, prefix='comment')
        citizen_form = CitizenForm(request.POST, request.FILES, prefix='citizen')

        fingerprint = RequestFingerprint(request)

        # this checks update is_valid too
        if report_form.is_valid() and (not request.POST["comment-text"] or comment_form.is_valid()) and citizen_form.is_valid() and not fingerprint.is_duplicate():
            # this saves the update as part of the report.
            report = report_form.save(commit=False)

            file_formset = ReportFileFormSet(request.POST, request.FILES, instance=report, prefix='files', queryset=ReportFile.objects.none())

            if file_formset.is_valid():
                fingerprint.save()

                citizen = citizen_form.save()

                report.citizen = citizen
                report.save()

                if report_form.cleaned_data['subscription']:
                    report.subscribe_author()

                if request.POST["comment-text"]:
                    comment = comment_form.save(commit=False)
                    comment.created_by = citizen
                    comment.report = report
                    comment.save()

                files = file_formset.save(commit=False)
                for report_file in files:
                    report_file.created_by = citizen
                    #report_file.report = report
                    report_file.save()
                messages.add_message(request, messages.SUCCESS, _("Newly created report successfull"))
                return HttpResponseRedirect(report.get_absolute_url())

            else:
                report = None

    else:
        report_form = CitizenReportForm(initial={
            'x': request.REQUEST.get('x'),
            'y': request.REQUEST.get('y')
        }, prefix='report')
        file_formset = ReportFileFormSet(prefix='files', queryset=ReportFile.objects.none())
        comment_form = ReportCommentForm(prefix='comment')
        citizen_form = CitizenForm(prefix='citizen')

    reports_nearby = Report.objects.all().distance(pnt).filter(point__distance_lte=(pnt, 150)).order_by('distance').public()
    reports = Report.objects.all()
    return render_to_response("reports/new.html",
            {
                "report":report,
                "available_zips":ZipCode.objects,
                "all_zips":ZipCode.objects.all(),
                "category_classes":ReportMainCategoryClass.objects.prefetch_related('categories').all().order_by('name_'+ get_language()),
                "comment_form":comment_form,
                "file_formset":file_formset,
                "report_form": report_form,
                "citizen_form": citizen_form,
                "pnt":pnt,
                "reports":reports,
                "reports_nearby":reports_nearby
            },
            context_instance=RequestContext(request))

def verify(request):
    pnt = dict_to_point(request.REQUEST)
    reports_nearby = Report.objects.all().distance(pnt).filter(point__distance_lte=(pnt, 20)).order_by('distance').public()[0:6]

    if reports_nearby:
        return render_to_response("reports/verify.html",
            {
                "reports_nearby":reports_nearby
            },
            context_instance=RequestContext(request))

    return new(request)

def report_prepare(request, location = None, error_msg = None):
    '''Deprecated, no sense'''
    return HttpResponseRedirect(reverse('home'))

def show(request, slug, report_id):
    report = get_object_or_404(Report, id=report_id)
    if report.citizen:
        user_to_show = report.citizen
    else:
        user_to_show = report.created_by

    return render_to_response("reports/show.html",
            {
                "report": report,
                "subscribed": request.user.is_authenticated() and ReportSubscription.objects.filter(report=report, subscriber=request.user).exists(),
                "author": user_to_show,
                'activity_list' : report.activities.all(),
            },
            context_instance=RequestContext(request))

def document(request, slug, report_id):
    ReportFileFormSet = inlineformset_factory(Report, ReportFile, form=ReportFileForm, extra=0)
    report = get_object_or_404(Report, id=report_id)

    if request.method == "POST":
        comment = None
        comment_form = ReportCommentForm(request.POST, request.FILES, prefix='comment')
        file_formset = ReportFileFormSet(request.POST, request.FILES, instance=report, prefix='files', queryset=ReportFile.objects.none())
        citizen_form = CitizenForm(request.POST, request.FILES, prefix='citizen')

        # this checks update is_valid too
        if file_formset.is_valid() and (not request.POST["comment-text"] or comment_form.is_valid()) and citizen_form.is_valid():
            # this saves the update as part of the report.
            citizen = citizen_form.save()

            if request.POST["comment-text"] and len(request.POST["comment-text"]) > 0:
                comment            = comment_form.save(commit=False)
                comment.report     = report
                comment.created_by = citizen
                comment.save()

            files = file_formset.save()

            for report_file in files:
                report_file.created_by = citizen
                report_file.save()

            # if request.POST.get("citizen_subscription", False):
            #     ReportSubscription(report=report, subscriber=report.created_by).save()

            report.trigger_updates_added(files=files, comment=comment)

            messages.add_message(request, messages.SUCCESS, _("You attachments has been sent"))
            return HttpResponseRedirect(report.get_absolute_url())
    else:
        file_formset = ReportFileFormSet(prefix='files', queryset=ReportFile.objects.none())
        comment_form = ReportCommentForm(prefix='comment')
        citizen_form = CitizenForm(prefix='citizen')

    return render_to_response("reports/document.html",
            {
                "report": report,
                "subscribed": request.user.is_authenticated() and ReportSubscription.objects.filter(report=report, subscriber=request.user).exists(),
                "file_formset": file_formset,
                "comment_form": comment_form,
                "citizen_form": citizen_form,
            },
            context_instance=RequestContext(request))

def update( request, report_id):
    report = get_object_or_404(Report, id=report_id)
    if request.REQUEST.has_key('is_fixed'):
        form = MarkAsDoneForm(request.POST, instance=report)
        #Save the mark as done motivation in the database
        form.save()

        if "pro" in request.path:
            return HttpResponseRedirect(report.get_absolute_url_pro())
        else:
            return HttpResponseRedirect(report.get_absolute_url())

    # ????? DEPRECATED ?????
    if request.method == 'POST':
        if request.POST['form-type'] == u"comment-form":
            comment_form = ReportCommentForm(request.POST)
            if comment_form.is_valid():
                comment_form.save(request.user, report)

        if request.POST['form-type'] == u"file-form":
            #set default title if not given
            fileTitle = request.POST.get("title")
            if (fileTitle == ""):
                  request.POST.__setitem__("title", request.FILES.get('file').name)
            file_form = ReportFileForm(request.POST,request.FILES)
            if file_form.is_valid:
                file_form.save(request.user, report)

        if "pro" in request.path:
            return HttpResponseRedirect(report.get_absolute_url_pro())
        else:
            return HttpResponseRedirect(report.get_absolute_url())
    raise Http404()

def search_ticket(request):
    try:
        report_id = request.REQUEST.get('report_id')

        if hasattr(request, 'fmsuser') and request.fmsuser.is_pro():
            report = Report.objects.get(id=report_id)

            return HttpResponseRedirect(report.get_absolute_url_pro())

        report = Report.objects.get(id=report_id, private=False)
        return HttpResponseRedirect(report.get_absolute_url())

    except Report.DoesNotExist:
        messages.add_message(request, messages.ERROR, _("No incident found with this ticket number"))
        return HttpResponseRedirect(reverse('home'))

def index(request):
    return render_to_response("reports/reports_map.html", {
        'zipcodes' : ZipCode.objects.filter(hide=False).order_by('name_' + get_language())
    }, context_instance=RequestContext(request))
<commit_msg>replace Report.objects.all by ...visible()<commit_after>
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, Http404
from django.forms.models import inlineformset_factory
from django.template import RequestContext
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.core.urlresolvers import reverse
from django.utils.translation import get_language
from django.conf import settings
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage

from django_fixmystreet.fixmystreet.models import Report, ReportFile, ReportSubscription, OrganisationEntity, ZipCode, ReportMainCategoryClass
from django_fixmystreet.fixmystreet.forms import CitizenReportForm, CitizenForm, ReportCommentForm, ReportFileForm, MarkAsDoneForm
from django_fixmystreet.fixmystreet.utils import dict_to_point, RequestFingerprint

import logging
logger = logging.getLogger(__name__)

def new(request):
    ReportFileFormSet = inlineformset_factory(Report, ReportFile, form=ReportFileForm, extra=0)

    pnt = dict_to_point(request.REQUEST)
    report=None
    file_formset = ReportFileFormSet(prefix='files', queryset=ReportFile.objects.none())

    if request.method == "POST":
        report_form = CitizenReportForm(request.POST, request.FILES, prefix='report')
        comment_form = ReportCommentForm(request.POST, request.FILES, prefix='comment')
        citizen_form = CitizenForm(request.POST, request.FILES, prefix='citizen')

        fingerprint = RequestFingerprint(request)

        # this checks update is_valid too
        if report_form.is_valid() and (not request.POST["comment-text"] or comment_form.is_valid()) and citizen_form.is_valid() and not fingerprint.is_duplicate():
            # this saves the update as part of the report.
            report = report_form.save(commit=False)

            file_formset = ReportFileFormSet(request.POST, request.FILES, instance=report, prefix='files', queryset=ReportFile.objects.none())

            if file_formset.is_valid():
                fingerprint.save()

                citizen = citizen_form.save()

                report.citizen = citizen
                report.save()

                if report_form.cleaned_data['subscription']:
                    report.subscribe_author()

                if request.POST["comment-text"]:
                    comment = comment_form.save(commit=False)
                    comment.created_by = citizen
                    comment.report = report
                    comment.save()

                files = file_formset.save(commit=False)
                for report_file in files:
                    report_file.created_by = citizen
                    #report_file.report = report
                    report_file.save()
                messages.add_message(request, messages.SUCCESS, _("Newly created report successfull"))
                return HttpResponseRedirect(report.get_absolute_url())

            else:
                report = None

    else:
        report_form = CitizenReportForm(initial={
            'x': request.REQUEST.get('x'),
            'y': request.REQUEST.get('y')
        }, prefix='report')
        file_formset = ReportFileFormSet(prefix='files', queryset=ReportFile.objects.none())
        comment_form = ReportCommentForm(prefix='comment')
        citizen_form = CitizenForm(prefix='citizen')

    reports_nearby = Report.visibles.all().distance(pnt).filter(point__distance_lte=(pnt, 150)).order_by('distance').public()
    reports = Report.visibles.all()
    return render_to_response("reports/new.html",
            {
                "report":report,
                "available_zips":ZipCode.objects,
                "all_zips":ZipCode.objects.all(),
                "category_classes":ReportMainCategoryClass.objects.prefetch_related('categories').all().order_by('name_'+ get_language()),
                "comment_form":comment_form,
                "file_formset":file_formset,
                "report_form": report_form,
                "citizen_form": citizen_form,
                "pnt":pnt,
                "reports":reports,
                "reports_nearby":reports_nearby
            },
            context_instance=RequestContext(request))

def verify(request):
    pnt = dict_to_point(request.REQUEST)
    reports_nearby = Report.visibles.all().distance(pnt).filter(point__distance_lte=(pnt, 20)).order_by('distance').public()[0:6]

    if reports_nearby:
        return render_to_response("reports/verify.html",
            {
                "reports_nearby":reports_nearby
            },
            context_instance=RequestContext(request))

    return new(request)

def report_prepare(request, location = None, error_msg = None):
    '''Deprecated, no sense'''
    return HttpResponseRedirect(reverse('home'))

def show(request, slug, report_id):
    report = get_object_or_404(Report, id=report_id)
    if report.citizen:
        user_to_show = report.citizen
    else:
        user_to_show = report.created_by

    return render_to_response("reports/show.html",
            {
                "report": report,
                "subscribed": request.user.is_authenticated() and ReportSubscription.objects.filter(report=report, subscriber=request.user).exists(),
                "author": user_to_show,
                'activity_list' : report.activities.all(),
            },
            context_instance=RequestContext(request))

def document(request, slug, report_id):
    ReportFileFormSet = inlineformset_factory(Report, ReportFile, form=ReportFileForm, extra=0)
    report = get_object_or_404(Report, id=report_id)

    if request.method == "POST":
        comment = None
        comment_form = ReportCommentForm(request.POST, request.FILES, prefix='comment')
        file_formset = ReportFileFormSet(request.POST, request.FILES, instance=report, prefix='files', queryset=ReportFile.objects.none())
        citizen_form = CitizenForm(request.POST, request.FILES, prefix='citizen')

        # this checks update is_valid too
        if file_formset.is_valid() and (not request.POST["comment-text"] or comment_form.is_valid()) and citizen_form.is_valid():
            # this saves the update as part of the report.
            citizen = citizen_form.save()

            if request.POST["comment-text"] and len(request.POST["comment-text"]) > 0:
                comment            = comment_form.save(commit=False)
                comment.report     = report
                comment.created_by = citizen
                comment.save()

            files = file_formset.save()

            for report_file in files:
                report_file.created_by = citizen
                report_file.save()

            # if request.POST.get("citizen_subscription", False):
            #     ReportSubscription(report=report, subscriber=report.created_by).save()

            report.trigger_updates_added(files=files, comment=comment)

            messages.add_message(request, messages.SUCCESS, _("You attachments has been sent"))
            return HttpResponseRedirect(report.get_absolute_url())
    else:
        file_formset = ReportFileFormSet(prefix='files', queryset=ReportFile.objects.none())
        comment_form = ReportCommentForm(prefix='comment')
        citizen_form = CitizenForm(prefix='citizen')

    return render_to_response("reports/document.html",
            {
                "report": report,
                "subscribed": request.user.is_authenticated() and ReportSubscription.objects.filter(report=report, subscriber=request.user).exists(),
                "file_formset": file_formset,
                "comment_form": comment_form,
                "citizen_form": citizen_form,
            },
            context_instance=RequestContext(request))

def update( request, report_id):
    report = get_object_or_404(Report, id=report_id)
    if request.REQUEST.has_key('is_fixed'):
        form = MarkAsDoneForm(request.POST, instance=report)
        #Save the mark as done motivation in the database
        form.save()

        if "pro" in request.path:
            return HttpResponseRedirect(report.get_absolute_url_pro())
        else:
            return HttpResponseRedirect(report.get_absolute_url())

    # ????? DEPRECATED ?????
    if request.method == 'POST':
        if request.POST['form-type'] == u"comment-form":
            comment_form = ReportCommentForm(request.POST)
            if comment_form.is_valid():
                comment_form.save(request.user, report)

        if request.POST['form-type'] == u"file-form":
            #set default title if not given
            fileTitle = request.POST.get("title")
            if (fileTitle == ""):
                  request.POST.__setitem__("title", request.FILES.get('file').name)
            file_form = ReportFileForm(request.POST,request.FILES)
            if file_form.is_valid:
                file_form.save(request.user, report)

        if "pro" in request.path:
            return HttpResponseRedirect(report.get_absolute_url_pro())
        else:
            return HttpResponseRedirect(report.get_absolute_url())
    raise Http404()

def search_ticket(request):
    try:
        report_id = request.REQUEST.get('report_id')

        if hasattr(request, 'fmsuser') and request.fmsuser.is_pro():
            report = Report.objects.get(id=report_id)

            return HttpResponseRedirect(report.get_absolute_url_pro())

        report = Report.objects.get(id=report_id, private=False)
        return HttpResponseRedirect(report.get_absolute_url())

    except Report.DoesNotExist:
        messages.add_message(request, messages.ERROR, _("No incident found with this ticket number"))
        return HttpResponseRedirect(reverse('home'))

def index(request):
    return render_to_response("reports/reports_map.html", {
        'zipcodes' : ZipCode.objects.filter(hide=False).order_by('name_' + get_language())
    }, context_instance=RequestContext(request))
<|endoftext|>


<commit_before>from __future__ import print_function
import os
import shutil
import yaml
import sys
import subprocess as sp
from .utils import get_species
from .utils import get_ggd_channels
from .utils import get_conda_env
from collections import OrderedDict
from .utils import check_output, conda_root

SPECIES_LIST = [x.encode('ascii') for x in get_species()]
CHANNEL_LIST = [x.encode('ascii') for x in get_ggd_channels()]

def add_make_bash(p):
    c = p.add_parser('make-recipe', help="make a new ggd/conda recipe give a bash script")
    c.add_argument("-c", "--channel", help="the ggd channel to use. (Default = genomics)", choices=[x.decode('ascii') for x in CHANNEL_LIST],
                    default='genomics')
    c.add_argument("-d", "--dependency", default=[], action="append",
                    help="any software dependencies (in bioconda, conda-forge) or data-dependency (in ggd)" +
                    ". May be as many times as needed.")
    c.add_argument("-e", "--extra-file", default=[], action="append",
                    help="any files that the recipe creates that are not a *.gz and *.gz.tbi pair. May be used more than once")
    c.add_argument("-p", "--platform", default="noarch", help="Whether to use noarch as the platfrom or the system platform. If set to 'none' the system platform will be used. (Default = noarch. Noarch means no architecture and is platform agnostic.)",
                    choices=["noarch", "none"])
    c2 = c.add_argument_group("required arguments")
    c2.add_argument("-s", "--species", help="species recipe is for", choices=[x.decode('ascii') for x in SPECIES_LIST],
                    required=True)
    c2.add_argument("-g", "--genome-build", help="genome-build the recipe is for",
                    required=True)
    c2.add_argument("--authors", help="authors of the recipe", default=os.environ.get("USER", ""))
    c2.add_argument("-gv", "--ggd_version", help="The version of the ggd package. (First time package = 1, updated package > 1)",
                    required=True)
    c2.add_argument("-dv", "--data_version", help="The version of the data (itself) being downloaded and processed (EX: dbsnp-127)",
                    required=True)
    c2.add_argument("--summary", help="a comment describing the recipe",
                default="", required=True)
    c2.add_argument("-k", "--keyword", help="a keyword to associate with the recipe." +
                    " may be specified more that once.", action="append", default=[],
                    required=True)
    c.add_argument("name", help="name of recipe")
    c.add_argument("script", help="bash script that contains the commands that build the recipe")

    c.set_defaults(func=make_bash)

def make_bash(parser, args):

    name = args.name.replace(args.species, "").replace(args.genome_build, "").strip("- ").strip()
    name = "{0}-{1}-v{2}".format(args.genome_build, name, args.ggd_version).lower()
    assert name.strip() != "{0}--v{1}".format(args.genome_build,args.ggd_version) ## test for missing name
    assert args.summary.strip() != ""

    try:
        os.makedirs(name)
    except OSError:
        shutil.rmtree(name)
        os.makedirs(name)

    from .check_recipe import _check_build
    print("checking", args.genome_build)
    _check_build(args.species, args.genome_build)

    try:
        recipe_bash = open(args.script).read()
    except IOError as e:
        print(e)
        sys.exit(1)

    # use these to automate inserting some dependencies.
    look = {'tabix': 'htslib', 'bgzip': 'htslib', 'perl': 'perl',
            'gsort': 'gsort',
            'samtools': 'samtools', 'gzip': 'zlib',
            'zcat': 'zlib', 'gunzip': 'zlib', 'vt': 'vt'}
    deps = sorted(
              set([look.get(p, p) for p in args.dependency] +
                  [look[prog] for prog in look if prog in recipe_bash]))

    if args.platform == "noarch":
        yml1 = {"build": {
                      "noarch": "generic",
                      "binary_relocation": False,
                      "detect_binary_files_with_prefix": False,
                      "number": 0}}
    else:
        yml1 = {"build": {
                      "binary_relocation": False,
                      "detect_binary_files_with_prefix": False,
                      "number": 0}}
    yml2 = {"extra": {
                    "authors": args.authors,
                    "extra-files": args.extra_file,
                }}
    yml3 = {"package": {"name": name, "version": args.ggd_version}}
    yml4 = {"requirements": {"build": deps[:],
                    "run": deps[:]}}
    yml5 = { "source": {"path": "."}}

    yml6 = {"about": {
                    "identifiers": {
                    "species": args.species,
                    "genome-build": args.genome_build
                },
                    "keywords": args.keyword,
                    "summary": args.summary,
                    "tags": {
                        "data-version": args.data_version,
                        "ggd-channel": args.channel
                    },
                }}

    with open(os.path.join(name, "meta.yaml"), "a") as fh:
        fh.write(yaml.dump(yml1, default_flow_style=False))
        fh.write(yaml.dump(yml2, default_flow_style=False))
        fh.write(yaml.dump(yml3, default_flow_style=False))
        fh.write(yaml.dump(yml4, default_flow_style=False))
        fh.write(yaml.dump(yml5, default_flow_style=False))
        fh.write(yaml.dump(yml6, default_flow_style=False))

    with open(os.path.join(name, "post-link.sh"), "w") as fh:
        fh.write("""#!/bin/bash
set -eo pipefail -o nounset

if [[ -z $(conda info --envs | grep "*" | grep -o "\/.*") ]]; then
    export CONDA_ROOT=$(conda info --root)
    env_dir=$CONDA_ROOT
    export RECIPE_DIR=$CONDA_ROOT/share/ggd/{species}/{build}/{name}/{version}
elif [[ $(conda info --envs | grep "*" | grep -o "\/.*") == "base" ]]; then
    export CONDA_ROOT=$(conda info --root)
    env_dir=$CONDA_ROOT
    export RECIPE_DIR=$CONDA_ROOT/share/ggd/{species}/{build}/{name}/{version}
else
    env_dir=$(conda info --envs | grep "*" | grep -o "\/.*")
    export CONDA_ROOT=$env_dir
    export RECIPE_DIR=$env_dir/share/ggd/{species}/{build}/{name}/{version}
fi

PKG_DIR=`find "$CONDA_ROOT/pkgs/" -name "$PKG_NAME-$PKG_VERSION*" | grep -v ".tar.bz2" |  grep "$PKG_VERSION.*$PKG_BUILDNUM$"`

if [ -d $RECIPE_DIR ]; then
    rm -r $RECIPE_DIR
fi

mkdir -p $RECIPE_DIR

recipe_env_name="ggd_{name}"
recipe_env_name="$(echo "$recipe_env_name" | sed 's/-/_/g')"

activate_dir="$env_dir/etc/conda/activate.d"
deactivate_dir="$env_dir/etc/conda/deactivate.d"

mkdir -p $activate_dir
mkdir -p $deactivate_dir

echo "export $recipe_env_name=$RECIPE_DIR" >> $activate_dir/env_vars.sh
echo "unset $recipe_env_name">> $deactivate_dir/env_vars.sh

(cd $RECIPE_DIR && bash $PKG_DIR/info/recipe/recipe.sh)

cd $RECIPE_DIR

## Iterate over new files and replace file name with data package name and data version
for f in *; do
    ext="${ext_string}"
    filename="{filename_string}"
    (mv $f "{name}.$ext")
done

echo 'Recipe successfully built!'
""".format(species=args.species,
           name=name,
           build=args.genome_build,
           version=args.ggd_version,
           ext_string="{f#*.}", ## Bash get extention. (.bed, .bed.gz, etc.)
           filename_string="{f%%.*}"))

    with open(os.path.join(name, "recipe.sh"), "w") as fh:
        fh.write("#!/bin/sh\nset -eo pipefail -o nounset\n")
        fh.write(open(args.script).read())

    print("\n\t-> Wrote output to %s/" % name)
    print("\n\t-> To test that the recipe is working, and before pushing the new recipe to gogetdata/ggd-recipes, please run: \n\t\t$ ggd check-recipe %s/"  % name)

    return(True)
<commit_msg>Bug Fixes Extra files in the meta.yaml file now reflect the new data file naming convention<commit_after>from __future__ import print_function
import os
import shutil
import yaml
import sys
import subprocess as sp
from .utils import get_species
from .utils import get_ggd_channels
from .utils import get_conda_env
from collections import OrderedDict
from .utils import check_output, conda_root

SPECIES_LIST = [x.encode('ascii') for x in get_species()]
CHANNEL_LIST = [x.encode('ascii') for x in get_ggd_channels()]

def add_make_bash(p):
    c = p.add_parser('make-recipe', help="make a new ggd/conda recipe give a bash script")
    c.add_argument("-c", "--channel", help="the ggd channel to use. (Default = genomics)", choices=[x.decode('ascii') for x in CHANNEL_LIST],
                    default='genomics')
    c.add_argument("-d", "--dependency", default=[], action="append",
                    help="any software dependencies (in bioconda, conda-forge) or data-dependency (in ggd)" +
                    ". May be as many times as needed.")
    c.add_argument("-e", "--extra-file", default=[], action="append",
                    help="any files that the recipe creates that are not a *.gz and *.gz.tbi pair. May be used more than once")
    c.add_argument("-p", "--platform", default="noarch", help="Whether to use noarch as the platfrom or the system platform. If set to 'none' the system platform will be used. (Default = noarch. Noarch means no architecture and is platform agnostic.)",
                    choices=["noarch", "none"])
    c2 = c.add_argument_group("required arguments")
    c2.add_argument("-s", "--species", help="species recipe is for", choices=[x.decode('ascii') for x in SPECIES_LIST],
                    required=True)
    c2.add_argument("-g", "--genome-build", help="genome-build the recipe is for",
                    required=True)
    c2.add_argument("--authors", help="authors of the recipe", default=os.environ.get("USER", ""))
    c2.add_argument("-gv", "--ggd_version", help="The version of the ggd package. (First time package = 1, updated package > 1)",
                    required=True)
    c2.add_argument("-dv", "--data_version", help="The version of the data (itself) being downloaded and processed (EX: dbsnp-127)",
                    required=True)
    c2.add_argument("--summary", help="a comment describing the recipe",
                default="", required=True)
    c2.add_argument("-k", "--keyword", help="a keyword to associate with the recipe." +
                    " may be specified more that once.", action="append", default=[],
                    required=True)
    c.add_argument("name", help="name of recipe")
    c.add_argument("script", help="bash script that contains the commands that build the recipe")

    c.set_defaults(func=make_bash)

def make_bash(parser, args):

    name = args.name.replace(args.species, "").replace(args.genome_build, "").strip("- ").strip()
    name = "{0}-{1}-v{2}".format(args.genome_build, name, args.ggd_version).lower()
    assert name.strip() != "{0}--v{1}".format(args.genome_build,args.ggd_version) ## test for missing name
    assert args.summary.strip() != ""

    try:
        os.makedirs(name)
    except OSError:
        shutil.rmtree(name)
        os.makedirs(name)

    from .check_recipe import _check_build
    print("checking", args.genome_build)
    _check_build(args.species, args.genome_build)

    try:
        recipe_bash = open(args.script).read()
    except IOError as e:
        print(e)
        sys.exit(1)

    # use these to automate inserting some dependencies.
    look = {'tabix': 'htslib', 'bgzip': 'htslib', 'perl': 'perl',
            'gsort': 'gsort',
            'samtools': 'samtools', 'gzip': 'zlib',
            'zcat': 'zlib', 'gunzip': 'zlib', 'vt': 'vt'}
    deps = sorted(
              set([look.get(p, p) for p in args.dependency] +
                  [look[prog] for prog in look if prog in recipe_bash]))

    extra_files = []
    for f in args.extra_file:
        flist = f.strip().split(".")
        flist[0] = name
        extra_files.append(".".join(flist))

    if args.platform == "noarch":
        yml1 = {"build": {
                      "noarch": "generic",
                      "binary_relocation": False,
                      "detect_binary_files_with_prefix": False,
                      "number": 0}}
    else:
        yml1 = {"build": {
                      "binary_relocation": False,
                      "detect_binary_files_with_prefix": False,
                      "number": 0}}
    yml2 = {"extra": {
                    "authors": args.authors,
                    "extra-files": extra_files,
                }}
    yml3 = {"package": {"name": name, "version": args.ggd_version}}
    yml4 = {"requirements": {"build": deps[:],
                    "run": deps[:]}}
    yml5 = { "source": {"path": "."}}

    yml6 = {"about": {
                    "identifiers": {
                    "species": args.species,
                    "genome-build": args.genome_build
                },
                    "keywords": args.keyword,
                    "summary": args.summary,
                    "tags": {
                        "data-version": args.data_version,
                        "ggd-channel": args.channel
                    },
                }}

    with open(os.path.join(name, "meta.yaml"), "a") as fh:
        fh.write(yaml.dump(yml1, default_flow_style=False))
        fh.write(yaml.dump(yml2, default_flow_style=False))
        fh.write(yaml.dump(yml3, default_flow_style=False))
        fh.write(yaml.dump(yml4, default_flow_style=False))
        fh.write(yaml.dump(yml5, default_flow_style=False))
        fh.write(yaml.dump(yml6, default_flow_style=False))

    with open(os.path.join(name, "post-link.sh"), "w") as fh:
        fh.write("""#!/bin/bash
set -eo pipefail -o nounset

if [[ -z $(conda info --envs | grep "*" | grep -o "\/.*") ]]; then
    export CONDA_ROOT=$(conda info --root)
    env_dir=$CONDA_ROOT
    export RECIPE_DIR=$CONDA_ROOT/share/ggd/{species}/{build}/{name}/{version}
elif [[ $(conda info --envs | grep "*" | grep -o "\/.*") == "base" ]]; then
    export CONDA_ROOT=$(conda info --root)
    env_dir=$CONDA_ROOT
    export RECIPE_DIR=$CONDA_ROOT/share/ggd/{species}/{build}/{name}/{version}
else
    env_dir=$(conda info --envs | grep "*" | grep -o "\/.*")
    export CONDA_ROOT=$env_dir
    export RECIPE_DIR=$env_dir/share/ggd/{species}/{build}/{name}/{version}
fi

PKG_DIR=`find "$CONDA_ROOT/pkgs/" -name "$PKG_NAME-$PKG_VERSION*" | grep -v ".tar.bz2" |  grep "$PKG_VERSION.*$PKG_BUILDNUM$"`

if [ -d $RECIPE_DIR ]; then
    rm -r $RECIPE_DIR
fi

mkdir -p $RECIPE_DIR

recipe_env_name="ggd_{name}"
recipe_env_name="$(echo "$recipe_env_name" | sed 's/-/_/g')"

activate_dir="$env_dir/etc/conda/activate.d"
deactivate_dir="$env_dir/etc/conda/deactivate.d"

mkdir -p $activate_dir
mkdir -p $deactivate_dir

echo "export $recipe_env_name=$RECIPE_DIR" >> $activate_dir/env_vars.sh
echo "unset $recipe_env_name">> $deactivate_dir/env_vars.sh

(cd $RECIPE_DIR && bash $PKG_DIR/info/recipe/recipe.sh)

cd $RECIPE_DIR

## Iterate over new files and replace file name with data package name and data version
for f in *; do
    ext="${ext_string}"
    filename="{filename_string}"
    (mv $f "{name}.$ext")
done

echo 'Recipe successfully built!'
""".format(species=args.species,
           name=name,
           build=args.genome_build,
           version=args.ggd_version,
           ext_string="{f#*.}", ## Bash get extention. (.bed, .bed.gz, etc.)
           filename_string="{f%%.*}"))

    with open(os.path.join(name, "recipe.sh"), "w") as fh:
        fh.write("#!/bin/sh\nset -eo pipefail -o nounset\n")
        fh.write(open(args.script).read())

    print("\n\t-> Wrote output to %s/" % name)
    print("\n\t-> To test that the recipe is working, and before pushing the new recipe to gogetdata/ggd-recipes, please run: \n\t\t$ ggd check-recipe %s/"  % name)

    return(True)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from cms.models import Page
from cms.utils.i18n import get_language_list
from django.db import migrations, models

def forwards(apps, schema_editor):
    BlogConfig = apps.get_model('djangocms_blog', 'BlogConfig')
    BlogConfigTranslation = apps.get_model('djangocms_blog', 'BlogConfigTranslation')
    Post = apps.get_model('djangocms_blog', 'Post')
    BlogCategory = apps.get_model('djangocms_blog', 'BlogCategory')
    GenericBlogPlugin = apps.get_model('djangocms_blog', 'GenericBlogPlugin')
    LatestPostsPlugin = apps.get_model('djangocms_blog', 'LatestPostsPlugin')
    AuthorEntriesPlugin = apps.get_model('djangocms_blog', 'AuthorEntriesPlugin')
    config = None
    for page in Page.objects.drafts().filter(application_urls='BlogApp'):
        config, created = BlogConfig.objects.get_or_create(namespace=page.application_namespace)
        if not BlogConfigTranslation.objects.exists():
            for lang in get_language_list():
                title = page.get_title(lang)
                translation = BlogConfigTranslation.objects.create(language_code=lang, master_id=config.pk, app_title=title)
    if config:
        for model in (Post, BlogCategory, GenericBlogPlugin, LatestPostsPlugin, AuthorEntriesPlugin):
            for item in model.objects.filter(app_config__isnull=True):
                item.app_config = config
                item.save()

def backwards(apps, schema_editor):
    # No need for backward data migration
    pass

class Migration(migrations.Migration):

    dependencies = [
        ('djangocms_blog', '0013_auto_20160201_2235'),
    ]

    operations = [
        migrations.RunPython(forwards, backwards),
    ]
<commit_msg>Add proper dependency to 0014_auto migration<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from distutils.version import LooseVersion

import cms
from cms.models import Page
from cms.utils.i18n import get_language_list
from django.db import migrations, models

def forwards(apps, schema_editor):
    BlogConfig = apps.get_model('djangocms_blog', 'BlogConfig')
    BlogConfigTranslation = apps.get_model('djangocms_blog', 'BlogConfigTranslation')
    Post = apps.get_model('djangocms_blog', 'Post')
    BlogCategory = apps.get_model('djangocms_blog', 'BlogCategory')
    GenericBlogPlugin = apps.get_model('djangocms_blog', 'GenericBlogPlugin')
    LatestPostsPlugin = apps.get_model('djangocms_blog', 'LatestPostsPlugin')
    AuthorEntriesPlugin = apps.get_model('djangocms_blog', 'AuthorEntriesPlugin')
    config = None
    for page in Page.objects.drafts().filter(application_urls='BlogApp'):
        config, created = BlogConfig.objects.get_or_create(namespace=page.application_namespace)
        if not BlogConfigTranslation.objects.exists():
            for lang in get_language_list():
                title = page.get_title(lang)
                translation = BlogConfigTranslation.objects.create(language_code=lang, master_id=config.pk, app_title=title)
    if config:
        for model in (Post, BlogCategory, GenericBlogPlugin, LatestPostsPlugin, AuthorEntriesPlugin):
            for item in model.objects.filter(app_config__isnull=True):
                item.app_config = config
                item.save()

def backwards(apps, schema_editor):
    # No need for backward data migration
    pass

class Migration(migrations.Migration):

    if LooseVersion(cms.__version__) >= LooseVersion('3.1'):
        dependencies = [
            ('cms', '0004_auto_20140924_1038'),
            ('djangocms_blog', '0013_auto_20160201_2235'),
        ]
    else:
        dependencies = [
            ('djangocms_blog', '0013_auto_20160201_2235'),
        ]

    operations = [
        migrations.RunPython(forwards, backwards),
    ]
<|endoftext|>


<commit_before>#! /usr/bin/env python
#
# ipg.py -- Example of Ginga widget interaction with IPython.
#
# Eric Jeschke (eric@naoj.org)
#
# Contains code from IPython tutorial programs "qtapp_ip.py" and "kapp.py".
#
import sys, os
import logging

from ginga import AstroImage
from ginga.qtw.QtHelp import QtGui, QtCore
from ginga.qtw.FitsImageCanvasQt import FitsImageCanvas

from IPython.lib.kernel import connect_qtconsole
from IPython.zmq.ipkernel import IPKernelApp

STD_FORMAT = '%(asctime)s | %(levelname)1.1s | %(filename)s:%(lineno)d (%(funcName)s) | %(message)s'

# global ref to keep an object from being collected
app_ref = None

class SimpleKernelApp(object):
    """A minimal object that uses an IPython kernel and has a few methods
    to manipulate a namespace and open Qt consoles tied to the kernel.

    Code is modified from IPython tutorial programs 'qtapp_ip.py' and
    'kapp.py'.
    """

    def __init__(self, gui, shell):
        self.shell = shell

        if shell == None:
            # Start IPython kernel with GUI event loop support
            self.ipkernel = IPKernelApp.instance()
            self.ipkernel.initialize(['python', '--gui=%s' % gui,
                                      #'--log-level=10'  # for debugging
                                      ])
            # This application will also act on the shell user namespace
            self.namespace = self.ipkernel.shell.user_ns

        else:
            self.ipkernel = shell.config.IPKernelApp
            self.namespace = shell.user_ns

        # To create and track active qt consoles
        self.consoles = []

    def new_qt_console(self, evt=None):
        """start a new qtconsole connected to our kernel"""
        if hasattr(self.ipkernel, 'profile'):
            return connect_qtconsole(self.ipkernel.connection_file,
                                     profile=self.ipkernel.profile)
        else:
            return connect_qtconsole(self.ipkernel.connection_file)

    def cleanup_consoles(self, evt=None):
        for c in self.consoles:
            c.kill()

    def start(self):
        if self.shell == None:
            self.ipkernel.start()

class StartMenu(QtGui.QMainWindow):

    def __init__(self, logger, app, kapp):
        super(StartMenu, self).__init__()
        self.logger = logger
        self.count = 0
        self.viewers = {}
        self.app = app
        self.kapp = kapp
        self.app.aboutToQuit.connect(self.quit)

        vbox = QtGui.QVBoxLayout()
        vbox.setContentsMargins(QtCore.QMargins(2, 2, 2, 2))
        vbox.setSpacing(1)

        hbox = QtGui.QHBoxLayout()
        hbox.setContentsMargins(QtCore.QMargins(4, 2, 4, 2))

        console = QtGui.QPushButton('Qt Console')
        if self.kapp.ipkernel == None:
            console.setEnabled(False)
        console.clicked.connect(self.kapp.new_qt_console)

        newfits = QtGui.QPushButton('New Viewer')
        newfits.clicked.connect(self.new_viewer)

        wquit = QtGui.QPushButton("Quit")
        wquit.clicked.connect(self.quit)

        hbox.addStretch(1)
        for w in (console, newfits, wquit):
            hbox.addWidget(w, stretch=0)

        hw = QtGui.QWidget()
        hw.setLayout(hbox)
        vbox.addWidget(hw, stretch=0)

        vw = QtGui.QWidget()
        self.setCentralWidget(vw)
        vw.setLayout(vbox)
        self.setWindowTitle("Ginga IPython Console")

    def new_viewer(self, name=None):
        if not name:
            self.count += 1
            name = 'v%d' % self.count

        # create a ginga basic object for user interaction
        fi = FitsImageCanvas(self.logger, render='widget')
        fi.enable_autocuts('on')
        fi.set_autocut_params('zscale')
        fi.enable_autozoom('on')
        fi.enable_draw(True)
        fi.set_drawtype('ruler')
        fi.set_drawcolor('blue')
        fi.set_callback('drag-drop', self.drop_file, name)
        fi.set_bg(0.2, 0.2, 0.2)
        fi.ui_setActive(True)

        # expose the fits viewer to the shared namespace
        self.kapp.namespace[name] = fi

        # enable various interactive operations
        bd = fi.get_bindings()
        bd.enable_pan(True)
        bd.enable_zoom(True)
        bd.enable_cuts(True)
        bd.enable_flip(True)
        bd.enable_rotate(True)
        bd.enable_cmap(True)

        # get the ginga Qt widget
        w = fi.get_widget()
        w.resize(512, 512)

        # pack it into a qt window with a couple other buttons
        vw = QtGui.QWidget()
        vbox = QtGui.QVBoxLayout()
        vbox.setContentsMargins(QtCore.QMargins(2, 2, 2, 2))
        vbox.setSpacing(1)
        vbox.addWidget(w, stretch=1)

        # for simple WCS readout
        readout = QtGui.QLabel("")
        fi.set_callback('none-move', self.motion, readout)
        vbox.addWidget(readout, stretch=0,
                       alignment=QtCore.Qt.AlignCenter)

        hbox = QtGui.QHBoxLayout()
        hbox.setContentsMargins(QtCore.QMargins(4, 2, 4, 2))

        wopen = QtGui.QPushButton("Open File")
        def _open_file(name):
            return lambda: self.open_file(name)
        wopen.clicked.connect(_open_file(name))
        wquit = QtGui.QPushButton("Close")
        def _close(name):
            return lambda: self.close_viewer(name)
        wquit.clicked.connect(_close(name))

        hbox.addStretch(1)
        for w in (wopen, wquit):
            hbox.addWidget(w, stretch=0)

        hw = QtGui.QWidget()
        hw.setLayout(hbox)
        vbox.addWidget(hw, stretch=0)

        vw.setLayout(vbox)
        vw.resize(524, 540)
        vw.setWindowTitle("Ginga: %s" % name)
        self.viewers[name] = vw
        vw.show()

    def close_viewer(self, name):
        w = self.viewers[name]
        del self.viewers[name]
        # remove variable from namespace
        del self.kapp.namespace[name]
        w.close()

    def load_file(self, filepath, name):
        image = AstroImage.AstroImage(logger=self.logger)
        image.load_file(filepath)

        fi = self.kapp.namespace[name]
        fi.set_image(image)

    def open_file(self, name):
        res = QtGui.QFileDialog.getOpenFileName(self, "Open FITS file",
                                                ".", "FITS files (*.fits)")
        if isinstance(res, tuple):
            fileName = res[0].encode('ascii')
        else:
            fileName = str(res)
        self.load_file(fileName, name)

    def drop_file(self, fitsimage, paths, name):
        fileName = paths[0]
        self.load_file(fileName, name)

    def motion(self, fitsimage, button, data_x, data_y, readout):

        # Get the value under the data coordinates
        try:
            #value = fitsimage.get_data(data_x, data_y)
            # We report the value across the pixel, even though the coords
            # change halfway across the pixel
            value = fitsimage.get_data(int(data_x+0.5), int(data_y+0.5))

        except Exception:
            value = None

        fits_x, fits_y = data_x + 1, data_y + 1

        # Calculate WCS RA
        try:
            # NOTE: image function operates on DATA space coords
            image = fitsimage.get_image()
            if image == None:
                # No image loaded
                return
            ra_txt, dec_txt = image.pixtoradec(fits_x, fits_y,
                                               format='str', coords='fits')
        except Exception as e:
            self.logger.warn("Bad coordinate conversion: %s" % (
                str(e)))
            ra_txt  = 'BAD WCS'
            dec_txt = 'BAD WCS'

        text = "RA: %s  DEC: %s  X: %.2f  Y: %.2f  Value: %s" % (
            ra_txt, dec_txt, fits_x, fits_y, value)
        readout.setText(text)

    def quit(self):
        names = list(self.viewers.keys())
        for name in names:
            self.close_viewer(name)
        self.kapp.cleanup_consoles()
        self.close()

def start(kapp):
    global app_ref

    # create Qt app
    app = QtGui.QApplication([])
    app.connect(app, QtCore.SIGNAL('lastWindowClosed()'),
                app, QtCore.SLOT('quit()'))

    # create a logger
    logger = logging.getLogger("example1")
    logger.setLevel(logging.INFO)
    fmt = logging.Formatter(STD_FORMAT)
    # stderrHdlr = logging.StreamHandler()
    # stderrHdlr.setFormatter(fmt)
    # logger.addHandler(stderrHdlr)
    fileHdlr = logging.FileHandler("/dev/null")
    fileHdlr.setFormatter(fmt)
    logger.addHandler(fileHdlr)

    # here is our little launcher
    w = StartMenu(logger, app, kapp)
    app_ref = w
    w.show()
    app.setActiveWindow(w)

    #app.exec_()
    # Very important, IPython-specific step: this gets GUI event loop
    # integration going, and it replaces calling app.exec_()
    kapp.start()
    return w

def nb_start():
    """Call this function if importing from Qt notebook."""
    kapp = SimpleKernelApp('qt', get_ipython())
    return start(kapp)

def main(options, args):
    """Call this function if running as standalone app."""
    kapp = SimpleKernelApp('qt', None)
    return start(kapp)

if __name__ == '__main__':
    main(None, sys.argv[1:])

# END
<commit_msg>Fix for a renamed module in the ipython source<commit_after>#! /usr/bin/env python
#
# ipg.py -- Example of Ginga widget interaction with IPython.
#
# Eric Jeschke (eric@naoj.org)
#
# Contains code from IPython tutorial programs "qtapp_ip.py" and "kapp.py".
#
import sys, os
import logging

from ginga import AstroImage
from ginga.qtw.QtHelp import QtGui, QtCore
from ginga.qtw.FitsImageCanvasQt import FitsImageCanvas

from IPython.lib.kernel import connect_qtconsole
from IPython.kernel.zmq.kernelapp import IPKernelApp

STD_FORMAT = '%(asctime)s | %(levelname)1.1s | %(filename)s:%(lineno)d (%(funcName)s) | %(message)s'

# global ref to keep an object from being collected
app_ref = None

class SimpleKernelApp(object):
    """A minimal object that uses an IPython kernel and has a few methods
    to manipulate a namespace and open Qt consoles tied to the kernel.

    Code is modified from IPython tutorial programs 'qtapp_ip.py' and
    'kapp.py'.
    """

    def __init__(self, gui, shell):
        self.shell = shell

        if shell == None:
            # Start IPython kernel with GUI event loop support
            self.ipkernel = IPKernelApp.instance()
            self.ipkernel.initialize(['python', '--gui=%s' % gui,
                                      #'--log-level=10'  # for debugging
                                      ])
            # This application will also act on the shell user namespace
            self.namespace = self.ipkernel.shell.user_ns

        else:
            self.ipkernel = shell.config.IPKernelApp
            self.namespace = shell.user_ns

        # To create and track active qt consoles
        self.consoles = []

    def new_qt_console(self, evt=None):
        """start a new qtconsole connected to our kernel"""
        if hasattr(self.ipkernel, 'profile'):
            return connect_qtconsole(self.ipkernel.connection_file,
                                     profile=self.ipkernel.profile)
        else:
            return connect_qtconsole(self.ipkernel.connection_file)

    def cleanup_consoles(self, evt=None):
        for c in self.consoles:
            c.kill()

    def start(self):
        if self.shell == None:
            self.ipkernel.start()

class StartMenu(QtGui.QMainWindow):

    def __init__(self, logger, app, kapp):
        super(StartMenu, self).__init__()
        self.logger = logger
        self.count = 0
        self.viewers = {}
        self.app = app
        self.kapp = kapp
        self.app.aboutToQuit.connect(self.quit)

        vbox = QtGui.QVBoxLayout()
        vbox.setContentsMargins(QtCore.QMargins(2, 2, 2, 2))
        vbox.setSpacing(1)

        hbox = QtGui.QHBoxLayout()
        hbox.setContentsMargins(QtCore.QMargins(4, 2, 4, 2))

        console = QtGui.QPushButton('Qt Console')
        if self.kapp.ipkernel == None:
            console.setEnabled(False)
        console.clicked.connect(self.kapp.new_qt_console)

        newfits = QtGui.QPushButton('New Viewer')
        newfits.clicked.connect(self.new_viewer)

        wquit = QtGui.QPushButton("Quit")
        wquit.clicked.connect(self.quit)

        hbox.addStretch(1)
        for w in (console, newfits, wquit):
            hbox.addWidget(w, stretch=0)

        hw = QtGui.QWidget()
        hw.setLayout(hbox)
        vbox.addWidget(hw, stretch=0)

        vw = QtGui.QWidget()
        self.setCentralWidget(vw)
        vw.setLayout(vbox)
        self.setWindowTitle("Ginga IPython Console")

    def new_viewer(self, name=None):
        if not name:
            self.count += 1
            name = 'v%d' % self.count

        # create a ginga basic object for user interaction
        fi = FitsImageCanvas(self.logger, render='widget')
        fi.enable_autocuts('on')
        fi.set_autocut_params('zscale')
        fi.enable_autozoom('on')
        fi.enable_draw(True)
        fi.set_drawtype('ruler')
        fi.set_drawcolor('blue')
        fi.set_callback('drag-drop', self.drop_file, name)
        fi.set_bg(0.2, 0.2, 0.2)
        fi.ui_setActive(True)

        # expose the fits viewer to the shared namespace
        self.kapp.namespace[name] = fi

        # enable various interactive operations
        bd = fi.get_bindings()
        bd.enable_pan(True)
        bd.enable_zoom(True)
        bd.enable_cuts(True)
        bd.enable_flip(True)
        bd.enable_rotate(True)
        bd.enable_cmap(True)

        # get the ginga Qt widget
        w = fi.get_widget()
        w.resize(512, 512)

        # pack it into a qt window with a couple other buttons
        vw = QtGui.QWidget()
        vbox = QtGui.QVBoxLayout()
        vbox.setContentsMargins(QtCore.QMargins(2, 2, 2, 2))
        vbox.setSpacing(1)
        vbox.addWidget(w, stretch=1)

        # for simple WCS readout
        readout = QtGui.QLabel("")
        fi.set_callback('none-move', self.motion, readout)
        vbox.addWidget(readout, stretch=0,
                       alignment=QtCore.Qt.AlignCenter)

        hbox = QtGui.QHBoxLayout()
        hbox.setContentsMargins(QtCore.QMargins(4, 2, 4, 2))

        wopen = QtGui.QPushButton("Open File")
        def _open_file(name):
            return lambda: self.open_file(name)
        wopen.clicked.connect(_open_file(name))
        wquit = QtGui.QPushButton("Close")
        def _close(name):
            return lambda: self.close_viewer(name)
        wquit.clicked.connect(_close(name))

        hbox.addStretch(1)
        for w in (wopen, wquit):
            hbox.addWidget(w, stretch=0)

        hw = QtGui.QWidget()
        hw.setLayout(hbox)
        vbox.addWidget(hw, stretch=0)

        vw.setLayout(vbox)
        vw.resize(524, 540)
        vw.setWindowTitle("Ginga: %s" % name)
        self.viewers[name] = vw
        vw.show()

    def close_viewer(self, name):
        w = self.viewers[name]
        del self.viewers[name]
        # remove variable from namespace
        del self.kapp.namespace[name]
        w.close()

    def load_file(self, filepath, name):
        image = AstroImage.AstroImage(logger=self.logger)
        image.load_file(filepath)

        fi = self.kapp.namespace[name]
        fi.set_image(image)

    def open_file(self, name):
        res = QtGui.QFileDialog.getOpenFileName(self, "Open FITS file",
                                                ".", "FITS files (*.fits)")
        if isinstance(res, tuple):
            fileName = res[0].encode('ascii')
        else:
            fileName = str(res)
        self.load_file(fileName, name)

    def drop_file(self, fitsimage, paths, name):
        fileName = paths[0]
        self.load_file(fileName, name)

    def motion(self, fitsimage, button, data_x, data_y, readout):

        # Get the value under the data coordinates
        try:
            #value = fitsimage.get_data(data_x, data_y)
            # We report the value across the pixel, even though the coords
            # change halfway across the pixel
            value = fitsimage.get_data(int(data_x+0.5), int(data_y+0.5))

        except Exception:
            value = None

        fits_x, fits_y = data_x + 1, data_y + 1

        # Calculate WCS RA
        try:
            # NOTE: image function operates on DATA space coords
            image = fitsimage.get_image()
            if image == None:
                # No image loaded
                return
            ra_txt, dec_txt = image.pixtoradec(fits_x, fits_y,
                                               format='str', coords='fits')
        except Exception as e:
            self.logger.warn("Bad coordinate conversion: %s" % (
                str(e)))
            ra_txt  = 'BAD WCS'
            dec_txt = 'BAD WCS'

        text = "RA: %s  DEC: %s  X: %.2f  Y: %.2f  Value: %s" % (
            ra_txt, dec_txt, fits_x, fits_y, value)
        readout.setText(text)

    def quit(self):
        names = list(self.viewers.keys())
        for name in names:
            self.close_viewer(name)
        self.kapp.cleanup_consoles()
        self.close()

def start(kapp):
    global app_ref

    # create Qt app
    app = QtGui.QApplication([])
    app.connect(app, QtCore.SIGNAL('lastWindowClosed()'),
                app, QtCore.SLOT('quit()'))

    # create a logger
    logger = logging.getLogger("example1")
    logger.setLevel(logging.INFO)
    fmt = logging.Formatter(STD_FORMAT)
    # stderrHdlr = logging.StreamHandler()
    # stderrHdlr.setFormatter(fmt)
    # logger.addHandler(stderrHdlr)
    fileHdlr = logging.FileHandler("/dev/null")
    fileHdlr.setFormatter(fmt)
    logger.addHandler(fileHdlr)

    # here is our little launcher
    w = StartMenu(logger, app, kapp)
    app_ref = w
    w.show()
    app.setActiveWindow(w)

    #app.exec_()
    # Very important, IPython-specific step: this gets GUI event loop
    # integration going, and it replaces calling app.exec_()
    kapp.start()
    return w

def nb_start():
    """Call this function if importing from Qt notebook."""
    kapp = SimpleKernelApp('qt', get_ipython())
    return start(kapp)

def main(options, args):
    """Call this function if running as standalone app."""
    kapp = SimpleKernelApp('qt', None)
    return start(kapp)

if __name__ == '__main__':
    main(None, sys.argv[1:])

# END
<|endoftext|>


<commit_before>#!/usr/bin/python
#A script to add icons to an existing Openbox menu file
#This is untested and may not work at all as expected
#By Charles Bos

import sys
import os
import shutil

def usage() :
    print('''Usage:
  obiconadder.py <path to menu file>''')

def getDotDesktop(item, identifier) :
    for x in dotDesktops :
        file = open(x, "r", encoding = "utf-8")
        fileText = file.read()
        file.close()
        idStart = fileText.find(identifier)
        idEnd = fileText.find("\n", idStart)
        if item == fileText[idStart + 5:idEnd].split(" %")[0] : return x
    #As a fallback position, try and match the execute line from the file with
    #the .desktop filename
    if identifier == "Exec=" :
        for x in dotDesktops :
            if item == x.split("/")[-1].split(".desktop")[0] : return x
    return None

def getIcon(dotDesktop) :
    file = open(dotDesktop, encoding = "utf-8")
    fileText = file.read()
    file.close()
    iconStart = fileText.find("Icon=")
    if iconStart != -1 :
        iconEnd = fileText.find("\n", iconStart)
        for x in iconsList :
            if fileText[iconStart + 5:iconEnd] == x.split("/")[-1].split(".")[0] :
                return x
        return None
    else : return None

def getIconTheme() :
    #Try and get the icon theme from ~/.gtkrc-2.0
    homeDir = os.path.expanduser('~')
    try :
        file = open(homeDir + "/.gtkrc-2.0", "r")
        fileText = file.read()
        file.close()
        fileText = fileText.split("\n")
        for x in fileText :
            y = x.split("=")
            if y[0].strip() == "gtk-icon-theme-name" : theme = y[-1].strip().strip('"')
        if os.path.exists("/usr/share/icons/" + theme + "/48x48") : return theme
        else : return None
    except IOError :
        return None

#Handle args
args = sys.argv
if (len(args) != 2) or (args[1] == '-h') or (args[1] == '--help') :
    usage()
    os._exit(0)
elif os.path.isfile(args[1]) : menuFile = args[1]
else :
    usage()
    os._exit(0)

#Get locations of icons and .desktops
iconDirs = ["/usr/share/pixmaps", "/usr/share/icons/hicolor/48x48"]
if getIconTheme() != None : iconDirs.append("/usr/share/icons/" + getIconTheme() + "/48x48")
if os.path.exists("/usr/share/icons/gnome/48x48") : iconDirs.append("/usr/share/icons/gnome/48x48")
iconsList = []
for x in iconDirs :
    for root, dirs, files in os.walk(x, topdown=False):
        for name in files:
            iconsList.append(os.path.join(root, name))
dotDesktops = os.listdir("/usr/share/applications")
for x in dotDesktops : dotDesktops[dotDesktops.index(x)] = "/usr/share/applications/" + x

#Start scanning menu file
file = open(menuFile, "r")
fileText = file.read()
file.close()

#Get icons for applications
nameStart = fileText.find('<item label="')
nameEnd = fileText.find('">', nameStart)
while 0 <= nameStart <= len(fileText) :
    dotDesktop = getDotDesktop(fileText[nameStart + 13:nameEnd].split('" icon="')[0], "Name=")
    if dotDesktop != None : icon = getIcon(dotDesktop)
    else :
        #If identification by name fails, try by exec instead
        execStart = fileText.find("<execute>", nameEnd)
        execEnd = fileText.find("</execute>", execStart)
        dotDesktop = getDotDesktop(fileText[execStart + 9:execEnd], "Exec=")
        if dotDesktop != None : icon = getIcon(dotDesktop)
        else : icon = None
    if icon != None :
        fileText = fileText.replace(fileText[nameStart + 13:nameEnd], fileText[nameStart + 13:nameEnd].split('" icon="')[0] + '" icon="' + icon)
    else :
        fileText = fileText.replace(fileText[nameStart + 13:nameEnd], fileText[nameStart + 13:nameEnd].split('" icon="')[0])
    nameStart = fileText.find('<item label="', nameEnd)
    nameEnd = fileText.find('">', nameStart)

#Get icons for categories
#If user set theme cannot be found or does not contain an
#appropriate number of icons, fall back to gnome-icon-theme
try :
    if getIconTheme() != None :
        if len(os.listdir("/usr/share/icons/" + getIconTheme() + "/48x48/categories")) >= 15 :
            catTheme = getIconTheme()
    elif os.path.exists("/usr/share/icons/gnome/48x48") : catTheme = "gnome"
    else : catTheme = None
except IOError :
    if os.path.exists("/usr/share/icons/gnome/48x48") : catTheme = "gnome"
    else : catTheme = None

if catTheme != None :
    catStart = fileText.find('<menu id="')
    catEnd = fileText.find('" ', catStart)
    labelStart = fileText.find('label="', catEnd)
    labelEnd = fileText.find('">', labelStart)
    while 0 <= catStart <= len(fileText) :
        if os.path.isfile("/usr/share/icons/" + catTheme + "/48x48/categories/applications-" + fileText[labelStart + 7:labelEnd].lower() + ".png") :
            icon = "/usr/share/icons/" + catTheme + "/48x48/categories/applications-" + fileText[labelStart + 7:labelEnd].lower() + ".png"
        elif os.path.isfile("/usr/share/icons/" + catTheme + "/48x48/categories/package_" + fileText[labelStart + 7:labelEnd].lower() + ".png") :
            icon = "/usr/share/icons/" + catTheme + "/48x48/categories/package_" + fileText[labelStart + 7:labelEnd].lower() + ".png"
        else :
            icon = None
        if icon != None :
            fileText = fileText.replace(fileText[catStart:labelStart - 1], fileText[catStart:catEnd + 1] + ' icon="' + icon + '"')
        catStart = fileText.find('<menu id="', labelEnd)
        catEnd = fileText.find('" ', catStart)
        labelStart = fileText.find('label="', catEnd)
        labelEnd = fileText.find('">', labelStart)

#Backup original file
shutil.copyfile(menuFile, menuFile + ".bak")
#Now add the icons
#This will replace the contents of the old file
file = open(menuFile, "w")
print(fileText.rstrip("\n"), file = file)
file.close()<commit_msg>obiconadder.py - only perform replace on string extract and then reinsert into main string. this should completely fix the duplicate icon= entries on the first pass<commit_after>#!/usr/bin/python
#A script to add icons to an existing Openbox menu file
#This is untested and may not work at all as expected
#By Charles Bos

import sys
import os
import shutil

def usage() :
    print('''Usage:
  obiconadder.py <path to menu file>''')

def getDotDesktop(item, identifier) :
    for x in dotDesktops :
        file = open(x, "r", encoding = "utf-8")
        fileText = file.read()
        file.close()
        idStart = fileText.find(identifier)
        idEnd = fileText.find("\n", idStart)
        if item == fileText[idStart + 5:idEnd].split(" %")[0] : return x
    #As a fallback position, try and match the execute line from the file with
    #the .desktop filename
    if identifier == "Exec=" :
        for x in dotDesktops :
            if item == x.split("/")[-1].split(".desktop")[0] : return x
    return None

def getIcon(dotDesktop) :
    file = open(dotDesktop, encoding = "utf-8")
    fileText = file.read()
    file.close()
    iconStart = fileText.find("Icon=")
    if iconStart != -1 :
        iconEnd = fileText.find("\n", iconStart)
        for x in iconsList :
            if fileText[iconStart + 5:iconEnd] == x.split("/")[-1].split(".")[0] :
                return x
        return None
    else : return None

def getIconTheme() :
    #Try and get the icon theme from ~/.gtkrc-2.0
    homeDir = os.path.expanduser('~')
    try :
        file = open(homeDir + "/.gtkrc-2.0", "r")
        fileText = file.read()
        file.close()
        fileText = fileText.split("\n")
        for x in fileText :
            y = x.split("=")
            if y[0].strip() == "gtk-icon-theme-name" : theme = y[-1].strip().strip('"')
        if os.path.exists("/usr/share/icons/" + theme + "/48x48") : return theme
        else : return None
    except IOError :
        return None

#Handle args
args = sys.argv
if (len(args) != 2) or (args[1] == '-h') or (args[1] == '--help') :
    usage()
    os._exit(0)
elif os.path.isfile(args[1]) : menuFile = args[1]
else :
    usage()
    os._exit(0)

#Get locations of icons and .desktops
iconDirs = ["/usr/share/pixmaps", "/usr/share/icons/hicolor/48x48"]
if getIconTheme() != None : iconDirs.append("/usr/share/icons/" + getIconTheme() + "/48x48")
if os.path.exists("/usr/share/icons/gnome/48x48") : iconDirs.append("/usr/share/icons/gnome/48x48")
iconsList = []
for x in iconDirs :
    for root, dirs, files in os.walk(x, topdown=False):
        for name in files:
            iconsList.append(os.path.join(root, name))
dotDesktops = os.listdir("/usr/share/applications")
for x in dotDesktops : dotDesktops[dotDesktops.index(x)] = "/usr/share/applications/" + x

#Start scanning menu file
file = open(menuFile, "r")
fileText = file.read()
file.close()

#Get icons for applications
nameStart = fileText.find('<item label="')
nameEnd = fileText.find('">', nameStart)
while 0 <= nameStart <= len(fileText) :
    dotDesktop = getDotDesktop(fileText[nameStart + 13:nameEnd].split('" icon="')[0], "Name=")
    if dotDesktop != None : icon = getIcon(dotDesktop)
    else :
        #If identification by name fails, try by exec instead
        execStart = fileText.find("<execute>", nameEnd)
        execEnd = fileText.find("</execute>", execStart)
        dotDesktop = getDotDesktop(fileText[execStart + 9:execEnd], "Exec=")
        if dotDesktop != None : icon = getIcon(dotDesktop)
        else : icon = None
    newEntry = fileText[nameStart + 13:nameEnd]
    if icon != None :
        newEntry = newEntry.replace(fileText[nameStart + 13:nameEnd], fileText[nameStart + 13:nameEnd].split('" icon="')[0] + '" icon="' + icon)
    else :
        newEntry = newEntry.replace(fileText[nameStart + 13:nameEnd], fileText[nameStart + 13:nameEnd].split('" icon="')[0])
    fileText = fileText[:nameStart + 13] + newEntry + fileText[nameEnd:]
    nameStart = fileText.find('<item label="', nameEnd)
    nameEnd = fileText.find('">', nameStart)

#Get icons for categories
#If user set theme cannot be found or does not contain an
#appropriate number of icons, fall back to gnome-icon-theme
try :
    if getIconTheme() != None :
        if len(os.listdir("/usr/share/icons/" + getIconTheme() + "/48x48/categories")) >= 15 :
            catTheme = getIconTheme()
    elif os.path.exists("/usr/share/icons/gnome/48x48") : catTheme = "gnome"
    else : catTheme = None
except IOError :
    if os.path.exists("/usr/share/icons/gnome/48x48") : catTheme = "gnome"
    else : catTheme = None

if catTheme != None :
    catStart = fileText.find('<menu id="')
    catEnd = fileText.find('" ', catStart)
    labelStart = fileText.find('label="', catEnd)
    labelEnd = fileText.find('">', labelStart)
    while 0 <= catStart <= len(fileText) :
        if os.path.isfile("/usr/share/icons/" + catTheme + "/48x48/categories/applications-" + fileText[labelStart + 7:labelEnd].lower() + ".png") :
            icon = "/usr/share/icons/" + catTheme + "/48x48/categories/applications-" + fileText[labelStart + 7:labelEnd].lower() + ".png"
        elif os.path.isfile("/usr/share/icons/" + catTheme + "/48x48/categories/package_" + fileText[labelStart + 7:labelEnd].lower() + ".png") :
            icon = "/usr/share/icons/" + catTheme + "/48x48/categories/package_" + fileText[labelStart + 7:labelEnd].lower() + ".png"
        else :
            icon = None
        if icon != None :
            fileText = fileText.replace(fileText[catStart:labelStart - 1], fileText[catStart:catEnd + 1] + ' icon="' + icon + '"')
        catStart = fileText.find('<menu id="', labelEnd)
        catEnd = fileText.find('" ', catStart)
        labelStart = fileText.find('label="', catEnd)
        labelEnd = fileText.find('">', labelStart)

#Backup original file
shutil.copyfile(menuFile, menuFile + ".bak")
#Now add the icons
#This will replace the contents of the old file
file = open(menuFile, "w")
print(fileText.rstrip("\n"), file = file)
file.close()<|endoftext|>


<commit_before>from astropy.visualization.mpl_normalize import ImageNormalize
from astropy.io import fits
from astropy.wcs import WCS
#from rand_bkg import bkg_boxes
from astropy.convolution import Gaussian2DKernel
from astropy.stats import sigma_clipped_stats
from photutils.detection import detect_sources
from photutils.utils import random_cmap
from scipy.ndimage import binary_dilation
import pandas as pd
import numpy as np
import os
import glob
from collections import OrderedDict
from astropy.io import fits

def get_sdss_coords_offline(img, ota, inst,output='test.sdss'):
    hdulist = fits.open(img)
    hdu = hdulist[ota]

    if inst == 'podi':
        pvlist = hdu.header['PV*']
        for pv in pvlist:
            tpv = 'T'+pv
            hdu.header.rename_keyword(pv, tpv, force=False)
    xdim = hdu.header['NAXIS1']
    ydim = hdu.header['NAXIS2']

    sdss_cat_img = hdulist['CAT.PHOTCALIB']
    sdss_cat_img_df = pd.DataFrame.from_dict(sdss_cat_img.data)

    ota = float(ota.strip('OTA.SCI'))
    try:
        ota_matches_df = sdss_cat_img_df.iloc[np.where(sdss_cat_img_df['ODI_OTA'] == ota)]
        needed_columns = ['SDSS_RA','SDSS_DEC','SDSS_MAG_U',
                          'SDSS_ERR_U', u'SDSS_MAG_G', u'SDSS_ERR_G', u'SDSS_MAG_R',
                          'SDSS_ERR_R', u'SDSS_MAG_I', u'SDSS_ERR_I', u'SDSS_MAG_Z',
                          'SDSS_ERR_Z','ODI_OTA']

        output_df = ota_matches_df[needed_columns]
        output_df.to_csv(output,index=False)
    except KeyError:
        oditable = hdulist['CAT.ODI'].data
        oditalbe_df = pd.DataFrame.from_dict(oditable)

        ODI_RA = np.squeeze(np.array(oditalbe_df['RA']))
        ODI_DEC = np.squeeze( np.array(oditalbe_df['DEC']))
        ODI_OTA = np.squeeze( np.array(oditalbe_df['OTA']))

        junkdict = OrderedDict([('ODI_RA',ODI_RA),
                                ('ODI_DEC',ODI_DEC),
                                ('ODI_OTA',ODI_OTA.astype(float))])
        junk_df = pd.DataFrame.from_dict(junkdict)

        matched_df = pd.merge(sdss_cat_img_df,junk_df ,on = ['ODI_RA','ODI_DEC'],how='inner')

        needed_columns = np.insert(sdss_cat_img_df.columns.values,0,'ODI_OTA')

        full_df = matched_df[needed_columns]
        print ota
        ota_matches_df = full_df.iloc[np.where(full_df['ODI_OTA'] == ota)]
        needed_columns = ['SDSS_RA','SDSS_DEC','SDSS_MAG_U',
                          'SDSS_ERR_U', u'SDSS_MAG_G', u'SDSS_ERR_G', u'SDSS_MAG_R',
                          'SDSS_ERR_R', u'SDSS_MAG_I', u'SDSS_ERR_I', u'SDSS_MAG_Z',
                          'SDSS_ERR_Z','ODI_OTA']
        output_df = full_df[needed_columns]
        output_df.to_csv(output,index=False)
    hdulist.close()
    return xdim, ydim

def get_2mass_coords_offline(img, ota, inst,output='test.mass'):
    hdulist = odi.fits.open(img)
    hdu = hdulist[ota]

    if inst == 'podi':
        pvlist = hdu.header['PV*']
        for pv in pvlist:
            tpv = 'T'+pv
            hdu.header.rename_keyword(pv, tpv, force=False)
    xdim = hdu.header['NAXIS1']
    ydim = hdu.header['NAXIS2']

    mass_cat_img = hdulist['CAT.ODI+2MASS']
    mass_cat_img_df = mass_cat_img.data
    hdulist.close()

    ota = float(ota.strip('OTA.SCI'))
    ota_matches_df = mass_cat_img_df[np.where(mass_cat_img_df['OTA'] == ota)]

    junk_df = pd.DataFrame.from_dict(ota_matches_df)

    output_df = junk_df
    output_df.to_csv(output,index=False)
    return xdim, ydim

def main():
    pass

if __name__ == '__main__':
    main()<commit_msg>fix python imports<commit_after>from astropy.visualization.mpl_normalize import ImageNormalize
from astropy.wcs import WCS
#from rand_bkg import bkg_boxes
from astropy.convolution import Gaussian2DKernel
from astropy.stats import sigma_clipped_stats
from photutils.detection import detect_sources
from photutils.utils import random_cmap
from scipy.ndimage import binary_dilation
import pandas as pd
import numpy as np
import os
import glob
from collections import OrderedDict
from astropy.io import fits
import odi_config as odi

def get_sdss_coords_offline(img, ota, inst,output='test.sdss'):
    hdulist = odi.fits.open(img)
    hdu = hdulist[ota]

    if inst == 'podi':
        pvlist = hdu.header['PV*']
        for pv in pvlist:
            tpv = 'T'+pv
            hdu.header.rename_keyword(pv, tpv, force=False)
    xdim = hdu.header['NAXIS1']
    ydim = hdu.header['NAXIS2']

    sdss_cat_img = hdulist['CAT.PHOTCALIB']
    sdss_cat_img_df = pd.DataFrame.from_dict(sdss_cat_img.data)

    ota = float(ota.strip('OTA.SCI'))
    try:
        ota_matches_df = sdss_cat_img_df.iloc[np.where(sdss_cat_img_df['ODI_OTA'] == ota)]
        needed_columns = ['SDSS_RA','SDSS_DEC','SDSS_MAG_U',
                          'SDSS_ERR_U', u'SDSS_MAG_G', u'SDSS_ERR_G', u'SDSS_MAG_R',
                          'SDSS_ERR_R', u'SDSS_MAG_I', u'SDSS_ERR_I', u'SDSS_MAG_Z',
                          'SDSS_ERR_Z','ODI_OTA']

        output_df = ota_matches_df[needed_columns]
        output_df.to_csv(output,index=False)
    except KeyError:
        oditable = hdulist['CAT.ODI'].data
        oditalbe_df = pd.DataFrame.from_dict(oditable)

        ODI_RA = np.squeeze(np.array(oditalbe_df['RA']))
        ODI_DEC = np.squeeze( np.array(oditalbe_df['DEC']))
        ODI_OTA = np.squeeze( np.array(oditalbe_df['OTA']))

        junkdict = OrderedDict([('ODI_RA',ODI_RA),
                                ('ODI_DEC',ODI_DEC),
                                ('ODI_OTA',ODI_OTA.astype(float))])
        junk_df = pd.DataFrame.from_dict(junkdict)

        matched_df = pd.merge(sdss_cat_img_df,junk_df ,on = ['ODI_RA','ODI_DEC'],how='inner')

        needed_columns = np.insert(sdss_cat_img_df.columns.values,0,'ODI_OTA')

        full_df = matched_df[needed_columns]
        print ota
        ota_matches_df = full_df.iloc[np.where(full_df['ODI_OTA'] == ota)]
        needed_columns = ['SDSS_RA','SDSS_DEC','SDSS_MAG_U',
                          'SDSS_ERR_U', u'SDSS_MAG_G', u'SDSS_ERR_G', u'SDSS_MAG_R',
                          'SDSS_ERR_R', u'SDSS_MAG_I', u'SDSS_ERR_I', u'SDSS_MAG_Z',
                          'SDSS_ERR_Z','ODI_OTA']
        output_df = full_df[needed_columns]
        output_df.to_csv(output,index=False)
    hdulist.close()
    return xdim, ydim

def get_2mass_coords_offline(img, ota, inst,output='test.mass'):
    hdulist = odi.fits.open(img)
    hdu = hdulist[ota]

    if inst == 'podi':
        pvlist = hdu.header['PV*']
        for pv in pvlist:
            tpv = 'T'+pv
            hdu.header.rename_keyword(pv, tpv, force=False)
    xdim = hdu.header['NAXIS1']
    ydim = hdu.header['NAXIS2']

    mass_cat_img = hdulist['CAT.ODI+2MASS']
    mass_cat_img_df = mass_cat_img.data
    hdulist.close()

    ota = float(ota.strip('OTA.SCI'))
    ota_matches_df = mass_cat_img_df[np.where(mass_cat_img_df['OTA'] == ota)]

    junk_df = pd.DataFrame.from_dict(ota_matches_df)

    output_df = junk_df
    output_df.to_csv(output,index=False)
    return xdim, ydim

def main():
    pass

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


<commit_before>"""
This example prints the information available about a PicoScope.
"""

# this "if" statement is used so that Sphinx does not execute this script when the docs are being built
if __name__ == '__main__':

    from msl.examples.equipment.resources.picoscope import record

    scope = record.connect()
    print('Summary for {}'.format(record))

    # print all information that is available about the PicoScope
    print(scope.get_unit_info())

    print()

    # use the enum value to get the calibration date, do not print the member-name prefix
    print('The PicoScope was calibrated on {}'.format(scope.get_unit_info(5, False)))

    # use the enum member name to get the calibration date
    print(scope.get_unit_info('cal_date'))

    print('The current power source is ' + scope.current_power_source())
    if scope.IS_PS5000A:
        print('The device resolution is {!r}'.format(scope.get_device_resolution()))  # only valid for ps5000a scopes
    print('The maximum ADU value is {}'.format(scope.maximum_value()))
    print('The minimum ADU value is {}'.format(scope.minimum_value()))
    print('The voltage-offset range for a +/-1V voltage range is {}'.format(scope.get_analogue_offset('1V', 'dc')))
    print('The voltage ranges available for channel A are: {}'.format(scope.get_channel_information('A')))
    print('The maximum down-sampling ratio that can be used for a given number of samples in a given down-sampling '
          'mode is {}'.format(scope.get_max_down_sample_ratio(1000, mode='aggregate')))
    print('The maximum number of segments allowed is {}'.format(scope.get_max_segments()))
    print('AWG range of allowed sample values and waveform buffer sizes {}'.format(scope.sig_gen_arbitrary_min_max_values()))
<commit_msg>print empty string<commit_after>"""
This example prints the information available about a PicoScope.
"""

# this "if" statement is used so that Sphinx does not execute this script when the docs are being built
if __name__ == '__main__':

    from msl.examples.equipment.resources.picoscope import record

    scope = record.connect()
    print('Summary for {}'.format(record))

    # print all information that is available about the PicoScope
    print(scope.get_unit_info())

    print('')

    # use the enum value to get the calibration date, do not print the member-name prefix
    print('The PicoScope was calibrated on {}'.format(scope.get_unit_info(5, False)))

    # use the enum member name to get the calibration date
    print(scope.get_unit_info('cal_date'))

    print('The current power source is ' + scope.current_power_source())
    if scope.IS_PS5000A:
        print('The device resolution is {!r}'.format(scope.get_device_resolution()))  # only valid for ps5000a scopes
    print('The maximum ADU value is {}'.format(scope.maximum_value()))
    print('The minimum ADU value is {}'.format(scope.minimum_value()))
    print('The voltage-offset range for a +/-1V voltage range is {}'.format(scope.get_analogue_offset('1V', 'dc')))
    print('The voltage ranges available for channel A are: {}'.format(scope.get_channel_information('A')))
    print('The maximum down-sampling ratio that can be used for a given number of samples in a given down-sampling '
          'mode is {}'.format(scope.get_max_down_sample_ratio(1000, mode='aggregate')))
    print('The maximum number of segments allowed is {}'.format(scope.get_max_segments()))
    print('AWG range of allowed sample values and waveform buffer sizes {}'.format(scope.sig_gen_arbitrary_min_max_values()))
<|endoftext|>