forked from aboutcode-org/commoncode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatautils.py
More file actions
214 lines (187 loc) · 5.35 KB
/
datautils.py
File metadata and controls
214 lines (187 loc) · 5.35 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/commoncode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import attr
from attr.validators import in_ as choices # NOQA
import typing
"""
Utilities and helpers for data classes.
"""
HELP_METADATA = '__field_help'
LABEL_METADATA = '__field_label'
def attribute(default=attr.NOTHING, validator=None,
repr=False, eq=True, order=True, # NOQA
init=True, type=None, converter=None, # NOQA
help=None, label=None, metadata=None,): # NOQA
"""
A generic attribute with help metadata and that is not included in the
representation by default.
"""
metadata = metadata or dict()
if help:
metadata[HELP_METADATA] = help
if label:
metadata[LABEL_METADATA] = label
return attr.attrib(
default=default,
validator=validator,
repr=repr,
eq=eq,
order=order,
init=init,
metadata=metadata,
type=type,
converter=converter
)
def Boolean(default=False, validator=None, repr=False, eq=True, order=True, # NOQA
converter=None, label=None, help=None,): # NOQA
"""
A boolean attribute.
"""
return attribute(
default=default,
validator=validator,
repr=repr,
eq=eq,
order=order,
init=True,
type=bool,
converter=converter,
help=help,
label=label,
)
def TriBoolean(default=None, validator=None, repr=False, eq=True, order=True, # NOQA
converter=None, label=None, help=None,): # NOQA
"""
A tri-boolean attribute with possible values of None, True and False.
"""
return attribute(
default=default,
validator=validator,
repr=repr,
eq=eq,
order=order,
init=True,
type=bool,
converter=converter,
help=help,
label=label,
)
def String(default=None, validator=None, repr=False, eq=True, order=True, # NOQA
converter=None, label=None, help=None,): # NOQA
"""
A string attribute.
"""
return attribute(
default=default,
validator=validator,
repr=repr,
eq=eq,
order=order,
init=True,
type=str,
converter=converter,
help=help,
label=label,
)
def Integer(default=0, validator=None, repr=False, eq=True, order=True, # NOQA
converter=None, label=None, help=None,): # NOQA
"""
An integer attribute.
"""
converter = converter or attr.converters.optional(int)
return attribute(
default=default,
validator=validator,
repr=repr,
eq=eq,
order=order,
init=True,
type=int,
converter=converter,
help=help,
label=label,
)
def Float(default=0.0, validator=None, repr=False, eq=True, order=True, # NOQA
converter=None, label=None, help=None,): # NOQA
"""
A float attribute.
"""
return attribute(
default=default,
validator=validator,
repr=repr,
eq=eq,
order=order,
init=True,
type=float,
converter=converter,
help=help,
label=label,
)
def List(item_type=typing.Any, default=attr.NOTHING, validator=None,
repr=False, eq=True, order=True, # NOQA
converter=None, label=None, help=None,): # NOQA
"""
A list attribute: the optional item_type defines the type of items it stores.
"""
if default is attr.NOTHING:
default = attr.Factory(list)
return attribute(
default=default,
validator=validator,
repr=repr,
eq=eq,
order=order,
init=True,
type=typing.List[item_type],
converter=converter,
help=help,
label=label,
)
def Mapping(value_type=typing.Any, default=attr.NOTHING, validator=None,
repr=False, eq=True, order=True, # NOQA
converter=None, help=None, label=None): # NOQA
"""
A mapping attribute: the optional value_type defines the type of values it
stores. The key is always a string.
Notes: in Python 2 the type is Dict as there is no typing available for
dict for now.
"""
if default is attr.NOTHING:
default = attr.Factory(dict)
return attribute(
default=default,
validator=validator,
repr=repr,
eq=eq,
order=order,
init=True,
type=typing.Dict[str, value_type],
converter=converter,
help=help,
label=label,
)
##################################################
# FIXME: add proper support for dates!!!
##################################################
def Date(default=None, validator=None, repr=False, eq=True, order=True, # NOQA
converter=None, label=None, help=None,): # NOQA
"""
A date attribute. It always serializes to an ISO date string.
Behavior is TBD and for now this is exactly a string.
"""
return String(
default=default,
validator=validator,
repr=repr,
eq=eq,
order=order,
converter=converter,
help=help,
label=label,
)