View Issue Details

IDProjectCategoryView StatusLast Update
0002632FreeCADFeaturepublic2021-02-06 06:32
Reportersliptonic Assigned To 
PrioritynormalSeverityfeatureReproducibilityalways
Status closedResolutionfixed 
Platformall 
Product Version0.17 
Target Version0.20Fixed in Version0.17 
Summary0002632: Improvements to Prefs for Python
DescriptionI'd like to see a couple improvements to the preference parameter access methods for python.

I'm planning to save collections of tool data into the preference system to make them available between FreeCAD documents. The configuration of this data can represent a significant effort on the part of the user.

First, I'd like to be able to force a write of the parameters to disk immediately instead of waiting for FreeCAD to exit normally. I know this is possible from the Parameters dialog but I don't think it's possible from python.

Second, I'd like to be able to iterate through the keys in a Group.
I think currently the group isn't iterable so I can't retrieve a value without knowing the key in advance.
Tagsdocumentation, python
FreeCAD Information

Activities

sliptonic

2016-10-22 20:33

manager   ~0007416

Looks like iterating the group is done by Yorik. http://forum.freecadweb.org/viewtopic.php?f=10&t=16669

Kunda1

2017-01-17 04:23

administrator   ~0007830

@sliptonic is the iterating the group hack documented somewhere ?

> I'd like to be able to force a write of the parameters to disk immediately instead of waiting for FreeCAD to exit normally. I know this is possible from the Parameters dialog but I don't think it's possible from python.

What remains to be done for this feature?

sliptonic

2017-01-17 17:49

manager   ~0007842

>@sliptonic is the iterating the group hack documented somewhere ?
I'm not sure. @yorik might have a better answer.

>What remains to be done for this feature?
prefs are still not persisted to disk until FreeCAD exits normally. AFAIK, there's no python function to force and immediate save.

Kunda1

2017-05-01 23:21

administrator   ~0008843

Ping @yorik

yorik

2017-05-02 23:39

administrator   ~0008875

The feature is documented in the source, and therefore in the python console and the API docs.

About implementing a function that saves the parameters, that should be possible, indeed the parameter editor does that.

@sliptonic a workaround that I used in Draft is to keep a copy of the preference value you want, somewhere in a place where it will be kept alive, and update the preference value whenever it changes.

Kunda1

2017-10-21 21:33

administrator   ~0010336

@yorik Please advise how to proceed with this ticket? Should it be revised? rewritten? split? closed?

Kunda1

2017-10-23 13:17

administrator   ~0010341

@sliptonic care to weigh in?

yorik

2017-10-23 17:50

administrator   ~0010343

I don't plan to do anything more here. We can leave this open in case someone wants to code some kind of "write preferences to disk now" python function. Personally I don't really feel it necessary though, given the method indicated above that I used in Draft...

sliptonic

2017-10-23 19:30

manager   ~0010345

I believe we're going to migrate away from storing tools in the user preferences and store them in an external file. This will eliminate my immediate need.

However, I still think this is a deficiency that should be addressed. Anyone writing in Python can set a value in prefs with prefs.SetString() but the assumption then is the value will eventually be persisted to disk on the next normal exit. If FC crashes, this is lost. If a python programmer is writing directly to the preference system, he should be able to force persistence immediately.

yorik

2017-10-23 21:43

administrator   ~0010346

For future reference, below is a patch to add a python function to save to disk. But this seems of little use to me... @wmayer might be able to say if there is a specific reason why preferences are saved at freecad exit only?

diff --git a/src/Gui/Application.h b/src/Gui/Application.h
index afb4c3c46..2431860e8 100644
--- a/src/Gui/Application.h
+++ b/src/Gui/Application.h
@@ -241,6 +241,7 @@ public:
 
     PYFUNCDEF_S(sShowDownloads);
     PYFUNCDEF_S(sShowPreferences);
+    PYFUNCDEF_S(sSavePreferences);^M
 
     PYFUNCDEF_S(sCreateViewer);
 
diff --git a/src/Gui/ApplicationPy.cpp b/src/Gui/ApplicationPy.cpp
index 0f5bccc01..e4f4969e3 100644
--- a/src/Gui/ApplicationPy.cpp
+++ b/src/Gui/ApplicationPy.cpp
@@ -171,6 +171,9 @@ PyMethodDef Application::Methods[] = {
   {"showPreferences",               (PyCFunction) Application::sShowPreferences,1,
    "showPreferences([string,int]) -> None\n\n"
    "Shows the preferences window. If string and int are provided, the given page index in the given group is shown."},
+  {"savePreferences",               (PyCFunction) Application::sSavePreferences,1,^M
+   "savePreferences() -> None\n\n"^M
+   "Saves the current preferences parameters to disk."},^M
    {"createViewer",               (PyCFunction) Application::sCreateViewer,1,
     "createViewer([int]) -> View3DInventor/SplitView3DInventor\n\n"
     "shows and returns a viewer. If the integer argument is given and > 1: -> splitViewer"},
@@ -1153,6 +1156,18 @@ PyObject* Application::sShowPreferences(PyObject * /*self*/, PyObject *args,PyOb
     return Py_None;
 }
 
+PyObject* Application::sSavePreferences(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)^M
+{^M
+    if (!PyArg_ParseTuple(args, ""))             // convert args: Python->C ^M
+        return NULL;                             // NULL triggers exception ^M
+    ParameterManager parmgrS = App::GetApplication().GetSystemParameter();^M
+        return NULL;                             // NULL triggers exception ^M
+    ParameterManager parmgrS = App::GetApplication().GetSystemParameter();^M
+    parmgrS.SaveDocument();^M
+    ParameterManager parmgrU = App::GetApplication().GetUserParameter();^M
+    parmgrU.SaveDocument();^M
+    Py_INCREF(Py_None);^M
+    return Py_None;^M
+}^M
+^M
 PyObject* Application::sCreateViewer(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
 {
     int num_of_views = 1;

wmayer

2017-10-25 10:40

administrator   ~0010351

https://github.com/FreeCAD/FreeCAD/commit/0ad9436eabbf0481cc2f6c94fc06409797ac553e

Related Changesets

FreeCAD: master 0ad9436e

2017-10-25 05:39:06

wmayer

Details Diff
fixes 0002632: Improvements to Prefs for Python Affected Issues
0002632
mod - src/App/Application.h Diff File
mod - src/App/ApplicationPy.cpp Diff File

Issue History

Date Modified Username Field Change
2016-07-22 16:24 sliptonic New Issue
2016-10-22 20:33 sliptonic Note Added: 0007416
2017-01-17 04:23 Kunda1 Note Added: 0007830
2017-01-17 17:49 sliptonic Note Added: 0007842
2017-05-01 23:20 Kunda1 Tag Attached: python
2017-05-01 23:21 Kunda1 Note Added: 0008843
2017-05-02 23:39 yorik Note Added: 0008875
2017-10-19 12:42 Kunda1 Tag Attached: documentation
2017-10-19 12:43 Kunda1 Tag Attached: split ticket
2017-10-19 12:50 Kunda1 Assigned To => Kunda1
2017-10-19 12:50 Kunda1 Status new => assigned
2017-10-21 21:33 Kunda1 Note Added: 0010336
2017-10-21 21:34 Kunda1 Status assigned => feedback
2017-10-22 19:30 Kunda1 Assigned To Kunda1 =>
2017-10-23 13:17 Kunda1 Note Added: 0010341
2017-10-23 17:50 yorik Note Added: 0010343
2017-10-23 19:30 sliptonic Note Added: 0010345
2017-10-23 19:30 sliptonic Status feedback => new
2017-10-23 21:43 yorik Note Added: 0010346
2017-10-25 10:40 wmayer Status new => closed
2017-10-25 10:40 wmayer Resolution open => fixed
2017-10-25 10:40 wmayer Fixed in Version => 0.17
2017-10-25 10:40 wmayer Note Added: 0010351
2017-10-25 11:49 Kunda1 Tag Detached: split ticket
2019-07-29 15:05 Kunda1 Changeset attached => FreeCAD master 0ad9436e
2021-02-06 06:32 abdullah Target Version => 0.20