forked from release-engineering/pubtools-pulplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
35 lines (22 loc) · 926 Bytes
/
convert.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
import datetime
import six
from .attr import PULP2_PY_CONVERTER
def get_converter(field, value):
"""Given an attrs target field and an input value from Pulp,
return a converter function which should be used to convert the Pulp value
into a Python representation."""
metadata_converter = field.metadata.get(PULP2_PY_CONVERTER)
if metadata_converter:
# explicitly defined for this field, just return it
return metadata_converter
# Nothing explicitly defined, but check the types, there may still be
# some applicable default
if field.type is datetime.datetime and isinstance(value, six.string_types):
return read_timestamp
return null_convert
def null_convert(value):
return value
def read_timestamp(value):
return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
def write_timestamp(value):
return value.strftime("%Y-%m-%dT%H:%M:%SZ")