-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.py
More file actions
93 lines (70 loc) · 2.33 KB
/
constants.py
File metadata and controls
93 lines (70 loc) · 2.33 KB
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
'''
Definition of constants
'''
# credit to https://stackoverflow.com/questions/2682745/how-do-i-create-a-constant-in-python?rq=1
from collections import OrderedDict
from copy import deepcopy
class Constants(object):
"""Container of constant"""
__slots__ = ('__dict__')
def __init__(self, **kwargs):
if list(filter(lambda x: not x.isupper(), kwargs)):
raise AttributeError('Constant name should be uppercase.')
super(Constants, self).__setattr__(
'__dict__',
OrderedDict(map(lambda x: (x[0], deepcopy(x[1])), kwargs.items()))
)
def sort(self, key=None, reverse=False):
super(Constants, self).__setattr__(
'__dict__',
OrderedDict(sorted(self.__dict__.items(), key=key, reverse=reverse))
)
def __getitem__(self, name):
return self.__dict__[name]
def __len__(self):
return len(self.__dict__)
def __iter__(self):
for name in self.__dict__:
yield name
def keys(self):
return list(self)
def __str__(self):
return str(list(self))
def __repr__(self):
return '<%s: %s>' % (self.__class__.__name__, str(self.__dict__))
def __dir__(self):
return list(self)
def __setattr__(self, name, value):
raise AttributeError("Immutable attribute")
def __delattr__(*_):
raise AttributeError("Immutable attribute")
DATABASE_RESPONSES = Constants(
SUCCESS = 'Success in',
ERROR = 'There was an error',
UPDATE = ' updating the database',
CREATE = ' creating a record in the database',
)
# url consts
GENERAL = Constants(
# API_URL = 'http://localhost:500',
API = '/api',
OAUTH = '/oauth',
REDIRECT_URI = 'http://localhost:500/api/callback',
FLASK_SECRET = 'FLASK_SECRET'
)
# Goodreads-specific consts
GOODREADS = Constants(
PROVIDER = 'goodreads',
KEY = 'GOODREADS_KEY',
SECRET = 'GOODREADS_SECRET',
BASE_URL = 'https://www.goodreads.com',
)
# OAuth endpoints given in the Goodreads API documentation
# https://www.goodreads.com/api/documentation
GOODREADS_URLS = Constants(
REQUEST_TOKEN_URL = GOODREADS.BASE_URL + GENERAL.OAUTH + '/request_token',
AUTHORIZE_URL = GOODREADS.BASE_URL + GENERAL.OAUTH + '/authorize',
ACCESS_TOKEN_URL = GOODREADS.BASE_URL + GENERAL.OAUTH + '/access_token',
GET_USER_ID = GOODREADS.BASE_URL + GENERAL.API +'/auth_user',
GET_ALL_USERS_BOOKS = GOODREADS.BASE_URL + '/review/list?format=xml&v=2',
)