diff --git a/cmds.py b/cmds.py index 69435bf0..5264215f 100644 --- a/cmds.py +++ b/cmds.py @@ -41,6 +41,7 @@ "replaceJson", "relPath", "absPath", + "realPath", "normPath", "copyPath", "movePath", @@ -736,42 +737,16 @@ def absPath(data, start): return data -def generateUniquePath(path, attempts=1000): +def realPath(path): """ - Generate a unique path on disc. - - Example: - # If the following files exist then the next unique path will be 3. - # C:/tmp/file.text - # C:/tmp/file (2).text - - print generateUniquePath("C:/tmp/file.text") - # C:/tmp/file (3).text + Return the given path eliminating any symbolic link. - :type path: str - :type attempts: int - :rtype: str + :type path: str + :rtype: str """ - attempt = 1 # We start at one so that the first unique name is actually 2. - dirname, name, extension = splitPath(path) - path_ = u'{dirname}/{name} ({number}){extension}' - - while os.path.exists(path): - attempt += 1 - - path = path_.format( - name=name, - number=attempt, - dirname=dirname, - extension=extension - ) - - if attempt >= attempts: - msg = u'Cannot generate unique name for path {path}' - msg = msg.format(path=path) - raise ValueError(msg) - - return path + path = os.path.realpath(path) + path = os.path.expanduser(path) + return normPath(path) def normPath(path): @@ -850,6 +825,44 @@ def listPaths(path): yield value +def generateUniquePath(path, attempts=1000): + """ + Generate a unique path on disc. + + Example: + # If the following files exist then the next unique path will be 3. + # C:/tmp/file.text + # C:/tmp/file (2).text + + print generateUniquePath("C:/tmp/file.text") + # C:/tmp/file (3).text + + :type path: str + :type attempts: int + :rtype: str + """ + attempt = 1 # We start at one so that the first unique name is actually 2. + dirname, name, extension = splitPath(path) + path_ = u'{dirname}/{name} ({number}){extension}' + + while os.path.exists(path): + attempt += 1 + + path = path_.format( + name=name, + number=attempt, + dirname=dirname, + extension=extension + ) + + if attempt >= attempts: + msg = u'Cannot generate unique name for path {path}' + msg = msg.format(path=path) + raise ValueError(msg) + + return path + + def findPaths( path, match=None, diff --git a/librarywidget.py b/librarywidget.py index 9fb2f247..670ebb46 100644 --- a/librarywidget.py +++ b/librarywidget.py @@ -413,8 +413,7 @@ def setPath(self, path): :type path: str :rtype: None """ - path = os.path.abspath(path) - path = studiolibrary.normPath(path) + path = studiolibrary.realPath(path) if path == self.path(): logger.debug("The root path is already set.") @@ -494,8 +493,7 @@ def _showChangePathDialog(self): directory = path if not directory: - from os.path import expanduser - directory = expanduser("~") + directory = os.path.expanduser("~") dialog = QtWidgets.QFileDialog(None, QtCore.Qt.WindowStaysOnTopHint)