Location parsing regexes: use verbose REs

just added whitespace and comments, no semantic changes
This commit is contained in:
Thomas Waldmann 2016-10-10 23:28:22 +02:00
parent cf0359eba7
commit e9ba14c686

View file

@ -768,27 +768,49 @@ class Location:
"""Object representing a repository / archive location
"""
proto = user = host = port = path = archive = None
# path may not contain :: (it ends at :: or string end), but may contain single colons.
# path must not contain :: (it ends at :: or string end), but may contain single colons.
# to avoid ambiguities with other regexes, it must also not start with ":".
path_re = r'(?!:)(?P<path>([^:]|(:(?!:)))+)'
path_re = r"""
(?!:) # not starting with ":"
(?P<path>([^:]|(:(?!:)))+) # any chars, but no "::"
"""
# optional ::archive_name at the end, archive name must not contain "/".
# borg mount's FUSE filesystem creates one level of directories from
# the archive names and of course "/" is not valid in a directory name.
optional_archive_re = r'(?:::(?P<archive>[^/]+))?$'
optional_archive_re = r"""
(?:
:: # "::" as separator
(?P<archive>[^/]+) # archive name must not contain "/"
)?$""" # must match until the end
# regexes for misc. kinds of supported location specifiers:
ssh_re = re.compile(r'(?P<proto>ssh)://(?:(?P<user>[^@]+)@)?'
r'(?P<host>[^:/#]+)(?::(?P<port>\d+))?'
+ path_re + optional_archive_re)
file_re = re.compile(r'(?P<proto>file)://'
+ path_re + optional_archive_re)
ssh_re = re.compile(r"""
(?P<proto>ssh):// # ssh://
(?:(?P<user>[^@]+)@)? # user@ (optional)
(?P<host>[^:/#]+)(?::(?P<port>\d+))? # host or host:port
""" + path_re + optional_archive_re, re.VERBOSE) # path or path::archive
file_re = re.compile(r"""
(?P<proto>file):// # file://
""" + path_re + optional_archive_re, re.VERBOSE) # path or path::archive
# note: scp_re is also use for local pathes
scp_re = re.compile(r'((?:(?P<user>[^@]+)@)?(?P<host>[^:/]+):)?'
+ path_re + optional_archive_re)
scp_re = re.compile(r"""
(
(?:(?P<user>[^@]+)@)? # user@ (optional)
(?P<host>[^:/]+): # host: (don't match / in host to disambiguate from file:)
)? # user@host: part is optional
""" + path_re + optional_archive_re, re.VERBOSE) # path with optional archive
# get the repo from BORG_REPO env and the optional archive from param.
# if the syntax requires giving REPOSITORY (see "borg mount"),
# use "::" to let it use the env var.
# if REPOSITORY argument is optional, it'll automatically use the env.
env_re = re.compile(r'(?:::$)|' + optional_archive_re)
env_re = re.compile(r""" # the repo part is fetched from BORG_REPO
(?:::$) # just "::" is ok (when a pos. arg is required, no archive)
| # or
""" + optional_archive_re, re.VERBOSE) # archive name (optional, may be empty)
def __init__(self, text=''):
self.orig = text