Commit fa4c5944 by willmcgugan@gmail.com

Added constants rather than magic numbers #82

parent e6559cd5
...@@ -14,6 +14,7 @@ import threading ...@@ -14,6 +14,7 @@ import threading
import os import os
import paramiko import paramiko
from getpass import getuser from getpass import getuser
import errno
from fs.base import * from fs.base import *
from fs.path import * from fs.path import *
...@@ -22,9 +23,13 @@ from fs.utils import isdir, isfile ...@@ -22,9 +23,13 @@ from fs.utils import isdir, isfile
from fs import iotools from fs import iotools
ENOENT = errno.ENOENT
class WrongHostKeyError(RemoteConnectionError): class WrongHostKeyError(RemoteConnectionError):
pass pass
# SFTPClient appears to not be thread-safe, so we use an instance per thread # SFTPClient appears to not be thread-safe, so we use an instance per thread
if hasattr(threading, "local"): if hasattr(threading, "local"):
thread_local = threading.local thread_local = threading.local
...@@ -364,7 +369,7 @@ class SFTPFS(FS): ...@@ -364,7 +369,7 @@ class SFTPFS(FS):
try: try:
self.client.stat(npath) self.client.stat(npath)
except IOError, e: except IOError, e:
if getattr(e,"errno",None) == 2: if getattr(e,"errno",None) == ENOENT:
return False return False
raise raise
return True return True
...@@ -378,7 +383,7 @@ class SFTPFS(FS): ...@@ -378,7 +383,7 @@ class SFTPFS(FS):
try: try:
stat = self.client.stat(npath) stat = self.client.stat(npath)
except IOError, e: except IOError, e:
if getattr(e,"errno",None) == 2: if getattr(e,"errno",None) == ENOENT:
return False return False
raise raise
return statinfo.S_ISDIR(stat.st_mode) != 0 return statinfo.S_ISDIR(stat.st_mode) != 0
...@@ -390,7 +395,7 @@ class SFTPFS(FS): ...@@ -390,7 +395,7 @@ class SFTPFS(FS):
try: try:
stat = self.client.stat(npath) stat = self.client.stat(npath)
except IOError, e: except IOError, e:
if getattr(e,"errno",None) == 2: if getattr(e,"errno",None) == ENOENT:
return False return False
raise raise
return statinfo.S_ISREG(stat.st_mode) != 0 return statinfo.S_ISREG(stat.st_mode) != 0
...@@ -408,7 +413,7 @@ class SFTPFS(FS): ...@@ -408,7 +413,7 @@ class SFTPFS(FS):
else: else:
paths = self.client.listdir(npath) paths = self.client.listdir(npath)
except IOError, e: except IOError, e:
if getattr(e,"errno",None) == 2: if getattr(e,"errno",None) == ENOENT:
if self.isfile(path): if self.isfile(path):
raise ResourceInvalidError(path,msg="Can't list directory contents of a file: %(path)s") raise ResourceInvalidError(path,msg="Can't list directory contents of a file: %(path)s")
raise ResourceNotFoundError(path) raise ResourceNotFoundError(path)
...@@ -445,7 +450,7 @@ class SFTPFS(FS): ...@@ -445,7 +450,7 @@ class SFTPFS(FS):
attrs_map = dict((a.filename, a) for a in attrs) attrs_map = dict((a.filename, a) for a in attrs)
paths = attrs_map.keys() paths = attrs_map.keys()
except IOError, e: except IOError, e:
if getattr(e,"errno",None) == 2: if getattr(e,"errno",None) == ENOENT:
if self.isfile(path): if self.isfile(path):
raise ResourceInvalidError(path,msg="Can't list directory contents of a file: %(path)s") raise ResourceInvalidError(path,msg="Can't list directory contents of a file: %(path)s")
raise ResourceNotFoundError(path) raise ResourceNotFoundError(path)
...@@ -515,7 +520,7 @@ class SFTPFS(FS): ...@@ -515,7 +520,7 @@ class SFTPFS(FS):
try: try:
self.client.remove(npath) self.client.remove(npath)
except IOError, e: except IOError, e:
if getattr(e,"errno",None) == 2: if getattr(e,"errno",None) == ENOENT:
raise ResourceNotFoundError(path) raise ResourceNotFoundError(path)
elif self.isdir(path): elif self.isdir(path):
raise ResourceInvalidError(path,msg="Cannot use remove() on a directory: %(path)s") raise ResourceInvalidError(path,msg="Cannot use remove() on a directory: %(path)s")
...@@ -538,7 +543,7 @@ class SFTPFS(FS): ...@@ -538,7 +543,7 @@ class SFTPFS(FS):
try: try:
self.client.rmdir(npath) self.client.rmdir(npath)
except IOError, e: except IOError, e:
if getattr(e,"errno",None) == 2: if getattr(e,"errno",None) == ENOENT:
if self.isfile(path): if self.isfile(path):
raise ResourceInvalidError(path,msg="Can't use removedir() on a file: %(path)s") raise ResourceInvalidError(path,msg="Can't use removedir() on a file: %(path)s")
raise ResourceNotFoundError(path) raise ResourceNotFoundError(path)
...@@ -561,7 +566,7 @@ class SFTPFS(FS): ...@@ -561,7 +566,7 @@ class SFTPFS(FS):
try: try:
self.client.rename(nsrc,ndst) self.client.rename(nsrc,ndst)
except IOError, e: except IOError, e:
if getattr(e,"errno",None) == 2: if getattr(e,"errno",None) == ENOENT:
raise ResourceNotFoundError(src) raise ResourceNotFoundError(src)
if not self.isdir(dirname(dst)): if not self.isdir(dirname(dst)):
raise ParentDirectoryMissingError(dst) raise ParentDirectoryMissingError(dst)
...@@ -577,7 +582,7 @@ class SFTPFS(FS): ...@@ -577,7 +582,7 @@ class SFTPFS(FS):
try: try:
self.client.rename(nsrc,ndst) self.client.rename(nsrc,ndst)
except IOError, e: except IOError, e:
if getattr(e,"errno",None) == 2: if getattr(e,"errno",None) == ENOENT:
raise ResourceNotFoundError(src) raise ResourceNotFoundError(src)
if self.exists(dst): if self.exists(dst):
raise DestinationExistsError(dst) raise DestinationExistsError(dst)
...@@ -595,7 +600,7 @@ class SFTPFS(FS): ...@@ -595,7 +600,7 @@ class SFTPFS(FS):
try: try:
self.client.rename(nsrc,ndst) self.client.rename(nsrc,ndst)
except IOError, e: except IOError, e:
if getattr(e,"errno",None) == 2: if getattr(e,"errno",None) == ENOENT:
raise ResourceNotFoundError(src) raise ResourceNotFoundError(src)
if self.exists(dst): if self.exists(dst):
raise DestinationExistsError(dst) raise DestinationExistsError(dst)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment