Skip to content

Commit

Permalink
Python Embedding is optional and check install
Browse files Browse the repository at this point in the history
.. make python embedding optional, if it's not installed then
   disable in preferences (new checkbox similar to R).
  • Loading branch information
liversedge committed Feb 5, 2018
1 parent 8e5e95b commit 954570f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Core/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
#define GC_HOMEDIR "<system>homedirectory"
#define GC_START_HTTP "<system>starthttp"
#define GC_EMBED_R "<system>embedR"
#define GC_EMBED_PYTHON "<system>embedPython"

#define GC_SETTINGS_LAST "<system>mainwindow/lastOpened"
#define GC_SETTINGS_MAIN_GEOM "<system>mainwindow/geometry"
Expand Down
6 changes: 5 additions & 1 deletion src/Core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,11 @@ main(int argc, char *argv[])
#endif

#ifdef GC_WANT_PYTHON
if (noPy == false && python == NULL) python = new PythonEmbed(); // initialise python in this thread ?
bool embed = appsettings->value(NULL, GC_EMBED_PYTHON, true).toBool();
if (embed && noPy == false && python == NULL) {
python = new PythonEmbed(); // initialise python in this thread ?
if (python->loaded == false) python=NULL;
}
#endif

//this is the path within the current directory where GC will look for
Expand Down
10 changes: 10 additions & 0 deletions src/Gui/Pages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ GeneralPage::GeneralPage(Context *context) : context(context)
connect(embedR, SIGNAL(stateChanged(int)), this, SLOT(embedRchanged(int)));
#endif

#ifdef GC_WANT_PYTHON
embedPython = new QCheckBox(tr("Enable Python"), this);
embedPython->setChecked(appsettings->value(NULL, GC_EMBED_PYTHON, true).toBool());
configLayout->addWidget(embedPython, 7+offset,1, Qt::AlignLeft);
offset += 1;
#endif

//
// Athlete directory (home of athletes)
//
Expand Down Expand Up @@ -305,6 +312,9 @@ GeneralPage::saveClicked()
#ifdef GC_WANT_R
appsettings->setValue(GC_EMBED_R, embedR->isChecked());
#endif
#ifdef GC_WANT_PYTHON
appsettings->setValue(GC_EMBED_PYTHON, embedPython->isChecked());
#endif

qint32 state=0;

Expand Down
3 changes: 3 additions & 0 deletions src/Gui/Pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ class GeneralPage : public QWidget
#endif
#ifdef GC_WANT_R
QCheckBox *embedR;
#endif
#ifdef GC_WANT_PYTHON
QCheckBox *embedPython;
#endif
QLineEdit *garminHWMarkedit;
QLineEdit *hystedit;
Expand Down
13 changes: 13 additions & 0 deletions src/Python/PythonEmbed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ PythonEmbed::PythonEmbed(const bool verbose, const bool interactive) : verbose(v

// set the module path in the same way the interpreter would
PyObject *sys = PyImport_ImportModule("sys");

// did module import fail (python not installed)
if (sys == NULL) {

// abort embedding
QMessageBox msg(QMessageBox::Information, QObject::tr("Python not installed"),
QObject::tr("Python v3.6 or higher is required for Python.\nPython disabled in preferences."));
appsettings->setValue(GC_EMBED_PYTHON, false);
loaded=false;
msg.exec();
return;
}

PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyUnicode_FromString("."));

Expand Down

0 comments on commit 954570f

Please sign in to comment.