aboutsummaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/additional_tests.cc31
-rw-r--r--python/metadata_key.cc74
-rwxr-xr-xpython/metadata_key_TEST.py14
3 files changed, 109 insertions, 10 deletions
diff --git a/python/additional_tests.cc b/python/additional_tests.cc
index 76d9a6e..5cdc1ae 100644
--- a/python/additional_tests.cc
+++ b/python/additional_tests.cc
@@ -23,6 +23,8 @@
#include <paludis/util/tr1_memory.hh>
#include <paludis/util/set.hh>
+#include <paludis/util/wrapped_forward_iterator.hh>
+#include <paludis/util/indirect_iterator-impl.hh>
#include <paludis/environment.hh>
#include <paludis/environments/test/test_environment.hh>
#include <paludis/package_database.hh>
@@ -136,45 +138,52 @@ namespace mask
namespace metadata_key
{
- void test_metadata_key(MetadataKey & m)
+ void test_metadata_key(const MetadataKey & m)
{
m.raw_name();
m.human_name();
MetadataKeyType foo(m.type());
}
- void test_metadata_package_id_key(MetadataPackageIDKey & m)
+ void test_metadata_package_id_key(const MetadataPackageIDKey & m)
{
test_metadata_key(m);
m.value();
}
- void test_metadata_string_key(MetadataStringKey & m)
+ void test_metadata_string_key(const MetadataStringKey & m)
{
test_metadata_key(m);
m.value();
}
- void test_metadata_time_key(MetadataTimeKey & m)
+ void test_metadata_section_key(const MetadataSectionKey & m)
+ {
+ test_metadata_key(m);
+ std::for_each(indirect_iterator(m.begin_metadata()), indirect_iterator(m.end_metadata()),
+ &test_metadata_key);
+ }
+
+ void test_metadata_time_key(const MetadataTimeKey & m)
{
test_metadata_key(m);
time_t PALUDIS_ATTRIBUTE((unused)) t(m.value());
}
- void test_metadata_contents_key(MetadataContentsKey & m)
+ void test_metadata_contents_key(const MetadataContentsKey & m)
{
test_metadata_key(m);
m.value();
}
- void test_metadata_repository_mask_info_key(MetadataRepositoryMaskInfoKey & m)
+ void test_metadata_repository_mask_info_key(const MetadataRepositoryMaskInfoKey & m)
{
test_metadata_key(m);
m.value();
}
template <typename C_>
- void test_metadata_set_key(MetadataSetKey<C_> & m)
+ void test_metadata_set_key(const MetadataSetKey<C_> & m)
{
test_metadata_key(m);
m.value();
@@ -183,7 +192,7 @@ namespace metadata_key
}
template <>
- void test_metadata_set_key(MetadataSetKey<IUseFlagSet> & m)
+ void test_metadata_set_key(const MetadataSetKey<IUseFlagSet> & m)
{
test_metadata_key(m);
m.value();
@@ -198,7 +207,7 @@ namespace metadata_key
}
template <typename C_>
- void test_metadata_spec_tree_key(MetadataSpecTreeKey<C_> & m)
+ void test_metadata_spec_tree_key(const MetadataSpecTreeKey<C_> & m)
{
test_metadata_key(m);
m.value();
@@ -208,7 +217,7 @@ namespace metadata_key
}
template <>
- void test_metadata_spec_tree_key(MetadataSpecTreeKey<FetchableURISpecTree> & m)
+ void test_metadata_spec_tree_key(const MetadataSpecTreeKey<FetchableURISpecTree> & m)
{
test_metadata_key(m);
m.value();
@@ -314,6 +323,7 @@ void expose_additional_tests()
bp::def("test_metadata_restrict_spec_tree_key", &metadata_key::test_metadata_spec_tree_key<RestrictSpecTree>);
bp::def("test_metadata_fetchable_uri_spec_tree_key", &metadata_key::test_metadata_spec_tree_key<FetchableURISpecTree>);
bp::def("test_metadata_simple_uri_spec_tree_key", &metadata_key::test_metadata_spec_tree_key<SimpleURISpecTree>);
+ bp::def("test_metadata_section_key", &metadata_key::test_metadata_section_key);
/**
* Formatter tests
@@ -325,3 +335,4 @@ void expose_additional_tests()
bp::def("test_package_roles", &formatter::test_package_roles);
bp::def("test_can_space", &formatter::test_can_space);
}
+
diff --git a/python/metadata_key.cc b/python/metadata_key.cc
index 4df0d4f..b5e866d 100644
--- a/python/metadata_key.cc
+++ b/python/metadata_key.cc
@@ -133,6 +133,11 @@ class MetadataKeySptrToPythonVisitor :
{
obj = bp::object(tr1::static_pointer_cast<const MetadataSetKey<PackageIDSequence> >(_m_ptr));
}
+
+ void visit(const MetadataSectionKey & k)
+ {
+ obj = bp::object(tr1::static_pointer_cast<const MetadataSectionKey>(_m_ptr));
+ }
};
struct MetadataKeySptrToPython
@@ -193,6 +198,46 @@ struct MetadataStringKeyWrapper :
}
};
+struct MetadataSectionKeyWrapper :
+ MetadataSectionKey,
+ bp::wrapper<MetadataSectionKey>
+{
+ MetadataSectionKeyWrapper(const std::string & r, const std::string & h, const MetadataKeyType t) :
+ MetadataSectionKey(r, h, t)
+ {
+ }
+
+ static PyObject *
+ find_metadata(const MetadataSectionKey & self, const std::string & key)
+ {
+ MetadataSectionKey::MetadataConstIterator i(self.find_metadata(key));
+ if (i != self.end_metadata())
+ return bp::incref(bp::object(*i).ptr());
+ else
+ return Py_None;
+ }
+
+ virtual void need_keys_added() const
+ {
+ Lock l(get_mutex());
+
+ if (bp::override f = get_override("need_keys_added"))
+ f();
+ else
+ throw PythonMethodNotImplemented("MetadataSectionKey", "need_keys_added");
+ }
+
+ virtual const tr1::shared_ptr<const MetadataStringKey> title_key() const
+ {
+ Lock l(get_mutex());
+
+ if (bp::override f = get_override("title_key"))
+ return f();
+ else
+ throw PythonMethodNotImplemented("MetadataSectionKey", "title_key");
+ }
+};
+
struct MetadataTimeKeyWrapper :
MetadataTimeKey,
bp::wrapper<MetadataTimeKey>
@@ -727,6 +772,35 @@ void expose_metadata_key()
;
/**
+ * MetadataSectionKey
+ */
+ bp::register_ptr_to_python<tr1::shared_ptr<const MetadataSectionKey> >();
+ bp::implicitly_convertible<tr1::shared_ptr<MetadataSectionKeyWrapper>,
+ tr1::shared_ptr<MetadataKey> >();
+ bp::class_<MetadataSectionKeyWrapper, tr1::shared_ptr<MetadataSectionKeyWrapper>,
+ bp::bases<MetadataKey>, boost::noncopyable>
+ (
+ "MetadataSectionKey",
+ "A MetadataSectionKey holds a number of other MetadataKey instances, and\n"
+ "may have a title.\n\n"
+
+ "This class can be subclassed in Python.",
+ bp::init<const std::string &, const std::string &, MetadataKeyType>(
+ "__init__(raw_name, human_name, MetadataKeyType)"
+ )
+ )
+ .add_property("metadata", bp::range(&MetadataSectionKey::begin_metadata, &MetadataSectionKey::end_metadata),
+ "[ro] Iterable of MetadataKey\n"
+ "NEED_DOC"
+ )
+
+ .def("find_metadata", &MetadataSectionKeyWrapper::find_metadata,
+ "find_metadata(string) -> MetadataKey\n"
+ "NEED_DOC"
+ )
+ ;
+
+ /**
* MetadataTimeKey
*/
bp::register_ptr_to_python<tr1::shared_ptr<const MetadataTimeKey> >();
diff --git a/python/metadata_key_TEST.py b/python/metadata_key_TEST.py
index d9beeb1..cc55b74 100755
--- a/python/metadata_key_TEST.py
+++ b/python/metadata_key_TEST.py
@@ -298,6 +298,20 @@ class TestCase_02_MetadataKeys_suclassing(unittest.TestCase):
test_metadata_simple_uri_spec_tree_key(TestKey())
+ def test_16_section(self):
+ class TestKey(MetadataSectionKey):
+ def __init__(self):
+ MetadataSectionKey.__init__(self, "raw", "human", MetadataKeyType.NORMAL)
+
+ def need_keys_added(self):
+ return
+
+ def title_key(self):
+ return None
+
+ test_metadata_section_key(TestKey())
+
+
if __name__ == "__main__":
unittest.main()