extend FreeCAD.getUserMacroDir to accept boolean to either return actual or default macro directory
This commit is contained in:
@@ -86,7 +86,10 @@ PyMethodDef Application::Methods[] = {
|
||||
{"getUserAppDataDir", (PyCFunction) Application::sGetUserAppDataDir, METH_VARARGS,
|
||||
"Get the root directory of user settings"},
|
||||
{"getUserMacroDir", (PyCFunction) Application::sGetUserMacroDir, METH_VARARGS,
|
||||
"Get the directory of the user's macro directory"},
|
||||
"getUserMacroDir(bool=False) -> string"
|
||||
"Get the directory of the user's macro directory\n"
|
||||
"If parameter is False (the default) it returns the standard path in the"
|
||||
"user's home directory, otherwise it returns the user-defined path."},
|
||||
{"getHelpDir", (PyCFunction) Application::sGetHelpDir, METH_VARARGS,
|
||||
"Get the directory of the documentation"},
|
||||
{"getHomePath", (PyCFunction) Application::sGetHomePath, METH_VARARGS,
|
||||
@@ -588,10 +591,18 @@ PyObject* Application::sGetUserAppDataDir(PyObject * /*self*/, PyObject *args)
|
||||
|
||||
PyObject* Application::sGetUserMacroDir(PyObject * /*self*/, PyObject *args)
|
||||
{
|
||||
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
|
||||
return NULL; // NULL triggers exception
|
||||
PyObject *actual = Py_False;
|
||||
if (!PyArg_ParseTuple(args, "|O!", &PyBool_Type, &actual))
|
||||
return NULL;
|
||||
|
||||
Py::String user_macro_dir(Application::getUserMacroDir(),"utf-8");
|
||||
std::string macroDir = Application::getUserMacroDir();
|
||||
if (PyObject_IsTrue(actual)) {
|
||||
macroDir = App::GetApplication().
|
||||
GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")
|
||||
->GetASCII("MacroPath",macroDir.c_str());
|
||||
}
|
||||
|
||||
Py::String user_macro_dir(macroDir,"utf-8");
|
||||
return Py::new_reference_to(user_macro_dir);
|
||||
}
|
||||
|
||||
|
||||
@@ -117,7 +117,7 @@ def install_macro(macro, macro_repo_dir):
|
||||
"""
|
||||
if not macro.code:
|
||||
return False
|
||||
macro_dir = FreeCAD.getUserMacroDir()
|
||||
macro_dir = FreeCAD.getUserMacroDir(True)
|
||||
if not os.path.isdir(macro_dir):
|
||||
try:
|
||||
os.makedirs(macro_dir)
|
||||
@@ -165,7 +165,7 @@ def remove_macro(macro):
|
||||
if not macro.is_installed():
|
||||
# Macro not installed, nothing to do.
|
||||
return True
|
||||
macro_dir = FreeCAD.getUserMacroDir()
|
||||
macro_dir = FreeCAD.getUserMacroDir(True)
|
||||
macro_path = os.path.join(macro_dir, macro.filename)
|
||||
macro_path_with_macro_prefix = os.path.join(macro_dir, 'Macro_' + macro.filename)
|
||||
if os.path.exists(macro_path):
|
||||
@@ -184,9 +184,9 @@ def remove_macro(macro):
|
||||
def remove_directory_if_empty(dir):
|
||||
"""Remove the directory if it is empty
|
||||
|
||||
Directory FreeCAD.getUserMacroDir() will not be removed even if empty.
|
||||
Directory FreeCAD.getUserMacroDir(True) will not be removed even if empty.
|
||||
"""
|
||||
if dir == FreeCAD.getUserMacroDir():
|
||||
if dir == FreeCAD.getUserMacroDir(True):
|
||||
return
|
||||
if not os.listdir(dir):
|
||||
os.rmdir(dir)
|
||||
@@ -456,7 +456,7 @@ class AddonsInstaller(QtGui.QDialog):
|
||||
if not macro.is_installed():
|
||||
# Macro not installed, nothing to do.
|
||||
return
|
||||
macro_path = os.path.join(FreeCAD.getUserMacroDir(), macro.filename)
|
||||
macro_path = os.path.join(FreeCAD.getUserMacroDir(True), macro.filename)
|
||||
if os.path.exists(macro_path):
|
||||
macro_path = macro_path.replace("\\","/")
|
||||
|
||||
@@ -915,7 +915,7 @@ class InstallWorker(QtCore.QThread):
|
||||
self.download(self.repos[idx][1],clonedir)
|
||||
answer = translate("AddonsInstaller", "Workbench successfully installed. Please restart FreeCAD to apply the changes.")
|
||||
# symlink any macro contained in the module to the macros folder
|
||||
macro_dir = FreeCAD.getUserMacroDir()
|
||||
macro_dir = FreeCAD.getUserMacroDir(True)
|
||||
if not os.path.exists(macro_dir):
|
||||
os.makedirs(macro_dir)
|
||||
for f in os.listdir(clonedir):
|
||||
|
||||
@@ -56,8 +56,8 @@ class Macro(object):
|
||||
def is_installed(self):
|
||||
if self.on_git and not self.src_filename:
|
||||
return False
|
||||
return (os.path.exists(os.path.join(FreeCAD.getUserMacroDir(), self.filename))
|
||||
or os.path.exists(os.path.join(FreeCAD.getUserMacroDir(), 'Macro_' + self.filename)))
|
||||
return (os.path.exists(os.path.join(FreeCAD.getUserMacroDir(True), self.filename))
|
||||
or os.path.exists(os.path.join(FreeCAD.getUserMacroDir(True), 'Macro_' + self.filename)))
|
||||
|
||||
def fill_details_from_file(self, filename):
|
||||
with open(filename) as f:
|
||||
|
||||
Reference in New Issue
Block a user