def testIndirect(self):
        urn = "@urn:eupspkg:pex_policy:tests/urn:indirect_parent_good.paf"
        p = Policy(urn)
        self.assertEqual(p.get("urn_full.name"), "Simple Policy")
        self.assertEqual(p.get("urn_brief.name"), "Simple Policy")
        self.assertEqual(p.get("urn_mixed_case.name"), "Simple Policy")
        self.assertEqual(p.get("local.foo"), "bar")

        p = Policy()
        UrnPolicyFile("pex_policy:tests/urn:level_1.paf").load(p)
        self.assertEqual(p.get("foo.bar.baz.qux.quux"), "schmazzle")
    def testIndirect(self):
        urn = "@urn:eupspkg:pex_policy:tests/urn:indirect_parent_good.paf"
        p = Policy(urn)
        self.assertEqual(p.get("urn_full.name"), "Simple Policy")
        self.assertEqual(p.get("urn_brief.name"), "Simple Policy")
        self.assertEqual(p.get("urn_mixed_case.name"), "Simple Policy")
        self.assertEqual(p.get("local.foo"), "bar")

        p = Policy()
        UrnPolicyFile("pex_policy:tests/urn:level_1.paf").load(p)
        self.assertEqual(p.get("foo.bar.baz.qux.quux"), "schmazzle")
    def testReference(self):
        addr = "pex_policy:examples:EventTransmitter_policy.paf"
        p = Policy()

        p.set("transmitter.logVerbosity", "not")
        UrnPolicyFile(addr).load(p)
        self.assertEqual(p.get("transmitter.logVerbosity"), "debug")

        p.set("transmitter.logVerbosity", "not")
        UrnPolicyFile("urn:eupspkg:" + addr).load(p)
        self.assertEqual(p.get("transmitter.logVerbosity"), "debug")

        p.set("transmitter.logVerbosity", "not")
        UrnPolicyFile("@@" + addr).load(p)
        self.assertEqual(p.get("transmitter.logVerbosity"), "debug")
    def testReference(self):
        addr = "pex_policy:examples:EventTransmitter_policy.paf"
        p = Policy()

        p.set("transmitter.logVerbosity", "not")
        UrnPolicyFile(addr).load(p)
        self.assertEqual(p.get("transmitter.logVerbosity"), "debug")

        p.set("transmitter.logVerbosity", "not")
        UrnPolicyFile("urn:eupspkg:" + addr).load(p)
        self.assertEqual(p.get("transmitter.logVerbosity"), "debug")

        p.set("transmitter.logVerbosity", "not")
        UrnPolicyFile("@@" + addr).load(p)
        self.assertEqual(p.get("transmitter.logVerbosity"), "debug")
Exemplo n.º 5
0
    def testRepos(self):
        # when the repository is mis-specified, local files cannot be loaded
        upf = UrnPolicyFile("pex_policy:tests:urn/indirect_parent_good.paf")
        # we expect it to look in <package>/tests/simple.paf
        expectedFile = os.environ["PEX_POLICY_DIR"] + "/tests/simple.paf"
        self.assertRaisesEx(IoErrorException,
                            "failure opening Policy file: " + expectedFile,
                            upf.load, "Wrong repository dir.", Policy())

        # a PAF file designed to have "tests" as it repository
        p = Policy()
        UrnPolicyFile("pex_policy:tests:urn/local_tests_repos.paf").load(p)
        self.assert_(p.get("local.polish") == "fancy")
    def testLoading(self):
        p = Policy("urn:eupspkg:pex_policy:tests/urn:level_1.paf")
        self.assertEqual(p.get("foo.bar.baz.qux.quux"), "schmazzle")

        self.assertRaiseLCE(BadNameError, "Wrong number of terms",
                            Policy, "URN too short",
                            "urn:eupspkg:foo.paf")
        self.assertRaiseLCE(lsst.pex.exceptions.IoError, "failure opening Policy file",
                            Policy, "URN abbrev '@' not allowed in constructor",
                            "@pex_policy:tests/urn:level_1.paf")

        urn = "urn:eupspkg:pex_policy:tests/dictionary:defaults_dictionary_good.paf"
        self.assertRaiseLCE(lsst.pex.exceptions.IoError, "/./defaults_dictionary_indirect",
                            Policy.createPolicyFromUrn,
                            "doesn't support loading undecorated DictionaryFile",
                            urn)
        urn = "urn:eupspkg:pex_policy:tests/dictionary:defaults_dictionary_partial.paf"
        p = Policy.createPolicyFromUrn(urn)
        # make sure all reference types worked
        # self.assertEqual(p.get("indirect.string_type"), "foo")
        # self.assertEqual(p.get("indirect2.string_type"), "foo")
        self.assertEqual(p.get("indirect3.string_type"), "foo")
        self.assertEqual(p.get("indirect4.string_type"), "foo")
    def testRepos(self):
        # when the repository is mis-specified, local files cannot be loaded
        upf = UrnPolicyFile("pex_policy:tests:urn/indirect_parent_good.paf")
        # we expect it to look in <package>/tests/simple.paf
        pexPolicyDir = lsst.utils.getPackageDir('pex_policy')
        expectedFile = pexPolicyDir + "/tests/simple.paf"
        self.assertRaiseLCE(lsst.pex.exceptions.IoError,
                            "failure opening Policy file: " + expectedFile,
                            upf.load, "Wrong repository dir.", Policy())

        # a PAF file designed to have "tests" as it repository
        p = Policy()
        UrnPolicyFile("pex_policy:tests:urn/local_tests_repos.paf").load(p)
        self.assertEqual(p.get("local.polish"), "fancy")
    def testRepos(self):
        # when the repository is mis-specified, local files cannot be loaded
        upf = UrnPolicyFile("pex_policy:tests:urn/indirect_parent_good.paf")
        # we expect it to look in <package>/tests/simple.paf
        pexPolicyDir = lsst.utils.getPackageDir('pex_policy')
        expectedFile = pexPolicyDir + "/tests/simple.paf"
        self.assertRaiseLCE(lsst.pex.exceptions.IoError,
                            "failure opening Policy file: " + expectedFile,
                            upf.load, "Wrong repository dir.", Policy())

        # a PAF file designed to have "tests" as it repository
        p = Policy()
        UrnPolicyFile("pex_policy:tests:urn/local_tests_repos.paf").load(p)
        self.assertEqual(p.get("local.polish"), "fancy")
    def testLoading(self):
        p = Policy("urn:eupspkg:pex_policy:tests/urn:level_1.paf")
        self.assertEqual(p.get("foo.bar.baz.qux.quux"), "schmazzle")

        self.assertRaiseLCE(BadNameError, "Wrong number of terms", Policy,
                            "URN too short", "urn:eupspkg:foo.paf")
        self.assertRaiseLCE(lsst.pex.exceptions.IoError,
                            "failure opening Policy file", Policy,
                            "URN abbrev '@' not allowed in constructor",
                            "@pex_policy:tests/urn:level_1.paf")

        urn = "urn:eupspkg:pex_policy:tests/dictionary:defaults_dictionary_good.paf"
        self.assertRaiseLCE(
            lsst.pex.exceptions.IoError, "/./defaults_dictionary_indirect",
            Policy.createPolicyFromUrn,
            "doesn't support loading undecorated DictionaryFile", urn)
        urn = "urn:eupspkg:pex_policy:tests/dictionary:defaults_dictionary_partial.paf"
        p = Policy.createPolicyFromUrn(urn)
        # make sure all reference types worked
        # self.assertEqual(p.get("indirect.string_type"), "foo")
        # self.assertEqual(p.get("indirect2.string_type"), "foo")
        self.assertEqual(p.get("indirect3.string_type"), "foo")
        self.assertEqual(p.get("indirect4.string_type"), "foo")
Exemplo n.º 10
0
    def __init__(self, ampPolicy="cfhtAmpBBoxPolicy.paf"):
        CcdInfo.__init__(self)          # Is this the best way to do this?
        
        self.nCcd = 36                  # Number of CCDs (0-indexed)
        self.nAmp = 8                   # Number of amplifiers (0-indexed)

        ampBBoxDb = Policy(ampPolicy)

        for amp in self.ampList():
            p = ampBBoxDb.get("CcdBBox.Amp%d" % amp)
            self.ampBBox[amp] = afwGeom.Box2I(afwGeom.Point2I(p.get("x0"), p.get("y0")),
                                              afwGeom.Extent2I(p.get("width"), p.get("height")))
            
        for amp in self.ampList():      # Build by hand
            bbox = self.getAmpBBox(amp)
            bbox.shift(32, 0)           # Allow for overclock
        
            self.trimSecBBox[amp] = bbox
Exemplo n.º 11
0
    def __init__(self, ampPolicy="cfhtAmpBBoxPolicy.paf"):
        CcdInfo.__init__(self)  # Is this the best way to do this?

        self.nCcd = 36  # Number of CCDs (0-indexed)
        self.nAmp = 8  # Number of amplifiers (0-indexed)

        ampBBoxDb = Policy(ampPolicy)

        for amp in self.ampList():
            p = ampBBoxDb.get("CcdBBox.Amp%d" % amp)
            self.ampBBox[amp] = afwGeom.Box2I(
                afwGeom.Point2I(p.get("x0"), p.get("y0")),
                afwGeom.Extent2I(p.get("width"), p.get("height")))

        for amp in self.ampList():  # Build by hand
            bbox = self.getAmpBBox(amp)
            bbox.shift(32, 0)  # Allow for overclock

            self.trimSecBBox[amp] = bbox
Exemplo n.º 12
0
class SliceInfoStage(Stage):
    '''Compute per-slice information.'''
    def __init__(self, stageId=-1, stagePolicy=None):
        Stage.__init__(self, stageId, stagePolicy)
        self.ampBBoxDb = Policy(self._policy.get("ampBBoxDbPath"))

    def preprocess(self):
        self.activeClipboard = self.inputQueue.getNextDataset()
        self._impl(self.activeClipboard)
        # Let postprocess() put self.activeClipboard on the output queue

    def process(self):
        """
        Compute the ampId and ccdId corresponding to this slice.
        """
        clipboard = self.inputQueue.getNextDataset()
        self._impl(clipboard)
        self.outputQueue.addDataset(clipboard)

    def _impl(self, clipboard):
        sliceId = self.getRank()

        nAmps = self._policy.get("nAmps")
        nCcds = self._policy.get("nCcds")

        ccdFormula = self._policy.get("ccdIdFormula")
        ampFormula = self._policy.get("ampIdFormula")

        ccdId = eval(ccdFormula)
        ampId = eval(ampFormula)

        clipboard.put("ccdId", ccdId)
        clipboard.put("ampId", ampId)
        clipboard.put("ampBBox", self.lookupAmpBBox(ampId, ccdId))

    def lookupAmpBBox(self, ampId, ccdId):
        key = "CcdBBox.Amp%d" % ampId
        p = self.ampBBoxDb.get(key)
        return afwImage.BBox(afwImage.PointI(p.get("x0"), p.get("y0")),
                             p.get("width"), p.get("height"))
Exemplo n.º 13
0
    def testEmptySubdict(self):
        d = Dictionary(self.getTestDictionary("empty_subdictionary.paf"))
        p = Policy()
        p.set("empty_required", Policy(self.getTestDictionary("simple_policy.paf")))
        p.mergeDefaults(d)
        self.assert_(p.get("empty_sub_with_default.foo") == "bar")
        p.setDictionary(d)
        # this works because there is a definition for "empty_sub_with_default.foo"
        p.set("empty_sub_with_default.foo", "baz")

        p2 = Policy()
        p2.set("foo", "baz")
        p.set("empty_sub_no_default", p2)
        # this fails because Policy tries to makeDef("empty_sub_no_default.foo")
        # which fails because there's only a definition for "empty_sub_no_default",
        # but it doesn't contain any sub-definitions
        # p.set("empty_sub_no_default.foo", "baz")
        self.assertRaiseLCE(DictionaryError,
                            "empty_sub_no_default.dictionary not found",
                            p.set, "Empty policy definition -- if this fails, "
                            "it means a known bug has been fixed.  That's good.",
                            "empty_sub_no_default.foo", "baz")
Exemplo n.º 14
0
    def testEmptySubdict(self):
        d = Dictionary(self.getTestDictionary("empty_subdictionary.paf"))
        p = Policy()
        p.set("empty_required", Policy(self.getTestDictionary("simple_policy.paf")))
        p.mergeDefaults(d)
        self.assertEqual(p.get("empty_sub_with_default.foo"), "bar")
        p.setDictionary(d)
        # this works because there is a definition for "empty_sub_with_default.foo"
        p.set("empty_sub_with_default.foo", "baz")

        p2 = Policy()
        p2.set("foo", "baz")
        p.set("empty_sub_no_default", p2)
        # this fails because Policy tries to makeDef("empty_sub_no_default.foo")
        # which fails because there's only a definition for "empty_sub_no_default",
        # but it doesn't contain any sub-definitions
        # p.set("empty_sub_no_default.foo", "baz")
        self.assertRaiseLCE(DictionaryError,
                            "empty_sub_no_default.dictionary not found",
                            p.set, "Empty policy definition -- if this fails, "
                            "it means a known bug has been fixed.  That's good.",
                            "empty_sub_no_default.foo", "baz")
Exemplo n.º 15
0
class PolicyBlackboardItem(BlackboardItem):
    """
    An implementation of a BlackboardItem that stores properities via a
    policy
    """
    def __init__(self, policyfile=None):
        """
        create an item with the given properties.
        @param policyfile    A policy
        """
        # the properties attached to this items
        self._props = None
        if policyfile:
            self._props = Policy.createPolicy(policyfile)
        else:
            self._props = Policy()

    def getProperty(self, name, default=None):
        """
        return the value for a property
        @param name      the property name
        @parma default   the default value to return if the name is not set
        """
        if not self._props.exists(name):
            return default
        elif self._props.isArray(name):
            return self._props.getArray(name)
        else:
            return self._props.get(name)

    def hasProperty(self, name):
        """
        return True if the property with the given name is available
        """
        return self._props.exists(name)

    def __getitem__(self, name):
        if not self._props.exists(name):
            raise KeyError(name)
        return BlackboardItem.__getitem__(self, name)

    def _setProperty(self, name, val):
        # set a property value
        if isinstance(val, list):
            self._props.set(name, val.pop(0))
            for v in val:
                self._props.add(name, v)
        else:
            self._props.set(name, val)

    def getPropertyNames(self):
        """
        return the property names that make up this item
        """
        return self._props.names()

    def _copyFrom(self, item):
        for name in item.getPropertyNames():
            self._setProperty(name, item.getProperty(name))

    @staticmethod
    def createFormatter():
        return PolicyBlackboardItem._Fmtr()

    class _Fmtr(object):
        def write(self, filename, item):
            pol = None
            if isinstance(item, PolicyBlackboardItem):
                pol = item._props
            else:
                delegate = PolicyBlackboardItem()
                delegate._copyFrom(item)
                pol = delegate._props

            writer = PAFWriter(filename)
            try:
                writer.write(pol, True)
            finally:
                writer.close()

        def openItem(self, filename):
            out = PolicyBlackboardItem()
            out._props = Policy.createPolicy(filename)
            return out

        def filenameExt(self):
            """
            return the recommended extension for the format this writes out
            """
            return "paf"
Exemplo n.º 16
0
    def testPolicySetget(self):
        p = Policy()
        self.assert_(not p.exists("foo"), "empty existence test failed")
        self.assertEqual(p.valueCount("foo.bar"), 0,
                         "empty valueCount test failed")

        self.assertRaises(NameNotFound, p.getTypeInfo, "foo")

        p.set("doall", "true")

        # non-existence tests on a non-empty policy
        self.failUnless(not p.exists("foo"),
                        "non-empty non-existence test failed")
        self.assertEqual(p.valueCount("foo.bar"), 0,
                         "empty valueCount test failed")
        self.failUnless(not p.isInt("foo"),
                        "non-empty non-existence type test failed")
        self.assertRaises(NameNotFound, p.getTypeInfo, "foo")

        # existence tests
        self.assert_(p.exists("doall"), "non-empty existence test failed")
        self.assertEquals(p.valueCount("doall"), 1,
                          "single valueCount test failed")

        self.assertRaises(Exception, p.getInt, "doall")
        self.assertRaises(Exception, p.getDoubleArray, "doall")

        self.assertEquals(p.get("doall"), "true",
                          "top-level getString failed")
        p.set("doall", "duh")
        self.assertEquals(p.get("doall"), "duh",
                          "top-level getString failed")
        
        # test array access
        ary = p.getArray("doall")
        self.assertEquals(len(ary), 1,
                          "scalar property has more than one value")


        self.assertEquals(ary[0], "duh", "scalar access via array failed")

        p.add("doall", "never")
        self.assertEquals(p.valueCount("doall"), 2,
                          "2-elem. valueCount test failed")
        self.assertEquals(p.get("doall"), "never", "top-level add failed")
        ary = p.getArray("doall")
        self.assertEquals(len(ary), 2,
                          "scalar property has wrong number of values")
        self.assertEquals(ary[0], "duh",
                          "scalar access via (2-el) array failed")
        self.assertEquals(ary[-1], "never",
                          "scalar access via (2-el) array failed")

        # test hierarchical access

        # list names

        # test types
        p.set("pint", 5)
        self.assertEquals(p.getInt("pint"), 5, "support for type int failed")
        self.assertEquals(type(p.get("pint")), type(5),
                          "auto-typing for int failed")
        p.set("pdbl", 5.1)
        self.assertAlmostEquals(p.getDouble("pdbl"), 5.1, 7, 
                                "support for type double failed")
        self.assertEquals(type(p.get("pdbl")), type(5.1),
                          "auto-typing for double failed")
        p.set("pbool", True)
        self.assert_(p.getBool("pbool"), "support for type bool failed")
        self.assertEquals(type(p.get("pbool")), type(True),
                          "auto-typing for bool failed")
        p.add("pbool", False)

        # test shallow & deep copies

        # test raise NameNotFound if not present
        try:
            p.get("nonexistent")
            self.fail() # should never reach here
        except Exception, e:
            self.assert_(isinstance(e, NameNotFound))
Exemplo n.º 17
0
    def testPolicySetget(self):
        p = Policy()
        self.assertFalse(p.exists("foo"), "empty existence test failed")
        self.assertEqual(p.valueCount("foo.bar"), 0,
                         "empty valueCount test failed")

        self.assertRaises(lsst.pex.exceptions.Exception, p.getTypeInfo, "foo")

        p.set("doall", "true")

        # non-existence tests on a non-empty policy
        self.assertFalse(p.exists("foo"),
                         "non-empty non-existence test failed")
        self.assertEqual(p.valueCount("foo.bar"), 0,
                         "empty valueCount test failed")
        self.assertFalse(p.isInt("foo"),
                         "non-empty non-existence type test failed")
        self.assertRaises(lsst.pex.exceptions.Exception, p.getTypeInfo, "foo")

        # existence tests
        self.assertTrue(p.exists("doall"), "non-empty existence test failed")
        self.assertEqual(p.valueCount("doall"), 1,
                         "single valueCount test failed")

        self.assertRaises(lsst.pex.exceptions.Exception, p.getInt, "doall")
        self.assertRaises(lsst.pex.exceptions.Exception, p.getDoubleArray,
                          "doall")

        self.assertEqual(p.get("doall"), "true", "top-level getString failed")
        p.set("doall", "duh")
        self.assertEqual(p.get("doall"), "duh", "top-level getString failed")

        # test array access
        ary = p.getArray("doall")
        self.assertEqual(len(ary), 1,
                         "scalar property has more than one value")

        self.assertEqual(ary[0], "duh", "scalar access via array failed")

        p.add("doall", "never")
        self.assertEqual(p.valueCount("doall"), 2,
                         "2-elem. valueCount test failed")
        self.assertEqual(p.get("doall"), "never", "top-level add failed")
        ary = p.getArray("doall")
        self.assertEqual(len(ary), 2,
                         "scalar property has wrong number of values")
        self.assertEqual(ary[0], "duh",
                         "scalar access via (2-el) array failed")
        self.assertEqual(ary[-1], "never",
                         "scalar access via (2-el) array failed")

        # test hierarchical access

        # list names

        # test types
        p.set("pint", 5)
        self.assertEqual(p.getInt("pint"), 5, "support for type int failed")
        self.assertIsInstance(p.get("pint"), int, "auto-typing for int failed")
        p.set("pdbl", 5.1)
        self.assertAlmostEqual(p.getDouble("pdbl"), 5.1, 7,
                               "support for type double failed")
        self.assertEqual(type(p.get("pdbl")), type(5.1),
                         "auto-typing for double failed")
        p.set("pbool", True)
        self.assertTrue(p.getBool("pbool"), "support for type bool failed")
        self.assertEqual(type(p.get("pbool")), type(True),
                         "auto-typing for bool failed")
        p.add("pbool", False)

        # test shallow & deep copies

        # test raise NameNotFound if not present
        self.assertRaises(NameNotFound, p.get, "nonexistent")
        self.assertRaises(NameNotFound, p.getArray, "nonexistent")
        self.assertRaises(NameNotFound, p.getDouble, "nonexistent")
Exemplo n.º 18
0
class PolicyBlackboardItem(BlackboardItem):
    """
    An implementation of a BlackboardItem that stores properities via a
    policy
    """

    def __init__(self, policyfile=None):
        """
        create an item with the given properties.
        @param policyfile    A policy
        """
        # the properties attached to this items
        self._props = None
        if policyfile:
            self._props = Policy.createPolicy(policyfile)
        else:
            self._props = Policy()

    def getProperty(self, name, default=None):
        """
        return the value for a property
        @param name      the property name
        @parma default   the default value to return if the name is not set
        """
        if not self._props.exists(name):
            return default
        elif self._props.isArray(name):
            return self._props.getArray(name)
        else:
            return self._props.get(name)

    def hasProperty(self, name):
        """
        return True if the property with the given name is available
        """
        return self._props.exists(name)

    def __getitem__(self, name):
        if not self._props.exists(name):
            raise KeyError(name)
        return BlackboardItem.__getitem__(self, name)

    def _setProperty(self, name, val):
        # set a property value
        if isinstance(val, list):
            self._props.set(name, val.pop(0))
            for v in val:
                self._props.add(name, v)
        else:
            self._props.set(name, val)

    def getPropertyNames(self):
        """
        return the property names that make up this item
        """
        return self._props.names()

    def _copyFrom(self, item):
        for name in item.getPropertyNames():
            self._setProperty(name, item.getProperty(name))

    @staticmethod
    def createFormatter():
        return PolicyBlackboardItem._Fmtr()

    class _Fmtr(object):
        def write(self, filename, item):
            pol = None
            if isinstance(item, PolicyBlackboardItem):
                pol = item._props
            else:
                delegate = PolicyBlackboardItem()
                delegate._copyFrom(item)
                pol = delegate._props

            writer = PAFWriter(filename)
            try:
                writer.write(pol, True)
            finally:
                writer.close()

        def openItem(self, filename):
            out = PolicyBlackboardItem()
            out._props = Policy.createPolicy(filename)
            return out

        def filenameExt(self):
            """
            return the recommended extension for the format this writes out
            """
            return "paf"