-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
147 lines (112 loc) · 4.63 KB
/
__init__.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
"""Extended yap package for comment management.
With eyap, you can choose a backend to manage your comments. A key goal of
the eyap package is that you can write your code in a generic way and easily
switch the back-end. For example, you can use a simple file back-end with
something like like the following:
```
>>> import eyap, tempfile, os, shutil # Import some things used for demo.
>>> backend = 'file' # Use a simple file backend for demo.
>>> finfo = {'owner': 'emin63', 'realm': tempfile.mkdtemp(), 'topic': 'test'}
>>> comment_thread = eyap.Make.comment_thread('file', **finfo)
>>> comment_thread.add_comment('Testing things', allow_create=True)
>>> comment_thread.add_comment('is great!')
```
The code above will create a new comment thread and add a comment to it
while also creating the thread if it does not exist. Later, you could
access the thread via something like:
```
>>> print(str(comment_thread.lookup_comments())) # doctest: +ELLIPSIS
========================================
Subject: Testing things ...
Timestamp: ...
----------
Testing things
========================================
Subject: is great! ...
Timestamp: ...
----------
is great!
```
The main reason for the existence of something like eyap is so that you
could use pretty much the same exact code with a different backend. For
example, using 'github' instead of 'file' and setting realm/owner to the
organization/repo in github while providing a valid user/token for github
access will post or read an issue from github.
```
>>> ginfo = {'owner': 'emin63', 'realm': 'eyap', 'topic': 'Describe usage'}
>>> g_thread = eyap.Make.comment_thread('github', **ginfo)
>>> print(str(g_thread.lookup_comments())) # doctest: +ELLIPSIS
========================================
Subject: We need a simple description of how to u ...
Timestamp: ...
----------
We need a simple description of how to use eyap.
========================================
Subject: Start with top-level README.md ...
Timestamp: ...
----------
Start with top-level README.md
========================================
Subject: All done! ...
Timestamp: ...
----------
All done!
```
Note that in the above, we only read from github since we did not provide
any username or password. If you had a username and token or password, you
could post comments to github as well.
Plesae do *NOT* post to above issue; use your own repo for tests. :)
Finally, we cleanup the file based backend since we don't need it anymore.
```
>>> shutil.rmtree(finfo['realm']) # Remove the directory to cleanup test.
>>> os.path.exists(finfo['realm']) # Verify we cleaned up.
False
```
See the [README.md on
github](https://github.com/emin63/eyap/blob/master/README.md) for
further details on usage, how to create new backends, etc.
"""
import logging
import doctest
from eyap.core import comments, github_comments
try: # Try to import redis if possible
from eyap.core import redis_comments
except ImportError as prob: # Create mock class to complain about install.
msg = '\n'.join(['Could not import redis_comments because of error:',
str(prob), 'If the problem is that redis is missing,'
'Consider installing via `pip install redis`.'])
logging.debug(msg)
class ComplainOnRedis:
"Mock module to complain if we try to use redis backend."
class RedisCommentThread:
"Mock class to complain if we try to use redis backend."
def __init__(self, *args, **kwargs):
dummy = args, kwargs
raise Exception(msg)
redis_comments = ComplainOnRedis
class Make(object):
_known_backends = {
'file': comments.FileCommentThread,
'github': github_comments.GitHubCommentThread,
'redis': redis_comments.RedisCommentThread
}
@classmethod
def comment_thread(cls, backend, *args, **kwargs):
"""Create a comment thread for the desired backend.
:arg backend: String name of backend (e.g., 'file',
'github', 'redis', etc.).
:arg *args, **kwargs: Arguments to be passed to contructor
for that backend.
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
:returns: A CommentThread sub-class for the given backend.
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
PURPOSE: Some simple syntatic sugar for creating the desired
backend.
"""
ct_cls = cls._known_backends.get(backend)
if not ct_cls:
return None
return ct_cls(*args, **kwargs)
if __name__ == '__main__':
doctest.testmod()
print('Finished Tests')