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

Commit 3513d4f

Browse files
committed
cleanup: add python namespace
1 parent 4746eff commit 3513d4f

File tree

4 files changed

+38
-38
lines changed

4 files changed

+38
-38
lines changed

src/juci.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void Application::on_startup() {
113113
set_app_menu(Menu::get().juci_menu);
114114
set_menubar(Menu::get().window_menu);
115115
}
116-
PythonInterpreter::get();
116+
Python::Interpreter::get();
117117
}
118118

119119
Application::Application() : Gtk::Application("no.sout.juci", Gio::APPLICATION_NON_UNIQUE | Gio::APPLICATION_HANDLES_COMMAND_LINE) {

src/python_interpreter.cc

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ inline pybind11::module pyobject_from_gobj(gpointer ptr){
1313
return pybind11::module(Py_None, false);
1414
}
1515

16-
PythonInterpreter::PythonInterpreter(){
16+
Python::Interpreter::Interpreter(){
1717
auto init_juci_api=[](){
1818
pybind11::module(pygobject_init(-1,-1,-1),false);
1919
pybind11::module api("jucpp","Python bindings for juCi++");
@@ -58,20 +58,20 @@ PythonInterpreter::PythonInterpreter(){
5858
if(module_name!="__pycache__"){
5959
auto module=import(module_name);
6060
if(!module)
61-
std::cerr << std::string(PythonError()) << std::endl;
61+
std::cerr << std::string(Error()) << std::endl;
6262
}
6363
}
6464
}
6565

66-
pybind11::module PythonInterpreter::get_loaded_module(const std::string &module_name){
66+
pybind11::module Python::Interpreter::get_loaded_module(const std::string &module_name){
6767
return pybind11::module(PyImport_AddModule(module_name.c_str()), true);
6868
}
6969

70-
pybind11::module PythonInterpreter::import(const std::string &module_name){
70+
pybind11::module Python::Interpreter::import(const std::string &module_name){
7171
return pybind11::module(PyImport_ImportModule(module_name.c_str()), false);
7272
}
7373

74-
void PythonInterpreter::add_path(const boost::filesystem::path &path){
74+
void Python::Interpreter::add_path(const boost::filesystem::path &path){
7575
std::wstring sys_path(Py_GetPath());
7676
if(!sys_path.empty())
7777
#ifdef _WIN32
@@ -83,15 +83,15 @@ void PythonInterpreter::add_path(const boost::filesystem::path &path){
8383
Py_SetPath(sys_path.c_str());
8484
}
8585

86-
PythonInterpreter::~PythonInterpreter(){
87-
auto err=PythonError();
86+
Python::Interpreter::~Interpreter(){
87+
auto err=Error();
8888
if(Py_IsInitialized())
8989
Py_Finalize();
9090
if(err)
9191
std::cerr << std::string(err) << std::endl;
9292
}
9393

94-
PythonError::PythonError(){
94+
Python::Error::Error(){
9595
pybind11::object error(PyErr_Occurred(), false);
9696
if(error){
9797
PyObject *exception,*value,*traceback;
@@ -107,10 +107,10 @@ PythonError::PythonError(){
107107
}
108108
}
109109

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

114-
PythonError::operator bool(){
114+
Python::Error::operator bool(){
115115
return !exp.empty();
116116
}

src/python_interpreter.h

+23-23
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@
66

77
#include <iostream>
88
using namespace std;
9+
namespace Python {
10+
class Interpreter {
11+
private:
12+
Interpreter();
13+
~Interpreter();
14+
wchar_t *argv;
15+
public:
16+
static Interpreter& get(){
17+
static Interpreter singleton;
18+
return singleton;
19+
}
20+
pybind11::module get_loaded_module(const std::string &module_name);
21+
pybind11::module import(const std::string &module_name);
22+
void add_path(const boost::filesystem::path &path);
23+
};
924

10-
class PythonInterpreter {
11-
private:
12-
PythonInterpreter();
13-
~PythonInterpreter();
14-
wchar_t *argv;
15-
public:
16-
static PythonInterpreter& get(){
17-
static PythonInterpreter singleton;
18-
return singleton;
19-
}
20-
pybind11::module get_loaded_module(const std::string &module_name);
21-
pybind11::module import(const std::string &module_name);
22-
void add_path(const boost::filesystem::path &path);
23-
};
24-
25-
class PythonError {
26-
public:
27-
PythonError();
28-
operator std::string();
29-
operator bool();
30-
std::string exp, val, trace;
31-
};
32-
25+
class Error {
26+
public:
27+
Error();
28+
operator std::string();
29+
operator bool();
30+
std::string exp, val, trace;
31+
};
32+
} // namespace Python
3333
#endif // JUCI_PYTHON_INTERPRETER_H_

src/window.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,15 @@ void Window::set_menu_actions() {
254254
}
255255
if(file_path>Config::get().python.plugin_directory){
256256
auto stem=file_path.stem().string();
257-
auto module=PythonInterpreter::get().get_loaded_module(stem);
257+
auto module=Python::Interpreter::get().get_loaded_module(stem);
258258
if(module){
259259
auto module_new=pybind11::module(PyImport_ReloadModule(module.ptr()),false);
260260
if(module_new)
261261
Terminal::get().print("Python module "+stem + " has been reloaded \n");
262-
else PythonError();
262+
else Python::Error();
263263
}else{
264-
PythonError();
265-
module=PythonInterpreter::get().import(stem);
264+
Python::Error();
265+
module=Python::Interpreter::get().import(stem);
266266
if(module)
267267
Terminal::get().print("Python module "+stem + " has been reloaded \n");
268268
}

0 commit comments

Comments
 (0)