From e9ba14c6862bcf55be56660c78a9376a2ad959ff Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Mon, 10 Oct 2016 23:28:22 +0200 Subject: [PATCH] Location parsing regexes: use verbose REs just added whitespace and comments, no semantic changes --- borg/helpers.py | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/borg/helpers.py b/borg/helpers.py index ef4b06874..24a8231a4 100644 --- a/borg/helpers.py +++ b/borg/helpers.py @@ -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_re = r""" + (?!:) # not starting with ":" + (?P([^:]|(:(?!:)))+) # 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[^/]+))?$' + optional_archive_re = r""" + (?: + :: # "::" as separator + (?P[^/]+) # archive name must not contain "/" + )?$""" # must match until the end + # regexes for misc. kinds of supported location specifiers: - ssh_re = re.compile(r'(?Pssh)://(?:(?P[^@]+)@)?' - r'(?P[^:/#]+)(?::(?P\d+))?' - + path_re + optional_archive_re) - file_re = re.compile(r'(?Pfile)://' - + path_re + optional_archive_re) + ssh_re = re.compile(r""" + (?Pssh):// # ssh:// + (?:(?P[^@]+)@)? # user@ (optional) + (?P[^:/#]+)(?::(?P\d+))? # host or host:port + """ + path_re + optional_archive_re, re.VERBOSE) # path or path::archive + + file_re = re.compile(r""" + (?Pfile):// # 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[^@]+)@)?(?P[^:/]+):)?' - + path_re + optional_archive_re) + scp_re = re.compile(r""" + ( + (?:(?P[^@]+)@)? # user@ (optional) + (?P[^:/]+): # 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