File: column.py

package info (click to toggle)
python-irodsclient 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,352 kB
  • sloc: python: 16,650; xml: 525; sh: 104; awk: 5; sql: 3; makefile: 3
file content (161 lines) | stat: -rw-r--r-- 3,662 bytes parent folder | download
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
from datetime import datetime, timezone
from calendar import timegm


class QueryKey:

    def __init__(self, column_type):
        self.column_type = column_type

    def __lt__(self, other):
        return Criterion("<", self, other)

    def __le__(self, other):
        return Criterion("<=", self, other)

    def __eq__(self, other):
        return Criterion("=", self, other)

    def __ne__(self, other):
        return Criterion("<>", self, other)

    def __gt__(self, other):
        return Criterion(">", self, other)

    def __ge__(self, other):
        return Criterion(">=", self, other)


class Criterion:

    def __init__(self, op, query_key, value):
        self.op = op
        self.query_key = query_key
        self.value = value

    @property
    def irods_value(self):
        return self.query_key.column_type.to_irods(self.value)


class In(Criterion):

    def __init__(self, query_key, value):
        super(In, self).__init__("in", query_key, value)

    @property
    def irods_value(self):
        v = "("
        comma = ""
        for element in self.value:
            v += "{}'{}'".format(comma, element)
            comma = ","
        v += ")"
        return v


class Like(Criterion):

    def __init__(self, query_key, value):
        super(Like, self).__init__("like", query_key, value)


class NotLike(Criterion):

    def __init__(self, query_key, value):
        super(NotLike, self).__init__("not like", query_key, value)


class Between(Criterion):

    def __init__(self, query_key, value):
        super(Between, self).__init__("between", query_key, value)

    @property
    def irods_value(self):
        lower_bound, upper_bound = self.value
        return "{} {}".format(
            self.query_key.column_type.to_irods(lower_bound),
            self.query_key.column_type.to_irods(upper_bound),
        )


class Column(QueryKey):

    def __init__(self, column_type, icat_key, icat_id, min_version=(0, 0, 0)):
        self.icat_key = icat_key
        self.icat_id = icat_id
        self.min_version = min_version
        super(Column, self).__init__(column_type)

    def __repr__(self):
        return "<{}.{} {} {}>".format(
            self.__class__.__module__,
            self.__class__.__name__,
            self.icat_id,
            self.icat_key,
        )

    def __hash__(self):
        return hash((self.column_type, self.icat_key, self.icat_id))


class Keyword(QueryKey):

    def __init__(self, column_type, icat_key):
        self.icat_key = icat_key
        super(Keyword, self).__init__(column_type)


# consider renaming columnType
class ColumnType:

    @staticmethod
    def to_python(string):
        pass

    @staticmethod
    def to_irods(data):
        pass


class Integer(ColumnType):

    @staticmethod
    def to_python(string):
        return int(string)

    @staticmethod
    def to_irods(data):
        return "'{}'".format(data)


class String(ColumnType):

    @staticmethod
    def to_python(string):
        return string

    @staticmethod
    def to_irods(data):
        try:
            # Convert to Unicode string (aka decode)
            data = str(data, "utf-8", "replace")
        except TypeError:
            # Some strings are already Unicode so they do not need decoding
            pass
        return "'{}'".format(data)


class DateTime(ColumnType):

    @staticmethod
    def to_python(string):
        return datetime.fromtimestamp(int(string), timezone.utc)

    @staticmethod
    def to_irods(data):
        try:
            return "'{:0>11}'".format(timegm(data.utctimetuple()))
        except AttributeError:
            return "'{}'".format(data)