Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions LibraryTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
import inspect
import zipfile
import subprocess

import importlib
import Container
import Menu

from Utilities import replaceAll, getPYFileListFromInit, path_to_module, printOnStatusBar, NotificationMessage, install_and_import
from Utilities import replaceAll, getPYFileListFromInit, path_to_module, printOnStatusBar, NotificationMessage, install_and_import, module_list
from Decorators import BuzyCursorNotification
from Components import BlockFactory, DEVSComponent, GetClass
from ZipManager import Zip, getPythonModelFileName
Expand Down Expand Up @@ -311,6 +311,39 @@ def OnDelete(self, evt):
else:
wx.MessageBox(_("No library selected!"),_("Delete Manager"))

def UpdateSubLib(self, path:str)->bool:
""" Do update lib.
"""
### reload .py module from path
for s in module_list(path):
module_name = ".".join(s.split('.')[1:])
if module_name in sys.modules:
module = sys.modules[module_name]
dirname = os.path.dirname(module.__file__)
try:
### .amd or .cmd
if zipfile.is_zipfile(dirname):
zf = Zip(dirname)
if isinstance(zf.ReImport(), Exception):
return False
else:
importlib.reload(module)
except:
return False
return True

###
@BuzyCursorNotification
def OnUpdateSubLib(self, evt):
""" ReImport all module (.py and .amd/.cmd) in lib.
"""
item = self.GetFocusedItem()
path = self.GetItemPyData(item)
if self.UpdateSubLib(path):
NotificationMessage(_('Information'), _('%s has been updated!')%os.path.basename(path), parent=self, timeout=5)
else:
NotificationMessage(_('Error'), _('%s has not been updated! See traceback for more information')%os.path.basename(path), parent=self, timeout=5)

###
def OnNewModel(self, evt):
""" New model action has been invoked.
Expand Down Expand Up @@ -922,28 +955,38 @@ def UpdateDomain(self, path):
def OnUpdateAll(self, event):
""" Update all imported domain
"""
self.UpdateAll()

NotificationMessage(_('Information'), _("All librairies have been succeffully updated!"), self, timeout=5)
result = self.UpdateAll()
if len(result) == 0:
NotificationMessage(_('Information'), _("All libraries have been succeffully updated!"), self, timeout=5)
else:
NotificationMessage(_('Error'), _("The following libraries updates crash:\n %s")%" \n".join(map(os.path.basename,result)), self, timeout=5)

def OnMCCClick(self, event):
"""
"""
tb = event.GetEventObject()
LibraryTree.COMPARE_BY_MACABE_METRIC = not tb.GetToolState(Menu.ID_MCC_LIB)
self.UpdateAll()
self.OnUpdateAll(event)

###
def UpdateAll(self):
""" Update all loaded libaries.
""" Update all loaded libraries.
"""

fault = set()
### update all Domain
for item in self.GetItemChildren(self.GetRootItem()):
self.UpdateDomain(self.GetPyData(item))
path = self.GetItemPyData(item)
if self.UpdateSubLib(path):
self.UpdateDomain(self.GetPyData(item))
else:
fault.add(path)

### to sort domain
wx.CallAfter(self.SortChildren,self.root)

return fault

#self.SortChildren(self.root)

###
Expand Down Expand Up @@ -1108,4 +1151,4 @@ def OnItemDocumentation(self, evt):
def OnInfo(self, event):
"""
"""
wx.MessageBox(_('Libraries Import Manager.\nYou can import, refresh or upgrade librairies using right options.\nDefault libraries directory is %s.')%(DOMAIN_PATH))
wx.MessageBox(_('Libraries Import Manager.\nYou can import, refresh or upgrade libraries using right options.\nDefault libraries directory is %s.')%(DOMAIN_PATH))
5 changes: 5 additions & 0 deletions Menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
ID_UPDATE_LIB = wx.NewIdRef()
ID_HELP_LIB = wx.NewIdRef()
ID_NEW_MODEL_LIB = wx.NewIdRef()
ID_UPDATE_SUBLIB = wx.NewIdRef()
ID_DELETE_LIB = wx.NewIdRef()

# Attribute popup menu identifiers
Expand Down Expand Up @@ -721,7 +722,11 @@ def __init__(self, parent):
new_model = wx.MenuItem(self, ID_NEW_MODEL_LIB, _('New Model'), _('Add a new model to the selected library'))
new_model.SetBitmap(wx.Bitmap(os.path.join(ICON_PATH_16_16, 'new.png')))
InsertItem(0, new_model)
update_lib = wx.MenuItem(self, ID_UPDATE_SUBLIB, _('Update'), _('Update all models of the selected library'))
update_lib.SetBitmap(wx.Bitmap(os.path.join(ICON_PATH_16_16, 'db_refresh2.png')))
InsertItem(1, update_lib)
self.Bind(wx.EVT_MENU, parent.OnNewModel, id=ID_NEW_MODEL_LIB)
self.Bind(wx.EVT_MENU, parent.OnUpdateSubLib, id=ID_UPDATE_SUBLIB)

else:

Expand Down
3 changes: 1 addition & 2 deletions ReloadModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ def recompile(modulename):
if os.path.isfile(modulename) and os.path.exists(modulename):
import ZipManager
zf = ZipManager.Zip(modulename)
return zf.Recompile()
return zf.ReImport()
else:

try:
### first, see if the module can be imported at all...
name, ext = os.path.splitext(modulename)
Expand Down
19 changes: 16 additions & 3 deletions Utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import imp
import tempfile
import pathlib

#import shlex
from copy import deepcopy
from datetime import datetime
Expand Down Expand Up @@ -127,16 +128,28 @@ def NotificationMessage(title,message,parent,flag=wx.ICON_INFORMATION, timeout=F
else:
notify.Show()

def now():
def now()->str:
""" Returns the current time formatted. """

t = time.localtime(time.time())
st = time.strftime("%d %B %Y @ %H:%M:%S", t)

return st


def shortNow():
def module_list(topdir:str)->[str]:
for root,dirs,files in os.walk(topdir):
modpath = os.path.basename(topdir)
r = os.path.relpath(root,topdir)
if r != '.':
modpath += '.' + r
for extension in ('*.py', '*.amd', '*.cmd'):
for f in fnmatch.filter(files, extension):
if f == '__init__.py':
yield modpath
elif f not in ['__main__.py']:
yield '.'.join([modpath,os.path.splitext(f)[0]])

def shortNow()->str:
""" Returns the current time formatted. """

t = time.localtime(time.time())
Expand Down
Loading