From 7fe094ac02880e94fc73dd93a1ba31b72f238053 Mon Sep 17 00:00:00 2001 From: Eric Trombly Date: Thu, 9 Apr 2020 18:13:02 -0500 Subject: [PATCH 1/2] add lazyloader support lazy_loader is copied to Ext now, modified external imports to lazy_load add a few more imports to be lazy loaded, think the install path is correct now [TD]"<" symbol embedded in html revert changes to path modules for testing use lazyloader in PathAreaOp.py add back in deferred loading temp change to print error message in tests temp change to print error message in tests add _init__.py to lazy_loader make install in CMakeLists.txt one line --- src/3rdParty/CMakeLists.txt | 2 + src/3rdParty/lazy_loader/CMakeLists.txt | 12 ++++ src/3rdParty/lazy_loader/__init__.py | 0 src/3rdParty/lazy_loader/lazy_loader.py | 60 +++++++++++++++++++ src/Mod/Path/PathScripts/PathAreaOp.py | 7 ++- .../Path/PathScripts/PathCircularHoleBase.py | 11 ++-- src/Mod/Path/PathScripts/PathDeburr.py | 5 +- .../Path/PathScripts/PathDressupDogbone.py | 7 ++- .../Path/PathScripts/PathDressupDragknife.py | 5 +- .../PathScripts/PathDressupHoldingTags.py | 5 +- .../Path/PathScripts/PathDressupRampEntry.py | 5 +- src/Mod/Path/PathScripts/PathDressupTag.py | 7 ++- .../Path/PathScripts/PathDressupZCorrect.py | 5 +- src/Mod/Path/PathScripts/PathEngrave.py | 7 ++- src/Mod/Path/PathScripts/PathEngraveBase.py | 5 +- src/Mod/Path/PathScripts/PathGeom.py | 5 +- src/Mod/Path/PathScripts/PathGetPoint.py | 5 +- src/Mod/Path/PathScripts/PathJob.py | 7 ++- src/Mod/Path/PathScripts/PathJobGui.py | 7 ++- src/Mod/Path/PathScripts/PathMillFace.py | 5 +- src/Mod/Path/PathScripts/PathOp.py | 5 +- src/Mod/Path/PathScripts/PathOpTools.py | 5 +- src/Mod/Path/PathScripts/PathPocket.py | 5 +- src/Mod/Path/PathScripts/PathPocketShape.py | 9 ++- .../Path/PathScripts/PathPocketShapeGui.py | 5 +- .../Path/PathScripts/PathProfileContour.py | 7 ++- src/Mod/Path/PathScripts/PathProfileEdges.py | 9 ++- src/Mod/Path/PathScripts/PathProfileFaces.py | 7 ++- src/Mod/Path/PathScripts/PathSimulatorGui.py | 7 ++- src/Mod/Path/PathScripts/PathStock.py | 5 +- src/Mod/Path/PathScripts/PathSurface.py | 9 ++- src/Mod/Path/PathScripts/PathToolBit.py | 5 +- .../Path/PathScripts/PathToolControllerGui.py | 5 +- src/Mod/Path/PathScripts/PathUtils.py | 9 ++- src/Mod/Path/PathScripts/PathWaterline.py | 9 ++- src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp | 17 ++++-- 36 files changed, 233 insertions(+), 57 deletions(-) create mode 100644 src/3rdParty/lazy_loader/CMakeLists.txt create mode 100644 src/3rdParty/lazy_loader/__init__.py create mode 100644 src/3rdParty/lazy_loader/lazy_loader.py diff --git a/src/3rdParty/CMakeLists.txt b/src/3rdParty/CMakeLists.txt index b509ba7814..7bde0b1ac7 100644 --- a/src/3rdParty/CMakeLists.txt +++ b/src/3rdParty/CMakeLists.txt @@ -2,3 +2,5 @@ if (BUILD_SMESH AND NOT FREECAD_USE_EXTERNAL_SMESH) add_subdirectory(salomesmesh) endif() + +add_subdirectory(lazy_loader) \ No newline at end of file diff --git a/src/3rdParty/lazy_loader/CMakeLists.txt b/src/3rdParty/lazy_loader/CMakeLists.txt new file mode 100644 index 0000000000..7ed973983c --- /dev/null +++ b/src/3rdParty/lazy_loader/CMakeLists.txt @@ -0,0 +1,12 @@ +SET(lazy_loader + lazy_loader.py + __init__.py +) +add_custom_target(lazy_loader ALL SOURCES + ${lazy_loader} +) +SET_PYTHON_PREFIX_SUFFIX(lazy_loader) + +fc_copy_sources(lazy_loader "${CMAKE_BINARY_DIR}/Ext/lazy_loader" ${lazy_loader}) +install (FILES ${lazy_loader} DESTINATION "${CMAKE_INSTALL_PREFIX}/Ext/lazy_loader") + diff --git a/src/3rdParty/lazy_loader/__init__.py b/src/3rdParty/lazy_loader/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/3rdParty/lazy_loader/lazy_loader.py b/src/3rdParty/lazy_loader/lazy_loader.py new file mode 100644 index 0000000000..2a443fc30c --- /dev/null +++ b/src/3rdParty/lazy_loader/lazy_loader.py @@ -0,0 +1,60 @@ +# Copyright 2015 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== + +"""A LazyLoader class.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import importlib +import types + + +class LazyLoader(types.ModuleType): + """Lazily import a module, mainly to avoid pulling in large dependencies. + + `contrib`, and `ffmpeg` are examples of modules that are large and not always + needed, and this allows them to only be loaded when they are used. + """ + + # The lint error here is incorrect. + def __init__(self, local_name, parent_module_globals, name, warning=None): # pylint: disable=super-on-old-class + self._local_name = local_name + self._parent_module_globals = parent_module_globals + self._warning = warning + + super(LazyLoader, self).__init__(name) + + def _load(self): + """Load the module and insert it into the parent's globals.""" + # Import the target module and insert it into the parent's namespace + module = importlib.import_module(self.__name__) + self._parent_module_globals[self._local_name] = module + + # Update this object's dict so that if someone keeps a reference to the + # LazyLoader, lookups are efficient (__getattr__ is only called on lookups + # that fail). + self.__dict__.update(module.__dict__) + + return module + + def __getattr__(self, item): + module = self._load() + return getattr(module, item) + + def __dir__(self): + module = self._load() + return dir(module) \ No newline at end of file diff --git a/src/Mod/Path/PathScripts/PathAreaOp.py b/src/Mod/Path/PathScripts/PathAreaOp.py index 3ccb4a39d6..b40e932eb8 100644 --- a/src/Mod/Path/PathScripts/PathAreaOp.py +++ b/src/Mod/Path/PathScripts/PathAreaOp.py @@ -28,9 +28,12 @@ import PathScripts.PathLog as PathLog import PathScripts.PathOp as PathOp import PathScripts.PathUtils as PathUtils import PathScripts.PathGeom as PathGeom -import Draft import math -import Part + +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Draft = LazyLoader('Draft', globals(), 'Draft') +Part = LazyLoader('Part', globals(), 'Part') # from PathScripts.PathUtils import waiting_effects from PySide import QtCore diff --git a/src/Mod/Path/PathScripts/PathCircularHoleBase.py b/src/Mod/Path/PathScripts/PathCircularHoleBase.py index 570d7c09e8..09e57a770d 100644 --- a/src/Mod/Path/PathScripts/PathCircularHoleBase.py +++ b/src/Mod/Path/PathScripts/PathCircularHoleBase.py @@ -28,10 +28,7 @@ # * * # *************************************************************************** -import ArchPanel import FreeCAD -import DraftGeomUtils -import Part import PathScripts.PathLog as PathLog import PathScripts.PathOp as PathOp import PathScripts.PathUtils as PathUtils @@ -39,8 +36,14 @@ import PathScripts.PathUtils as PathUtils from PySide import QtCore import PathScripts.PathGeom as PathGeom +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +ArchPanel = LazyLoader('ArchPanel', globals(), 'ArchPanel') +Draft = LazyLoader('Draft', globals(), 'Draft') +Part = LazyLoader('Part', globals(), 'Part') +DraftGeomUtils = LazyLoader('DraftGeomUtils', globals(), 'DraftGeomUtils') + import math -import Draft if FreeCAD.GuiUp: import FreeCADGui diff --git a/src/Mod/Path/PathScripts/PathDeburr.py b/src/Mod/Path/PathScripts/PathDeburr.py index aa1b1e6d21..26f0fe2acc 100644 --- a/src/Mod/Path/PathScripts/PathDeburr.py +++ b/src/Mod/Path/PathScripts/PathDeburr.py @@ -24,7 +24,6 @@ # *************************************************************************** import FreeCAD -import Part import PathScripts.PathEngraveBase as PathEngraveBase import PathScripts.PathLog as PathLog import PathScripts.PathOp as PathOp @@ -33,6 +32,10 @@ import math from PySide import QtCore +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + __title__ = "Path Deburr Operation" __author__ = "sliptonic (Brad Collette), Schildkroet" __url__ = "http://www.freecadweb.org" diff --git a/src/Mod/Path/PathScripts/PathDressupDogbone.py b/src/Mod/Path/PathScripts/PathDressupDogbone.py index 80e697a691..8af09df11d 100644 --- a/src/Mod/Path/PathScripts/PathDressupDogbone.py +++ b/src/Mod/Path/PathScripts/PathDressupDogbone.py @@ -22,10 +22,8 @@ # * * # *************************************************************************** from __future__ import print_function -import DraftGeomUtils import FreeCAD import math -import Part import Path import PathScripts.PathDressup as PathDressup import PathScripts.PathGeom as PathGeom @@ -35,6 +33,11 @@ import PathScripts.PathUtils as PathUtils from PySide import QtCore +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +DraftDraftGeomUtils = LazyLoader('DraftDraftGeomUtils', globals(), 'DraftDraftGeomUtils') +Part = LazyLoader('Part', globals(), 'Part') + LOG_MODULE = PathLog.thisModule() PathLog.setLevel(PathLog.Level.NOTICE, LOG_MODULE) diff --git a/src/Mod/Path/PathScripts/PathDressupDragknife.py b/src/Mod/Path/PathScripts/PathDressupDragknife.py index 927c07fc44..38badc176d 100644 --- a/src/Mod/Path/PathScripts/PathDressupDragknife.py +++ b/src/Mod/Path/PathScripts/PathDressupDragknife.py @@ -27,9 +27,12 @@ import FreeCAD import Path from PySide import QtCore import math -import DraftVecUtils as D import PathScripts.PathUtils as PathUtils +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +D = LazyLoader('DraftVecUtils', globals(), 'DraftVecUtils') + __doc__ = """Dragknife Dressup object and FreeCAD command""" if FreeCAD.GuiUp: diff --git a/src/Mod/Path/PathScripts/PathDressupHoldingTags.py b/src/Mod/Path/PathScripts/PathDressupHoldingTags.py index e1dd5daba6..d619f0fd13 100644 --- a/src/Mod/Path/PathScripts/PathDressupHoldingTags.py +++ b/src/Mod/Path/PathScripts/PathDressupHoldingTags.py @@ -22,7 +22,6 @@ # * * # *************************************************************************** import FreeCAD -import Part import Path import PathScripts.PathDressup as PathDressup import PathScripts.PathGeom as PathGeom @@ -36,6 +35,10 @@ from PathScripts.PathDressupTagPreferences import HoldingTagPreferences from PathScripts.PathUtils import waiting_effects from PySide import QtCore +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule()) #PathLog.trackModule() diff --git a/src/Mod/Path/PathScripts/PathDressupRampEntry.py b/src/Mod/Path/PathScripts/PathDressupRampEntry.py index ad363b984b..1255472d23 100644 --- a/src/Mod/Path/PathScripts/PathDressupRampEntry.py +++ b/src/Mod/Path/PathScripts/PathDressupRampEntry.py @@ -23,7 +23,6 @@ # *************************************************************************** import FreeCAD import Path -import Part import PathScripts.PathDressup as PathDressup import PathScripts.PathGeom as PathGeom import PathScripts.PathLog as PathLog @@ -32,6 +31,10 @@ import math from PathScripts import PathUtils from PySide import QtCore +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + if FreeCAD.GuiUp: import FreeCADGui diff --git a/src/Mod/Path/PathScripts/PathDressupTag.py b/src/Mod/Path/PathScripts/PathDressupTag.py index 403210fbd3..a610e7939a 100644 --- a/src/Mod/Path/PathScripts/PathDressupTag.py +++ b/src/Mod/Path/PathScripts/PathDressupTag.py @@ -22,14 +22,17 @@ # * * # *************************************************************************** import FreeCAD -import DraftGeomUtils -import Part import PathScripts.PathDressup as PathDressup import PathScripts.PathGeom as PathGeom import PathScripts.PathLog as PathLog import PathScripts.PathUtils as PathUtils import math +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +DraftGeomUtils = LazyLoader('DraftGeomUtils', globals(), 'DraftGeomUtils') +Part = LazyLoader('Part', globals(), 'Part') + from PathScripts.PathDressupTagPreferences import HoldingTagPreferences from PySide import QtCore diff --git a/src/Mod/Path/PathScripts/PathDressupZCorrect.py b/src/Mod/Path/PathScripts/PathDressupZCorrect.py index b3e766d89e..8f26281d66 100644 --- a/src/Mod/Path/PathScripts/PathDressupZCorrect.py +++ b/src/Mod/Path/PathScripts/PathDressupZCorrect.py @@ -27,7 +27,6 @@ # *************************************************************************** import FreeCAD import FreeCADGui -import Part import Path import PathScripts.PathGeom as PathGeom import PathScripts.PathLog as PathLog @@ -35,6 +34,10 @@ import PathScripts.PathUtils as PathUtils from PySide import QtCore, QtGui +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + """Z Depth Correction Dressup. This dressup takes a probe file as input and does bilinear interpolation of the Zdepths to correct for a surface which is not parallel to the milling table/bed. The probe file should conform to the format specified by the linuxcnc G38 probe logging: 9-number coordinate consisting of XYZABCUVW http://linuxcnc.org/docs/html/gcode/g-code.html#gcode:g38 """ diff --git a/src/Mod/Path/PathScripts/PathEngrave.py b/src/Mod/Path/PathScripts/PathEngrave.py index 8063e73484..f1e2d4cdae 100644 --- a/src/Mod/Path/PathScripts/PathEngrave.py +++ b/src/Mod/Path/PathScripts/PathEngrave.py @@ -22,9 +22,7 @@ # * * # *************************************************************************** -import ArchPanel import FreeCAD -import Part import Path import PathScripts.PathEngraveBase as PathEngraveBase import PathScripts.PathLog as PathLog @@ -33,6 +31,11 @@ import PathScripts.PathUtils as PathUtils from PySide import QtCore +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +ArchPanel = LazyLoader('ArchPanel', globals(), 'ArchPanel') +Part = LazyLoader('Part', globals(), 'Part') + __doc__ = "Class and implementation of Path Engrave operation" PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule()) diff --git a/src/Mod/Path/PathScripts/PathEngraveBase.py b/src/Mod/Path/PathScripts/PathEngraveBase.py index 459e60a839..7606ec9b58 100644 --- a/src/Mod/Path/PathScripts/PathEngraveBase.py +++ b/src/Mod/Path/PathScripts/PathEngraveBase.py @@ -22,7 +22,6 @@ # * * # *************************************************************************** -import DraftGeomUtils import Path import PathScripts.PathGeom as PathGeom import PathScripts.PathLog as PathLog @@ -30,6 +29,10 @@ import PathScripts.PathOp as PathOp import PathScripts.PathOpTools as PathOpTools import copy +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +DraftGeomUtils = LazyLoader('DraftGeomUtils', globals(), 'DraftGeomUtils') + from PySide import QtCore __doc__ = "Base class for all ops in the engrave family." diff --git a/src/Mod/Path/PathScripts/PathGeom.py b/src/Mod/Path/PathScripts/PathGeom.py index fba8d1ddb1..5782fa747f 100644 --- a/src/Mod/Path/PathScripts/PathGeom.py +++ b/src/Mod/Path/PathScripts/PathGeom.py @@ -23,7 +23,6 @@ # *************************************************************************** import FreeCAD -import Part import Path import PathScripts.PathLog as PathLog import math @@ -31,6 +30,10 @@ import math from FreeCAD import Vector from PySide import QtCore +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + __title__ = "PathGeom - geometry utilities for Path" __author__ = "sliptonic (Brad Collette)" __url__ = "http://www.freecadweb.org" diff --git a/src/Mod/Path/PathScripts/PathGetPoint.py b/src/Mod/Path/PathScripts/PathGetPoint.py index fc54f69498..93ac673fc2 100644 --- a/src/Mod/Path/PathScripts/PathGetPoint.py +++ b/src/Mod/Path/PathScripts/PathGetPoint.py @@ -22,11 +22,14 @@ # * * # *************************************************************************** -import Draft import FreeCAD import FreeCADGui import PathScripts.PathLog as PathLog +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Draft = LazyLoader('Draft', globals(), 'Draft') + from PySide import QtCore, QtGui from pivy import coin diff --git a/src/Mod/Path/PathScripts/PathJob.py b/src/Mod/Path/PathScripts/PathJob.py index c57291b470..2225c2fa9f 100644 --- a/src/Mod/Path/PathScripts/PathJob.py +++ b/src/Mod/Path/PathScripts/PathJob.py @@ -22,8 +22,6 @@ # * * # *************************************************************************** -import ArchPanel -import Draft import FreeCAD import PathScripts.PathIconViewProvider as PathIconViewProvider import PathScripts.PathLog as PathLog @@ -34,6 +32,11 @@ import PathScripts.PathToolController as PathToolController import PathScripts.PathUtil as PathUtil import json +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +ArchPanel = LazyLoader('ArchPanel', globals(), 'ArchPanel') +Draft = LazyLoader('Draft', globals(), 'Draft') + from PathScripts.PathPostProcessor import PostProcessor from PySide import QtCore diff --git a/src/Mod/Path/PathScripts/PathJobGui.py b/src/Mod/Path/PathScripts/PathJobGui.py index 186b2066bd..0b033cc4a9 100644 --- a/src/Mod/Path/PathScripts/PathJobGui.py +++ b/src/Mod/Path/PathScripts/PathJobGui.py @@ -22,8 +22,6 @@ # * * # *************************************************************************** -import Draft -import DraftVecUtils import FreeCAD import FreeCADGui import PathScripts.PathJob as PathJob @@ -42,6 +40,11 @@ import PathScripts.PathUtils as PathUtils import math import traceback +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Draft = LazyLoader('Draft', globals(), 'Draft') +DraftVecUtils = LazyLoader('DraftVecUtils', globals(), 'DraftVecUtils') + from PySide import QtCore, QtGui from collections import Counter from contextlib import contextmanager diff --git a/src/Mod/Path/PathScripts/PathMillFace.py b/src/Mod/Path/PathScripts/PathMillFace.py index ed1fcfcbb0..b2f2f699c3 100644 --- a/src/Mod/Path/PathScripts/PathMillFace.py +++ b/src/Mod/Path/PathScripts/PathMillFace.py @@ -25,7 +25,6 @@ from __future__ import print_function import FreeCAD -import Part import PathScripts.PathLog as PathLog import PathScripts.PathPocketBase as PathPocketBase import PathScripts.PathUtils as PathUtils @@ -33,6 +32,10 @@ import PathScripts.PathUtils as PathUtils from PySide import QtCore import numpy +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + __title__ = "Path Mill Face Operation" __author__ = "sliptonic (Brad Collette)" __url__ = "http://www.freecadweb.org" diff --git a/src/Mod/Path/PathScripts/PathOp.py b/src/Mod/Path/PathScripts/PathOp.py index 41297bf474..fab24c28c2 100644 --- a/src/Mod/Path/PathScripts/PathOp.py +++ b/src/Mod/Path/PathScripts/PathOp.py @@ -23,7 +23,6 @@ # *************************************************************************** import FreeCAD -import Part import Path import PathScripts.PathGeom as PathGeom import PathScripts.PathLog as PathLog @@ -33,6 +32,10 @@ import PathScripts.PathUtils as PathUtils from PathScripts.PathUtils import waiting_effects from PySide import QtCore +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + __title__ = "Base class for all operations." __author__ = "sliptonic (Brad Collette)" __url__ = "http://www.freecadweb.org" diff --git a/src/Mod/Path/PathScripts/PathOpTools.py b/src/Mod/Path/PathScripts/PathOpTools.py index 50a0484c36..40e7105ca5 100644 --- a/src/Mod/Path/PathScripts/PathOpTools.py +++ b/src/Mod/Path/PathScripts/PathOpTools.py @@ -23,13 +23,16 @@ # *************************************************************************** import FreeCAD -import Part import PathScripts.PathGeom as PathGeom import PathScripts.PathLog as PathLog import math from PySide import QtCore +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + __title__ = "PathOpTools - Tools for Path operations." __author__ = "sliptonic (Brad Collette)" __url__ = "http://www.freecadweb.org" diff --git a/src/Mod/Path/PathScripts/PathPocket.py b/src/Mod/Path/PathScripts/PathPocket.py index 355178ff52..27fd1a9441 100644 --- a/src/Mod/Path/PathScripts/PathPocket.py +++ b/src/Mod/Path/PathScripts/PathPocket.py @@ -23,7 +23,6 @@ # *************************************************************************** import FreeCAD -import Part import PathScripts.PathLog as PathLog import PathScripts.PathOp as PathOp import PathScripts.PathPocketBase as PathPocketBase @@ -31,6 +30,10 @@ import PathScripts.PathUtils as PathUtils from PySide import QtCore +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + __title__ = "Path 3D Pocket Operation" __author__ = "Yorik van Havre " __url__ = "http://www.freecadweb.org" diff --git a/src/Mod/Path/PathScripts/PathPocketShape.py b/src/Mod/Path/PathScripts/PathPocketShape.py index 7c98f71d9c..6cc24e5ba4 100644 --- a/src/Mod/Path/PathScripts/PathPocketShape.py +++ b/src/Mod/Path/PathScripts/PathPocketShape.py @@ -24,15 +24,18 @@ # *************************************************************************** import FreeCAD -import Part import PathScripts.PathGeom as PathGeom import PathScripts.PathLog as PathLog import PathScripts.PathOp as PathOp import PathScripts.PathPocketBase as PathPocketBase import PathScripts.PathUtils as PathUtils -import TechDraw import math -import Draft + +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Draft = LazyLoader('Draft', globals(), 'Draft') +Part = LazyLoader('Part', globals(), 'Part') +TechDraw = LazyLoader('TechDraw', globals(), 'TechDraw') from PySide import QtCore diff --git a/src/Mod/Path/PathScripts/PathPocketShapeGui.py b/src/Mod/Path/PathScripts/PathPocketShapeGui.py index c498e64750..f085d062c1 100644 --- a/src/Mod/Path/PathScripts/PathPocketShapeGui.py +++ b/src/Mod/Path/PathScripts/PathPocketShapeGui.py @@ -24,7 +24,6 @@ import FreeCAD import FreeCADGui -import Part import PathScripts.PathGeom as PathGeom import PathScripts.PathGui as PathGui import PathScripts.PathLog as PathLog @@ -35,6 +34,10 @@ import PathScripts.PathPocketBaseGui as PathPocketBaseGui from PySide import QtCore, QtGui from pivy import coin +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + __title__ = "Path Pocket Shape Operation UI" __author__ = "sliptonic (Brad Collette)" __url__ = "http://www.freecadweb.org" diff --git a/src/Mod/Path/PathScripts/PathProfileContour.py b/src/Mod/Path/PathScripts/PathProfileContour.py index 8a08ceeea1..5aaeb8c56d 100644 --- a/src/Mod/Path/PathScripts/PathProfileContour.py +++ b/src/Mod/Path/PathScripts/PathProfileContour.py @@ -24,9 +24,7 @@ from __future__ import print_function -import ArchPanel import FreeCAD -import Part import Path import PathScripts.PathProfileBase as PathProfileBase import PathScripts.PathLog as PathLog @@ -34,6 +32,11 @@ import PathScripts.PathLog as PathLog from PathScripts import PathUtils from PySide import QtCore +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +ArchPanel = LazyLoader('ArchPanel', globals(), 'ArchPanel') +Part = LazyLoader('Part', globals(), 'Part') + FreeCAD.setLogLevel('Path.Area', 0) PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule()) diff --git a/src/Mod/Path/PathScripts/PathProfileEdges.py b/src/Mod/Path/PathScripts/PathProfileEdges.py index e01ee25d4c..515e0de5dd 100644 --- a/src/Mod/Path/PathScripts/PathProfileEdges.py +++ b/src/Mod/Path/PathScripts/PathProfileEdges.py @@ -23,18 +23,21 @@ # *************************************************************************** import FreeCAD -import Part import Path import PathScripts.PathLog as PathLog import PathScripts.PathOp as PathOp import PathScripts.PathProfileBase as PathProfileBase import PathScripts.PathUtils as PathUtils -import DraftGeomUtils -import Draft import math import PySide +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Draft = LazyLoader('Draft', globals(), 'Draft') +Part = LazyLoader('Part', globals(), 'Part') +DraftGeomUtils = LazyLoader('DraftGeomUtils', globals(), 'DraftGeomUtils') + PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule()) # PathLog.trackModule(PathLog.thisModule()) diff --git a/src/Mod/Path/PathScripts/PathProfileFaces.py b/src/Mod/Path/PathScripts/PathProfileFaces.py index 6f2233b215..281d848699 100644 --- a/src/Mod/Path/PathScripts/PathProfileFaces.py +++ b/src/Mod/Path/PathScripts/PathProfileFaces.py @@ -23,9 +23,7 @@ # * * # *************************************************************************** -import ArchPanel import FreeCAD -import Part import Path import PathScripts.PathLog as PathLog import PathScripts.PathOp as PathOp @@ -35,6 +33,11 @@ import numpy from PySide import QtCore +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +ArchPanel = LazyLoader('ArchPanel', globals(), 'ArchPanel') +Part = LazyLoader('Part', globals(), 'Part') + __title__ = "Path Profile Faces Operation" __author__ = "sliptonic (Brad Collette), Schildkroet" __url__ = "http://www.freecadweb.org" diff --git a/src/Mod/Path/PathScripts/PathSimulatorGui.py b/src/Mod/Path/PathScripts/PathSimulatorGui.py index 50014e0272..428dcc394c 100644 --- a/src/Mod/Path/PathScripts/PathSimulatorGui.py +++ b/src/Mod/Path/PathScripts/PathSimulatorGui.py @@ -1,6 +1,4 @@ import FreeCAD -import Mesh -import Part import Path import PathScripts.PathDressup as PathDressup import PathScripts.PathGeom as PathGeom @@ -13,6 +11,11 @@ from FreeCAD import Vector, Base _filePath = os.path.dirname(os.path.abspath(__file__)) +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Mesh = LazyLoader('Mesh', globals(), 'Mesh') +Part = LazyLoader('Part', globals(), 'Part') + if FreeCAD.GuiUp: import FreeCADGui from PySide import QtGui, QtCore diff --git a/src/Mod/Path/PathScripts/PathStock.py b/src/Mod/Path/PathScripts/PathStock.py index dfb15658a0..e1ab825627 100644 --- a/src/Mod/Path/PathScripts/PathStock.py +++ b/src/Mod/Path/PathScripts/PathStock.py @@ -23,13 +23,16 @@ '''used to create material stock around a machined part- for visualization ''' import FreeCAD -import Part import PathScripts.PathIconViewProvider as PathIconViewProvider import PathScripts.PathLog as PathLog import math from PySide import QtCore +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule()) #PathLog.trackModule(PathLog.thisModule()) diff --git a/src/Mod/Path/PathScripts/PathSurface.py b/src/Mod/Path/PathScripts/PathSurface.py index 9e63b6f359..40cd771398 100644 --- a/src/Mod/Path/PathScripts/PathSurface.py +++ b/src/Mod/Path/PathScripts/PathSurface.py @@ -30,7 +30,6 @@ from __future__ import print_function import FreeCAD -import MeshPart import Path import PathScripts.PathLog as PathLog import PathScripts.PathUtils as PathUtils @@ -39,8 +38,12 @@ import PathScripts.PathOp as PathOp from PySide import QtCore import time import math -import Part -import Draft + +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +MeshPart = LazyLoader('MeshPart', globals(), 'MeshPart') +Draft = LazyLoader('Draft', globals(), 'Draft') +Part = LazyLoader('Part', globals(), 'Part') if FreeCAD.GuiUp: import FreeCADGui diff --git a/src/Mod/Path/PathScripts/PathToolBit.py b/src/Mod/Path/PathScripts/PathToolBit.py index eeae4a70ae..7863e31df7 100644 --- a/src/Mod/Path/PathScripts/PathToolBit.py +++ b/src/Mod/Path/PathScripts/PathToolBit.py @@ -23,7 +23,6 @@ # *************************************************************************** import FreeCAD -import Part import PathScripts.PathGeom as PathGeom import PathScripts.PathLog as PathLog import PathScripts.PathPreferences as PathPreferences @@ -36,6 +35,10 @@ import math import os import zipfile +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + __title__ = "Tool bits." __author__ = "sliptonic (Brad Collette)" __url__ = "http://www.freecadweb.org" diff --git a/src/Mod/Path/PathScripts/PathToolControllerGui.py b/src/Mod/Path/PathScripts/PathToolControllerGui.py index 3a05fbe301..f71658172a 100644 --- a/src/Mod/Path/PathScripts/PathToolControllerGui.py +++ b/src/Mod/Path/PathScripts/PathToolControllerGui.py @@ -24,7 +24,6 @@ import FreeCAD import FreeCADGui -import Part import PathScripts import PathScripts.PathGui as PathGui import PathScripts.PathLog as PathLog @@ -34,6 +33,10 @@ import PathScripts.PathUtil as PathUtil from PySide import QtCore, QtGui +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +Part = LazyLoader('Part', globals(), 'Part') + # Qt translation handling def translate(context, text, disambig=None): return QtCore.QCoreApplication.translate(context, text, disambig) diff --git a/src/Mod/Path/PathScripts/PathUtils.py b/src/Mod/Path/PathScripts/PathUtils.py index 0ea9c90c30..29dfcdb2bd 100644 --- a/src/Mod/Path/PathScripts/PathUtils.py +++ b/src/Mod/Path/PathScripts/PathUtils.py @@ -23,21 +23,24 @@ # *************************************************************************** '''PathUtils -common functions used in PathScripts for filtering, sorting, and generating gcode toolpath data ''' import FreeCAD -import Part import Path import PathScripts import PathScripts.PathGeom as PathGeom -import TechDraw import math import numpy -from DraftGeomUtils import geomType from FreeCAD import Vector from PathScripts import PathJob from PathScripts import PathLog from PySide import QtCore from PySide import QtGui +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +geomType = LazyLoader('DraftDraftGeomUtils', globals(), 'DraftDraftGeomUtils.geomType') +Part = LazyLoader('Part', globals(), 'Part') +TechDraw = LazyLoader('TechDraw', globals(), 'TechDraw') + PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule()) #PathLog.trackModule(PathLog.thisModule()) diff --git a/src/Mod/Path/PathScripts/PathWaterline.py b/src/Mod/Path/PathScripts/PathWaterline.py index 0362680580..c1c8b66cb6 100644 --- a/src/Mod/Path/PathScripts/PathWaterline.py +++ b/src/Mod/Path/PathScripts/PathWaterline.py @@ -26,7 +26,6 @@ from __future__ import print_function import FreeCAD -import MeshPart import Path import PathScripts.PathLog as PathLog import PathScripts.PathUtils as PathUtils @@ -35,8 +34,12 @@ import PathScripts.PathOp as PathOp from PySide import QtCore import time import math -import Part -import Draft + +# lazily loaded modules +from lazy_loader.lazy_loader import LazyLoader +MeshPart = LazyLoader('MeshPart', globals(), 'MeshPart') +Draft = LazyLoader('Draft', globals(), 'Draft') +Part = LazyLoader('Part', globals(), 'Part') if FreeCAD.GuiUp: import FreeCADGui diff --git a/src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp b/src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp index 02c25ce9a4..64ff8f49a7 100644 --- a/src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp @@ -158,10 +158,19 @@ void QGIViewAnnotation::drawAnnotation() if (it != annoText.begin()) { ss << "
"; } - std::string u8String = Base::Tools::escapedUnicodeToUtf8(*it); -// what madness turns \' into \\\\\'? - std::string apos = std::regex_replace((u8String), std::regex("\\\\\'"), "'"); - ss << apos; + //TODO: there is still a bug here. entering "'" works, save and restore works, but edit after + // save and restore brings "\'" back into text. manually deleting the "\" fixes it until the next + // save/restore/edit cycle. + // a guess is that the editor for propertyStringList is too enthusiastic about substituting. + // the substituting might be necessary for using the strings in Python. + // ' doesn't seem to help in this case. + + std::string u8String = Base::Tools::escapedUnicodeToUtf8(*it); //from \x??\x?? to real utf8 + std::string apos = std::regex_replace((u8String), std::regex("\\\\"), ""); //remove doubles. + apos = std::regex_replace((apos), std::regex("\\'"), "'"); //replace escaped apos + //"less than" symbol chops off line. need to use html sub. + std::string lt = std::regex_replace((apos), std::regex("<"), "<"); + ss << lt; } ss << "

\n\n "; From 7bd2ec425eb2f5acc9fa37be9d3263dc3af722af Mon Sep 17 00:00:00 2001 From: Eric Trombly Date: Tue, 14 Apr 2020 10:44:15 -0500 Subject: [PATCH 2/2] add docstring to __init__.py --- src/3rdParty/lazy_loader/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/3rdParty/lazy_loader/__init__.py b/src/3rdParty/lazy_loader/__init__.py index e69de29bb2..db8200b5bc 100644 --- a/src/3rdParty/lazy_loader/__init__.py +++ b/src/3rdParty/lazy_loader/__init__.py @@ -0,0 +1,13 @@ +""" +LazyLoader will defer import of a module until first usage. Usage: +from lazy_loader.lazy_loader import LazyLoader +numpy = LazyLoader("numpy", globals(), "numpy") + +or + +whatever = LazyLoader("module", globals(), "module.whatever") + +or to replicate import module as something + +something = LazyLoader("module", globals(), "module") +""" \ No newline at end of file