forked from nephila/giturlparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.py
More file actions
153 lines (121 loc) · 3.3 KB
/
Copy pathresult.py
File metadata and controls
153 lines (121 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from copy import copy
from .platforms import PLATFORMS
# Possible values to extract from a Git Url
REQUIRED_ATTRIBUTES = (
"domain",
"repo",
)
class GitUrlParsed:
platform = None
def __init__(self, parsed_info):
self._parsed = parsed_info
# Set parsed objects as attributes
for k, v in parsed_info.items():
setattr(self, k, v)
for name, platform in PLATFORMS:
if name == self.platform:
self._platform_obj = platform
break
def _valid_attrs(self):
return all([getattr(self, attr, None) for attr in REQUIRED_ATTRIBUTES]) # NOQA
@property
def valid(self):
return all(
[
self._valid_attrs(),
]
)
##
# Alias properties
##
def _update_url(self):
protocol = getattr(self, "protocol", None)
if protocol:
self.url = self.format(protocol)
self._parsed["url"] = self.format(protocol)
@property
def host(self):
return self.domain
@property
def resource(self):
return self.domain
@property
def name(self):
return self.repo
@name.setter
def name(self, new_name):
self.repo = new_name
self._parsed["repo"] = new_name
self._update_url()
@property
def owner(self):
return self._parsed["owner"]
@owner.setter
def owner(self, new_owner):
self._parsed["owner"] = new_owner
self._update_url()
@property
def user(self):
if hasattr(self, "_user"):
return self._user
return self.owner
@property
def groups(self):
if self.groups_path:
return self.groups_path.split("/")
else:
return []
def format(self, protocol): # noqa : A0003
"""Reformat URL to protocol."""
items = copy(self._parsed)
items["port_slash"] = "%s/" % self.port if self.port else ""
items["groups_slash"] = "%s/" % self.groups_path if self.groups_path else ""
items["dot_git"] = "" if items["repo"].endswith(".git") else ".git"
return self._platform_obj.FORMATS[protocol] % items
@property
def normalized(self):
"""Normalize URL."""
return self.format(self.protocol)
##
# Rewriting
##
@property
def url2ssh(self):
return self.format("ssh")
@property
def url2http(self):
return self.format("http")
@property
def url2https(self):
return self.format("https")
@property
def url2git(self):
return self.format("git")
# All supported Urls for a repo
@property
def urls(self):
return {protocol: self.format(protocol) for protocol in self._platform_obj.PROTOCOLS}
##
# Platforms
##
@property
def github(self):
return self.platform == "github"
@property
def bitbucket(self):
return self.platform == "bitbucket"
@property
def friendcode(self):
return self.platform == "friendcode"
@property
def assembla(self):
return self.platform == "assembla"
@property
def gitlab(self):
return self.platform == "gitlab"
##
# Get data as dict
##
@property
def data(self):
return dict(self._parsed)