/
protocols.py
68 lines (57 loc) · 1.87 KB
/
protocols.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
""" Defines basic protocol interfaces for providing CRUD operations to/from arbitrary
data stores. Specific registered Protocol parsers are assigned by matching one or more
URI components (properties of a RestUri(ParseResult) object)::
* scheme
* hostname
* port
* path
"""
import os
from re import match
import sys
from seres import parsers
if sys.version_info.major == 2:
import urllib2 as urllib
from urllib import unquote
else:
import urllib.request as urllib
from urllib.parse import unquote
class Protocol(parsers.Parser):
@classmethod
def create(cls, ru, ptext):
# Pushes a new set of entries (as plaintext) to the given REST URI
raise NotImplementedError("This protocol parser has an unimplemented CREATE interface")
@classmethod
def read(cls, ru):
# Queries a set of entries from the given REST URI
raise NotImplementedError("This protocol parser has an unimplemented READ interface")
@classmethod
def update(cls, ru, ptext):
# Pushes a new set of entries (as plaintext) to the given REST URI
raise NotImplementedError("This protocol parser has an unimplemented UPDATE interface")
@classmethod
def delete(cls, ru):
# Deletes a set of entries from the given REST URI
raise NotImplementedError("This protocol parser has an unimplemented DELETE interface")
class LocalFile(Protocol):
@classmethod
def get_filters(cls):
f = super(LocalFile, cls).get_filters()
f['scheme'] = "^file$"
return f
@classmethod
def create(cls, ru, ptext):
with open(ru.get_full_path(), "w") as f:
f.write(ptext)
@classmethod
def read(cls, ru):
with open(ru.get_full_path(), "r") as f:
content = f.read()
return content
@classmethod
def update(cls, ru, ptext):
with open(ru.get_full_path(), "w") as f:
f.write(ptext)
@classmethod
def delete(cls, ru):
os.remove(ru.get_full_path())