From a09e4ebe0f1b4415f52c31446c4f5d32d30057cb Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Wed, 9 Apr 2014 18:45:03 +0800 Subject: [PATCH] Lazy loading of workplace to prevent dependency issues. Fixes #79 --- lib/command.py | 7 +++---- lib/commands/config.py | 1 - lib/commands/doctor.py | 21 +++++++++++++++++++++ lib/commands/init.py | 1 - 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/command.py b/lib/command.py index 3dbfdc4..1eecfdb 100644 --- a/lib/command.py +++ b/lib/command.py @@ -24,7 +24,6 @@ http://github.com/FMCorz/mdk import argparse import sys -import workplace class Command(object): @@ -46,15 +45,12 @@ class Command(object): ) ] _description = 'Undocumented command' - _loadWorkplace = True __C = None __Wp = None def __init__(self, config): self.__C = config - if self._loadWorkplace: - self.__Wp = workplace.Workplace() def argumentError(self, message): raise CommandArgumentError(message) @@ -76,6 +72,9 @@ class Command(object): @property def Wp(self): + if not self.__Wp: + import workplace + self.__Wp = workplace.Workplace() return self.__Wp diff --git a/lib/commands/config.py b/lib/commands/config.py index 9023d9a..054aebf 100644 --- a/lib/commands/config.py +++ b/lib/commands/config.py @@ -88,7 +88,6 @@ class ConfigCommand(Command): ) ] _description = 'Manage your configuration' - _loadWorkplace = False def dictDisplay(self, data, ident=0): for name in sorted(data.keys()): diff --git a/lib/commands/doctor.py b/lib/commands/doctor.py index 5ac2c55..2ffda9c 100644 --- a/lib/commands/doctor.py +++ b/lib/commands/doctor.py @@ -143,7 +143,11 @@ class DoctorCommand(Command): print 'Checking integration instances branches' + if not self._checkWorkplace(): + return + instances = self.Wp.list(integration=True) + for identifier in instances: M = self.Wp.get(identifier) stablebranch = M.get('stablebranch') @@ -260,6 +264,10 @@ class DoctorCommand(Command): """Check that the correct remotes are used""" print 'Checking remotes' + + if not self._checkWorkplace(): + return + remotes = { 'mine': self.C.get('remotes.mine'), 'stable': self.Wp.getCachedRemote() if self.C.get('useCacheAsUpstreamRemote') else self.C.get('remotes.stable'), @@ -291,6 +299,10 @@ class DoctorCommand(Command): """Check the wwwroot of the instances""" print 'Checking wwwroot' + + if not self._checkWorkplace(): + return + instances = self.Wp.resolveMultiple(self.Wp.list()) wwwroot = '%s://%s/' % (self.C.get('scheme'), self.C.get('host')) @@ -308,3 +320,12 @@ class DoctorCommand(Command): if args.fix: print ' Setting %s on %s' % (expected, M.get('identifier')) M.updateConfig('wwwroot', expected) + + def _checkWorkplace(self, indent=2): + """Returns whether the workplace is available or, and print a message if it is not.""" + try: + self.Wp + except ImportError: + print ' ' * indent + 'The workplace could not be loaded, did you install the dependencies?' + return False + return True diff --git a/lib/commands/init.py b/lib/commands/init.py index 659342b..48e3b4a 100644 --- a/lib/commands/init.py +++ b/lib/commands/init.py @@ -45,7 +45,6 @@ class InitCommand(Command): ) ] _description = 'Initialise MDK' - _loadWorkplace = False def resolve_directory(self, path, user): if path.startswith('~'): -- 2.11.0