-
Notifications
You must be signed in to change notification settings - Fork 2
/
process.py
237 lines (164 loc) · 5.73 KB
/
process.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""Pre- and post-processing of data prior to being input or output
# Overview
Assume all data is written as plain-text documents. Formatting occurs
within the plain-text and is interpreted based on the file-extension.
E.g. ".json" is run through json.loads() whilst a ".txt" is
run through str()
Processing occurs before/after data is read/written.
------------ ------------- ------------
| disk | ----> | postprocess | ----> | python |
|____________| |_____________| |____________|
------------ ------------- ------------
| python | ----> | preprocess | ----> | disk |
|____________| |_____________| |____________|
# Examples
When reading a plain-text document, processing means to first cast
the raw data to a string, str()
Reading a key/value store, such as Json, processing means running it
through json.loads() which converts the raw string into a Python dict.
Reading a Google Drive document, processing means authorising with
Google and downloading the document from the internet.
# Writing data
Open Folder is a file-based database. This means that at the end
of each write, a file must get written to disk. Whether it be
writing to Shotgun, Google Drive or Streaming data explicitly, a
record of said operation is always stored on disk.
When updating a document on Google Drive, it may not be necessary to
update the link on disk, and thus the disk-writing action may be skipped.
"""
from abc import ABCMeta, abstractmethod
import logging
import json
# from numbers import Number # Used to map dt to key ext.
# import ConfigParser
log = logging.getLogger('openmetadata.process')
def processoutgoing(raw, format):
"""Process outgoing data"""
process = mapping.get(format)
if not process:
return None
return process.outgoing(raw)
def processincoming(raw, format):
"""Process incoming data"""
process = mapping.get(format)
if not process:
return None
return process.incoming(raw)
def cast(raw, format):
"""Cast `raw` to datatype appropriate to `format`"""
process = mapping.get(format)
if not process:
return None
return process.cast(raw)
class AbstractFormat(object):
"""Required interface to each format"""
__metaclass__ = ABCMeta
@abstractmethod
def outgoing(cls, raw):
"""Process --> Written
`raw` is interpreted based on the given format and
may be of any datatype.
Output is given in an appropriate Python data-structure.
"""
pass
@abstractmethod
def incoming(cls, raw):
"""Process <-- Read"""
pass
@abstractmethod
def cast(cls, raw):
pass
class DotTxt(AbstractFormat):
@classmethod
def outgoing(cls, raw):
return str(raw or '')
@classmethod
def incoming(cls, raw):
return str(raw)
@classmethod
def cast(cls, raw):
return str(raw)
class DotMdw(DotTxt):
"""Markdown is identical to Txt"""
class DotJson(AbstractFormat):
@classmethod
def outgoing(self, raw):
processed = {}
try:
processed = json.dumps(raw, indent=4)
except ValueError as e:
log.debug(e)
except TypeError as e:
raise TypeError("Data corrupt | %s\n%s" % (raw, e))
return processed
@classmethod
def incoming(self, raw):
processed = json.loads(raw)
return processed
@classmethod
def cast(self, raw):
return
# class DotIni(AbstractFormat):
# @classmethod
# def outgoing(self, raw):
# config = ConfigParser.ConfigParser()
# config.optionxform = str # Case-sensitive
# config.read_string(raw)
# # Convert to dictionary
# data = {}
# for section in config.sections():
# data[section] = {}
# for option in config.options(section):
# data[section][option] = config.get(section, option)
# return data
# @classmethod
# def incoming(self, raw):
# pass
# class DotGdoc(AbstractFormat):
# @classmethod
# def outgoing(self, raw):
# raise NotImplementedError
# # Raw is a Gdoc data-structure
# link = raw.get('link')
# # Download document
# google = gdata.login(usr, pw)
# document = google.download(link)
# return document
# @classmethod
# def incoming(self, raw):
# raise NotImplementedError
# link = raw.get('link')
# data = raw.get('data')
# # Upload document
# google = gdata.login(usr, pw)
# google.upload(data, link)
# # Create what will eventually be written to disk.
# # E.g. {"url": link, "resource_id": "document:1Euj54DtjdkRFd"}
# gdoc = raw.dump()
# return gdoc
# Cast channel-extension to key-extension
channel_to_file = {
'.kvs': '.json',
'.txt': '.txt',
'.mdw': '.txt',
}
mapping = {
'.txt': DotTxt,
'.mdw': DotMdw,
'.json': DotJson
# '.ini': DotIni,
# '.gdoc': DotGdoc
}
if __name__ == '__main__':
import openmetadata as om
path = r'A:\development\marcus\scripts\python\about\test\.meta\chan4.kvs\properties.json'
key = om.Key(path)
key.read()
print key.path
print key.exists
print key.data
# inputted = {"Key": "Value"}
# asstring = processoutgoing(inputted, '.json')
# asdict = processincoming(asstring, '.json')
# print asstring
# print asdict