aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2008-11-24 18:31:31 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2008-11-24 18:31:31 +0000
commit18c6b34313f7b5d81f50a400f6a2f4bce7ceb7f7 (patch)
tree46249081751c7a8118c237b354d9e5875ed5fb3f
parentbbb8d84a9afb74cb1ad0c33143d977ad63daef3e (diff)
downloadpaludis-18c6b34313f7b5d81f50a400f6a2f4bce7ceb7f7.tar.gz
paludis-18c6b34313f7b5d81f50a400f6a2f4bce7ceb7f7.tar.xz
Python choices
-rw-r--r--python/Makefile.am3
-rw-r--r--python/additional_tests.cc7
-rw-r--r--python/choices.cc164
-rw-r--r--python/choices_TEST.py137
-rwxr-xr-xpython/choices_TEST_cleanup.sh9
-rwxr-xr-xpython/choices_TEST_setup.sh45
-rw-r--r--python/metadata_key.cc63
-rwxr-xr-xpython/metadata_key_TEST.py13
-rw-r--r--python/paludis_python.hh1
-rw-r--r--python/paludis_python_so.cc1
10 files changed, 443 insertions, 0 deletions
diff --git a/python/Makefile.am b/python/Makefile.am
index d518361..ca94fb3 100644
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -25,6 +25,7 @@ MAINTAINERCLEANFILES = Makefile.in
IF_PYTHON_TESTS = \
action_TEST.py \
contents_TEST.py \
+ choices_TEST.py \
dep_label_TEST.py \
dep_list_TEST.py \
dep_spec_TEST.py \
@@ -56,6 +57,7 @@ IF_PYTHON_SOURCES = \
validated.hh \
about.cc \
action.cc \
+ choices.cc \
contents.cc \
dep_label.cc \
dep_list.cc \
@@ -93,6 +95,7 @@ EXTRA_DIST = $(IF_PYTHON_TESTS) $(IF_PYTHON_SOURCES) \
$(IF_PYTHON_QA_TESTS) $(IF_PYTHON_QA_SOURCES) \
paludis_python_so.cc \
additional_tests_so.cc \
+ choices_TEST_setup.sh choices_TEST_cleanup.sh \
dep_list_TEST_setup.sh dep_list_TEST_cleanup.sh \
environment_TEST_setup.sh environment_TEST_cleanup.sh \
mask_TEST_setup.sh mask_TEST_cleanup.sh \
diff --git a/python/additional_tests.cc b/python/additional_tests.cc
index c5f60ee..a96a7f4 100644
--- a/python/additional_tests.cc
+++ b/python/additional_tests.cc
@@ -176,6 +176,12 @@ namespace metadata_key
m.value();
}
+ void test_metadata_choices_key(const MetadataValueKey<std::tr1::shared_ptr<const Choices> > & m)
+ {
+ test_metadata_key(m);
+ m.value();
+ }
+
void test_metadata_repository_mask_info_key(const MetadataValueKey<std::tr1::shared_ptr<const RepositoryMaskInfo> > & m)
{
test_metadata_key(m);
@@ -306,6 +312,7 @@ void expose_additional_tests()
bp::def("test_metadata_string_key", &metadata_key::test_metadata_string_key);
bp::def("test_metadata_time_key", &metadata_key::test_metadata_time_key);
bp::def("test_metadata_contents_key", &metadata_key::test_metadata_contents_key);
+ bp::def("test_metadata_choices_key", &metadata_key::test_metadata_choices_key);
bp::def("test_metadata_repository_mask_info_key", &metadata_key::test_metadata_repository_mask_info_key);
bp::def("test_metadata_keyword_name_set_key", &metadata_key::test_metadata_set_key<KeywordNameSet>);
bp::def("test_metadata_string_set_key", &metadata_key::test_metadata_set_key<Set<std::string> >);
diff --git a/python/choices.cc b/python/choices.cc
new file mode 100644
index 0000000..6a174b1
--- /dev/null
+++ b/python/choices.cc
@@ -0,0 +1,164 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2008 Ciaran McCreesh
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <python/paludis_python.hh>
+#include <python/exception.hh>
+#include <python/validated.hh>
+
+#include <paludis/util/validated.hh>
+#include <paludis/util/wrapped_forward_iterator.hh>
+#include <paludis/choice.hh>
+
+using namespace paludis;
+using namespace paludis::python;
+namespace bp = boost::python;
+
+void expose_choices()
+{
+ bp::class_<Choices, std::tr1::shared_ptr<const Choices>, boost::noncopyable> choices(
+ "Choices",
+ "A collection of configurable values for a PackageID",
+ bp::init<>("__init__()")
+ );
+
+ choices
+ .def("find_by_name_with_prefix", &Choices::find_by_name_with_prefix,
+ "find_by_name_with_prefix(ChoiceNameWithPrefix) -> ChoiceValue or nil\n"
+ )
+
+ .def("has_matching_contains_every_value_prefix", &Choices::has_matching_contains_every_value_prefix,
+ "has_matching_contains_every_value_prefix(ChoiceNameWithPrefix) -> bool\n"
+ )
+
+ .def("__iter__", bp::range(&Choices::begin, &Choices::end),
+ "[ro] Iterable of Choice\n"
+ )
+ ;
+
+ bp::class_<Choice, std::tr1::shared_ptr<const Choice>, boost::noncopyable> choice(
+ "Choice",
+ "An individual choice in a Choices collection.",
+ bp::no_init
+ );
+
+ choice
+ .add_property("raw_name", &Choice::raw_name,
+ "[ro] String\n"
+ )
+
+ .add_property("human_name", &Choice::human_name,
+ "[ro] String\n"
+ )
+
+ .add_property("prefix", &Choice::prefix,
+ "[ro] ChoicePrefixName\n"
+ )
+
+ .add_property("contains_every_value", &Choice::contains_every_value,
+ "[ro] bool\n"
+ )
+
+ .add_property("hidden", &Choice::hidden,
+ "[ro] bool\n"
+ )
+
+ .add_property("show_with_no_prefix", &Choice::show_with_no_prefix,
+ "[ro] bool\n"
+ )
+
+ .add_property("consider_added_or_changed", &Choice::consider_added_or_changed,
+ "[ro] bool\n"
+ )
+
+ .def("__iter__", bp::range(&Choice::begin, &Choice::end),
+ "[ro] Iterable of ChoiceValue\n"
+ )
+ ;
+
+ bp::class_<ChoiceValue, std::tr1::shared_ptr<const ChoiceValue>, boost::noncopyable> choice_value(
+ "ChoiceValue",
+ "An individual value in a ChoiceValue",
+ bp::no_init
+ );
+
+ choice_value
+ .add_property("unprefixed_name", &ChoiceValue::unprefixed_name,
+ "[ro] UnprefixedChoiceName\n"
+ )
+
+ .add_property("name_with_prefix", &ChoiceValue::name_with_prefix,
+ "[ro] ChoiceNameWithPrefix\n"
+ )
+
+ .add_property("enabled", &ChoiceValue::enabled,
+ "[ro] bool\n"
+ )
+
+ .add_property("enabled_by_default", &ChoiceValue::enabled_by_default,
+ "[ro] bool\n"
+ )
+
+ .add_property("locked", &ChoiceValue::locked,
+ "[ro] bool\n"
+ )
+
+ .add_property("description", &ChoiceValue::description,
+ "[ro] String\n"
+ )
+
+ .add_property("explicitly_listed", &ChoiceValue::explicitly_listed,
+ "[ro] bool\n"
+ )
+
+ ;
+
+ ExceptionRegister::get_instance()->add_exception<ChoiceNameWithPrefixError>
+ ("ChoiceNameWithPrefixError", "NameError",
+ "Thrown if an invalid value is assigned to a ChoiceNameWithPrefix.");
+
+ register_shared_ptrs_to_python<ChoiceNameWithPrefix>();
+ class_validated<ChoiceNameWithPrefix>
+ (
+ "ChoiceNameWithPrefix",
+ "A choice name, including prefix and delim."
+ );
+
+ ExceptionRegister::get_instance()->add_exception<ChoicePrefixNameError>
+ ("ChoicePrefixNameError", "NameError",
+ "Thrown if an invalid value is assigned to a ChoicePrefixName.");
+
+ register_shared_ptrs_to_python<ChoicePrefixName>();
+ class_validated<ChoicePrefixName>
+ (
+ "ChoicePrefixName",
+ "A choice prefix name."
+ );
+
+ ExceptionRegister::get_instance()->add_exception<UnprefixedChoiceNameError>
+ ("UnprefixedChoiceNameError", "NameError",
+ "Thrown if an invalid value is assigned to an UnprefixedChoiceName.");
+
+ register_shared_ptrs_to_python<UnprefixedChoiceName>();
+ class_validated<UnprefixedChoiceName>
+ (
+ "UnprefixedChoiceName",
+ "A choice name, without prefix."
+ );
+}
+
diff --git a/python/choices_TEST.py b/python/choices_TEST.py
new file mode 100644
index 0000000..f785c31
--- /dev/null
+++ b/python/choices_TEST.py
@@ -0,0 +1,137 @@
+#!/usr/bin/env python
+# vim: set fileencoding=utf-8 sw=4 sts=4 et :
+
+#
+# Copyright (c) 2008 Ciaran McCreesh
+#
+# This file is part of the Paludis package manager. Paludis is free software;
+# you can redistribute it and/or modify it under the terms of the GNU General
+# Public License version 2, as published by the Free Software Foundation.
+#
+# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+# Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+import os
+
+repo_path = os.path.join(os.getcwd(), "choices_TEST_dir/testrepo")
+
+from paludis import *
+from additional_tests import *
+
+import unittest
+
+Log.instance.log_level = LogLevel.WARNING
+
+class TestCase_01_Choices(unittest.TestCase):
+ def setUp(self):
+ self.e = NoConfigEnvironment(repo_path, "/var/empty")
+ self.pid = iter(self.e.package_database.fetch_repository("testrepo").package_ids("foo/bar")).next()
+ self.choices = self.pid.find_metadata("PALUDIS_CHOICES").value()
+
+ def test_01_choices(self):
+ self.assert_(isinstance(self.choices, Choices), self.choices.__class__)
+
+ def test_02_find_by_name_with_prefix(self):
+ self.assert_(self.choices.find_by_name_with_prefix("testflag"))
+ self.assert_(isinstance(self.choices.find_by_name_with_prefix("testflag"), ChoiceValue))
+ self.assert_(not self.choices.find_by_name_with_prefix("monkey"))
+
+ def test_03_has_matching_contains_every_value_prefix(self):
+ self.assert_(self.choices.has_matching_contains_every_value_prefix("linguas_en"))
+ self.assert_(not self.choices.has_matching_contains_every_value_prefix("foo"))
+
+ def test_04_iter(self):
+ self.assert_(isinstance(iter(self.choices).next(), Choice))
+ found = False
+ for f in self.choices:
+ if f.raw_name == "USE":
+ found = True
+ self.assert_(found)
+
+class TestCase_02_Choice(unittest.TestCase):
+ def setUp(self):
+ self.e = NoConfigEnvironment(repo_path, "/var/empty")
+ self.pid = iter(self.e.package_database.fetch_repository("testrepo").package_ids("foo/bar")).next()
+ self.choices = self.pid.find_metadata("PALUDIS_CHOICES").value()
+ self.use = None
+ self.linguas = None
+ for f in self.choices:
+ if f.raw_name == "USE":
+ self.use = f
+ elif f.raw_name == "LINGUAS":
+ self.linguas = f
+
+ def test_01_use(self):
+ self.assert_(self.use)
+ self.assertEquals(self.use.raw_name, "USE")
+ self.assertEquals(self.use.human_name, "USE")
+ self.assertEquals(self.use.prefix, "")
+ self.assertEquals(self.use.contains_every_value, False)
+ self.assertEquals(self.use.hidden, False)
+ self.assertEquals(self.use.show_with_no_prefix, True)
+ self.assertEquals(self.use.consider_added_or_changed, True)
+
+ def test_02_linguas(self):
+ self.assert_(self.linguas)
+ self.assertEquals(self.linguas.raw_name, "LINGUAS")
+ self.assertEquals(self.linguas.human_name, "linguas")
+ self.assertEquals(self.linguas.prefix, "linguas")
+ self.assertEquals(self.linguas.contains_every_value, True)
+ self.assertEquals(self.linguas.hidden, False)
+ self.assertEquals(self.linguas.show_with_no_prefix, False)
+ self.assertEquals(self.linguas.consider_added_or_changed, True)
+
+ def test_03_use_iter(self):
+ self.assert_(isinstance(iter(self.use).next(), ChoiceValue))
+ found = False
+ for f in self.use:
+ if f.name_with_prefix == "testflag":
+ found = True
+ self.assert_(found)
+
+ def test_04_linguas_iter(self):
+ self.assert_(isinstance(iter(self.linguas).next(), ChoiceValue))
+ found = False
+ for f in self.linguas:
+ if f.name_with_prefix == "linguas_en":
+ found = True
+ self.assert_(found)
+
+class TestCase_03_ChoiceValue(unittest.TestCase):
+ def setUp(self):
+ self.e = NoConfigEnvironment(repo_path, "/var/empty")
+ self.pid = iter(self.e.package_database.fetch_repository("testrepo").package_ids("foo/bar")).next()
+ self.choices = self.pid.find_metadata("PALUDIS_CHOICES").value()
+ self.use_testflag = self.choices.find_by_name_with_prefix("testflag")
+ self.linguas_en = self.choices.find_by_name_with_prefix("linguas_en")
+
+ def test_01_use_testflag(self):
+ self.assert_(self.use_testflag)
+ self.assertEquals(self.use_testflag.unprefixed_name, "testflag")
+ self.assertEquals(self.use_testflag.name_with_prefix, "testflag")
+ self.assertEquals(self.use_testflag.enabled, False)
+ self.assertEquals(self.use_testflag.enabled_by_default, False)
+ self.assertEquals(self.use_testflag.locked, False)
+ self.assertEquals(self.use_testflag.description, "the test flag")
+ self.assertEquals(self.use_testflag.explicitly_listed, True)
+
+ def test_02_linguas_en(self):
+ self.assert_(self.linguas_en)
+ self.assertEquals(self.linguas_en.unprefixed_name, "en")
+ self.assertEquals(self.linguas_en.name_with_prefix, "linguas_en")
+ self.assertEquals(self.linguas_en.enabled, False)
+ self.assertEquals(self.linguas_en.enabled_by_default, False)
+ self.assertEquals(self.linguas_en.locked, False)
+ self.assertEquals(self.linguas_en.description, "English")
+ self.assertEquals(self.linguas_en.explicitly_listed, True)
+
+if __name__ == "__main__":
+ unittest.main()
+
diff --git a/python/choices_TEST_cleanup.sh b/python/choices_TEST_cleanup.sh
new file mode 100755
index 0000000..25a20a6
--- /dev/null
+++ b/python/choices_TEST_cleanup.sh
@@ -0,0 +1,9 @@
+#!/usr/bin/env bash
+# vim: set ft=sh sw=4 sts=4 et :
+
+if [ -d choices_TEST_dir ] ; then
+ rm -fr choices_TEST_dir
+else
+ true
+fi
+
diff --git a/python/choices_TEST_setup.sh b/python/choices_TEST_setup.sh
new file mode 100755
index 0000000..9a8b5b8
--- /dev/null
+++ b/python/choices_TEST_setup.sh
@@ -0,0 +1,45 @@
+#!/usr/bin/env bash
+# vim: set ft=sh sw=4 sts=4 et :
+
+mkdir choices_TEST_dir || exit 1
+cd choices_TEST_dir || exit 1
+
+mkdir -p testrepo/{eclass,distfiles,profiles/{testprofile,desc},foo/bar/files} || exit 1
+cd testrepo || exit 1
+echo "testrepo" > profiles/repo_name || exit 1
+cat <<END > profiles/categories || exit 1
+foo
+END
+cat <<END > profiles/profiles.desc
+test testprofile stable
+END
+cat <<END > profiles/testprofile/make.defaults
+ARCH=test
+USERLAND=test
+KERNEL=test
+USE_EXPAND=LINGUAS
+END
+
+cat <<"END" > foo/bar/bar-1.0.ebuild || exit 1
+DESCRIPTION="Test package"
+HOMEPAGE="http://paludis.pioto.org/"
+SRC_URI="http://example.com/${P}.tar.bz2"
+SLOT="0"
+IUSE="testflag linguas_en"
+LICENSE="GPL-2"
+KEYWORDS="test"
+RESTRICT="monkey"
+DEPEND="foo/bar"
+RDEPEND=""
+END
+
+cat <<END > profiles/use.desc
+testflag - the test flag
+END
+
+cat <<END > profiles/desc/linguas.desc
+en - English
+END
+
+cd ..
+
diff --git a/python/metadata_key.cc b/python/metadata_key.cc
index 9a52452..6e440ed 100644
--- a/python/metadata_key.cc
+++ b/python/metadata_key.cc
@@ -305,6 +305,27 @@ struct MetadataContentsKeyWrapper :
}
};
+struct MetadataChoicesKeyWrapper :
+ MetadataValueKey<std::tr1::shared_ptr<const Choices> > ,
+ bp::wrapper<MetadataValueKey<std::tr1::shared_ptr<const Choices> > >
+{
+ MetadataChoicesKeyWrapper(const std::string & r, const std::string & h, const MetadataKeyType t) :
+ MetadataValueKey<std::tr1::shared_ptr<const Choices> > (r, h, t)
+ {
+ }
+
+ virtual const std::tr1::shared_ptr<const Choices> value() const
+ PALUDIS_ATTRIBUTE((warn_unused_result))
+ {
+ Lock l(get_mutex());
+
+ if (bp::override f = get_override("value"))
+ return f();
+ else
+ throw PythonMethodNotImplemented("MetadataChoicesKey", "value");
+ }
+};
+
struct MetadataFSEntryKeyWrapper :
MetadataValueKey<FSEntry> ,
bp::wrapper<MetadataValueKey<FSEntry> >
@@ -928,6 +949,48 @@ void expose_metadata_key()
;
/**
+ * MetadataChoicesKey
+ */
+ bp::register_ptr_to_python<std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const Choices> > > >();
+ bp::implicitly_convertible<std::tr1::shared_ptr<MetadataChoicesKeyWrapper>,
+ std::tr1::shared_ptr<MetadataKey> >();
+ bp::class_<MetadataChoicesKeyWrapper, std::tr1::shared_ptr<MetadataChoicesKeyWrapper>,
+ bp::bases<MetadataKey>, boost::noncopyable>
+ (
+ "MetadataChoicesKey",
+ "A MetadataChoicesKey is a MetadataKey that holds a Choices heirarchy.\n\n"
+
+ "This class can be subclassed in Python.",
+ bp::init<const std::string &, const std::string &, MetadataKeyType>(
+ "__init__(raw_name, human_name, MetadataKeyType)"
+ )
+ )
+ .def("value", bp::pure_virtual(&MetadataValueKey<std::tr1::shared_ptr<const Choices> > ::value),
+ "value() -> Choices\n"
+ "Fetch our value."
+ )
+
+ //Work around epydoc bug
+ .def("raw_name", bp::pure_virtual(&MetadataKey::raw_name),
+ "raw_name() -> string\n"
+ "Fetch our raw name."
+ )
+
+ //Work around epydoc bug
+ .def("human_name", bp::pure_virtual(&MetadataKey::human_name),
+ "human_name() -> string\n"
+ "Fetch our human name."
+ )
+
+ //Work around epydoc bug
+ .def("type", bp::pure_virtual(&MetadataKey::type),
+ "type() -> MetadataKeyType\n"
+ "Fetch our key type."
+ )
+ ;
+
+
+ /**
* MetadataRepositoryMaskInfoKey
*/
bp::register_ptr_to_python<std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const RepositoryMaskInfo> > > >();
diff --git a/python/metadata_key_TEST.py b/python/metadata_key_TEST.py
index c063563..c552b3b 100755
--- a/python/metadata_key_TEST.py
+++ b/python/metadata_key_TEST.py
@@ -61,6 +61,10 @@ class TestCase_01_MetadataKeys(unittest.TestCase):
self.assert_(isinstance(self.pid.find_metadata("DEPEND"), MetadataDependencySpecTreeKey))
self.assertEquals(self.ipid.find_metadata("DEPEND"), None)
+ def test_011_choices(self):
+ self.assert_(isinstance(self.pid.find_metadata("PALUDIS_CHOICES"), MetadataChoicesKey))
+ self.assert_(isinstance(self.ipid.find_metadata("PALUDIS_CHOICES"), MetadataChoicesKey))
+
class TestCase_02_MetadataKeys_suclassing(unittest.TestCase):
def test_01_package_id(self):
class TestKey(MetadataPackageIDKey):
@@ -277,6 +281,15 @@ class TestCase_02_MetadataKeys_suclassing(unittest.TestCase):
test_metadata_section_key(TestKey())
+ def test_17_choices(self):
+ class TestKey(MetadataChoicesKey):
+ def __init__(self):
+ MetadataChoicesKey.__init__(self, "raw", "human", MetadataKeyType.NORMAL)
+
+ def value(self):
+ return Choices()
+
+ test_metadata_choices_key(TestKey())
if __name__ == "__main__":
diff --git a/python/paludis_python.hh b/python/paludis_python.hh
index fc14a19..01b53d3 100644
--- a/python/paludis_python.hh
+++ b/python/paludis_python.hh
@@ -173,6 +173,7 @@ namespace paludis
void expose_about() PALUDIS_VISIBLE;
void expose_action() PALUDIS_VISIBLE;
+void expose_choices() PALUDIS_VISIBLE;
void expose_contents() PALUDIS_VISIBLE;
void expose_dep_label() PALUDIS_VISIBLE;
void expose_dep_list() PALUDIS_VISIBLE;
diff --git a/python/paludis_python_so.cc b/python/paludis_python_so.cc
index 0fb710b..6c87989 100644
--- a/python/paludis_python_so.cc
+++ b/python/paludis_python_so.cc
@@ -55,6 +55,7 @@ BOOST_PYTHON_MODULE(paludis)
expose_filtered_generator();
expose_generator();
expose_selection();
+ expose_choices();
#ifdef ENABLE_QA
expose_qa();
#endif