summaryrefslogtreecommitdiff
path: root/src/pyinterp.h
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2012-03-02 01:36:58 -0600
committerJohn Wiegley <johnw@newartisans.com>2012-03-02 01:36:58 -0600
commitddba59b703fcfeb546627ee7e44a25fab49c0c12 (patch)
treecd7e6467fc1a9c926995a043f5aee7eafecc45d0 /src/pyinterp.h
parent116cbd050b5f731b711529a053f73bb1ec275620 (diff)
downloadfork-ledger-ddba59b703fcfeb546627ee7e44a25fab49c0c12.tar.gz
fork-ledger-ddba59b703fcfeb546627ee7e44a25fab49c0c12.tar.bz2
fork-ledger-ddba59b703fcfeb546627ee7e44a25fab49c0c12.zip
This now works: ledger --import os eval 'os.path.isdir("/tmp")'
Diffstat (limited to 'src/pyinterp.h')
-rw-r--r--src/pyinterp.h58
1 files changed, 42 insertions, 16 deletions
diff --git a/src/pyinterp.h b/src/pyinterp.h
index c3397840..8699f69d 100644
--- a/src/pyinterp.h
+++ b/src/pyinterp.h
@@ -38,21 +38,52 @@
namespace ledger {
+class python_module_t : public scope_t, public noncopyable
+{
+public:
+ string module_name;
+ python::object module_object;
+ python::dict module_globals;
+
+ explicit python_module_t(const string& name);
+ explicit python_module_t(const string& name, python::object obj);
+
+ void import_module(const string& name, bool import_direct = false);
+
+ virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
+ const string& name);
+
+ void define_global(const string& name, python::object obj) {
+ module_globals[name] = obj;
+ }
+
+ virtual string description() {
+ return module_name;
+ }
+};
+
+typedef std::map<PyObject *, shared_ptr<python_module_t> > python_module_map_t;
+
class python_interpreter_t : public session_t
{
public:
- python::object main_module;
- python::dict main_nspace;
- bool is_initialized;
+ bool is_initialized;
- python_interpreter_t()
- : session_t(), main_nspace(), is_initialized(false) {
+ shared_ptr<python_module_t> main_module;
+ python_module_map_t modules_map;
+
+ shared_ptr<python_module_t> import_module(const string& name) {
+ shared_ptr<python_module_t> mod(new python_module_t(name));
+ if (name != "__main__")
+ main_module->define_global(name, mod->module_object);
+ return mod;
+ }
+
+ python_interpreter_t() : session_t(), is_initialized(false) {
TRACE_CTOR(python_interpreter_t, "");
}
-
virtual ~python_interpreter_t() {
TRACE_DTOR(python_interpreter_t);
-
if (is_initialized)
Py_Finalize();
}
@@ -60,7 +91,6 @@ public:
void initialize();
void hack_system_paths();
- python::object import_into_main(const string& name);
python::object import_option(const string& name);
enum py_eval_mode_t {
@@ -69,14 +99,10 @@ public:
PY_EVAL_MULTI
};
- python::object eval(std::istream& in,
- py_eval_mode_t mode = PY_EVAL_EXPR);
- python::object eval(const string& str,
- py_eval_mode_t mode = PY_EVAL_EXPR);
- python::object eval(const char * c_str,
- py_eval_mode_t mode = PY_EVAL_EXPR) {
- string str(c_str);
- return eval(str, mode);
+ python::object eval(std::istream& in, py_eval_mode_t mode = PY_EVAL_EXPR);
+ python::object eval(const string& str, py_eval_mode_t mode = PY_EVAL_EXPR);
+ python::object eval(const char * c_str, py_eval_mode_t mode = PY_EVAL_EXPR) {
+ return eval(string(c_str), mode);
}
value_t python_command(call_scope_t& scope);