Skip to content

Commit

Permalink
Fix path issue when using symbolic links
Browse files Browse the repository at this point in the history
  • Loading branch information
krathjen committed Jan 28, 2018
1 parent 5b8c1c9 commit 0d6a650
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 37 deletions.
79 changes: 46 additions & 33 deletions cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"replaceJson",
"relPath",
"absPath",
"realPath",
"normPath",
"copyPath",
"movePath",
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions librarywidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 0d6a650

Please sign in to comment.