-
Notifications
You must be signed in to change notification settings - Fork 19
/
models.py
163 lines (137 loc) · 5.1 KB
/
models.py
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
154
155
156
157
158
159
160
161
162
163
COLORS = {
"UNFAVOURABLE": "b60205",
"NEGATIVE": "ff9f1c",
"NEUTRAL": "ffcc00",
"POSITIVE": "cfda2c",
"FAVOURABLE": "008672",
"BLACK": "000000",
"DARKER": "333333",
"DARK": "666666",
"MEDIUM": "999999",
"LIGHT": "cccccc",
"LIGHTER": "eeeeee",
"WHITE": "ffffff",
}
class Group:
"""
This model represents a group of labels. A group has some fixed parameters
- name, which may be prefixed to all child label names
- color, which acts as a fallback for child labels that do not specify one
- is_prefixed, which determines if group name is prefixed on child labels
- is_required, which determines if >=1 sub-label must be applied on issues
"""
def __init__(
self, color=None, is_prefixed=True, is_required=False, **kwargs
):
self.name = kwargs["name"]
self.color = color
self.is_prefixed = is_prefixed
self.is_required = is_required
self.labels = [] # This may or may not be populated, do not rely
def __str__(self):
return self.name
def __repr__(self):
return f"<Group '{self}'>"
class Label:
"""
This model represents a single label. A label is defined by four parameters
- name, which appears on the label
- description, which describes it in a little more detail
- emoji, which is a pictorial representation of the purpose of the label
- color, which is used as a background on the label element
A ``Label`` instance is associated to a ``Group`` instance by a many to one
relationship.
"""
def __init__(self, group=None, color=None, has_emoji_name=True, **kwargs):
self.name = kwargs["name"]
self.description = kwargs["description"]
self.emoji = kwargs["emoji"]
self.own_color = color
self.has_emoji_name = has_emoji_name
self.group = group
if group and self not in group.labels:
group.labels.append(self)
@property
def color(self):
"""
Return the color to use on the emoji label, given as a 6-digit
hexadecimal code without the prefix '#'. Labels can have their color
specified as a constant and if missing inherit color from the parent
group. If not resolved, the color defaults to pure black.
@return: the 6-digit hexadecimal code of the background color
"""
color = self.own_color
if color is None and self.group is not None:
color = self.group.color
elif color is None:
color = COLORS["BLACK"]
elif color in COLORS:
color = COLORS[color]
return color
@property
def qualified_name(self):
"""
Return the fully qualified name of the label. Most label groups prefix
the group name to the name of the label, separated by a dunder, as
indicated by the ``is_prefixed`` attribute on the associated ``Group``
instance.
@return: the fully qualified name of the label
"""
name = self.name
if self.group and self.group.is_prefixed:
name = f"{self.group}: {name}"
if self.has_emoji_name:
name = f"{self.emoji} {name}"
return name
@property
def emojified_description(self):
"""
TODO: Use this when GitHub supports Unicode in label descriptions
Get the description of the label prefixed with the emoji.
@return: the emoji-prefixed description
"""
return f"{self.emoji} {self.description}"
@property
def api_arguments(self):
"""
Get the dictionary of arguments to pass to the API for creating the
label. The API only accepts ``name``, ``color`` and ``description``.
@return: the API arguments as a dictionary
"""
return {
"name": self.qualified_name,
"color": self.color,
"description": self.description,
}
def __eq__(self, remote):
"""
Compare this instance with the corresponding PyGithub instance to
determine whether the two are equal.
@param remote: the PyGithub label instance to compare itself against
@return: whether the instance is equal to its remote counterpart
"""
return all(
[
self.qualified_name == remote.name,
self.color == remote.color,
self.description == remote.description,
]
)
def __ne__(self, remote):
"""
Compare this instance with the corresponding PyGithub instance to
determine whether the two are unequal and would need to be reconciled.
@param remote: the PyGithub label instance to compare itself against
@return: whether the instance is unequal to its remote counterpart
"""
return any(
[
self.qualified_name != remote.name,
self.color != remote.color,
self.description != remote.description,
]
)
def __str__(self):
return self.qualified_name
def __repr__(self):
return f"<Label '{self}'>"