aboutsummaryrefslogtreecommitdiffstats
path: root/misc/python
diff options
context:
space:
mode:
authorlloyd <[email protected]>2006-08-23 05:17:46 +0000
committerlloyd <[email protected]>2006-08-23 05:17:46 +0000
commit23a3fface9cb26c1a4bc0dfa50ef8c617c982df0 (patch)
treeb4c8445b9f2717efc0f5097c01daee7e136e1874 /misc/python
parent82ce25b181af3dfcc298f12d4b7328bd049089de (diff)
Finally get basic filters objects to work properly
Diffstat (limited to 'misc/python')
-rw-r--r--misc/python/botan/__init__.py7
-rw-r--r--misc/python/src/filter.cpp69
-rwxr-xr-xmisc/python/test.py8
3 files changed, 41 insertions, 43 deletions
diff --git a/misc/python/botan/__init__.py b/misc/python/botan/__init__.py
index e6bd5266a..781426921 100644
--- a/misc/python/botan/__init__.py
+++ b/misc/python/botan/__init__.py
@@ -5,5 +5,12 @@ init = LibraryInitializer()
def Filter(name):
return make_filter(name)
+def Pipe(*filters):
+ pipe = PipeObj();
+ for filter in filters:
+ if filter:
+ pipe.append(filter)
+ return pipe
+
#def Filter(name, key):
# return make_filter(name, key)
diff --git a/misc/python/src/filter.cpp b/misc/python/src/filter.cpp
index 099c1d610..b0f141e58 100644
--- a/misc/python/src/filter.cpp
+++ b/misc/python/src/filter.cpp
@@ -10,47 +10,21 @@ using namespace boost::python;
#include <botan/lookup.h>
using namespace Botan;
-class Python_Filter : public Fanout_Filter, public wrapper<Fanout_Filter>
+Filter* make_filter1(const std::string& name)
{
- public:
- virtual void write(const byte in[], u32bit len)
- {
- this->get_override("write")(in, len);
- }
- virtual void start_msg()
- {
- if(override start_msg = this->get_override("start_msg"))
- start_msg();
- Filter::start_msg();
- }
- virtual void end_msg()
- {
- if(override end_msg = this->get_override("end_msg"))
- end_msg();
- Filter::end_msg();
- }
-
- Python_Filter(Filter* f)
- {
- if(f) { attach(f); incr_owns(); }
- }
- };
-
-Python_Filter* make_filter1(const std::string& name)
- {
- printf("Trying to make filter of type %s\n", name.c_str());
-
if(have_hash(name))
- return new Python_Filter(new Hash_Filter(name));
- if(name == "Hex_Encoder")
- return new Python_Filter(new Hex_Encoder);
+ return new Hash_Filter(name);
+ else if(name == "Hex_Encoder")
+ return new Hex_Encoder;
+ else if(name == "Hex_Decoder")
+ return new Hex_Decoder;
- return 0;
+ throw Invalid_Argument("Filter " + name + " not found");
}
// FIXME: add new wrapper for Keyed_Filter here
-Python_Filter* make_filter2(const std::string& name,
- const SymmetricKey& key)
+Filter* make_filter2(const std::string& name,
+ const SymmetricKey& key)
{
printf("Trying to make a filter of type %s (key %s)\n", name.c_str(),
key.as_string().c_str());
@@ -59,7 +33,8 @@ Python_Filter* make_filter2(const std::string& name,
void export_filters()
{
- class_<Python_Filter, boost::noncopyable>("Filter", no_init)
+ class_<Filter, std::auto_ptr<Filter>, boost::noncopyable>
+ ("Filter", no_init)
.def("write", &Filter::write)
.def("start_msg", &Filter::start_msg)
.def("end_msg", &Filter::end_msg);
@@ -70,18 +45,34 @@ void export_filters()
return_value_policy<manage_new_object>());
}
+void append_filter(Pipe& pipe, std::auto_ptr<Filter> filter)
+ {
+ pipe.append(filter.get());
+ filter.release();
+ }
+
+void prepend_filter(Pipe& pipe, std::auto_ptr<Filter> filter)
+ {
+ pipe.prepend(filter.get());
+ filter.release();
+ }
+
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(rallas_ovls, read_all_as_string, 0, 1)
void export_pipe()
{
void (Pipe::*pipe_write_str)(const std::string&) = &Pipe::write;
- class_<Pipe, boost::noncopyable>("Pipe")
- .def(init<optional<Python_Filter*, Python_Filter*, Python_Filter*> >())
+ class_<Pipe, boost::noncopyable>("PipeObj")
+ .def(init<>())
.def_readonly("LAST_MESSAGE", &Pipe::LAST_MESSAGE)
.def_readonly("DEFAULT_MESSAGE", &Pipe::DEFAULT_MESSAGE)
.add_property("default_msg", &Pipe::default_msg, &Pipe::set_default_msg)
- .def("msg_count", &Pipe::message_count)
+ .add_property("msg_count", &Pipe::message_count)
+ .def("append", append_filter)
+ .def("prepend", prepend_filter)
+ .def("reset", &Pipe::reset)
+ .def("pop", &Pipe::pop)
.def("end_of_data", &Pipe::end_of_data)
.def("start_msg", &Pipe::start_msg)
.def("end_msg", &Pipe::end_msg)
diff --git a/misc/python/test.py b/misc/python/test.py
index c73cc9369..aa30ab5d6 100755
--- a/misc/python/test.py
+++ b/misc/python/test.py
@@ -3,17 +3,17 @@
import sys, botan
def do_hash(input):
- pipe = botan.Pipe(botan.Filter("MD5"), botan.Filter("Hex_Encoder"))
+ pipe = botan.Pipe(botan.Filter("Hex_Encoder"),
+ botan.Filter("Hex_Decoder"))
- print pipe
pipe.start_msg()
pipe.write(input)
pipe.end_msg()
- return pipe.read_all()
+ return pipe.read_all_as_string()
def main():
- print do_hash("foo")
+ print do_hash("hi chappy")
if __name__ == "__main__":
sys.exit(main())