Skip to content

Commit

Permalink
Execute one regex at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
shrikrishna committed Mar 23, 2018
1 parent af8b0e3 commit eba77f4
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions giturlparse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,44 +61,31 @@ def parse(self):
d = {
'pathname': None,
'protocols': self._get_protocols(),
'protocol': None,
'protocol': 'ssh',
'href': self._url,
'resource': None,
'user': None,
'port': None,
'name': None,
'owner': None,
}

regexp = (r'^(https?|git|ssh|rsync)\://'
'(?:(.+)@)*'
'([a-z0-9_.-]*)'
'[:/]*'
'([\d]+){0,1}'
'(/(.+)/(.+).git)')
m1 = re.search(regexp, self._url)

regexp = (r'^(?:(.+)@)*'
'([a-z0-9_.-]*)[:/]*'
'([\d]+){0,1}'
'([:/](.+)/(.+).git)')
m2 = re.search(regexp, self._url)

if m1:
d['pathname'] = m1.group(5)
d['protocol'] = m1.group(1)
d['resource'] = m1.group(3)
d['user'] = m1.group(2)
d['port'] = m1.group(4)
d['name'] = m1.group(7)
d['owner'] = m1.group(6)
elif m2:
d['pathname'] = re.sub(r'^:', '', m2.group(4))
d['protocol'] = 'ssh'
d['resource'] = m2.group(2)
d['user'] = m2.group(1)
d['name'] = m2.group(6)
d['owner'] = m2.group(5)
regexes = [
(r'^(?P<protocol>https?|git|ssh|rsync)\://'
'(?:(?P<user>.+)@)*'
'(?P<resource>[a-z0-9_.-]*)'
'[:/]*'
'(?P<port>[\d]+){0,1}'
'(?P<pathname>\/(?P<owner>.+)/(?P<name>.+).git)'),
(r'^(?:(?P<user>.+)@)*'
'(?P<resource>[a-z0-9_.-]*)[:/]*'
'(?P<port>[\d]+){0,1}'
'[:](?P<pathname>\/?(?P<owner>.+)/(?P<name>.+).git)')
]
for regex in regexes:
if re.search(regex, self._url):
m = re.search(regex, self._url)
d.update( m.groupdict() )
break
else:
msg = "Invalid URL '{}'".format(self._url)
raise ParserError(msg)
Expand Down

0 comments on commit eba77f4

Please sign in to comment.