Python源码示例:fnmatch.html()
示例1
def match(path: Path, pattern_list: Iterable) -> bool:
"""
Check if a Path matches to one of the patterns
Internally fnmatch is used.
See https://docs.python.org/3/library/fnmatch.html for details.
Arguments:
path (Path): Path to check if it matches to one of the patterns
pattern_list (iterable): Iterable (e.g tuple or list) of patterns to
match against the path
Returns:
Boolean: True if path matches a pattern of the list
"""
for pattern in pattern_list:
if fnmatch.fnmatch(str(path), pattern):
return True
return False
示例2
def ftp_MDTM(self, path):
"""
File Modification Time (MDTM)
The FTP command, MODIFICATION TIME (MDTM), can be used to determine
when a file in the server NVFS was last modified. This command has
existed in many FTP servers for many years, as an adjunct to the REST
command for STREAM mode, thus is widely available. However, where
supported, the "modify" fact that can be provided in the result from
the new MLST command is recommended as a superior alternative.
http://tools.ietf.org/html/rfc3659
"""
try:
newsegs = toSegments(self.workingDirectory, path)
except InvalidPath:
return defer.fail(FileNotFoundError(path))
def cbStat(result):
(modified,) = result
return (FILE_STATUS, time.strftime('%Y%m%d%H%M%S', time.gmtime(modified)))
return self.shell.stat(newsegs, ('modified',)).addCallback(cbStat)
示例3
def ftp_MDTM(self, path):
"""
File Modification Time (MDTM)
The FTP command, MODIFICATION TIME (MDTM), can be used to determine
when a file in the server NVFS was last modified. This command has
existed in many FTP servers for many years, as an adjunct to the REST
command for STREAM mode, thus is widely available. However, where
supported, the "modify" fact that can be provided in the result from
the new MLST command is recommended as a superior alternative.
http://tools.ietf.org/html/rfc3659
"""
try:
newsegs = toSegments(self.workingDirectory, path)
except InvalidPath:
return defer.fail(FileNotFoundError(path))
def cbStat(result):
(modified,) = result
return (FILE_STATUS, time.strftime('%Y%m%d%H%M%S', time.gmtime(modified)))
return self.shell.stat(newsegs, ('modified',)).addCallback(cbStat)
示例4
def _encodeName(self, name):
"""
Encode C{name} to be sent over the wire.
This encodes L{unicode} objects as UTF-8 and leaves L{bytes} as-is.
As described by U{RFC 3659 section
2.2<https://tools.ietf.org/html/rfc3659#section-2.2>}::
Various FTP commands take pathnames as arguments, or return
pathnames in responses. When the MLST command is supported, as
indicated in the response to the FEAT command, pathnames are to be
transferred in one of the following two formats.
pathname = utf-8-name / raw
utf-8-name = <a UTF-8 encoded Unicode string>
raw = <any string that is not a valid UTF-8 encoding>
Which format is used is at the option of the user-PI or server-PI
sending the pathname.
@param name: Name to be encoded.
@type name: L{bytes} or L{unicode}
@return: Wire format of C{name}.
@rtype: L{bytes}
"""
if isinstance(name, unicode):
return name.encode('utf-8')
return name
示例5
def ftp_SIZE(self, path):
"""
File SIZE
The FTP command, SIZE OF FILE (SIZE), is used to obtain the transfer
size of a file from the server-FTP process. This is the exact number
of octets (8 bit bytes) that would be transmitted over the data
connection should that file be transmitted. This value will change
depending on the current STRUcture, MODE, and TYPE of the data
connection or of a data connection that would be created were one
created now. Thus, the result of the SIZE command is dependent on
the currently established STRU, MODE, and TYPE parameters.
The SIZE command returns how many octets would be transferred if the
file were to be transferred using the current transfer structure,
mode, and type. This command is normally used in conjunction with
the RESTART (REST) command when STORing a file to a remote server in
STREAM mode, to determine the restart point. The server-PI might
need to read the partially transferred file, do any appropriate
conversion, and count the number of octets that would be generated
when sending the file in order to correctly respond to this command.
Estimates of the file transfer size MUST NOT be returned; only
precise information is acceptable.
http://tools.ietf.org/html/rfc3659
"""
try:
newsegs = toSegments(self.workingDirectory, path)
except InvalidPath:
return defer.fail(FileNotFoundError(path))
def cbStat(result):
(size,) = result
return (FILE_STATUS, str(size))
return self.shell.stat(newsegs, ('size',)).addCallback(cbStat)
示例6
def ftp_FEAT(self):
"""
Advertise the features supported by the server.
http://tools.ietf.org/html/rfc2389
"""
self.sendLine(RESPONSE[FEAT_OK][0])
for feature in self.FEATURES:
self.sendLine(' ' + feature)
self.sendLine(RESPONSE[FEAT_OK][1])
示例7
def ftp_OPTS(self, option):
"""
Handle OPTS command.
http://tools.ietf.org/html/draft-ietf-ftpext-utf-8-option-00
"""
return self.reply(OPTS_NOT_IMPLEMENTED, option)
示例8
def queueLogin(self, username, password):
"""
Login: send the username, send the password.
If the password is L{None}, the PASS command won't be sent. Also, if
the response to the USER command has a response code of 230 (User logged
in), then PASS won't be sent either.
"""
# Prepare the USER command
deferreds = []
userDeferred = self.queueStringCommand('USER ' + username, public=0)
deferreds.append(userDeferred)
# Prepare the PASS command (if a password is given)
if password is not None:
passwordCmd = FTPCommand('PASS ' + password, public=0)
self.queueCommand(passwordCmd)
deferreds.append(passwordCmd.deferred)
# Avoid sending PASS if the response to USER is 230.
# (ref: http://cr.yp.to/ftp/user.html#user)
def cancelPasswordIfNotNeeded(response):
if response[0].startswith('230'):
# No password needed!
self.actionQueue.remove(passwordCmd)
return response
userDeferred.addCallback(cancelPasswordIfNotNeeded)
# Error handling.
for deferred in deferreds:
# If something goes wrong, call fail
deferred.addErrback(self.fail)
# But also swallow the error, so we don't cause spurious errors
deferred.addErrback(lambda x: None)
示例9
def _encodeName(self, name):
"""
Encode C{name} to be sent over the wire.
This encodes L{unicode} objects as UTF-8 and leaves L{bytes} as-is.
As described by U{RFC 3659 section
2.2<https://tools.ietf.org/html/rfc3659#section-2.2>}::
Various FTP commands take pathnames as arguments, or return
pathnames in responses. When the MLST command is supported, as
indicated in the response to the FEAT command, pathnames are to be
transferred in one of the following two formats.
pathname = utf-8-name / raw
utf-8-name = <a UTF-8 encoded Unicode string>
raw = <any string that is not a valid UTF-8 encoding>
Which format is used is at the option of the user-PI or server-PI
sending the pathname.
@param name: Name to be encoded.
@type name: L{bytes} or L{unicode}
@return: Wire format of C{name}.
@rtype: L{bytes}
"""
if isinstance(name, unicode):
return name.encode('utf-8')
return name
示例10
def ftp_SIZE(self, path):
"""
File SIZE
The FTP command, SIZE OF FILE (SIZE), is used to obtain the transfer
size of a file from the server-FTP process. This is the exact number
of octets (8 bit bytes) that would be transmitted over the data
connection should that file be transmitted. This value will change
depending on the current STRUcture, MODE, and TYPE of the data
connection or of a data connection that would be created were one
created now. Thus, the result of the SIZE command is dependent on
the currently established STRU, MODE, and TYPE parameters.
The SIZE command returns how many octets would be transferred if the
file were to be transferred using the current transfer structure,
mode, and type. This command is normally used in conjunction with
the RESTART (REST) command when STORing a file to a remote server in
STREAM mode, to determine the restart point. The server-PI might
need to read the partially transferred file, do any appropriate
conversion, and count the number of octets that would be generated
when sending the file in order to correctly respond to this command.
Estimates of the file transfer size MUST NOT be returned; only
precise information is acceptable.
http://tools.ietf.org/html/rfc3659
"""
try:
newsegs = toSegments(self.workingDirectory, path)
except InvalidPath:
return defer.fail(FileNotFoundError(path))
def cbStat(result):
(size,) = result
return (FILE_STATUS, str(size))
return self.shell.stat(newsegs, ('size',)).addCallback(cbStat)
示例11
def ftp_FEAT(self):
"""
Advertise the features supported by the server.
http://tools.ietf.org/html/rfc2389
"""
self.sendLine(RESPONSE[FEAT_OK][0])
for feature in self.FEATURES:
self.sendLine(' ' + feature)
self.sendLine(RESPONSE[FEAT_OK][1])
示例12
def ftp_OPTS(self, option):
"""
Handle OPTS command.
http://tools.ietf.org/html/draft-ietf-ftpext-utf-8-option-00
"""
return self.reply(OPTS_NOT_IMPLEMENTED, option)
示例13
def queueLogin(self, username, password):
"""
Login: send the username, send the password.
If the password is L{None}, the PASS command won't be sent. Also, if
the response to the USER command has a response code of 230 (User logged
in), then PASS won't be sent either.
"""
# Prepare the USER command
deferreds = []
userDeferred = self.queueStringCommand('USER ' + username, public=0)
deferreds.append(userDeferred)
# Prepare the PASS command (if a password is given)
if password is not None:
passwordCmd = FTPCommand('PASS ' + password, public=0)
self.queueCommand(passwordCmd)
deferreds.append(passwordCmd.deferred)
# Avoid sending PASS if the response to USER is 230.
# (ref: http://cr.yp.to/ftp/user.html#user)
def cancelPasswordIfNotNeeded(response):
if response[0].startswith('230'):
# No password needed!
self.actionQueue.remove(passwordCmd)
return response
userDeferred.addCallback(cancelPasswordIfNotNeeded)
# Error handling.
for deferred in deferreds:
# If something goes wrong, call fail
deferred.addErrback(self.fail)
# But also swallow the error, so we don't cause spurious errors
deferred.addErrback(lambda x: None)