Skip to content

Commit 0cdb159

Browse files
authored
Merge pull request #6 from sks444/regex-array
Execute one regex at a time
2 parents af8b0e3 + eba77f4 commit 0cdb159

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

giturlparse/parser.py

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,44 +61,31 @@ def parse(self):
6161
d = {
6262
'pathname': None,
6363
'protocols': self._get_protocols(),
64-
'protocol': None,
64+
'protocol': 'ssh',
6565
'href': self._url,
6666
'resource': None,
6767
'user': None,
6868
'port': None,
6969
'name': None,
7070
'owner': None,
7171
}
72-
73-
regexp = (r'^(https?|git|ssh|rsync)\://'
74-
'(?:(.+)@)*'
75-
'([a-z0-9_.-]*)'
76-
'[:/]*'
77-
'([\d]+){0,1}'
78-
'(/(.+)/(.+).git)')
79-
m1 = re.search(regexp, self._url)
80-
81-
regexp = (r'^(?:(.+)@)*'
82-
'([a-z0-9_.-]*)[:/]*'
83-
'([\d]+){0,1}'
84-
'([:/](.+)/(.+).git)')
85-
m2 = re.search(regexp, self._url)
86-
87-
if m1:
88-
d['pathname'] = m1.group(5)
89-
d['protocol'] = m1.group(1)
90-
d['resource'] = m1.group(3)
91-
d['user'] = m1.group(2)
92-
d['port'] = m1.group(4)
93-
d['name'] = m1.group(7)
94-
d['owner'] = m1.group(6)
95-
elif m2:
96-
d['pathname'] = re.sub(r'^:', '', m2.group(4))
97-
d['protocol'] = 'ssh'
98-
d['resource'] = m2.group(2)
99-
d['user'] = m2.group(1)
100-
d['name'] = m2.group(6)
101-
d['owner'] = m2.group(5)
72+
regexes = [
73+
(r'^(?P<protocol>https?|git|ssh|rsync)\://'
74+
'(?:(?P<user>.+)@)*'
75+
'(?P<resource>[a-z0-9_.-]*)'
76+
'[:/]*'
77+
'(?P<port>[\d]+){0,1}'
78+
'(?P<pathname>\/(?P<owner>.+)/(?P<name>.+).git)'),
79+
(r'^(?:(?P<user>.+)@)*'
80+
'(?P<resource>[a-z0-9_.-]*)[:/]*'
81+
'(?P<port>[\d]+){0,1}'
82+
'[:](?P<pathname>\/?(?P<owner>.+)/(?P<name>.+).git)')
83+
]
84+
for regex in regexes:
85+
if re.search(regex, self._url):
86+
m = re.search(regex, self._url)
87+
d.update( m.groupdict() )
88+
break
10289
else:
10390
msg = "Invalid URL '{}'".format(self._url)
10491
raise ParserError(msg)

0 commit comments

Comments
 (0)