Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Commit

Permalink
cleanup: add python namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
zalox committed Apr 9, 2016
1 parent 4746eff commit 94c68fd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/juci.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void Application::on_startup() {
set_app_menu(Menu::get().juci_menu);
set_menubar(Menu::get().window_menu);
}
PythonInterpreter::get();
Python::Interpreter::get();
}

Application::Application() : Gtk::Application("no.sout.juci", Gio::APPLICATION_NON_UNIQUE | Gio::APPLICATION_HANDLES_COMMAND_LINE) {
Expand Down
23 changes: 13 additions & 10 deletions src/python_interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ inline pybind11::module pyobject_from_gobj(gpointer ptr){
return pybind11::module(Py_None, false);
}

PythonInterpreter::PythonInterpreter(){
Python::Interpreter::Interpreter(){
auto init_juci_api=[](){
pybind11::module(pygobject_init(-1,-1,-1),false);
pybind11::module api("jucpp","Python bindings for juCi++");
Expand Down Expand Up @@ -52,26 +52,29 @@ PythonInterpreter::PythonInterpreter(){
add_path(Config::get().python.site_packages);
add_path(plugin_path);
Py_Initialize();
long unsigned size = 0L;
argv=Py_DecodeLocale("",&size);
PySys_SetArgv(0,&argv);
boost::filesystem::directory_iterator end_it;
for(boost::filesystem::directory_iterator it(plugin_path);it!=end_it;it++){
auto module_name=it->path().stem().string();
if(module_name!="__pycache__"){
auto module=import(module_name);
if(!module)
std::cerr << std::string(PythonError()) << std::endl;
std::cerr << std::string(Error()) << std::endl;
}
}
}

pybind11::module PythonInterpreter::get_loaded_module(const std::string &module_name){
pybind11::module Python::Interpreter::get_loaded_module(const std::string &module_name){
return pybind11::module(PyImport_AddModule(module_name.c_str()), true);
}

pybind11::module PythonInterpreter::import(const std::string &module_name){
pybind11::module Python::Interpreter::import(const std::string &module_name){
return pybind11::module(PyImport_ImportModule(module_name.c_str()), false);
}

void PythonInterpreter::add_path(const boost::filesystem::path &path){
void Python::Interpreter::add_path(const boost::filesystem::path &path){
std::wstring sys_path(Py_GetPath());
if(!sys_path.empty())
#ifdef _WIN32
Expand All @@ -83,15 +86,15 @@ void PythonInterpreter::add_path(const boost::filesystem::path &path){
Py_SetPath(sys_path.c_str());
}

PythonInterpreter::~PythonInterpreter(){
auto err=PythonError();
Python::Interpreter::~Interpreter(){
auto err=Error();
if(Py_IsInitialized())
Py_Finalize();
if(err)
std::cerr << std::string(err) << std::endl;
}

PythonError::PythonError(){
Python::Error::Error(){
pybind11::object error(PyErr_Occurred(), false);
if(error){
PyObject *exception,*value,*traceback;
Expand All @@ -107,10 +110,10 @@ PythonError::PythonError(){
}
}

PythonError::operator std::string(){
Python::Error::operator std::string(){
return exp + "\n" + val + "\n" + trace;
}

PythonError::operator bool(){
Python::Error::operator bool(){
return !exp.empty();
}
46 changes: 23 additions & 23 deletions src/python_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@

#include <iostream>
using namespace std;
namespace Python {
class Interpreter {
private:
Interpreter();
~Interpreter();
wchar_t *argv;
public:
static Interpreter& get(){
static Interpreter singleton;
return singleton;
}
pybind11::module get_loaded_module(const std::string &module_name);
pybind11::module import(const std::string &module_name);
void add_path(const boost::filesystem::path &path);
};

class PythonInterpreter {
private:
PythonInterpreter();
~PythonInterpreter();
wchar_t *argv;
public:
static PythonInterpreter& get(){
static PythonInterpreter singleton;
return singleton;
}
pybind11::module get_loaded_module(const std::string &module_name);
pybind11::module import(const std::string &module_name);
void add_path(const boost::filesystem::path &path);
};

class PythonError {
public:
PythonError();
operator std::string();
operator bool();
std::string exp, val, trace;
};

class Error {
public:
Error();
operator std::string();
operator bool();
std::string exp, val, trace;
};
} // namespace Python
#endif // JUCI_PYTHON_INTERPRETER_H_
8 changes: 4 additions & 4 deletions src/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,15 @@ void Window::set_menu_actions() {
}
if(file_path>Config::get().python.plugin_directory){
auto stem=file_path.stem().string();
auto module=PythonInterpreter::get().get_loaded_module(stem);
auto module=Python::Interpreter::get().get_loaded_module(stem);
if(module){
auto module_new=pybind11::module(PyImport_ReloadModule(module.ptr()),false);
if(module_new)
Terminal::get().print("Python module "+stem + " has been reloaded \n");
else PythonError();
else Python::Error();
}else{
PythonError();
module=PythonInterpreter::get().import(stem);
Python::Error();
module=Python::Interpreter::get().import(stem);
if(module)
Terminal::get().print("Python module "+stem + " has been reloaded \n");
}
Expand Down

0 comments on commit 94c68fd

Please sign in to comment.