This repository has been archived by the owner on Apr 21, 2019. It is now read-only.
/
main_legacy.py
215 lines (146 loc) · 7.52 KB
/
main_legacy.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
#
# =====================================================
# |==| PROVIDENCE/CLARITY DATA ANALYSIS PLATFORM |==|
# |==| ----------------------------------- |==|
# |==| Author: Sam Gammon <sg@samgammon.com> |==|
# |==| Version: 0.1 DEV |==|
# |==| ------------------------------------------- |==|
# |==| COPYRIGHT (c) 2010. ALL RIGHTS RESERVED |==|
# =====================================================
#
import os
import logging
import exceptions
from google.appengine.api import namespace_manager
# Master meta-class for all P/C-related classes
class ProvidenceClarityObject(type):
def __call__(metacls, name, bases, dictionary):
return super(ProvidenceClarityObject, metacls).__call__(name, bases, dictionary)
# Controls the creation of the Platform class and object
class ProvidenceClarityPlatform(type):
__metaclass__ = ProvidenceClarityObject
def __new__(meta, classname, bases, classDict):
return type.__new__(meta, classname, bases, classDict)
# Controls the loading and creation of PC Controllers and proxies
class ProvidenceClarityController(type):
__metaclass__ = ProvidenceClarityObject
def __new__(meta, classname, bases, classDict):
return type.__new__(meta, classname, bases, classDict)
# Proxies a PC controller into the platform object
class PCControllerProxy(object):
__metaclass__ = ProvidenceClarityController
c_class = None
def __init__(self, controller):
if isinstance(controller, ProvidenceClarityController):
self.c_class = controller
def __get__(self, instance, owner):
pass
def __set__(self, instance, value):
raise NotImplemented('Platform controller properties are read-only.')
def __delete__(self, instance):
raise NotImplemented('Platform controller properties are read-only.')
# Providence Clarity Platform!
class Platform(object):
"""
============================= Providence/Clarity =============================
Main Class:
Everything is initialized and operated on from this master class,
which stores references to internal APIs and handlers.
Properties:
-- version: Stores a dict of version values for P/C.
-- full: A string of the system's full version. (ex: 0.5.131 DEV, r: string)
-- major: The system's 'major' version. (ex: 0.5, r: int/float)
-- minor: The system's 'minor' version. (ex: 131, r: int/float)
-- build: The system's build release type. (ex: DEV, r: string)
-- config: Stores a ref to P/C's config module (most useful for config.get, r: module)
-- config_path: Stores a path to P/C's config module. (r: string)
-- namespace: The current operating namespace for GAE APIs. (r: string)
-- api: Stores references to loaded API controllers. (r: dynamic module)
-- services: Stores references to loaded service controllers. (r: module)
Keyword Parameters:
-- namespace: Sets the current namespace to this string value.
"""
__metaclass__ = ProvidenceClarityPlatform
## ===== 1: Properties (Initialized Externally)
version = None
namespace = None
config = None
config_path = None
## ===== 2: Internals
__log_map = logging._levelNames
## ===== 3: Internal Methods
def __init__(self,config=False,**kwargs):
if config is False:
raise exceptions.ConfigRequired()
else:
self.config = config
# :: Namespace is usually extracted from subdomain - set via initialized keyword parameter
if self.config.get('enable','multitenancy',False):
_m_log = self.config.get('logging','multitenancy',False)
if self.config.get('force_namespace','multitenancy', False):
if isinstance(self.config.get('force_namespace','multitenancy',False), (str, unicode)) and self.config.get('force_namespace','multitenancy',False) is not '':
if namespace_manager.validate_namespace(self.config.get('force_namespace','multitenancy',False)):
if _m_log: logging.info('[i]: Setting request namespace to \''+config_get('force_namespace','multitenancy',False)+'\'.')
namespace_manager.set_namespace(self.config.get('force_namespace','multitenancy',False))
else:
if 'namespace' in kwargs or self.config.get('apps_mode_force', 'multitenancy', False):
if kwargs['namespace'] == self.config.get('apps_subdomain','multitenancy','apps') or self.config.get('apps_mode_force', 'multitenancy', False):
if _m_log: logging.info('[i]: Setting request namespace to \''+namespace_manager.google_apps_namespace()+'\' per APPS procedure.')
namespace_manager.set_namespace(namespace_manager.google_apps_namespace())
else:
if isinstance(kwargs['namespace'], (str, unicode)) and namespace_manager.validate_namespace(kwargs['namespace']):
if _m_log: logging.info('[i]: Setting request namespace to \''+kwargs['namespace']+'\' per domain split procedure.')
namespace_manager.set_namespace(kwargs['namespace'])
else:
if kwargs['namespace'] is not None:
if _m_log: logging.info('[i]: Given namespace \''+str(kwargs['namespace'])+'\' failed to pass validation. Ignoring.')
if 'version' in kwargs:
self.version = kwargs['version']
super(Platform, self).__init__()
#def __repr__(self):
# return '<ProvidenceClarity '
## ===== 4: Class Methods
@classmethod
def config(cls):
return cls.config
@classmethod
def version(cls):
return cls.version
## Exception Master
class PCException(Exception):
message = None
def __init__(self, msg=None):
self.message = msg
## Master Controller Object
class PCController(object):
pass
# Log controller for logging
class PCLogController(PCController):
# Logger wrapper, based on current config
def log(self,message_i, level='debug'):
if __log_map[str(self.config().get('threshold','logging')).upper()] >= __log_map[str(level).upper()]:
handler = self.config().get('handler','logging')
message = str(message_i)
if self.config().get('tag','logging') == True:
message = message + ' (Providence/Clarity v'+str(self.version)+')'
result = getattr(handler,str(level).lower())(message)
else:
return None
# Log wrapper for debug
def debug(self, message):
return self.log(message,'debug')
# Log wrapper for info
def info(self, message):
return self.log(message,'info')
# Log wrapper for warning
def warning(self, message):
return self.log(message,'warning')
# Log wrapper for error
def error(self, message):
return self.log(message,'error')
# Log wrapper for critical
def critical(self, message):
return self.log(message,'critical')
# Log wrapper for exit
def exit(self, message):
return self.log(message,'exit')