<commit_before>"""Builder for CVs."""
import datetime as dt
import os
import sys
from copy import copy, deepcopy

from regolith.builders.basebuilder import LatexBuilderBase
from regolith.fsclient import _id_key
from regolith.dates import month_to_int
from regolith.sorters import position_key, doc_date_key
from regolith.builders.cpbuilder import is_current
from regolith.stylers import sentencecase, month_fullnames
from regolith.tools import (
    all_docs_from_collection,
    filter_publications,
    filter_projects,
    filter_grants,
    make_bibtex_file,
    fuzzy_retrieval,
    filter_employment_for_advisees,
    filter_service,
    filter_facilities,
    filter_activities,
    filter_presentations,
    awards,
    filter_patents,
    filter_licenses,
    merge_collections)

BEGIN_YEAR = 2017
PERSON = "sbillinge"

class AppraisalBuilder(LatexBuilderBase):
    """Build CV from database entries"""

    btype = "ann-appraisal"

    def construct_global_ctx(self):
        """Constructs the global context"""
        super().construct_global_ctx()
        gtx = self.gtx
        rc = self.rc
        gtx["people"] = sorted(
            all_docs_from_collection(rc.client, "people"),
            key=position_key,
            reverse=True,
        )
        gtx["institutions"] = sorted(
            all_docs_from_collection(rc.client, "institutions"), key=_id_key
        )
        gtx["grants"] = sorted(
            all_docs_from_collection(rc.client, "grants"), key=_id_key
        )
        gtx["proposals"] = sorted(
            all_docs_from_collection(rc.client, "proposals"), key=_id_key
        )
        gtx["projects"] = sorted(
            all_docs_from_collection(rc.client, "projects"), key=_id_key
        )
        gtx["presentations"] = sorted(
            all_docs_from_collection(rc.client, "presentations"), key=_id_key
        )
        gtx["patents"] = sorted(
            all_docs_from_collection(rc.client, "patents"), key=_id_key
        )
        gtx["all_docs_from_collection"] = all_docs_from_collection
        gtx["float"] = float
        gtx["str"] = str
        gtx["zip"] = zip

    def latex(self):
        """Render latex template"""
        begin_year = BEGIN_YEAR
        pre_begin_year = begin_year - 1
        end_year = begin_year + 1
        post_end_year = begin_year + 2
        begin_period = dt.date(begin_year, 4, 1)
        pre_begin_period = dt.date(pre_begin_year, 4, 1)
        end_period = dt.date(end_year, 3, 31)
        post_end_period = dt.date(post_end_year, 3, 31)

        rc = self.rc
        me = [p for p in self.gtx["people"] if p["_id"] == PERSON][0]
        me["begin_period"] = dt.date.strftime(begin_period, "%m/%d/%Y")
        me["begin_period"] = dt.date.strftime(begin_period, "%m/%d/%Y")
        me["pre_begin_period"] = dt.date.strftime(pre_begin_period, "%m/%d/%Y")
        me["end_period"] = dt.date.strftime(end_period, "%m/%d/%Y")
        me["post_end_period"] = dt.date.strftime(post_end_period, "%m/%d/%Y")
        projs = filter_projects(
            self.gtx["projects"], set([PERSON])
        )
        #########
        # current and pending
        #########
        pi = fuzzy_retrieval(
            self.gtx["people"], ["aka", "name", "_id"], PERSON
        )
        #        pi['initials'] = "SJLB"

        grants = merge_collections(self.gtx["proposals"], self.gtx["grants"],
                                   "proposal_id")
        for g in grants:
            for person in g["team"]:
                rperson = fuzzy_retrieval(
                    self.gtx["people"], ["aka", "name"], person["name"]
                )
                if rperson:
                    person["name"] = rperson["name"]

        current_grants = [
            dict(g)
            for g in grants
            if is_current(
                *[
                    g.get(s, 1)
                    for s in [
                        "begin_day",
                        "begin_month",
                        "begin_year",
                        "end_day",
                        "end_month",
                        "end_year",
                    ]
                ]
            )
        ]

        current_grants, _, _ = filter_grants(
            current_grants, {pi["name"]}, pi=False, multi_pi=True
        )
#        print("current: {}".format(current_grants))

        pending_grants = [
            g
            for g in self.gtx["proposals"]
            if g["status"] == "pending"
        ]
#        print("pending: {}".format(pending_grants))
        for g in pending_grants:
            for person in g["team"]:
                rperson = fuzzy_retrieval(
                    self.gtx["people"], ["aka", "name"], person["name"]
                )
                if rperson:
                    person["name"] = rperson["name"]
        pending_grants, _, _ = filter_grants(
            pending_grants, {pi["name"]}, pi=False, multi_pi=True
        )
        grants = pending_grants + current_grants
        for grant in grants:
            grant.update(
                award_start_date="{2}/{1}/{0}".format(
                    grant["begin_day"],
                    month_to_int(grant["begin_month"]),
                    grant["begin_year"],
                ),
                award_end_date="{2}/{1}/{0}".format(
                    grant["end_day"],
                    month_to_int(grant["end_month"]),
                    grant["end_year"],
                ),
            )
        badids = [i["_id"] for i in current_grants if not i['cpp_info'].get('cppflag', "")]
        iter = copy(current_grants)
        for grant in iter:
            if grant["_id"] in badids:
                current_grants.remove(grant)
        #########
        # end current and pending
        #########

        #########
        # advising
        #########
        undergrads = filter_employment_for_advisees(self.gtx["people"],
                                                    begin_period,
                                                    "undergrad")
        masters = filter_employment_for_advisees(self.gtx["people"],
                                                 begin_period,
                                                 "ms")
        currents = filter_employment_for_advisees(self.gtx["people"],
                                                  begin_period,
                                                  "phd")
        graduateds = filter_employment_for_advisees(self.gtx["people"],
                                                    begin_period.replace(
                                                        year=end_year - 5),
                                                    "phd")
        postdocs = filter_employment_for_advisees(self.gtx["people"],
                                                  begin_period,
                                                  "postdoc")
        visitors = filter_employment_for_advisees(self.gtx["people"],
                                                  begin_period,
                                                  "visitor")
        iter = deepcopy(graduateds)
        for g in iter:
            if g.get("active"):
                graduateds.remove(g)

        ######################
        # service
        #####################
        mego = deepcopy(me)
        dept_service = filter_service([mego],
                                      begin_period, "department")
        mego = deepcopy(me)
        school_service = filter_service([mego],
                                        begin_period, "school")
        mego = deepcopy(me)
        uni_service = filter_service([mego],
                                     begin_period, "university")
        uni_service.extend(school_service)
        mego = deepcopy(me)
        prof_service = filter_service([mego],
                                      begin_period, "profession")
        mego = deepcopy(me)
        outreach = filter_service([mego],
                                  begin_period, "outreach")
        mego = deepcopy(me)
        lab = filter_facilities([mego],
                                begin_period, "laboratory")
        mego = deepcopy(me)
        shared = filter_facilities([mego],
                                   begin_period, "shared")
        mego = deepcopy(me)
        fac_other = filter_facilities([mego],
                                      begin_period, "other")
        mego = deepcopy(me)
        fac_teaching = filter_facilities([mego],
                                         begin_period, "fac_teaching")
        mego = deepcopy(me)
        fac_wishlist = filter_facilities([mego],
                                         begin_period, "fac_wishlist",
                                         verbose=False)
        mego = deepcopy(me)
        tch_wishlist = filter_facilities([mego],
                                         begin_period, "tch_wishlist")
        mego = deepcopy(me)
        curric_dev = filter_activities([mego],
                                       begin_period, "teaching")
        mego = deepcopy(me)
        other_activities = filter_activities([mego],
                                             begin_period, "other")

        ##########################
        # Presentation list
        ##########################
        keypres = filter_presentations(self.gtx["people"],
                                       self.gtx["presentations"],
                                       self.gtx["institutions"],
                                       types=["award", "plenary", "keynote"],
                                       since=begin_period, before=end_period,
                                       statuses=["accepted"])
        invpres = filter_presentations(self.gtx["people"],
                                       self.gtx["presentations"],
                                       self.gtx["institutions"],
                                       types=["invited"],
                                       since=begin_period, before=end_period,
                                       statuses=["accepted"])
        sempres = filter_presentations(self.gtx["people"],
                                       self.gtx["presentations"],
                                       self.gtx["institutions"],
                                       types=["colloquium", "seminar"],
                                       since=begin_period, before=end_period,
                                       statuses=["accepted"])
        declpres = filter_presentations(self.gtx["people"],
                                        self.gtx["presentations"],
                                        self.gtx["institutions"],
                                        types=["all"],
                                        since=begin_period, before=end_period,
                                        statuses=["declined"])

        #########################
        # Awards
        #########################
        ahs = awards(me, since=begin_period)

        ########################
        # Publications
        ########################
        names = frozenset(me.get("aka", []) + [me["name"]])
        pubs = filter_publications(
            all_docs_from_collection(rc.client, "citations"),
            names,
            reverse=True,
            bold=False,
            since=begin_period
        )
        bibfile = make_bibtex_file(
            pubs, pid=me["_id"], person_dir=self.bldir
        )
        articles = [prc for prc in pubs if
                    prc.get("entrytype") in "article"]
        nonarticletypes = ["book", "inbook", "proceedings", "inproceedings",
                           "incollection", "unpublished", "phdthesis", "misc"]
        nonarticles = [prc for prc in pubs if
                       prc.get("entrytype") in nonarticletypes]
        peer_rev_conf_pubs = [prc for prc in pubs if prc.get("peer_rev_conf")]
        pubiter = deepcopy(pubs)
        for prc in pubiter:
            if prc.get("peer_rev_conf"):
                peer_rev_conf_pubs = prc
                pubs.pop(prc)

        ##############
        # TODO: add Current Projects to Research summary section
        ##############

        #############
        # IP
        #############
        patents = filter_patents(self.gtx["patents"], self.gtx["people"],
                                 PERSON, since=begin_period)
        licenses = filter_licenses(self.gtx["patents"], self.gtx["people"],
                                   PERSON, since=begin_period)
        #############
        # hindex
        #############
        hindex = sorted(me["hindex"], key=doc_date_key).pop()

        #########################
        # render
        #########################
        #            "C:/Users/simon/scratch/billinge-ann-report.tex",
        self.render(
            "columbia_annual_report.tex",
            "billinge-ann-report.tex",
            pi=pi,
            p=me,
            projects=projs,
            pending=pending_grants,
            current=current_grants,
            undergrads=undergrads,
            masters=masters,
            currentphds=currents,
            graduatedphds=graduateds,
            postdocs=postdocs,
            visitors=visitors,
            dept_service=dept_service,
            uni_service=uni_service,
            prof_service=prof_service,
            outreach=outreach,
            lab=lab,
            shared=shared,
            facilities_other=fac_other,
            fac_teaching=fac_teaching,
            fac_wishlist=fac_wishlist,
            tch_wishlist=tch_wishlist,
            curric_dev=curric_dev,
            other_activities=other_activities,
            keypres=keypres,
            invpres=invpres,
            sempres=sempres,
            declpres=declpres,
            sentencecase=sentencecase,
            monthstyle=month_fullnames,
            ahs=ahs,
            pubs=articles,
            nonarticles=nonarticles,
            peer_rev_conf_pubs=peer_rev_conf_pubs,
            bibfile=bibfile,
            patents=patents,
            licenses=licenses,
            hindex=hindex,
        )
        self.pdf("billinge-ann-report")
<commit_msg>explicit begin and end year and month<commit_after>"""Builder for CVs."""
import datetime as dt
import os
import sys
from copy import copy, deepcopy

from regolith.builders.basebuilder import LatexBuilderBase
from regolith.fsclient import _id_key
from regolith.dates import month_to_int
from regolith.sorters import position_key, doc_date_key
from regolith.builders.cpbuilder import is_current
from regolith.stylers import sentencecase, month_fullnames
from regolith.tools import (
    all_docs_from_collection,
    filter_publications,
    filter_projects,
    filter_grants,
    make_bibtex_file,
    fuzzy_retrieval,
    filter_employment_for_advisees,
    filter_service,
    filter_facilities,
    filter_activities,
    filter_presentations,
    awards,
    filter_patents,
    filter_licenses,
    merge_collections)

BEGIN_YEAR = 2018
BEGIN_MONTH = 4
PERSON = "sbillinge"

class AppraisalBuilder(LatexBuilderBase):
    """Build CV from database entries"""

    btype = "ann-appraisal"

    def construct_global_ctx(self):
        """Constructs the global context"""
        super().construct_global_ctx()
        gtx = self.gtx
        rc = self.rc
        gtx["people"] = sorted(
            all_docs_from_collection(rc.client, "people"),
            key=position_key,
            reverse=True,
        )
        gtx["institutions"] = sorted(
            all_docs_from_collection(rc.client, "institutions"), key=_id_key
        )
        gtx["grants"] = sorted(
            all_docs_from_collection(rc.client, "grants"), key=_id_key
        )
        gtx["proposals"] = sorted(
            all_docs_from_collection(rc.client, "proposals"), key=_id_key
        )
        gtx["projects"] = sorted(
            all_docs_from_collection(rc.client, "projects"), key=_id_key
        )
        gtx["presentations"] = sorted(
            all_docs_from_collection(rc.client, "presentations"), key=_id_key
        )
        gtx["patents"] = sorted(
            all_docs_from_collection(rc.client, "patents"), key=_id_key
        )
        gtx["all_docs_from_collection"] = all_docs_from_collection
        gtx["float"] = float
        gtx["str"] = str
        gtx["zip"] = zip

    def latex(self):
        """Render latex template"""
        begin_year = BEGIN_YEAR
        begin_month = BEGIN_MONTH
        pre_begin_year = begin_year - 1
        end_year = begin_year + 1
        end_month = begin_month-1
        post_end_year = begin_year + 2
        begin_period = dt.date(begin_year, begin_month, 1)
        pre_begin_period = dt.date(pre_begin_year, begin_month, 1)
        end_period = dt.date(end_year, end_month, 31)
        post_end_period = dt.date(post_end_year, end_month, 31)

        rc = self.rc
        me = [p for p in self.gtx["people"] if p["_id"] == PERSON][0]
        me["begin_period"] = dt.date.strftime(begin_period, "%m/%d/%Y")
        me["begin_period"] = dt.date.strftime(begin_period, "%m/%d/%Y")
        me["pre_begin_period"] = dt.date.strftime(pre_begin_period, "%m/%d/%Y")
        me["end_period"] = dt.date.strftime(end_period, "%m/%d/%Y")
        me["post_end_period"] = dt.date.strftime(post_end_period, "%m/%d/%Y")
        projs = filter_projects(
            self.gtx["projects"], set([PERSON])
        )
        #########
        # current and pending
        #########
        pi = fuzzy_retrieval(
            self.gtx["people"], ["aka", "name", "_id"], PERSON
        )
        #        pi['initials'] = "SJLB"

        grants = merge_collections(self.gtx["proposals"], self.gtx["grants"],
                                   "proposal_id")
        for g in grants:
            for person in g["team"]:
                rperson = fuzzy_retrieval(
                    self.gtx["people"], ["aka", "name"], person["name"]
                )
                if rperson:
                    person["name"] = rperson["name"]

        current_grants = [
            dict(g)
            for g in grants
            if is_current(
                *[
                    g.get(s, 1)
                    for s in [
                        "begin_day",
                        "begin_month",
                        "begin_year",
                        "end_day",
                        "end_month",
                        "end_year",
                    ]
                ]
            )
        ]

        current_grants, _, _ = filter_grants(
            current_grants, {pi["name"]}, pi=False, multi_pi=True
        )
#        print("current: {}".format(current_grants))

        pending_grants = [
            g
            for g in self.gtx["proposals"]
            if g["status"] == "pending"
        ]
#        print("pending: {}".format(pending_grants))
        for g in pending_grants:
            for person in g["team"]:
                rperson = fuzzy_retrieval(
                    self.gtx["people"], ["aka", "name"], person["name"]
                )
                if rperson:
                    person["name"] = rperson["name"]
        pending_grants, _, _ = filter_grants(
            pending_grants, {pi["name"]}, pi=False, multi_pi=True
        )
        grants = pending_grants + current_grants
        for grant in grants:
            grant.update(
                award_start_date="{2}/{1}/{0}".format(
                    grant["begin_day"],
                    month_to_int(grant["begin_month"]),
                    grant["begin_year"],
                ),
                award_end_date="{2}/{1}/{0}".format(
                    grant["end_day"],
                    month_to_int(grant["end_month"]),
                    grant["end_year"],
                ),
            )
        badids = [i["_id"] for i in current_grants if not i['cpp_info'].get('cppflag', "")]
        iter = copy(current_grants)
        for grant in iter:
            if grant["_id"] in badids:
                current_grants.remove(grant)
        #########
        # end current and pending
        #########

        #########
        # advising
        #########
        undergrads = filter_employment_for_advisees(self.gtx["people"],
                                                    begin_period,
                                                    "undergrad")
        masters = filter_employment_for_advisees(self.gtx["people"],
                                                 begin_period,
                                                 "ms")
        currents = filter_employment_for_advisees(self.gtx["people"],
                                                  begin_period,
                                                  "phd")
        graduateds = filter_employment_for_advisees(self.gtx["people"],
                                                    begin_period.replace(
                                                        year=end_year - 5),
                                                    "phd")
        postdocs = filter_employment_for_advisees(self.gtx["people"],
                                                  begin_period,
                                                  "postdoc")
        visitors = filter_employment_for_advisees(self.gtx["people"],
                                                  begin_period,
                                                  "visitor")
        iter = deepcopy(graduateds)
        for g in iter:
            if g.get("active"):
                graduateds.remove(g)

        ######################
        # service
        #####################
        mego = deepcopy(me)
        dept_service = filter_service([mego],
                                      begin_period, "department")
        mego = deepcopy(me)
        school_service = filter_service([mego],
                                        begin_period, "school")
        mego = deepcopy(me)
        uni_service = filter_service([mego],
                                     begin_period, "university")
        uni_service.extend(school_service)
        mego = deepcopy(me)
        prof_service = filter_service([mego],
                                      begin_period, "profession")
        mego = deepcopy(me)
        outreach = filter_service([mego],
                                  begin_period, "outreach")
        mego = deepcopy(me)
        lab = filter_facilities([mego],
                                begin_period, "laboratory")
        mego = deepcopy(me)
        shared = filter_facilities([mego],
                                   begin_period, "shared")
        mego = deepcopy(me)
        fac_other = filter_facilities([mego],
                                      begin_period, "other")
        mego = deepcopy(me)
        fac_teaching = filter_facilities([mego],
                                         begin_period, "fac_teaching")
        mego = deepcopy(me)
        fac_wishlist = filter_facilities([mego],
                                         begin_period, "fac_wishlist",
                                         verbose=False)
        mego = deepcopy(me)
        tch_wishlist = filter_facilities([mego],
                                         begin_period, "tch_wishlist")
        mego = deepcopy(me)
        curric_dev = filter_activities([mego],
                                       begin_period, "teaching")
        mego = deepcopy(me)
        other_activities = filter_activities([mego],
                                             begin_period, "other")

        ##########################
        # Presentation list
        ##########################
        keypres = filter_presentations(self.gtx["people"],
                                       self.gtx["presentations"],
                                       self.gtx["institutions"],
                                       types=["award", "plenary", "keynote"],
                                       since=begin_period, before=end_period,
                                       statuses=["accepted"])
        invpres = filter_presentations(self.gtx["people"],
                                       self.gtx["presentations"],
                                       self.gtx["institutions"],
                                       types=["invited"],
                                       since=begin_period, before=end_period,
                                       statuses=["accepted"])
        sempres = filter_presentations(self.gtx["people"],
                                       self.gtx["presentations"],
                                       self.gtx["institutions"],
                                       types=["colloquium", "seminar"],
                                       since=begin_period, before=end_period,
                                       statuses=["accepted"])
        declpres = filter_presentations(self.gtx["people"],
                                        self.gtx["presentations"],
                                        self.gtx["institutions"],
                                        types=["all"],
                                        since=begin_period, before=end_period,
                                        statuses=["declined"])

        #########################
        # Awards
        #########################
        ahs = awards(me, since=begin_period)

        ########################
        # Publications
        ########################
        names = frozenset(me.get("aka", []) + [me["name"]])
        pubs = filter_publications(
            all_docs_from_collection(rc.client, "citations"),
            names,
            reverse=True,
            bold=False,
            since=begin_period
        )
        bibfile = make_bibtex_file(
            pubs, pid=me["_id"], person_dir=self.bldir
        )
        articles = [prc for prc in pubs if
                    prc.get("entrytype") in "article"]
        nonarticletypes = ["book", "inbook", "proceedings", "inproceedings",
                           "incollection", "unpublished", "phdthesis", "misc"]
        nonarticles = [prc for prc in pubs if
                       prc.get("entrytype") in nonarticletypes]
        peer_rev_conf_pubs = [prc for prc in pubs if prc.get("peer_rev_conf")]
        pubiter = deepcopy(pubs)
        for prc in pubiter:
            if prc.get("peer_rev_conf"):
                peer_rev_conf_pubs = prc
                pubs.pop(prc)

        ##############
        # TODO: add Current Projects to Research summary section
        ##############

        #############
        # IP
        #############
        patents = filter_patents(self.gtx["patents"], self.gtx["people"],
                                 PERSON, since=begin_period)
        licenses = filter_licenses(self.gtx["patents"], self.gtx["people"],
                                   PERSON, since=begin_period)
        #############
        # hindex
        #############
        hindex = sorted(me["hindex"], key=doc_date_key).pop()

        #########################
        # render
        #########################
        #            "C:/Users/simon/scratch/billinge-ann-report.tex",
        self.render(
            "columbia_annual_report.tex",
            "billinge-ann-report.tex",
            pi=pi,
            p=me,
            projects=projs,
            pending=pending_grants,
            current=current_grants,
            undergrads=undergrads,
            masters=masters,
            currentphds=currents,
            graduatedphds=graduateds,
            postdocs=postdocs,
            visitors=visitors,
            dept_service=dept_service,
            uni_service=uni_service,
            prof_service=prof_service,
            outreach=outreach,
            lab=lab,
            shared=shared,
            facilities_other=fac_other,
            fac_teaching=fac_teaching,
            fac_wishlist=fac_wishlist,
            tch_wishlist=tch_wishlist,
            curric_dev=curric_dev,
            other_activities=other_activities,
            keypres=keypres,
            invpres=invpres,
            sempres=sempres,
            declpres=declpres,
            sentencecase=sentencecase,
            monthstyle=month_fullnames,
            ahs=ahs,
            pubs=articles,
            nonarticles=nonarticles,
            peer_rev_conf_pubs=peer_rev_conf_pubs,
            bibfile=bibfile,
            patents=patents,
            licenses=licenses,
            hindex=hindex,
        )
        self.pdf("billinge-ann-report")
<|endoftext|>


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

import os
import shutil
import subprocess
import tempfile

from tempest.cmd import workspace
from tempest.lib.common.utils import data_utils
from tempest.tests import base

class TestTempestWorkspaceBase(base.TestCase):
    def setUp(self):
        super(TestTempestWorkspaceBase, self).setUp()
        self.name = data_utils.rand_uuid()
        self.path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.path, ignore_errors=True)
        store_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, store_dir, ignore_errors=True)
        self.store_file = os.path.join(store_dir, 'workspace.yaml')
        self.workspace_manager = workspace.WorkspaceManager(
            path=self.store_file)
        self.workspace_manager.register_new_workspace(self.name, self.path)

class TestTempestWorkspace(TestTempestWorkspaceBase):
    def _run_cmd_gets_return_code(self, cmd, expected):
        process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()
        return_code = process.returncode
        msg = ("%s failed with:\nstdout: %s\nstderr: %s" % (' '.join(cmd),
               stdout, stderr))
        self.assertEqual(return_code, expected, msg)

    def test_run_workspace_list(self):
        cmd = ['tempest', 'workspace', 'list',
               '--workspace-path', self.store_file]
        self._run_cmd_gets_return_code(cmd, 0)

    def test_run_workspace_register(self):
        name = data_utils.rand_uuid()
        path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, path, ignore_errors=True)
        cmd = ['tempest', 'workspace', 'register',
               '--workspace-path', self.store_file,
               '--name', name, '--path', path]
        self._run_cmd_gets_return_code(cmd, 0)
        self.assertIsNotNone(self.workspace_manager.get_workspace(name))

    def test_run_workspace_rename(self):
        new_name = data_utils.rand_uuid()
        cmd = ['tempest', 'workspace', 'rename',
               '--workspace-path', self.store_file,
               '--old-name', self.name, '--new-name', new_name]
        self._run_cmd_gets_return_code(cmd, 0)
        self.assertIsNone(self.workspace_manager.get_workspace(self.name))
        self.assertIsNotNone(self.workspace_manager.get_workspace(new_name))

    def test_run_workspace_move(self):
        new_path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, new_path, ignore_errors=True)
        cmd = ['tempest', 'workspace', 'move',
               '--workspace-path', self.store_file,
               '--name', self.name, '--path', new_path]
        self._run_cmd_gets_return_code(cmd, 0)
        self.assertEqual(
            self.workspace_manager.get_workspace(self.name), new_path)

    def test_run_workspace_remove_entry(self):
        cmd = ['tempest', 'workspace', 'remove',
               '--workspace-path', self.store_file,
               '--name', self.name]
        self._run_cmd_gets_return_code(cmd, 0)
        self.assertIsNone(self.workspace_manager.get_workspace(self.name))

    def test_run_workspace_remove_directory(self):
        cmd = ['tempest', 'workspace', 'remove',
               '--workspace-path', self.store_file,
               '--name', self.name, '--rmdir']
        self._run_cmd_gets_return_code(cmd, 0)
        self.assertIsNone(self.workspace_manager.get_workspace(self.name))

class TestTempestWorkspaceManager(TestTempestWorkspaceBase):
    def setUp(self):
        super(TestTempestWorkspaceManager, self).setUp()
        self.name = data_utils.rand_uuid()
        self.path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.path, ignore_errors=True)
        store_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, store_dir, ignore_errors=True)
        self.store_file = os.path.join(store_dir, 'workspace.yaml')
        self.workspace_manager = workspace.WorkspaceManager(
            path=self.store_file)
        self.workspace_manager.register_new_workspace(self.name, self.path)

    def test_workspace_manager_get(self):
        self.assertIsNotNone(self.workspace_manager.get_workspace(self.name))

    def test_workspace_manager_rename(self):
        new_name = data_utils.rand_uuid()
        self.workspace_manager.rename_workspace(self.name, new_name)
        self.assertIsNone(self.workspace_manager.get_workspace(self.name))
        self.assertIsNotNone(self.workspace_manager.get_workspace(new_name))

    def test_workspace_manager_move(self):
        new_path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, new_path, ignore_errors=True)
        self.workspace_manager.move_workspace(self.name, new_path)
        self.assertEqual(
            self.workspace_manager.get_workspace(self.name), new_path)

    def test_workspace_manager_remove_entry(self):
        self.workspace_manager.remove_workspace_entry(self.name)
        self.assertIsNone(self.workspace_manager.get_workspace(self.name))

    def test_workspace_manager_remove_directory(self):
        path = self.workspace_manager.remove_workspace_entry(self.name)
        self.workspace_manager.remove_workspace_directory(path)
        self.assertIsNone(self.workspace_manager.get_workspace(self.name))

    def test_path_expansion(self):
        name = data_utils.rand_uuid()
        path = os.path.join("~", name)
        os.makedirs(os.path.expanduser(path))
        self.addCleanup(shutil.rmtree, path, ignore_errors=True)
        self.workspace_manager.register_new_workspace(name, path)
        self.assertIsNotNone(self.workspace_manager.get_workspace(name))
<commit_msg>Improve Tempest workspace manager unit test coverage<commit_after># Copyright 2016 Rackspace
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os
import shutil
import subprocess
import tempfile

from mock import patch
try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO
from tempest.cmd import workspace
from tempest.lib.common.utils import data_utils
from tempest.tests import base

class TestTempestWorkspaceBase(base.TestCase):
    def setUp(self):
        super(TestTempestWorkspaceBase, self).setUp()
        self.name = data_utils.rand_uuid()
        self.path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.path, ignore_errors=True)
        store_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, store_dir, ignore_errors=True)
        self.store_file = os.path.join(store_dir, 'workspace.yaml')
        self.workspace_manager = workspace.WorkspaceManager(
            path=self.store_file)
        self.workspace_manager.register_new_workspace(self.name, self.path)

class TestTempestWorkspace(TestTempestWorkspaceBase):
    def _run_cmd_gets_return_code(self, cmd, expected):
        process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()
        return_code = process.returncode
        msg = ("%s failed with:\nstdout: %s\nstderr: %s" % (' '.join(cmd),
               stdout, stderr))
        self.assertEqual(return_code, expected, msg)

    def test_run_workspace_list(self):
        cmd = ['tempest', 'workspace', 'list',
               '--workspace-path', self.store_file]
        self._run_cmd_gets_return_code(cmd, 0)

    def test_run_workspace_register(self):
        name = data_utils.rand_uuid()
        path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, path, ignore_errors=True)
        cmd = ['tempest', 'workspace', 'register',
               '--workspace-path', self.store_file,
               '--name', name, '--path', path]
        self._run_cmd_gets_return_code(cmd, 0)
        self.assertIsNotNone(self.workspace_manager.get_workspace(name))

    def test_run_workspace_rename(self):
        new_name = data_utils.rand_uuid()
        cmd = ['tempest', 'workspace', 'rename',
               '--workspace-path', self.store_file,
               '--old-name', self.name, '--new-name', new_name]
        self._run_cmd_gets_return_code(cmd, 0)
        self.assertIsNone(self.workspace_manager.get_workspace(self.name))
        self.assertIsNotNone(self.workspace_manager.get_workspace(new_name))

    def test_run_workspace_move(self):
        new_path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, new_path, ignore_errors=True)
        cmd = ['tempest', 'workspace', 'move',
               '--workspace-path', self.store_file,
               '--name', self.name, '--path', new_path]
        self._run_cmd_gets_return_code(cmd, 0)
        self.assertEqual(
            self.workspace_manager.get_workspace(self.name), new_path)

    def test_run_workspace_remove_entry(self):
        cmd = ['tempest', 'workspace', 'remove',
               '--workspace-path', self.store_file,
               '--name', self.name]
        self._run_cmd_gets_return_code(cmd, 0)
        self.assertIsNone(self.workspace_manager.get_workspace(self.name))

    def test_run_workspace_remove_directory(self):
        cmd = ['tempest', 'workspace', 'remove',
               '--workspace-path', self.store_file,
               '--name', self.name, '--rmdir']
        self._run_cmd_gets_return_code(cmd, 0)
        self.assertIsNone(self.workspace_manager.get_workspace(self.name))

class TestTempestWorkspaceManager(TestTempestWorkspaceBase):
    def setUp(self):
        super(TestTempestWorkspaceManager, self).setUp()
        self.name = data_utils.rand_uuid()
        self.path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.path, ignore_errors=True)
        store_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, store_dir, ignore_errors=True)
        self.store_file = os.path.join(store_dir, 'workspace.yaml')
        self.workspace_manager = workspace.WorkspaceManager(
            path=self.store_file)
        self.workspace_manager.register_new_workspace(self.name, self.path)

    def test_workspace_manager_get(self):
        self.assertIsNotNone(self.workspace_manager.get_workspace(self.name))

    def test_workspace_manager_rename(self):
        new_name = data_utils.rand_uuid()
        self.workspace_manager.rename_workspace(self.name, new_name)
        self.assertIsNone(self.workspace_manager.get_workspace(self.name))
        self.assertIsNotNone(self.workspace_manager.get_workspace(new_name))

    def test_workspace_manager_move(self):
        new_path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, new_path, ignore_errors=True)
        self.workspace_manager.move_workspace(self.name, new_path)
        self.assertEqual(
            self.workspace_manager.get_workspace(self.name), new_path)

    def test_workspace_manager_remove_entry(self):
        self.workspace_manager.remove_workspace_entry(self.name)
        self.assertIsNone(self.workspace_manager.get_workspace(self.name))

    def test_workspace_manager_remove_directory(self):
        path = self.workspace_manager.remove_workspace_entry(self.name)
        self.workspace_manager.remove_workspace_directory(path)
        self.assertIsNone(self.workspace_manager.get_workspace(self.name))

    def test_path_expansion(self):
        name = data_utils.rand_uuid()
        path = os.path.join("~", name)
        os.makedirs(os.path.expanduser(path))
        self.addCleanup(shutil.rmtree, path, ignore_errors=True)
        self.workspace_manager.register_new_workspace(name, path)
        self.assertIsNotNone(self.workspace_manager.get_workspace(name))

    def test_workspace_name_not_exists(self):
        nonexistent_name = data_utils.rand_uuid()
        with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
            ex = self.assertRaises(SystemExit,
                                   self.workspace_manager._name_exists,
                                   nonexistent_name)
        self.assertEqual(1, ex.code)
        self.assertEqual(mock_stdout.getvalue(),
                         "A workspace was not found with name: %s\n" %
                         nonexistent_name)

    def test_workspace_name_already_exists(self):
        duplicate_name = self.name
        with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
            ex = self.assertRaises(SystemExit,
                                   self.workspace_manager.
                                   _workspace_name_exists,
                                   duplicate_name)
        self.assertEqual(1, ex.code)
        self.assertEqual(mock_stdout.getvalue(),
                         "A workspace already exists with name: %s.\n"
                         % duplicate_name)

    def test_workspace_manager_path_not_exist(self):
        fake_path = "fake_path"
        with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
            ex = self.assertRaises(SystemExit,
                                   self.workspace_manager._validate_path,
                                   fake_path)
        self.assertEqual(1, ex.code)
        self.assertEqual(mock_stdout.getvalue(),
                         "Path does not exist.\n")

    def test_workspace_manager_list_workspaces(self):
        listed = self.workspace_manager.list_workspaces()
        self.assertEqual(1, len(listed))
        self.assertIn(self.name, listed)
        self.assertEqual(self.path, listed.get(self.name))
<|endoftext|>


<commit_before>from django.conf.urls import url

from . import views
from .forms import VideoForm

urlpatterns = (
        url(r'^$', views.index, name='index'),
        url(r'^my_videos/(?P<person_url>[a-zA-Z0-9]+)/$',
            views.my_videos, name='my_videos'),
        url(r'^add_video/$',
            views.add_video, name='add_video'),
        url(r'^update_person/$',
            views.UpdatePerson.as_view(), name='update_person'),
        url(r'^remove_videos/$',
            views.remove_videos, name='remove_videos'),
        url(r'^most_popular/$',
            views.MostPopular.as_view(), name='most_popular'),
        url(r'^restore_cookie/(?P<person_url>[\w.@+-]+)/(?P<token>[\w.:_=-]+)$',
            views.restore_cookie, name='restore_cookie'),
        url(r'^send_password/(?P<person_url>[a-zA-Z0-9]+)/$',
            views.send_password, name='send_password'),
        )
<commit_msg>cache most_popular<commit_after>from django.conf.urls import url
from django.views.decorators.cache import cache_page
#TODO: probably better to put stuff like ONE_HOUR in constants.py or something

from . import views
from .forms import VideoForm

urlpatterns = (
        url(r'^$', views.index, name='index'),
        url(r'^my_videos/(?P<person_url>[a-zA-Z0-9]+)/$',
            views.my_videos, name='my_videos'),
        url(r'^add_video/$',
            views.add_video, name='add_video'),
        url(r'^update_person/$',
            views.UpdatePerson.as_view(), name='update_person'),
        url(r'^remove_videos/$',
            views.remove_videos, name='remove_videos'),
        url(r'^most_popular/$',
            cache_page(views.ONE_HOUR)(views.MostPopular.as_view()),
            name='most_popular'),
        url(r'^restore_cookie/(?P<person_url>[\w.@+-]+)/(?P<token>[\w.:_=-]+)$',
            views.restore_cookie, name='restore_cookie'),
        url(r'^send_password/(?P<person_url>[a-zA-Z0-9]+)/$',
            views.send_password, name='send_password'),
        )
<|endoftext|>


<commit_before># Copyright 2016 The Meson development team

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import shutil

from os import path
from .. import coredata, mesonlib, build
from ..mesonlib import MesonException
from . import ModuleReturnValue
from . import ExtensionModule
from . import permittedKwargs

PRESET_ARGS = {
    'glib': [
        '--from-code=UTF-8',
        '--add-comments',

        # https://developer.gnome.org/glib/stable/glib-I18N.html
        '--keyword=_',
        '--keyword=N_',
        '--keyword=C_:1c,2',
        '--keyword=NC_:1c,2',
        '--keyword=g_dcgettext:2',
        '--keyword=g_dngettext:2,3',
        '--keyword=g_dpgettext2:2c,3',

        '--flag=N_:1:pass-c-format',
        '--flag=C_:2:pass-c-format',
        '--flag=NC_:2:pass-c-format',
        '--flag=g_dngettext:2:pass-c-format',
        '--flag=g_strdup_printf:1:c-format',
        '--flag=g_string_printf:2:c-format',
        '--flag=g_string_append_printf:2:c-format',
        '--flag=g_error_new:3:c-format',
        '--flag=g_set_error:4:c-format',
    ]
}

class I18nModule(ExtensionModule):

    @staticmethod
    def _get_data_dirs(state, dirs):
        """Returns source directories of relative paths"""
        src_dir = path.join(state.environment.get_source_dir(), state.subdir)
        return [path.join(src_dir, d) for d in dirs]

    @permittedKwargs({'languages', 'data_dirs', 'preset', 'args', 'po_dir', 'type',
                      'input', 'output', 'install', 'install_dir'})
    def merge_file(self, state, args, kwargs):
        podir = kwargs.pop('po_dir', None)
        if not podir:
            raise MesonException('i18n: po_dir is a required kwarg')
        podir = path.join(state.build_to_src, state.subdir, podir)

        file_type = kwargs.pop('type', 'xml')
        VALID_TYPES = ('xml', 'desktop')
        if file_type not in VALID_TYPES:
            raise MesonException('i18n: "{}" is not a valid type {}'.format(file_type, VALID_TYPES))

        datadirs = self._get_data_dirs(state, mesonlib.stringlistify(kwargs.pop('data_dirs', [])))
        datadirs = '--datadirs=' + ':'.join(datadirs) if datadirs else None

        command = state.environment.get_build_command() + ['--internal', 'msgfmthelper',
                   '@INPUT@', '@OUTPUT@', file_type, podir]
        if datadirs:
            command.append(datadirs)

        kwargs['command'] = command
        ct = build.CustomTarget(kwargs['output'] + '_merge', state.subdir, kwargs)
        return ModuleReturnValue(ct, [ct])

    @permittedKwargs({'po_dir', 'data_dirs', 'type', 'languages', 'args', 'preset', 'install'})
    def gettext(self, state, args, kwargs):
        if len(args) != 1:
            raise coredata.MesonException('Gettext requires one positional argument (package name).')
        if not shutil.which('xgettext'):
            raise coredata.MesonException('Can not do gettext because xgettext is not installed.')
        packagename = args[0]
        languages = mesonlib.stringlistify(kwargs.get('languages', []))
        datadirs = self._get_data_dirs(state, mesonlib.stringlistify(kwargs.get('data_dirs', [])))
        extra_args = mesonlib.stringlistify(kwargs.get('args', []))

        preset = kwargs.pop('preset', None)
        if preset:
            preset_args = PRESET_ARGS.get(preset)
            if not preset_args:
                raise coredata.MesonException('i18n: Preset "{}" is not one of the valid options: {}'.format(
                                              preset, list(PRESET_ARGS.keys())))
            extra_args = set(preset_args + extra_args)

        pkg_arg = '--pkgname=' + packagename
        lang_arg = '--langs=' + '@@'.join(languages) if languages else None
        datadirs = '--datadirs=' + ':'.join(datadirs) if datadirs else None
        extra_args = '--extra-args=' + '@@'.join(extra_args) if extra_args else None

        potargs = state.environment.get_build_command() + ['--internal', 'gettext', 'pot', pkg_arg]
        if datadirs:
            potargs.append(datadirs)
        if extra_args:
            potargs.append(extra_args)
        pottarget = build.RunTarget(packagename + '-pot', potargs[0], potargs[1:], [], state.subdir)

        gmoargs = state.environment.get_build_command() + ['--internal', 'gettext', 'gen_gmo']
        if lang_arg:
            gmoargs.append(lang_arg)
        gmotarget = build.RunTarget(packagename + '-gmo', gmoargs[0], gmoargs[1:], [], state.subdir)

        updatepoargs = state.environment.get_build_command() + ['--internal', 'gettext', 'update_po', pkg_arg]
        if lang_arg:
            updatepoargs.append(lang_arg)
        if datadirs:
            updatepoargs.append(datadirs)
        if extra_args:
            updatepoargs.append(extra_args)
        updatepotarget = build.RunTarget(packagename + '-update-po', updatepoargs[0], updatepoargs[1:], [], state.subdir)

        targets = [pottarget, gmotarget, updatepotarget]

        install = kwargs.get('install') if ('install' in kwargs) else True
        if install:
            script = state.environment.get_build_command()
            args = ['--internal', 'gettext', 'install',
                    '--subdir=' + state.subdir,
                    '--localedir=' + state.environment.coredata.get_builtin_option('localedir'),
                    pkg_arg]
            if lang_arg:
                args.append(lang_arg)
            iscript = build.RunScript(script, args)
            targets.append(iscript)

        return ModuleReturnValue(None, targets)

def initialize():
    return I18nModule()
<commit_msg>Change ternary to another argument<commit_after># Copyright 2016 The Meson development team

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import shutil

from os import path
from .. import coredata, mesonlib, build
from ..mesonlib import MesonException
from . import ModuleReturnValue
from . import ExtensionModule
from . import permittedKwargs

PRESET_ARGS = {
    'glib': [
        '--from-code=UTF-8',
        '--add-comments',

        # https://developer.gnome.org/glib/stable/glib-I18N.html
        '--keyword=_',
        '--keyword=N_',
        '--keyword=C_:1c,2',
        '--keyword=NC_:1c,2',
        '--keyword=g_dcgettext:2',
        '--keyword=g_dngettext:2,3',
        '--keyword=g_dpgettext2:2c,3',

        '--flag=N_:1:pass-c-format',
        '--flag=C_:2:pass-c-format',
        '--flag=NC_:2:pass-c-format',
        '--flag=g_dngettext:2:pass-c-format',
        '--flag=g_strdup_printf:1:c-format',
        '--flag=g_string_printf:2:c-format',
        '--flag=g_string_append_printf:2:c-format',
        '--flag=g_error_new:3:c-format',
        '--flag=g_set_error:4:c-format',
    ]
}

class I18nModule(ExtensionModule):

    @staticmethod
    def _get_data_dirs(state, dirs):
        """Returns source directories of relative paths"""
        src_dir = path.join(state.environment.get_source_dir(), state.subdir)
        return [path.join(src_dir, d) for d in dirs]

    @permittedKwargs({'languages', 'data_dirs', 'preset', 'args', 'po_dir', 'type',
                      'input', 'output', 'install', 'install_dir'})
    def merge_file(self, state, args, kwargs):
        podir = kwargs.pop('po_dir', None)
        if not podir:
            raise MesonException('i18n: po_dir is a required kwarg')
        podir = path.join(state.build_to_src, state.subdir, podir)

        file_type = kwargs.pop('type', 'xml')
        VALID_TYPES = ('xml', 'desktop')
        if file_type not in VALID_TYPES:
            raise MesonException('i18n: "{}" is not a valid type {}'.format(file_type, VALID_TYPES))

        datadirs = self._get_data_dirs(state, mesonlib.stringlistify(kwargs.pop('data_dirs', [])))
        datadirs = '--datadirs=' + ':'.join(datadirs) if datadirs else None

        command = state.environment.get_build_command() + ['--internal', 'msgfmthelper',
                   '@INPUT@', '@OUTPUT@', file_type, podir]
        if datadirs:
            command.append(datadirs)

        kwargs['command'] = command
        ct = build.CustomTarget(kwargs['output'] + '_merge', state.subdir, kwargs)
        return ModuleReturnValue(ct, [ct])

    @permittedKwargs({'po_dir', 'data_dirs', 'type', 'languages', 'args', 'preset', 'install'})
    def gettext(self, state, args, kwargs):
        if len(args) != 1:
            raise coredata.MesonException('Gettext requires one positional argument (package name).')
        if not shutil.which('xgettext'):
            raise coredata.MesonException('Can not do gettext because xgettext is not installed.')
        packagename = args[0]
        languages = mesonlib.stringlistify(kwargs.get('languages', []))
        datadirs = self._get_data_dirs(state, mesonlib.stringlistify(kwargs.get('data_dirs', [])))
        extra_args = mesonlib.stringlistify(kwargs.get('args', []))

        preset = kwargs.pop('preset', None)
        if preset:
            preset_args = PRESET_ARGS.get(preset)
            if not preset_args:
                raise coredata.MesonException('i18n: Preset "{}" is not one of the valid options: {}'.format(
                                              preset, list(PRESET_ARGS.keys())))
            extra_args = set(preset_args + extra_args)

        pkg_arg = '--pkgname=' + packagename
        lang_arg = '--langs=' + '@@'.join(languages) if languages else None
        datadirs = '--datadirs=' + ':'.join(datadirs) if datadirs else None
        extra_args = '--extra-args=' + '@@'.join(extra_args) if extra_args else None

        potargs = state.environment.get_build_command() + ['--internal', 'gettext', 'pot', pkg_arg]
        if datadirs:
            potargs.append(datadirs)
        if extra_args:
            potargs.append(extra_args)
        pottarget = build.RunTarget(packagename + '-pot', potargs[0], potargs[1:], [], state.subdir)

        gmoargs = state.environment.get_build_command() + ['--internal', 'gettext', 'gen_gmo']
        if lang_arg:
            gmoargs.append(lang_arg)
        gmotarget = build.RunTarget(packagename + '-gmo', gmoargs[0], gmoargs[1:], [], state.subdir)

        updatepoargs = state.environment.get_build_command() + ['--internal', 'gettext', 'update_po', pkg_arg]
        if lang_arg:
            updatepoargs.append(lang_arg)
        if datadirs:
            updatepoargs.append(datadirs)
        if extra_args:
            updatepoargs.append(extra_args)
        updatepotarget = build.RunTarget(packagename + '-update-po', updatepoargs[0], updatepoargs[1:], [], state.subdir)

        targets = [pottarget, gmotarget, updatepotarget]

        install = kwargs.get('install', True)
        if install:
            script = state.environment.get_build_command()
            args = ['--internal', 'gettext', 'install',
                    '--subdir=' + state.subdir,
                    '--localedir=' + state.environment.coredata.get_builtin_option('localedir'),
                    pkg_arg]
            if lang_arg:
                args.append(lang_arg)
            iscript = build.RunScript(script, args)
            targets.append(iscript)

        return ModuleReturnValue(None, targets)

def initialize():
    return I18nModule()
<|endoftext|>


<commit_before>#vim: set encoding=utf-8
import logging
from itertools import takewhile
import re
import string

from regparser.citations import internal_citations, Label
from regparser.layer.layer import Layer
from regparser.tree.struct import Node, walk

class InternalCitationParser(Layer):
    def __init__(self, tree):
        Layer.__init__(self, tree)
        self.known_citations = set()
        self.verify_citations = True

    def pre_process(self):
        """As a preprocessing step, run through the entire tree, collecting
        all labels."""
        def per_node(node):
            self.known_citations.add(tuple(node.label))
        walk(self.tree, per_node)

    def process(self, node):
        citations_list = self.parse(node.text, label=Label.from_node(node))
        if citations_list:
            return citations_list

    def remove_missing_citations(self, citations, text):
        """Remove any citations to labels we have not seen before (i.e.
        those collected in the pre_processing stage)"""
        final = []
        for c in citations:
            if tuple(c.label.to_list()) in self.known_citations:
                final.append(c)
            else:
                logging.warning("Missing citation? %s %r"
                                % (text[c.start:c.end], c.label))
        return final

    def parse(self, text, label):
        """ Parse the provided text, pulling out all the internal
        (self-referential) citations. """

        to_layer = lambda pc: {'offsets': [(pc.start, pc.end)],
                               'citation': pc.label.to_list()}
        citations = internal_citations(text, label, require_marker=True)
        if self.verify_citations:
            citations = self.remove_missing_citations(citations, text)
        all_citations = list(map(to_layer, citations))

        return self.strip_whitespace(text, all_citations)

    def strip_whitespace(self, text, citations):
        """Modifies the offsets to exclude any trailing whitespace. Modifies
        the offsets in place."""
        for citation in citations:
            for i in range(len(citation['offsets'])):
                start, end = citation['offsets'][i]
                string = text[start:end]
                lstring = string.lstrip()
                rstring = string.rstrip()
                new_start = start + (len(string) - len(lstring))
                new_end = end - (len(string) - len(rstring))
                citation['offsets'][i] = (new_start, new_end)
        return citations
<commit_msg>Reorder imports<commit_after>#vim: set encoding=utf-8
from itertools import takewhile
import logging
import re
import string

from regparser.citations import internal_citations, Label
from regparser.layer.layer import Layer
from regparser.tree.struct import Node, walk

class InternalCitationParser(Layer):
    def __init__(self, tree):
        Layer.__init__(self, tree)
        self.known_citations = set()
        self.verify_citations = True

    def pre_process(self):
        """As a preprocessing step, run through the entire tree, collecting
        all labels."""
        def per_node(node):
            self.known_citations.add(tuple(node.label))
        walk(self.tree, per_node)

    def process(self, node):
        citations_list = self.parse(node.text, label=Label.from_node(node))
        if citations_list:
            return citations_list

    def remove_missing_citations(self, citations, text):
        """Remove any citations to labels we have not seen before (i.e.
        those collected in the pre_processing stage)"""
        final = []
        for c in citations:
            if tuple(c.label.to_list()) in self.known_citations:
                final.append(c)
            else:
                logging.warning("Missing citation? %s %r"
                                % (text[c.start:c.end], c.label))
        return final

    def parse(self, text, label):
        """ Parse the provided text, pulling out all the internal
        (self-referential) citations. """

        to_layer = lambda pc: {'offsets': [(pc.start, pc.end)],
                               'citation': pc.label.to_list()}
        citations = internal_citations(text, label, require_marker=True)
        if self.verify_citations:
            citations = self.remove_missing_citations(citations, text)
        all_citations = list(map(to_layer, citations))

        return self.strip_whitespace(text, all_citations)

    def strip_whitespace(self, text, citations):
        """Modifies the offsets to exclude any trailing whitespace. Modifies
        the offsets in place."""
        for citation in citations:
            for i in range(len(citation['offsets'])):
                start, end = citation['offsets'][i]
                string = text[start:end]
                lstring = string.lstrip()
                rstring = string.rstrip()
                new_start = start + (len(string) - len(lstring))
                new_end = end - (len(string) - len(rstring))
                citation['offsets'][i] = (new_start, new_end)
        return citations
<|endoftext|>


<commit_before>#vim: set encoding=utf-8
import re
import logging

from lxml import etree

from regparser.tree.struct import Node
from regparser.tree.paragraph import p_levels
from regparser.tree.node_stack import NodeStack
from regparser.tree.xml_parser.appendices import build_non_reg_text
from regparser.tree import reg_text
from regparser.tree.xml_parser import tree_utils

def determine_level(c, current_level, next_marker=None):
    """ Regulation paragraphs are hierarchical. This determines which level
    the paragraph is at. Convert between p_level indexing and depth here by
    adding one"""
    potential = [i for i in range(len(p_levels)) if c in p_levels[i]]

    if len(potential) > 1 and next_marker:     # resolve ambiguity
        following = [i for i in range(len(p_levels))
                     if next_marker in p_levels[i]]

        #   Add character index
        potential = [(level, p_levels[level].index(c)) for level in potential]
        following = [(level, p_levels[level].index(next_marker))
                     for level in following]

        #   Check if we can be certain using the following marker
        for pot_level, pot_idx in potential:
            for next_level, next_idx in following:
                if (    #   E.g. i followed by A or i followed by 1
                    (next_idx == 0 and next_level == pot_level + 1)
                    or  #   E.g. i followed by ii
                    (next_level == pot_level and next_idx == pot_idx + 1)
                    or  #   E.g. i followed by 3
                    (next_level < pot_level and next_idx > 0)):
                    return pot_level + 1
        logging.warning("Ambiguous marker (%s) not followed by something "
                        + "disambiguating (%s)", c, next_marker)
        return potential[0][0] + 1

    else:
        return potential[0] + 1

def get_reg_part(reg_doc):
    """
    The CFR Part number for a regulation is contained within
    an EAR tag, for a Federal Register notice it's in a REGTEXT tag. Get the
    part number of the regulation.
    """

    #FR notice
    reg_text_xml = reg_doc.xpath('//REGTEXT')
    if reg_text_xml:
        return reg_text_xml[0].attrib['PART']

    #e-CFR XML
    reg_ear = reg_doc.xpath('//PART/EAR')
    if reg_ear:
        return reg_ear[0].text.split('Pt.')[1].strip()

def get_title(reg_doc):
    """ Extract the title of the regulation. """
    parent = reg_doc.xpath('//PART/HD')[0]
    title = parent.text
    return title

def build_tree(reg_xml):
    doc = etree.fromstring(reg_xml)

    reg_part = get_reg_part(doc)
    title = get_title(doc)

    tree = Node("", [], [reg_part], title)

    part = doc.xpath('//PART')[0]

    subpart_xmls = [c for c in part.getchildren() if c.tag == 'SUBPART']
    if len(subpart_xmls) > 0:
        subparts = [build_subpart(reg_part, s) for s in subpart_xmls]
        tree.children = subparts
    else:
        section_xmls = [c for c in part.getchildren() if c.tag == 'SECTION']
        sections = []
        for section_xml in section_xmls:
            sections.extend(build_from_section(reg_part, section_xml))
        empty_part = reg_text.build_empty_part(reg_part)
        empty_part.children = sections
        tree.children = [empty_part]

    non_reg_sections = build_non_reg_text(reg_xml, reg_part)
    tree.children += non_reg_sections

    return tree

def get_subpart_title(subpart_xml):
    hds = subpart_xml.xpath('./HD')
    return [hd.text for hd in hds][0]

def build_subpart(reg_part, subpart_xml):
    subpart_title = get_subpart_title(subpart_xml)
    subpart = reg_text.build_subpart(subpart_title, reg_part)

    sections = []
    for ch in subpart_xml.getchildren():
        if ch.tag == 'SECTION':
            sections.extend(build_from_section(reg_part, ch))

    subpart.children = sections
    return subpart

def get_markers(text):
    """ Extract all the paragraph markers from text  """
    markers = tree_utils.get_paragraph_markers(text)
    collapsed_markers = tree_utils.get_collapsed_markers(text)
    markers_list = [m for m in markers] + [m for m in collapsed_markers]

    return markers_list

def get_markers_and_text(node, markers_list):
    node_text = tree_utils.get_node_text(node)
    text_with_tags = tree_utils.get_node_text_tags_preserved(node)

    if len(markers_list) > 1:
        actual_markers = ['(%s)' % m for m in markers_list]
        plain_markers = [m.replace('<E T="03">', '').replace('</E>', '')
                         for m in actual_markers]
        node_texts = tree_utils.split_text(node_text, plain_markers)
        tagged_texts = tree_utils.split_text(text_with_tags, actual_markers)
        node_text_list = zip(node_texts, tagged_texts)
    elif markers_list:
        node_text_list = [(node_text, text_with_tags)]
    return zip(markers_list, node_text_list)

def next_marker(xml_node, remaining_markers):
    """Try to determine the marker following the current xml_node. Remaining
    markers is a list of other marks *within* the xml_node. May return
    None"""
    #   More markers in this xml node
    if remaining_markers:
        return remaining_markers[0][0]

    #   Check the next xml node
    sib = xml_node.getnext()
    if sib is not None:
        next_text = tree_utils.get_node_text(sib)
        next_markers = get_markers(next_text)
        if next_markers:
            return next_markers[0]

def build_from_section(reg_part, section_xml):
    p_level = 1
    m_stack = NodeStack()
    section_texts = []
    for ch in (ch for ch in section_xml.getchildren() if ch.tag == 'P'):
        text = tree_utils.get_node_text(ch)
        tagged_text = tree_utils.get_node_text_tags_preserved(ch)
        markers_list = get_markers(tagged_text)

        if not markers_list:
            node_text = tree_utils.get_node_text(ch)
            tagged_text = tree_utils.get_node_text_tags_preserved(ch)
            section_texts.append((node_text, tagged_text))
        else:
            markers_and_text = get_markers_and_text(ch, markers_list)

            #   Easier to reason if we view the list as a stack
            markers_and_text = list(reversed(markers_and_text))
            while markers_and_text:
                m, node_text = markers_and_text.pop()
                m_sans_markup = m.replace('<E T="03">', '').replace('</E>', '')
                n = Node(node_text[0], [], [str(m_sans_markup)])
                n.tagged_text = unicode(node_text[1])

                new_p_level = determine_level(
                    m, p_level, next_marker(ch, markers_and_text))

                last = m_stack.peek()
                if len(last) == 0:
                    m_stack.push_last((new_p_level, n))
                else:
                    tree_utils.add_to_stack(m_stack, new_p_level, n)
                p_level = new_p_level

    section_no = section_xml.xpath('SECTNO')[0].text
    subject_xml = section_xml.xpath('SUBJECT')
    if not subject_xml:
        subject_xml = section_xml.xpath('RESERVED')
    subject_text = subject_xml[0].text

    nodes = []
    section_nums = []
    for match in re.finditer(r'%s\.(\d+)' % reg_part, section_no):
        section_nums.append(int(match.group(1)))

    if '-' in section_no:    #   Span of section numbers
        first, last = section_nums
        section_nums = []
        for i in range(first, last + 1):
            section_nums.append(i)

    for section_number in section_nums:
        section_number = str(section_number)
        plain_sect_texts = [s[0] for s in section_texts]
        tagged_sect_texts = [s[1] for s in section_texts]

        section_text = ' '.join([section_xml.text] + plain_sect_texts)
        tagged_section_text = ' '.join([section_xml.text] + tagged_sect_texts)
        section_title = u"§ " + reg_part + "." + section_number
        if subject_text:
            section_title += " " + subject_text

        sect_node = Node(
            section_text, label=[reg_part, section_number],
            title=section_title)
        sect_node.tagged_text = tagged_section_text

        m_stack.add_to_bottom((1, sect_node))

        while m_stack.size() > 1:
            tree_utils.unwind_stack(m_stack)

        nodes.append(m_stack.pop()[0][1])

    return nodes
<commit_msg>Also check for double section markers when encountering reserved ranges<commit_after>#vim: set encoding=utf-8
import re
import logging

from lxml import etree

from regparser.tree.struct import Node
from regparser.tree.paragraph import p_levels
from regparser.tree.node_stack import NodeStack
from regparser.tree.xml_parser.appendices import build_non_reg_text
from regparser.tree import reg_text
from regparser.tree.xml_parser import tree_utils

def determine_level(c, current_level, next_marker=None):
    """ Regulation paragraphs are hierarchical. This determines which level
    the paragraph is at. Convert between p_level indexing and depth here by
    adding one"""
    potential = [i for i in range(len(p_levels)) if c in p_levels[i]]

    if len(potential) > 1 and next_marker:     # resolve ambiguity
        following = [i for i in range(len(p_levels))
                     if next_marker in p_levels[i]]

        #   Add character index
        potential = [(level, p_levels[level].index(c)) for level in potential]
        following = [(level, p_levels[level].index(next_marker))
                     for level in following]

        #   Check if we can be certain using the following marker
        for pot_level, pot_idx in potential:
            for next_level, next_idx in following:
                if (    #   E.g. i followed by A or i followed by 1
                    (next_idx == 0 and next_level == pot_level + 1)
                    or  #   E.g. i followed by ii
                    (next_level == pot_level and next_idx == pot_idx + 1)
                    or  #   E.g. i followed by 3
                    (next_level < pot_level and next_idx > 0)):
                    return pot_level + 1
        logging.warning("Ambiguous marker (%s) not followed by something "
                        + "disambiguating (%s)", c, next_marker)
        return potential[0][0] + 1

    else:
        return potential[0] + 1

def get_reg_part(reg_doc):
    """
    The CFR Part number for a regulation is contained within
    an EAR tag, for a Federal Register notice it's in a REGTEXT tag. Get the
    part number of the regulation.
    """

    #FR notice
    reg_text_xml = reg_doc.xpath('//REGTEXT')
    if reg_text_xml:
        return reg_text_xml[0].attrib['PART']

    #e-CFR XML
    reg_ear = reg_doc.xpath('//PART/EAR')
    if reg_ear:
        return reg_ear[0].text.split('Pt.')[1].strip()

def get_title(reg_doc):
    """ Extract the title of the regulation. """
    parent = reg_doc.xpath('//PART/HD')[0]
    title = parent.text
    return title

def build_tree(reg_xml):
    doc = etree.fromstring(reg_xml)

    reg_part = get_reg_part(doc)
    title = get_title(doc)

    tree = Node("", [], [reg_part], title)

    part = doc.xpath('//PART')[0]

    subpart_xmls = [c for c in part.getchildren() if c.tag == 'SUBPART']
    if len(subpart_xmls) > 0:
        subparts = [build_subpart(reg_part, s) for s in subpart_xmls]
        tree.children = subparts
    else:
        section_xmls = [c for c in part.getchildren() if c.tag == 'SECTION']
        sections = []
        for section_xml in section_xmls:
            sections.extend(build_from_section(reg_part, section_xml))
        empty_part = reg_text.build_empty_part(reg_part)
        empty_part.children = sections
        tree.children = [empty_part]

    non_reg_sections = build_non_reg_text(reg_xml, reg_part)
    tree.children += non_reg_sections

    return tree

def get_subpart_title(subpart_xml):
    hds = subpart_xml.xpath('./HD')
    return [hd.text for hd in hds][0]

def build_subpart(reg_part, subpart_xml):
    subpart_title = get_subpart_title(subpart_xml)
    subpart = reg_text.build_subpart(subpart_title, reg_part)

    sections = []
    for ch in subpart_xml.getchildren():
        if ch.tag == 'SECTION':
            sections.extend(build_from_section(reg_part, ch))

    subpart.children = sections
    return subpart

def get_markers(text):
    """ Extract all the paragraph markers from text  """
    markers = tree_utils.get_paragraph_markers(text)
    collapsed_markers = tree_utils.get_collapsed_markers(text)
    markers_list = [m for m in markers] + [m for m in collapsed_markers]

    return markers_list

def get_markers_and_text(node, markers_list):
    node_text = tree_utils.get_node_text(node)
    text_with_tags = tree_utils.get_node_text_tags_preserved(node)

    if len(markers_list) > 1:
        actual_markers = ['(%s)' % m for m in markers_list]
        plain_markers = [m.replace('<E T="03">', '').replace('</E>', '')
                         for m in actual_markers]
        node_texts = tree_utils.split_text(node_text, plain_markers)
        tagged_texts = tree_utils.split_text(text_with_tags, actual_markers)
        node_text_list = zip(node_texts, tagged_texts)
    elif markers_list:
        node_text_list = [(node_text, text_with_tags)]
    return zip(markers_list, node_text_list)

def next_marker(xml_node, remaining_markers):
    """Try to determine the marker following the current xml_node. Remaining
    markers is a list of other marks *within* the xml_node. May return
    None"""
    #   More markers in this xml node
    if remaining_markers:
        return remaining_markers[0][0]

    #   Check the next xml node
    sib = xml_node.getnext()
    if sib is not None:
        next_text = tree_utils.get_node_text(sib)
        next_markers = get_markers(next_text)
        if next_markers:
            return next_markers[0]

def build_from_section(reg_part, section_xml):
    p_level = 1
    m_stack = NodeStack()
    section_texts = []
    for ch in (ch for ch in section_xml.getchildren() if ch.tag == 'P'):
        text = tree_utils.get_node_text(ch)
        tagged_text = tree_utils.get_node_text_tags_preserved(ch)
        markers_list = get_markers(tagged_text)

        if not markers_list:
            node_text = tree_utils.get_node_text(ch)
            tagged_text = tree_utils.get_node_text_tags_preserved(ch)
            section_texts.append((node_text, tagged_text))
        else:
            markers_and_text = get_markers_and_text(ch, markers_list)

            #   Easier to reason if we view the list as a stack
            markers_and_text = list(reversed(markers_and_text))
            while markers_and_text:
                m, node_text = markers_and_text.pop()
                m_sans_markup = m.replace('<E T="03">', '').replace('</E>', '')
                n = Node(node_text[0], [], [str(m_sans_markup)])
                n.tagged_text = unicode(node_text[1])

                new_p_level = determine_level(
                    m, p_level, next_marker(ch, markers_and_text))

                last = m_stack.peek()
                if len(last) == 0:
                    m_stack.push_last((new_p_level, n))
                else:
                    tree_utils.add_to_stack(m_stack, new_p_level, n)
                p_level = new_p_level

    section_no = section_xml.xpath('SECTNO')[0].text
    subject_xml = section_xml.xpath('SUBJECT')
    if not subject_xml:
        subject_xml = section_xml.xpath('RESERVED')
    subject_text = subject_xml[0].text

    nodes = []
    section_nums = []
    for match in re.finditer(r'%s\.(\d+)' % reg_part, section_no):
        section_nums.append(int(match.group(1)))

    #  Span of section numbers
    if u'§§' == section_no[:2] and '-' in section_no:
        first, last = section_nums
        section_nums = []
        for i in range(first, last + 1):
            section_nums.append(i)

    for section_number in section_nums:
        section_number = str(section_number)
        plain_sect_texts = [s[0] for s in section_texts]
        tagged_sect_texts = [s[1] for s in section_texts]

        section_text = ' '.join([section_xml.text] + plain_sect_texts)
        tagged_section_text = ' '.join([section_xml.text] + tagged_sect_texts)
        section_title = u"§ " + reg_part + "." + section_number
        if subject_text:
            section_title += " " + subject_text

        sect_node = Node(
            section_text, label=[reg_part, section_number],
            title=section_title)
        sect_node.tagged_text = tagged_section_text

        m_stack.add_to_bottom((1, sect_node))

        while m_stack.size() > 1:
            tree_utils.unwind_stack(m_stack)

        nodes.append(m_stack.pop()[0][1])

    return nodes
<|endoftext|>


<commit_before>
from django.conf import settings

registry = {}

def register_setting(name="", editable=False, description="", default=None,
                     append=False):
    """
    Registers a setting that can be edited via the admin.
    """
    # Check project's settings module for overriden default.
    if append and name in registry:
        registry[name] += default
    else:
        default = getattr(settings, name, default)
        registry[name] = {"name": name, "description": description,
            "editable": editable, "default": default, "type": type(default)}

class Settings(object):
    """
    An object that provides settings via dynamic attribute access. Settings
    that are registered as editable and can therefore be stored in the
    database are *all* loaded once only, the first time *any* editable
    setting is accessed. When accessing uneditable settings their default
    values are used. The Settings object also provides access to Django
    settings via django.conf.settings in order to provide a consistent
    method of access for all settings.
    """

    def __init__(self):
        """
        Marking loaded as True to begin with prevents some nasty errors
        when the DB table is first created.
        """
        self._loaded = True
        self._editable_cache = {}

    def use_editable(self):
        """
        Empty the editable settings cache and set the loaded flag to False
        so that settings will be loaded from the DB on next access. If the
        conf app is not installed then set the loaded flag to True in order
        to bypass DB lookup entirely.
        """
        self._loaded = __name__ not in getattr(self, "INSTALLED_APPS")
        self._editable_cache = {}

    def __getattr__(self, name):

        # Lookup name as a registered setting or a Django setting.
        try:
            setting = registry[name]
        except KeyError:
            from django.conf import settings
            return getattr(settings, name)

        # First access for an editable setting - load from DB into cache.
        if setting["editable"] and not self._loaded:
            from mezzanine.conf.models import Setting
            for setting_obj in Setting.objects.all():
                setting_type = registry[setting_obj.name]["type"]
                if setting_type is bool:
                    setting_value = setting_obj.value != "False"
                else:
                    setting_value = setting_type(setting_obj.value)
                self._editable_cache[setting_obj.name] = setting_value

        # Use cached editable setting if found, otherwise use default.
        try:
            setting_value = self._editable_cache[name]
        except KeyError:
            pass
        else:
            return setting_value
        return setting["default"]

for app in [__name__] + [a for a in settings.INSTALLED_APPS if a != __name__]:
    try:
        __import__("%s.defaults" % app)
    except ImportError:
        pass

settings = Settings()
<commit_msg>Fixed settings append hook again.<commit_after>
from django.conf import settings

registry = {}

def register_setting(name="", editable=False, description="", default=None,
                     append=False):
    """
    Registers a setting that can be edited via the admin.
    """
    # Check project's settings module for overriden default.
    if append and name in registry:
        registry[name]["default"] += default
    else:
        default = getattr(settings, name, default)
        registry[name] = {"name": name, "description": description,
            "editable": editable, "default": default, "type": type(default)}

class Settings(object):
    """
    An object that provides settings via dynamic attribute access. Settings
    that are registered as editable and can therefore be stored in the
    database are *all* loaded once only, the first time *any* editable
    setting is accessed. When accessing uneditable settings their default
    values are used. The Settings object also provides access to Django
    settings via django.conf.settings in order to provide a consistent
    method of access for all settings.
    """

    def __init__(self):
        """
        Marking loaded as True to begin with prevents some nasty errors
        when the DB table is first created.
        """
        self._loaded = True
        self._editable_cache = {}

    def use_editable(self):
        """
        Empty the editable settings cache and set the loaded flag to False
        so that settings will be loaded from the DB on next access. If the
        conf app is not installed then set the loaded flag to True in order
        to bypass DB lookup entirely.
        """
        self._loaded = __name__ not in getattr(self, "INSTALLED_APPS")
        self._editable_cache = {}

    def __getattr__(self, name):

        # Lookup name as a registered setting or a Django setting.
        try:
            setting = registry[name]
        except KeyError:
            from django.conf import settings
            return getattr(settings, name)

        # First access for an editable setting - load from DB into cache.
        if setting["editable"] and not self._loaded:
            from mezzanine.conf.models import Setting
            for setting_obj in Setting.objects.all():
                setting_type = registry[setting_obj.name]["type"]
                if setting_type is bool:
                    setting_value = setting_obj.value != "False"
                else:
                    setting_value = setting_type(setting_obj.value)
                self._editable_cache[setting_obj.name] = setting_value

        # Use cached editable setting if found, otherwise use default.
        try:
            setting_value = self._editable_cache[name]
        except KeyError:
            pass
        else:
            return setting_value
        return setting["default"]

for app in [__name__] + [a for a in settings.INSTALLED_APPS if a != __name__]:
    try:
        __import__("%s.defaults" % app)
    except ImportError:
        pass

settings = Settings()
<|endoftext|>


<commit_before>""" Plot a K2 TPF and overlay a sky survey image."""

from __future__ import division, print_function, absolute_import

import argparse

from tpf import TargetPixelFile
from figure import K2Fig

def k2pix():
    """ Script to plot a K2 TPF and overlay a sky survey image."""
    parser = argparse.ArgumentParser(
        description="Plots a co-added Target Pixel File (TPF) from NASA's "
                    "Kepler/K2/TESS with a survey image overlaid.")
    parser.add_argument('-v', '--verbose', action='store_true',
                        help='')
    parser.add_argument('--output', metavar='FILENAME',
                        type=str, default=None,
                        help='.gif or .mp4 output filename (default: gif with the same name as the input file)')
    parser.add_argument('--survey', metavar='SURVEY',
                        type=str, default=None,
                        help='survey (available in skyview) to overlay on the K2 image.')
    parser.add_argument('--stretch', type=str, default='log',
                        help='type of contrast stretching: "linear", "sqrt", '
                             '"power", "log", or "asinh" (default is "log")')
    parser.add_argument('--min_percent', metavar='%', type=float, default=1.,
                        help='minimum cut percentile (default: 1.0)')
    parser.add_argument('--max_percent', metavar='%', type=float, default=95.,
                        help='maximum cut percentile (default: 95)')
    parser.add_argument('--cmap', type=str,
                        default='gray', help='matplotlib color map name '
                                             '(default: gray)')
    parser.add_argument('--contour_color', type=str,
                        default='red', help='matplotlib color name '
                                             '(default: red)')
    parser.add_argument('tpf_filename', nargs='+',
                        help='path to one or more Target Pixel Files (TPF)')

    args = parser.parse_args(args)

    # # What is the data column to show?
    # if args.raw:
    #     data_col = 'RAW_CNTS'
    # elif args.background:
    #     data_col = 'FLUX_BKG'
    # elif args.cosmic:
    #     data_col = 'COSMIC_RAYS'
    # else:
    #     data_col = 'FLUX'

    for fn in args.tpf_filename:
        try:
            tpf = TargetPixelFile(fn, verbose=args.verbose)
            fig = K2Fig(tpf)
            fig.create_figure(output_filename=args.output,
                              stretch=args.stretch,
                              min_percent=args.min_percent,
                              max_percent=args.max_percent,
                              cmap=args.cmap,
                              contour_color=args.contour_color)
        except Exception as e:
            if args.verbose:
                raise e
            else:
                print('ERROR: {}'.format(e))

                 #
                #  output_filename, stretch='log', vmin=1, vmax=None,
                #                   cmap='gray', data_col='FLUX', min_percent=1,
                #                   max_percent=95

# Example use
if __name__ == '__main__':

    # fn = ('http://archive.stsci.edu/missions/kepler/target_pixel_files/'
    #       '0007/000757076/kplr000757076-2010174085026_lpd-targ.fits.gz')
    fn = ("https://archive.stsci.edu/missions/k2/target_pixel_files/c5/"
          "211900000/72000/ktwo211972478-c05_lpd-targ.fits.gz")
    tpf = TargetPixelFile(fn)
    fig = K2Fig(tpf)
    # Then run with all default arguments to generate image
    fig.create_figure("overplot.png")
<commit_msg>minor input fix in main<commit_after>""" Plot a K2 TPF and overlay a sky survey image."""

from __future__ import division, print_function, absolute_import

import argparse

from tpf import TargetPixelFile
from figure import K2Fig

def k2pix():
    """ Script to plot a K2 TPF and overlay a sky survey image."""
    parser = argparse.ArgumentParser(
        description="Plots a co-added Target Pixel File (TPF) from NASA's "
                    "Kepler/K2/TESS with a survey image overlaid.")
    parser.add_argument('-v', '--verbose', action='store_true',
                        help='')
    parser.add_argument('--output', metavar='FILENAME',
                        type=str, default=None,
                        help='.gif or .mp4 output filename (default: gif with the same name as the input file)')
    parser.add_argument('--survey', metavar='SURVEY',
                        type=str, default=None,
                        help='survey (available in skyview) to overlay on the K2 image.')
    parser.add_argument('--stretch', type=str, default='log',
                        help='type of contrast stretching: "linear", "sqrt", '
                             '"power", "log", or "asinh" (default is "log")')
    parser.add_argument('--min_percent', metavar='%', type=float, default=1.,
                        help='minimum cut percentile (default: 1.0)')
    parser.add_argument('--max_percent', metavar='%', type=float, default=95.,
                        help='maximum cut percentile (default: 95)')
    parser.add_argument('--cmap', type=str,
                        default='gray', help='matplotlib color map name '
                                             '(default: gray)')
    parser.add_argument('--contour_color', type=str,
                        default='red', help='matplotlib color name '
                                             '(default: red)')
    parser.add_argument('tpf_filename', nargs='+',
                        help='path to one or more Target Pixel Files (TPF)')

    args = parser.parse_args(args)

    # # What is the data column to show?
    # if args.raw:
    #     data_col = 'RAW_CNTS'
    # elif args.background:
    #     data_col = 'FLUX_BKG'
    # elif args.cosmic:
    #     data_col = 'COSMIC_RAYS'
    # else:
    #     data_col = 'FLUX'

    for fn in args.tpf_filename:
        try:
            tpf = TargetPixelFile(fn, verbose=args.verbose)
            fig = K2Fig(tpf)
            fig.create_figure(output_filename=args.output,
                              stretch=args.stretch,
                              min_percent=args.min_percent,
                              max_percent=args.max_percent,
                              cmap=args.cmap,
                              contour_color=args.contour_color)
        except Exception as e:
            if args.verbose:
                raise e
            else:
                print('ERROR: {}'.format(e))

                 #
                #  output_filename, stretch='log', vmin=1, vmax=None,
                #                   cmap='gray', data_col='FLUX', min_percent=1,
                #                   max_percent=95

# Example use
if __name__ == '__main__':

    # fn = ('http://archive.stsci.edu/missions/kepler/target_pixel_files/'
    #       '0007/000757076/kplr000757076-2010174085026_lpd-targ.fits.gz')
    fn = ("https://archive.stsci.edu/missions/k2/target_pixel_files/c5/"
          "211900000/72000/ktwo211972478-c05_lpd-targ.fits.gz")
    tpf = TargetPixelFile(fn)
    fig = K2Fig(tpf)
    # Then run with all default arguments to generate image
    fig.create_figure("overplot.png","2MASS-K")
<|endoftext|>


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

from django.db import models, migrations
import datetime
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('notifications', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Notice',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('message', models.TextField(verbose_name='message')),
                ('added', models.DateTimeField(default=datetime.datetime.now, verbose_name='added')),
                ('unseen', models.BooleanField(default=True, verbose_name='unseen')),
                ('archived', models.BooleanField(default=False, verbose_name='archived')),
                ('notice_type', models.ForeignKey(verbose_name='notice type', to='notifications.NoticeType')),
                ('recipient', models.ForeignKey(related_name='received_notices', verbose_name='recipient', to=settings.AUTH_USER_MODEL)),
                ('sender', models.ForeignKey(related_name='sent_notices', verbose_name='sender', to=settings.AUTH_USER_MODEL, null=True)),
            ],
            options={
                'ordering': ['-added'],
                'verbose_name': 'notice',
                'verbose_name_plural': 'notices',
            },
            bases=(models.Model,),
        ),
    ]
<commit_msg>applied 2to3 fixers<commit_after># -*- coding: utf-8 -*-

from django.db import models, migrations
import datetime
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('notifications', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Notice',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('message', models.TextField(verbose_name='message')),
                ('added', models.DateTimeField(default=datetime.datetime.now, verbose_name='added')),
                ('unseen', models.BooleanField(default=True, verbose_name='unseen')),
                ('archived', models.BooleanField(default=False, verbose_name='archived')),
                ('notice_type', models.ForeignKey(verbose_name='notice type', to='notifications.NoticeType')),
                ('recipient', models.ForeignKey(related_name='received_notices', verbose_name='recipient', to=settings.AUTH_USER_MODEL)),
                ('sender', models.ForeignKey(related_name='sent_notices', verbose_name='sender', to=settings.AUTH_USER_MODEL, null=True)),
            ],
            options={
                'ordering': ['-added'],
                'verbose_name': 'notice',
                'verbose_name_plural': 'notices',
            },
            bases=(models.Model,),
        ),
    ]
<|endoftext|>