Skip to content

Commit

Permalink
Merge branch 'importer'
Browse files Browse the repository at this point in the history
  • Loading branch information
Aranuvir committed May 20, 2018
2 parents deea134 + 7d489d3 commit 397db61
Showing 1 changed file with 103 additions and 89 deletions.
192 changes: 103 additions & 89 deletions makehuman/plugins/5_settings_userplugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@
import gui3d
import gui
import os
import glob
import zipfile
import log
import getpath
import shutil

class UserPluginCheckBox(gui.CheckBox):

def __init__(self, module):
def __init__(self, module, path=''):
super(UserPluginCheckBox, self).__init__(module, module in gui3d.app.getSetting('activeUserPlugins'))
self.module = module
self.path = path

def onClicked(self, event):
if self.selected:
Expand All @@ -69,54 +69,56 @@ def __init__(self, category):

info_msg = "Install new plugins by either copying to the user plugins folder or using the built in installer.\n"\
"The installer only handles Python script files and plugin packages in plain zip file format."\
"\n\nTo (de-)activate a plugin it must be (un-)checked in the list. Changes only come into effect" \
" after MakeHuman is restarted."
"\n\nTo activate a plugin it must be checked in the list. Then click the \"Activate\"-Button or " \
"restart MakeHuman.\nTo deactivate a plugin uncheck it in the list and restart MakeHuman."

gui3d.TaskView.__init__(self, category, 'User Plugins')

userPlugins = self.getUserPlugins()
self.userPlugins = getUserPlugins()
activePlugins = gui3d.app.getSetting('activeUserPlugins')

for plugin in activePlugins:
if plugin not in userPlugins:
if plugin not in self.userPlugins:
activePlugins.remove(plugin)

gui3d.app.setSetting('activeUserPlugins', activePlugins)
gui3d.app.saveSettings()

self.home = os.path.expanduser('~')

self.scroll = self.addTopWidget(gui.VScrollArea())
scroll = self.addTopWidget(gui.VScrollArea())
self.userPluginBox = gui.GroupBox('User Plugins')
self.userPluginBox.setSizePolicy(gui.SizePolicy.MinimumExpanding, gui.SizePolicy.Preferred)
self.scroll.setWidget(self.userPluginBox)
scroll.setWidget(self.userPluginBox)

for i, plugin in enumerate(userPlugins):
self.userPluginBox.addWidget(UserPluginCheckBox(plugin), row=i, alignment=gui.QtCore.Qt.AlignTop)
for i, plugin in enumerate(sorted(self.userPlugins.keys())):
self.userPluginBox.addWidget(UserPluginCheckBox(plugin, self.userPlugins.get(plugin)), row=i, alignment=gui.QtCore.Qt.AlignTop)

self.installWidget = gui.QtWidgets.QWidget()
installWidget = gui.QtWidgets.QWidget()
installWidgetLayout = gui.QtWidgets.QVBoxLayout()
self.installWidget.setLayout(installWidgetLayout)
self.addLeftWidget(self.installWidget)
installWidget.setLayout(installWidgetLayout)
self.addLeftWidget(installWidget)

self.installBox = gui.GroupBox('')
installBox = gui.GroupBox('')
self.installPyButton = gui.Button('Install Plugin File')
self.installBox.addWidget(self.installPyButton)
installBox.addWidget(self.installPyButton)
self.installZipButton = gui.Button('Install Zipped Plugin')
self.installBox.addWidget(self.installZipButton)
installWidgetLayout.addWidget(self.installBox)
installBox.addWidget(self.installZipButton)
installWidgetLayout.addWidget(installBox)

self.reloadBox = gui.GroupBox('')
actionsBox = gui.GroupBox('')
self.reloadButton = gui.Button('Reload Plugins Folder')
self.reloadBox.addWidget(self.reloadButton)
installWidgetLayout.addWidget(self.reloadBox)

self.infoBox = gui.GroupBox('Info')
self.infoText = gui.TextView(info_msg)
self.infoText.setWordWrap(True)
self.infoText.setSizePolicy(gui.SizePolicy.Ignored, gui.SizePolicy.MinimumExpanding)
self.infoBox.addWidget(self.infoText)
installWidgetLayout.addWidget(self.infoBox)
actionsBox.addWidget(self.reloadButton)
self.activateButton = gui.Button('Activate Plugins')
actionsBox.addWidget(self.activateButton)
installWidgetLayout.addWidget(actionsBox)

infoBox = gui.GroupBox('Info')
infoText = gui.TextView(info_msg)
infoText.setWordWrap(True)
infoText.setSizePolicy(gui.SizePolicy.Ignored, gui.SizePolicy.MinimumExpanding)
infoBox.addWidget(infoText)
installWidgetLayout.addWidget(infoBox)
installWidgetLayout.addStretch(1)

@self.installZipButton.mhEvent
Expand All @@ -127,22 +129,19 @@ def onClicked(event):

dest_path = getpath.getPath('plugins')
if os.path.isfile(filename):
result = self.decompress(filename, dest_path)
if result == 1:
gui3d.app.prompt('Error', 'Not a zip file {0:s}'.format(filename), 'OK')
result = decompress(filename, dest_path)
if result == 0:
gui3d.app.prompt('Info', 'The plugin copied successfully. To activate, check '
'the plugin in the list and press the "Activate"-Button or restart MakeHuman.',
'OK', helpId='installPluginHelp')
elif result == 3:
gui3d.app.prompt('Warning', 'Potentially dangerous zip file, containing files with unsuitable path. '
'Inspect/fix the zip file before usage!', 'OK')
elif result == -1:
elif result == 4:
gui3d.app.prompt('Error', 'Zip file {0:s} contains exiting files.'.format(filename), 'OK')
elif result == 0:
gui3d.app.prompt('Info', 'The plugin copied successfully. To activate check '
'the plugin in the list and restart MakeHuman.', 'OK', helpId='installPluginHelp' )
for child in self.userPluginBox.children:
self.userPluginBox.removeWidget(child)
updatePlugins = self.getUserPlugins()
for i, plugin in enumerate(updatePlugins):
self.userPluginBox.addWidget(UserPluginCheckBox(plugin), row = i, alignment=gui.QtCore.Qt.AlignTop)
elif result == 1:
gui3d.app.prompt('Error', 'Not a zip file {0:s}'.format(filename), 'OK')
self.updatePluginList()
self.home = os.path.dirname(filename)

@self.installPyButton.mhEvent
Expand All @@ -154,61 +153,76 @@ def onClicked(event):
shutil.copy2(filename, getpath.getPath('plugins'))
except OSError as e:
gui3d.app.prompt('Error', 'Failed to copy {0:s} to user plugins folder'.format(filename), 'OK')
for child in self.userPluginBox.children:
self.userPluginBox.removeWidget(child)
updatePlugins = self.getUserPlugins()
for i, plugin in enumerate(updatePlugins):
self.userPluginBox.addWidget(UserPluginCheckBox(plugin), row=i, alignment=gui.QtCore.Qt.AlignTop)
gui3d.app.prompt('Info', 'The plugin copied successfully. To activate check '
'the plugin in the list and restart MakeHuman.', 'OK', helpId='installPluginHelp')
self.updatePluginList()
gui3d.app.prompt('Info', 'The plugin copied successfully. To activate, check '
'the plugin in the list and press the "Activate"-Button or restart MakeHuman.',
'OK', helpId='installPluginHelp')
self.home = os.path.dirname(filename)

@self.reloadButton.mhEvent
def onClicked(event):
self.updatePluginList()

@self.activateButton.mhEvent
def onClicked(event):
for child in self.userPluginBox.children:
self.userPluginBox.removeWidget(child)
updatePlugins = self.getUserPlugins()
for i, plugin in enumerate(updatePlugins):
self.userPluginBox.addWidget(UserPluginCheckBox(plugin), row=i, alignment=gui.QtCore.Qt.AlignTop)

def getUserPlugins(self):
pluginList = []

userPlugins = glob.glob(getpath.getPath(os.path.join("plugins/", '[!_]*.py')))
if userPlugins:
for userPlugin in userPlugins:
pluginList.append(os.path.splitext(os.path.basename(userPlugin))[0])

for fname in os.listdir(getpath.getPath("plugins/")):
if fname[0] != "_":
folder = os.path.join("plugins", fname)
if os.path.isdir(getpath.getPath(folder)) and ("__init__.py" in os.listdir(getpath.getPath(folder))):
pluginList.append(fname)

pluginList.sort()
return pluginList

def decompress(self, filename, dest_path):
if not zipfile.is_zipfile(filename):
log.message('Not a zip file: {0}'.format(filename))
return 1
if not os.path.isdir(dest_path):
log.message('Not a directory: {0}'.format(dest_path))
return 2
with zipfile.ZipFile(filename) as zf:
for f in zf.infolist():
if os.path.isabs(os.path.split(f.filename)[0]) or '..' in os.path.split(f.filename)[0]:
log.warning('Potentially dangerous zip file, '
'containing files with unsuitable path: {0} {1}'.format(filename, f.filename))
return 3
if os.path.exists(os.path.join(dest_path, f.filename)):
log.warning('Zip file contains existing file: {0} {1}'.format(filename, f.filename))
return -1
try:
zf.extractall(dest_path)
except:
pass
return 0
if child.selected:
if not child.module in gui3d.app.modules:
gui3d.app.loadPlugin(name=child.module, location=child.path)
else:
log.message('Module %s already exists and will not be imported a second time.', child.module)

def updatePluginList(self):
for child in self.userPluginBox.children:
self.userPluginBox.removeWidget(child)
updatePlugins = getUserPlugins()
for i, plugin in enumerate(sorted(updatePlugins.keys())):
self.userPluginBox.addWidget(UserPluginCheckBox(plugin, updatePlugins.get(plugin)), row=i,
alignment=gui.QtCore.Qt.AlignTop)


def getUserPlugins():

pluginsToLoad = {}
user_path = getpath.getPath('plugins')

for file in os.listdir(user_path):

location = os.path.join(user_path, file)

if os.path.isdir(location) and not file.startswith('_'):
pLocation = os.path.join(location, '__init__.py')
if os.path.isfile(pLocation):
pluginsToLoad[file] = pLocation

elif os.path.isfile(location) and file.endswith('.py') and not file.startswith('_'):
name = os.path.splitext(file)[0]
pluginsToLoad[name] = location

return pluginsToLoad

def decompress(filename, dest_path):
if not zipfile.is_zipfile(filename):
log.message('Not a zip file: {0}'.format(filename))
return 1
if not os.path.isdir(dest_path):
log.message('Not a directory: {0}'.format(dest_path))
return 2
with zipfile.ZipFile(filename) as zf:
for f in zf.infolist():
if os.path.isabs(os.path.split(f.filename)[0]) or '..' in os.path.split(f.filename)[0]:
log.warning('Potentially dangerous zip file, '
'containing files with unsuitable path: {0} {1}'.format(filename, f.filename))
return 3
if os.path.exists(os.path.join(dest_path, f.filename)):
log.warning('Zip file contains existing file: {0} {1}'.format(filename, f.filename))
return 4
try:
zf.extractall(dest_path)
except:
pass
return 0


def load(app):
category = app.getCategory('Settings')
Expand Down

0 comments on commit 397db61

Please sign in to comment.