Skip to content

Commit 3b46596

Browse files
committed
Expose the new class registry methods.
Added wrappers for _aliasType, _deregisterType, isRegisteredType, and getAliasedTypes.
1 parent c168d85 commit 3b46596

3 files changed

Lines changed: 146 additions & 1 deletion

File tree

diffpy/srreal/_docstrings.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ def get_registry_docstrings(cls):
3737
("clone", doc_HasClassRegistry_clone),
3838
("type", doc_HasClassRegistry_type),
3939
("_registerThisType", doc_HasClassRegistry__registerThisType),
40+
("_aliasType", doc_HasClassRegistry__aliasType),
41+
("_deregisterType", doc_HasClassRegistry__deregisterType),
4042
("createByType", doc_HasClassRegistry_createByType),
43+
("isRegisteredType", doc_HasClassRegistry_isRegisteredType),
44+
("getAliasedTypes", doc_HasClassRegistry_getAliasedTypes),
4145
("getRegisteredTypes", doc_HasClassRegistry_getRegisteredTypes),
4246
)}
4347
return rv
@@ -74,6 +78,40 @@ class to support pickling and the `createByType` factory.
7478
"""
7579

7680

81+
doc_HasClassRegistry__aliasType = """\
82+
Register the specified class type under another string alias.
83+
84+
Parameters
85+
----------
86+
tp : str
87+
string type identifying a registered @NAME@ class.
88+
alias : str
89+
string alias to be used for the `tp` type.
90+
91+
Raises
92+
------
93+
RuntimeError
94+
When they `tp` type is unknown or if the `alias` type is already
95+
registered.
96+
"""
97+
98+
99+
doc_HasClassRegistry__deregisterType = """\
100+
Cancel registration of the specified string type and any of its aliases.
101+
102+
Parameters
103+
----------
104+
tp : str
105+
string type or an alias of a registered @NAME@ class.
106+
107+
Returns
108+
-------
109+
count : int
110+
Number of unregistered names or aliases.
111+
Return 0 if `tp` is not a registered type.
112+
"""
113+
114+
77115
doc_HasClassRegistry_createByType = """\
78116
Return a new @NAME@ instance of the specified string type.
79117
@@ -87,6 +125,30 @@ class to support pickling and the `createByType` factory.
87125
See Also
88126
--------
89127
getRegisteredTypes : Return set of the recognized type strings.
128+
getAliasedTypes : Return dictionary of string aliases.
129+
"""
130+
131+
132+
doc_HasClassRegistry_isRegisteredType = """\
133+
Check if the given string is registered as a @NAME@ type.
134+
135+
Parameters
136+
----------
137+
tp : str
138+
string name or an alias to be checked.
139+
140+
Return ``True`` if `tp` is known to the registry either as
141+
a standard type or its alias.
142+
"""
143+
144+
145+
doc_HasClassRegistry_getAliasedTypes = """\
146+
Get all aliases registered for the @NAME@ string types.
147+
148+
Returns
149+
-------
150+
dict
151+
a map of registered aliases to their corresponding standard names.
90152
"""
91153

92154

diffpy/srreal/tests/testpdfbaseline.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def setUp(self):
2121

2222

2323
def tearDown(self):
24+
for tp in PDFBaseline.getRegisteredTypes():
25+
PDFBaseline._deregisterType(tp)
26+
self.linear._registerThisType()
27+
self.zero._registerThisType()
2428
return
2529

2630

@@ -87,13 +91,69 @@ def test_type(self):
8791
return
8892

8993

94+
def test__aliasType(self):
95+
"""check PDFBaseline._aliasType.
96+
"""
97+
self.assertRaises(ValueError, PDFBaseline.createByType, "alias")
98+
self.assertRaises(RuntimeError, PDFBaseline._aliasType,
99+
"invalid", "alias")
100+
self.assertRaises(RuntimeError, PDFBaseline._aliasType,
101+
"linear", "zero")
102+
PDFBaseline._aliasType("linear", "alias")
103+
bl = PDFBaseline.createByType("alias")
104+
self.assertEqual("linear", bl.type())
105+
self.assertTrue(isinstance(bl, LinearBaseline))
106+
# second registration is a no-op
107+
PDFBaseline._aliasType("linear", "alias")
108+
bl1 = PDFBaseline.createByType("alias")
109+
self.assertTrue(isinstance(bl1, LinearBaseline))
110+
# no other type can be aliased to the existing name.
111+
self.assertRaises(RuntimeError, PDFBaseline._aliasType,
112+
"zero", "alias")
113+
return
114+
115+
116+
def test__deregisterType(self):
117+
"""check PDFBaseline._deregisterType.
118+
"""
119+
self.assertEqual(0, PDFBaseline._deregisterType("nonexistent"))
120+
PDFBaseline._aliasType("linear", "alias")
121+
self.assertEqual(2, PDFBaseline._deregisterType("alias"))
122+
self.assertFalse('linear' in PDFBaseline.getRegisteredTypes())
123+
self.assertEqual(0, PDFBaseline._deregisterType("alias"))
124+
return
125+
126+
90127
def test_createByType(self):
91128
"""check PDFBaseline.createByType()
92129
"""
93130
self.assertRaises(ValueError, PDFBaseline.createByType, 'notregistered')
94131
return
95132

96133

134+
def test_isRegisteredType(self):
135+
"""check PDFBaseline.isRegisteredType()
136+
"""
137+
self.assertTrue(PDFBaseline.isRegisteredType("linear"))
138+
self.assertFalse(PDFBaseline.isRegisteredType("nonexistent"))
139+
PDFBaseline._deregisterType("linear")
140+
self.assertFalse(PDFBaseline.isRegisteredType("linear"))
141+
return
142+
143+
144+
def test_getAliasedTypes(self):
145+
"""check PDFBaseline.getAliasedTypes()
146+
"""
147+
self.assertEqual({}, PDFBaseline.getAliasedTypes())
148+
PDFBaseline._aliasType("linear", "foo")
149+
PDFBaseline._aliasType("linear", "bar")
150+
PDFBaseline._aliasType("linear", "linear")
151+
PDFBaseline._aliasType("bar", "foo")
152+
self.assertEqual({'bar' : 'linear', 'foo' : 'linear'},
153+
PDFBaseline.getAliasedTypes())
154+
return
155+
156+
97157
def test_getRegisteredTypes(self):
98158
"""check PDFBaseline.getRegisteredTypes
99159
"""

srrealmodule/srreal_registry.hpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,14 @@ class wrapper_registry_configurator
7272
::boost::python::object get_registry_docstrings(::boost::python::object& cls);
7373

7474

75-
/// helper wrapper function for return value conversion.
75+
/// helper wrapper functions for return value conversions.
76+
77+
template <class W>
78+
::boost::python::object getAliasedTypes_asdict()
79+
{
80+
return convertToPythonDict(W::getAliasedTypes());
81+
}
82+
7683
template <class W>
7784
::boost::python::object getRegisteredTypes_asset()
7885
{
@@ -94,7 +101,11 @@ C& wrap_registry_methods(C& boostpythonclass)
94101
const char* doc_clone = CString(d["clone"]);
95102
const char* doc_type = CString(d["type"]);
96103
const char* doc__registerThisType = CString(d["_registerThisType"]);
104+
const char* doc__aliasType = CString(d["_aliasType"]);
105+
const char* doc__deregisterType = CString(d["_deregisterType"]);
97106
const char* doc_createByType = CString(d["createByType"]);
107+
const char* doc_isRegisteredType = CString(d["isRegisteredType"]);
108+
const char* doc_getAliasedTypes = CString(d["getAliasedTypes"]);
98109
const char* doc_getRegisteredTypes = CString(d["getRegisteredTypes"]);
99110
// define the class registry related methods.
100111
boostpythonclass
@@ -105,9 +116,21 @@ C& wrap_registry_methods(C& boostpythonclass)
105116
doc_type)
106117
.def("_registerThisType", &B::registerThisType,
107118
doc__registerThisType)
119+
.def("_aliasType", &B::aliasType,
120+
(bp::arg("tp"), bp::arg("alias")), doc__aliasType)
121+
.staticmethod("_aliasType")
122+
.def("_deregisterType", &B::deregisterType,
123+
bp::arg("tp"), doc__deregisterType)
124+
.staticmethod("_deregisterType")
108125
.def("createByType", &B::createByType,
109126
bp::arg("tp"), doc_createByType)
110127
.staticmethod("createByType")
128+
.def("isRegisteredType", &B::isRegisteredType,
129+
bp::arg("tp"), doc_isRegisteredType)
130+
.staticmethod("isRegisteredType")
131+
.def("getAliasedTypes", getAliasedTypes_asdict<B>,
132+
doc_getAliasedTypes)
133+
.staticmethod("getAliasedTypes")
111134
.def("getRegisteredTypes", getRegisteredTypes_asset<B>,
112135
doc_getRegisteredTypes)
113136
.staticmethod("getRegisteredTypes")

0 commit comments

Comments
 (0)