Skip to content

Commit df401d6

Browse files
committed
Improved code readability with long error/warning messages
1 parent e53bf97 commit df401d6

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

mbed/mbed.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,9 @@ def fromrepo(cls, path=None):
573573
if path is None:
574574
path = Repo.findrepo(os.getcwd())
575575
if path is None:
576-
error('Cannot find the program or library in the current path \"%s\".\nPlease change your working directory to a different location or use command \"new\" to create a new program.' % os.getcwd(), 1)
576+
error(
577+
"Cannot find the program or library in the current path \"%s\".\n"
578+
"Please change your working directory to a different location or use \"mbed new\" to create a new program." % os.getcwd(), 1)
577579

578580
repo.path = os.path.abspath(path)
579581
repo.name = os.path.basename(repo.path)
@@ -741,29 +743,42 @@ def rm_untracked(self):
741743
os.remove(file)
742744

743745
def can_update(self, clean, force):
746+
err = None
744747
if (self.is_local or self.url is None) and not force:
745-
return False, "Preserving local library \"%s\" in \"%s\".\nPlease publish this library to a remote URL to be able to restore it at any time.\nYou can use --ignore switch to ignore all local libraries and update only the published ones.\nYou can also use --force switch to remove all local libraries. WARNING: This action cannot be undone." % (self.name, self.path)
748+
err = (
749+
"Preserving local library \"%s\" in \"%s\".\nPlease publish this library to a remote URL to be able to restore it at any time."
750+
"You can use --ignore switch to ignore all local libraries and update only the published ones.\n"
751+
"You can also use --force switch to remove all local libraries. WARNING: This action cannot be undone." % (self.name, self.path))
746752
if not clean and self.scm.dirty():
747-
return False, "Uncommitted changes in \"%s\" in \"%s\".\nPlease discard or stash them first and then retry update.\nYou can also use --clean switch to discard all uncommitted changes. WARNING: This action cannot be undone." % (self.name, self.path)
753+
err = (
754+
"Uncommitted changes in \"%s\" in \"%s\".\nPlease discard or stash them first and then retry update.\n"
755+
"You can also use --clean switch to discard all uncommitted changes. WARNING: This action cannot be undone." % (self.name, self.path))
748756
if not force and self.scm.outgoing():
749-
return False, "Unpublished changes in \"%s\" in \"%s\".\nPlease publish them first using the \"publish\" command.\nYou can also use --force to discard all local commits and replace the library with the one included in this revision. WARNING: This action cannot be undone." % (self.name, self.path)
757+
err = (
758+
"Unpublished changes in \"%s\" in \"%s\".\nPlease publish them first using the \"publish\" command.\n"
759+
"You can also use --force to discard all local commits and replace the library with the one included in this revision. WARNING: This action cannot be undone." % (self.name, self.path))
750760

751-
return True, "OK"
761+
return (False, err) if err else (True, "OK")
752762

753763
def check_repo(self, show_warning=None):
764+
err = None
754765
if not os.path.isdir(self.path):
755-
if show_warning:
756-
warning("Library reference \"%s\" points to non-existing library in \"%s\"\nYou can use \"deploy\" command to import the missing libraries.\nYou can also use \"sync\" command to synchronize and remove all invalid library references." % (os.path.basename(self.lib), self.path))
757-
else:
758-
error("Library reference \"%s\" points to non-existing library in \"%s\"\nYou can use \"deploy\" command to import the missing libraries\nYou can also use \"sync\" command to synchronize and remove all invalid library references." % (os.path.basename(self.lib), self.path), 1)
759-
return False
766+
err = (
767+
"Library reference \"%s\" points to non-existing library in \"%s\"\n"
768+
"You can use \"mbed deploy\" to import the missing libraries.\n"
769+
"You can also use \"mbed sync\" to synchronize and remove all invalid library references." % (os.path.basename(self.lib), self.path))
760770
elif not self.isrepo(self.path):
771+
err = (
772+
"Library reference \"%s\" points to a folder \"%s\", which is not a valid repository.\n"
773+
"You can remove the conflicting folder manually and use \"mbed deploy\" to import the missing libraries\n"
774+
"You can also remove library reference \"%s\" and use \"mbed sync\" again." % (os.path.basename(self.lib), self.path, self.lib))
775+
776+
if err:
761777
if show_warning:
762-
warning("Library reference \"%s\" points to a folder \"%s\", which is not a valid repository.\nYou can remove the conflicting folder manually and use \"deploy\" command to import the missing libraries\nYou can also remove library reference \"%s\" and use the \"sync\" command again." % (os.path.basename(self.lib), self.path, self.lib))
778+
warning(err)
763779
else:
764-
error("Library reference \"%s\" points to a folder \"%s\", which is not a valid repository.\nYou can remove the conflicting folder manually and use \"deploy\" command to import the missing libraries\nYou can also remove library reference \"%s\" and use the \"sync\" command again." % (os.path.basename(self.lib), self.path, self.lib), 1)
780+
error(err, 1)
765781
return False
766-
767782
return True
768783

769784

@@ -892,7 +907,8 @@ def import_(url, path=None, depth=None, protocol=None, top=True):
892907

893908
repo = Repo.fromurl(url, path)
894909
if top and cwd_type != "directory":
895-
error("Cannot import program in the specified location \"%s\" because it's already part of a program.\nPlease change your working directory to a different location or use command \"mbed add\" to import the URL as a library." % os.path.abspath(repo.path), 1)
910+
error("Cannot import program in the specified location \"%s\" because it's already part of a program.\n"
911+
"Please change your working directory to a different location or use \"mbed add\" to import the URL as a library." % os.path.abspath(repo.path), 1)
896912

897913
if os.path.isdir(repo.path) and len(os.listdir(repo.path)) > 1:
898914
error("Directory \"%s\" is not empty. Please ensure that the destination folder is empty." % repo.path, 1)
@@ -990,7 +1006,9 @@ def publish(all=None, top=True):
9901006

9911007
repo = Repo.fromrepo()
9921008
if repo.is_local:
993-
error("%s \"%s\" in \"%s\" is a local repository.\nPlease associate it with a remote repository URL before attempting to publish.\nRead more about %s repositories here:\nhttp://developer.mbed.org/handbook/how-to-publish-with-%s/" % ("Program" if top else "Library", repo.name, repo.path, repo.scm.name, repo.scm.name), 1)
1009+
error(
1010+
"%s \"%s\" in \"%s\" is a local repository.\nPlease associate it with a remote repository URL before attempting to publish.\n"
1011+
"Read more about %s repositories here:\nhttp://developer.mbed.org/handbook/how-to-publish-with-%s/" % ("Program" if top else "Library", repo.name, repo.path, repo.scm.name, repo.scm.name), 1)
9941012

9951013
for lib in repo.libs:
9961014
if lib.check_repo():
@@ -1030,7 +1048,9 @@ def update(rev=None, clean=False, force=False, ignore=False, top=True, depth=Non
10301048
repo = Repo.fromrepo()
10311049

10321050
if top and not rev and repo.scm.isdetached():
1033-
error("This %s is in detached HEAD state, and you won't be able to receive updates from the remote repository until you either checkout a branch or create a new one.\nYou can checkout a branch using \"%s checkout <branch_name>\" command before running \"mbed update\"." % (cwd_type, repo.scm.name), 1)
1051+
error(
1052+
"This %s is in detached HEAD state, and you won't be able to receive updates from the remote repository until you either checkout a branch or create a new one.\n"
1053+
"You can checkout a branch using \"%s checkout <branch_name>\" command before running \"mbed update\"." % (cwd_type, repo.scm.name), 1)
10341054

10351055
# Fetch from remote repo
10361056
action("Updating %s \"%s\" to %s" % (
@@ -1387,7 +1407,10 @@ def toolchain(name=None):
13871407
error('Subrocess exit with error code %d' % e[0], e[0])
13881408
except OSError as e:
13891409
if e[0] == 2:
1390-
error('Could not detect one of the command-line tools.\nPlease verify that you have Git and Mercurial installed and accessible from your current path by executing commands "git" and "hg".\nHint: check the last executed command above.', e[0])
1410+
error(
1411+
"Could not detect one of the command-line tools.\n"
1412+
"Please verify that you have Git and Mercurial installed and accessible from your current path by executing commands \"git\" and \"hg\".\n"
1413+
"Hint: check the last executed command above.", e[0])
13911414
else:
13921415
error('OS Error: %s' % e[1], e[0])
13931416
except KeyboardInterrupt as e:

0 commit comments

Comments
 (0)