/
base.py
3623 lines (3073 loc) · 146 KB
/
base.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Hive Appier Framework
# Copyright (c) 2008-2016 Hive Solutions Lda.
#
# This file is part of Hive Appier Framework.
#
# Hive Appier Framework is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Appier Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Appier Framework. If not, see <http://www.apache.org/licenses/>.
__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """
__version__ = "1.0.0"
""" The version of the module """
__revision__ = "$LastChangedRevision$"
""" The revision number of the module """
__date__ = "$LastChangedDate$"
""" The last change date of the module """
__copyright__ = "Copyright (c) 2008-2016 Hive Solutions Lda."
""" The copyright for the module """
__license__ = "Apache License, Version 2.0"
""" The license for the module """
import os
import re
import sys
import imp
import time
import json
import uuid
import atexit
import locale
import inspect
import datetime
import itertools
import mimetypes
import threading
import traceback
import logging.handlers
from . import log
from . import http
from . import meta
from . import util
from . import data
from . import smtp
from . import async
from . import cache
from . import model
from . import config
from . import legacy
from . import session
from . import request
from . import defines
from . import compress
from . import settings
from . import observer
from . import controller
from . import structures
from . import exceptions
APP = None
""" The global reference to the application object this
should be a singleton object and so no multiple instances
of an app may exist in the same process """
LEVEL = None
""" The global reference to the (parsed/processed) debug
level that is going to be used for some core assumptions
in situations where no app is created (eg: api clients) """
NAME = "appier"
""" The name to be used to describe the framework while working
on its own environment, this is just a descriptive value """
VERSION = "1.5.34"
""" The version of the framework that is currently installed
this value may be used for debugging/diagnostic purposes """
PLATFORM = "%s %d.%d.%d.%s %s" % (
sys.subversion[0] if hasattr(sys, "subversion") else "CPython",
sys.version_info[0],
sys.version_info[1],
sys.version_info[2],
sys.version_info[3],
sys.platform
)
""" Extra system information containing some of the details
of the technical platform that is running the system, this
string should be exposed carefully to avoid extra information
from being exposed to outside agents """
API_VERSION = 1
""" The incremental version number that may be used to
check on the level of compatibility for the api """
BUFFER_SIZE = 4096
""" The size of the buffer so be used while sending data using
the static file serving approach (important for performance) """
MAX_LOG_SIZE = 524288
""" The maximum amount of bytes for a log file created by
the rotating file handler, after this value is reached a
new file is created for the buffering of the results """
MAX_LOG_COUNT = 5
""" The maximum number of files stores as backups for the
rotating file handler, note that these values are stored
just for extra debugging purposes """
RUNNING = "running"
""" The running state for the app, indicating that the
complete api set is being served correctly """
STOPPED = "stopped"
""" The stopped state for the app, indicating that some
of the api components may be down """
REPLACE_REGEX = re.compile("(?<!\(\?P)\<((\w+)(\([\"'].*?[\"']\))?:)?(\w+)\>")
""" The regular expression to be used in the replacement
of the capture groups for the urls, this regex will capture
any named group not changed until this stage (eg: int,
string, regex, etc.) """
INT_REGEX = re.compile("\<int:(\w+)\>")
""" The regular expression to be used in the replacement
of the integer type based groups for the urls """
REGEX_REGEX = re.compile("\<regex\([\"'](.*?)[\"']\):(\w+)\>")
""" Regular expression that is going to be used for the
replacement of regular expression types with the proper
group in the final url based route regex """
BODYLESS_METHODS = (
"GET",
"HEAD",
"OPTIONS",
"DELETE"
)
""" The sequence that defines the complete set of
http methods that are considered to be bodyless,
meaning that no contents should be expected under
it's body, content length should be zero """
ESCAPE_EXTENSIONS = (
".xml",
".html",
".xhtml",
".liquid",
".xml.tpl",
".html.tpl",
".xhtml.tpl"
)
""" The sequence containing the various extensions
for which the autoescape mode will be enabled by
default as expected by the end developer """
TYPES_R = dict(
int = int,
str = legacy.UNICODE,
regex = legacy.UNICODE
)
""" Map that resolves a data type from the string representation
to the proper type value to be used in casting """
EXCLUDED_NAMES = (
"server",
"host",
"port",
"ssl",
"key_file",
"cer_file"
)
""" The sequence that contains the names that are considered
excluded from the auto parsing of parameters """
EMPTY_METHODS = (
"HEAD",
)
""" Sequence containing the complete set of http methods, that
should have an empty body as defined by http specification """
BASE_HEADERS = (
("X-Powered-By", "%s/%s" % (NAME, VERSION)),
)
""" The sequence containing the headers considered to be basic
and that are going to be applied to all of the requests received
by the appier framework (water marking each of the requests) """
REQUEST_LOCK = threading.RLock()
""" The lock to be used in the application handling of request
so that no two request get handled at the same time for the current
app instance, as that would create some serious problems """
CASTERS = {
list : lambda v: [y for y in itertools.chain(*[x.split(",") for x in v])],
bool : lambda v: v if type(v) == bool else\
not v in ("", "0", "false", "False")
}
""" The map associating the various data types with a proper custom
caster to be used for special data types (more complex) under some
of the simple casting operations """
CASTER_MULTIPLE = {
list : True
}
""" Map that associates the various data type values with the proper
value for the multiple (fields) for the (get) field operation, this
way it's possible to defined a pre-defined multiple value taking into
account the target data type """
class App(
legacy.with_meta(
meta.Indexed,
observer.Observable,
compress.Compress
)
):
"""
The base application object that should be inherited
from all the application in the appier environment.
This object is responsible for the starting of all the
structures and for the routing of the request.
It should also be compliant with the WSGI specification.
"""
_BASE_ROUTES = []
""" Set of routes meant to be enable in a static
environment using for instance decorators this is
required because at the time of application loading
there's no application instance available """
_ERROR_HANDLERS = {}
""" The dictionary associating the error object (may be
both an integer code or an exception class) with the
proper method that is going to be used to handle that
error when it is raised """
_CUSTOM_HANDLERS = {}
""" Map that associates the various custom key values and
the tuples that describe the various handlers associated
with such actions (this is considered a generic value) """
_RESOLVED = False
""" Global variable responsible for setting the context to
when the the resolution of the handlers and routes has completed,
this value should be changed only one time per execution or
loading of the modules context """
def __init__(
self,
name = None,
locales = ("en_us",),
parts = (),
level = None,
handlers = None,
service = True,
safe = False,
payload = False,
cache_s = 604800,
cache_c = cache.MemoryCache,
session_c = session.FileSession
):
observer.Observable.__init__(self)
compress.Compress.__init__(self)
self.name = name or self.__class__.__name__
self.locales = locales
self.parts = parts
self.service = service
self.safe = safe
self.payload = payload
self.cache_s = cache_s
self.cache_c = cache_c
self.session_c = session_c
self.description = self._description()
self.copyright = None
self.server = None
self.host = None
self.port = None
self.ssl = False
self.local_url = None
self.cache_d = self.cache_c()
self.adapter = data.MongoAdapter()
self.manager = async.QueueManager(self)
self.routes_v = None
self.tid = None
self.type = "default"
self.status = STOPPED
self.start_time = None
self.start_date = None
self.touch_time = None
self.random = str(uuid.uuid4())
self.secret = self.random
self.cache = datetime.timedelta(seconds = cache_s)
self.login_route = "base.login"
self.part_routes = []
self.context = {}
self.models = {}
self.controllers = {}
self.names = {}
self.libraries = {}
self.lib_loaders = {}
self._locale_d = locales[0]
self._user_routes = None
self._core_routes = None
self._own = self
self._set_global()
self._load_paths()
self._load_config()
self._load_logging(level)
self._load_settings()
self._load_handlers(handlers)
self._load_session()
self._load_adapter()
self._load_manager()
self._load_request()
self._load_context()
self._load_bundles()
self._load_controllers()
self._load_models()
self._load_parts()
self._load_libraries()
self._load_templating()
self._load_imaging()
self._load_patches()
self._set_config()
self._set_variables()
def __getattr__(self, name):
if not name in ("session",):
raise AttributeError("'%s' not found" % name)
if not hasattr(self, "request"):
raise AttributeError("'%s' not found" % name)
if not hasattr(self.request, name):
raise AttributeError("'%s' not found" % name)
return getattr(self.request, name)
@property
def request(self):
if not self.safe: return self._request
if self.is_main(): return self._request
return self.mock
@property
def mock(self):
return self._mock
@property
def own(self):
if not self.safe: return self._own
if self.is_main(): return self._own
return self
@property
def crypt_secret(self):
if not self.secret: return None
if self.secret == self.random: return None
return self.secret
@staticmethod
def load():
logging.basicConfig(format = log.LOGGING_FORMAT)
@staticmethod
def add_route(*args, **kwargs):
route = App.norm_route(*args, **kwargs)
App._BASE_ROUTES.append(route)
@staticmethod
def add_error(error, method, scope = None, json = False, context = None):
error_handlers = App._ERROR_HANDLERS.get(error, [])
error_handlers.append([method, scope, json, context])
App._ERROR_HANDLERS[error] = error_handlers
@staticmethod
def add_exception(exception, method, scope = None, json = False, context = None):
error_handlers = App._ERROR_HANDLERS.get(exception, [])
error_handlers.append([method, scope, json, context])
App._ERROR_HANDLERS[exception] = error_handlers
@staticmethod
def add_custom(key, method, context = None):
custom_handlers = App._CUSTOM_HANDLERS.get(key, [])
custom_handlers.append([method, context])
App._CUSTOM_HANDLERS[key] = custom_handlers
@staticmethod
def norm_route(method, expression, function, async = False, json = False, context = None):
# creates the list that will hold the various parameters (type and
# name tuples) and the map that will map the name of the argument
# to the string representing the original expression of it so that
# it may be latter used for reference (as specified in definition)
param_t = []
names_t = {}
# retrieves the data type of the provided method and in case it
# references a string type converts it into a simple tuple otherwise
# uses it directly, then creates the options dictionary with the
# series of values that are going to be used as options in the route
method_t = type(method)
method = (method,) if method_t in legacy.STRINGS else method
opts = dict(
json = json,
async = async,
base = expression,
param_t = param_t,
names_t = names_t,
)
# creates a new match based iterator to try to find all the parameter
# references in the provided expression so that meta information may
# be created on them to be used latter in replacements
iterator = REPLACE_REGEX.finditer(expression)
for match in iterator:
# retrieves the group information on the various groups and unpacks
# them creating the param tuple from the resolved type and the name
# of the parameter (to be used in parameter passing casting)
_type_s, type_t, extras, name = match.groups()
type_r = TYPES_R.get(type_t, str)
param = (type_r, name)
# creates the target (replacement) expression taking into account if
# the type values has been provided or not, note that for expression
# with type the extras value is used in case it exists
if type_t: target = "<" + type_t + (extras or "") + ":" + name + ">"
else: target = "<" + name + ">"
# adds the parameter to the list of parameter tuples and then sets the
# target replacement association (name to target string)
param_t.append(param)
names_t[name] = target
# runs the regex based replacement chain that should translate
# the expression from a simplified domain into a regex based domain
# that may be correctly compiled into the rest environment then
# creates the route list, compiling the expression and returns it
# to the caller method so that it may be used in the current environment
expression = "^" + expression + "$"
expression = INT_REGEX.sub(r"(?P[\1>[\\d]+)", expression)
expression = REGEX_REGEX.sub(r"(?P[\2>\1)", expression)
expression = REPLACE_REGEX.sub(r"(?P[\4>[\@\:\.\\s\\w-]+)", expression)
expression = expression.replace("?P[", "?P<")
return [method, re.compile(expression, re.UNICODE), function, context, opts]
def unload(self):
self._unload_models()
self._unload_logging()
def start(self, refresh = True):
if self.status == RUNNING: return
self._print_welcome()
self.tid = threading.current_thread().ident
self.start_time = time.time()
self.start_date = datetime.datetime.utcnow()
self.touch_time = "?t=%d" % self.start_time
if refresh: self.refresh()
if self.manager: self.manager.start()
self.status = RUNNING
self.trigger("start")
def stop(self, refresh = True):
if self.status == STOPPED: return
self._print_bye()
self.tid = None
self.status = STOPPED
self.refresh()
self.trigger("stop")
def refresh(self):
self._set_url()
def serve(
self,
server = "legacy",
host = "127.0.0.1",
port = 8080,
ipv6 = False,
ssl = False,
key_file = None,
cer_file = None,
threaded = False,
conf = True,
**kwargs
):
server = config.conf("SERVER", server) if conf else server
host = config.conf("HOST", host) if conf else host
port = config.conf("PORT", port, cast = int) if conf else port
ipv6 = config.conf("IPV6", ipv6, cast = bool) if conf else cer_file
ssl = config.conf("SSL", ssl, cast = bool) if conf else ssl
key_file = config.conf("KEY_FILE", key_file) if conf else key_file
cer_file = config.conf("CER_FILE", cer_file) if conf else cer_file
servers = config.conf_prefix("SERVER_") if conf else dict()
for name, value in servers.items():
name_s = name.lower()[7:]
if name_s in EXCLUDED_NAMES: continue
kwargs[name_s] = value
kwargs["handlers"] = self.handlers
kwargs["level"] = self.level
self.logger.info("Starting '%s' with '%s' ..." % (self.name, server))
self.server = server; self.host = host; self.port = port; self.ssl = ssl
self.start()
method = getattr(self, "serve_" + server)
names = method.__code__.co_varnames
if "ipv6" in names: kwargs["ipv6"] = ipv6
if "ssl" in names: kwargs["ssl"] = ssl
if "key_file" in names: kwargs["key_file"] = key_file
if "cer_file" in names: kwargs["cer_file"] = cer_file
if threaded: util.BaseThread(
target = self.serve_final,
args = (server, method, host, port, kwargs),
daemon = True
).start()
else: self.serve_final(server, method, host, port, kwargs)
def serve_final(self, server, method, host, port, kwargs):
try: return_value = method(host = host, port = port, **kwargs)
except BaseException as exception:
lines = traceback.format_exc().splitlines()
self.logger.critical("Unhandled exception received: %s" % legacy.UNICODE(exception))
for line in lines: self.logger.warning(line)
raise
self.stop()
self.logger.info("Stopped '%s'' in '%s' ..." % (self.name, server))
return return_value
def serve_legacy(self, host, port, **kwargs):
"""
Starts the serving process for the application using the python's
legacy wsgi server implementation, this server is considered unstable
and should only be used for development/testing purposes.
:type host: String
:param host: The host name of ip address to bind the server
to, this value should be represented as a string.
:type port: int
:param port: The tcp port for the bind operation of the
server (listening operation).
"""
import wsgiref.simple_server
httpd = wsgiref.simple_server.make_server(host, port, self.application)
httpd.serve_forever()
def serve_netius(
self,
host,
port,
ipv6 = False,
ssl = False,
key_file = None,
cer_file = None,
**kwargs
):
"""
Starts serving the current application using the hive solutions
python based web server netius http, this is supposed to be used
with care as the server is still under development.
For more information on the netius http servers please refer
to the https://github.com/hivesolutions/netius site.
:type host: String
:param host: The host name of ip address to bind the server
to, this value should be represented as a string.
:type port: int
:param port: The tcp port for the bind operation of the
server (listening operation).
:type ipv6: bool
:param ipv6: If the server should be started under the IPv6 mode
meaning that a socket is opened for that protocol, instead of the
typical IPv4 version.
:type ssl: bool
:param ssl: If the ssl framework for encryption should be used
in the creation of the server socket.
:type key_file: String
:param key_file: The path to the file containing the private key
that is going to be used in the ssl communication.
:type cer_file: String
:param cer_file: The path to the certificate file to be used in
the ssl based communication.
"""
import netius.servers
server = netius.servers.WSGIServer(self.application, **kwargs)
server.serve(
host = host,
port = port,
ipv6 = ipv6,
ssl = ssl,
key_file = key_file,
cer_file = cer_file
)
def serve_waitress(self, host, port, **kwargs):
"""
Starts the serving of the current application using the
python based waitress server in the provided host and
port as requested.
For more information on the waitress http server please
refer to https://pypi.python.org/pypi/waitress.
:type host: String
:param host: The host name of ip address to bind the server
to, this value should be represented as a string.
:type port: int
:param port: The tcp port for the bind operation of the
server (listening operation).
"""
import waitress
waitress.serve(self.application, host = host, port = port)
def serve_tornado(self, host, port, ssl = False, key_file = None, cer_file = None, **kwargs):
import tornado.wsgi
import tornado.httpserver
ssl_options = ssl and dict(
keyfile = key_file,
certfile = cer_file
) or None
container = tornado.wsgi.WSGIContainer(self.application)
server = tornado.httpserver.HTTPServer(container, ssl_options = ssl_options)
server.listen(port, address = host)
instance = tornado.ioloop.IOLoop.instance()
instance.start()
def serve_cherry(self, host, port, **kwargs):
import cherrypy.wsgiserver
server = cherrypy.wsgiserver.CherryPyWSGIServer(
(host, port),
self.application
)
try: server.start()
except (KeyboardInterrupt, SystemExit): server.stop()
def serve_gunicorn(self, host, port, workers = 1, **kwargs):
import gunicorn.app.base
class GunicornApplication(gunicorn.app.base.BaseApplication):
def __init__(self, application, options = None):
self.application = application
self.options = options or {}
gunicorn.app.base.BaseApplication.__init__(self)
def load_config(self):
for key, value in legacy.iteritems(self.options):
self.cfg.set(key, value)
def load(self):
return self.application
options = dict(
bind = "%s:%d" % (host, port),
workers = workers
)
server = GunicornApplication(self.application, options)
server.run()
def load_jinja(self, **kwargs):
try: import jinja2
except: self.jinja = None; return
loader = jinja2.FileSystemLoader(self.templates_path)
self.jinja = jinja2.Environment(
loader = loader,
extensions = ("jinja2.ext.do",)
)
self.add_filter(self.to_locale_jinja, "locale", context = True)
self.add_filter(self.nl_to_br_jinja, "nl_to_br", context = True)
self.add_filter(self.sp_to_nbsp_jinja, "sp_to_nbsp", context = True)
self.add_filter(self.echo, "echo")
self.add_filter(self.echo, "handle")
self.add_filter(self.dumps, "dumps")
self.add_filter(self.loads, "loads")
self.add_filter(self.typeof, "type")
self.add_filter(self.script_tag_jinja, "script_tag", context = True)
self.add_filter(self.css_tag_jinja, "css_tag", context = True)
self.add_filter(self.css_tag_jinja, "stylesheet_tag", context = True)
self.add_filter(self.asset_url, "asset_url")
for name, value in self.context.items(): self.add_global(value, name)
def add_filter(self, method, name = None, context = False):
"""
Adds a filter to the current context in the various template
handlers that support this kind of operation.
Note that a call to this method may not have any behavior in
case the handler does not support filters.
:type method: Method
:param method: The method that is going to be added as the
filter handler, by default the method name is used as the name
for the filter.
:type name: String
:param name: The optional name to be used as the filter name
this is the name to be used in the template.
:type context: bool
:param context: If the filter to be added should have the current
template context passed as argument.
"""
name = name or method.__name__
if context: method.__func__.evalcontextfilter = True
self.jinja.filters[name] = method
def add_global(self, symbol, name):
if self.jinja: self.add_global_jinja(symbol, name)
def add_global_jinja(self, symbol, name, target = None):
target = target or self.jinja
_globals = getattr(target, "globals")
_globals[name] = symbol
def load_pil(self):
try: import PIL.Image
except: self.pil = None; return
self.pil = PIL
self._pil_image = PIL.Image
def close(self):
pass
def routes(self):
return []
def all_routes(self):
return App._BASE_ROUTES + self.user_routes() + self.core_routes()
def user_routes(self):
if self._user_routes: return self._user_routes
routes = self.routes()
self._user_routes = [App.norm_route(*route) for route in routes]
return self._user_routes
def core_routes(self):
if self._core_routes: return self._core_routes
self.base_routes = [
(("GET",), "/static/.*", self.static),
(("GET",), "/appier/static/.*", self.static_res),
(("GET",), "/<str:part>/static/.*", self.static_part)
]
self.extra_routes = [
(("GET",), "/", self.info),
(("GET",), "/favicon.ico", self.icon),
(("GET",), "/info", self.info),
(("GET",), "/version", self.version),
(("GET",), "/log", self.logging),
(("GET",), "/debug", self.debug),
(("GET", "POST"), "/login", self.login),
(("GET", "POST"), "/logout", self.logout)
] if self.service else []
core_routes = self.part_routes + self.base_routes + self.extra_routes
self._core_routes = [App.norm_route(*route) for route in core_routes]
return self._core_routes
def application(self, environ, start_response):
self.prepare()
try: return self.application_l(environ, start_response)
finally: self.restore()
def prepare(self):
"""
Method responsible for the preparation of the application state
into the typical structure expected at the start of the handling
of request received from the top level server infra-structure.
"""
REQUEST_LOCK.acquire()
def restore(self):
"""
Method responsible for the restoring of the application state
back to normal after the handling of an application request.
This method should be called safely using the finally keyword
and the execution of it should avoid the raising of an exception
so that the application behavior remains static.
"""
self._request.close()
self._request = self._mock
self._own = self
REQUEST_LOCK.release()
def application_l(self, environ, start_response):
# unpacks the various fields provided by the wsgi layer
# in order to use them in the current request handling
method = environ["REQUEST_METHOD"]
path = environ["PATH_INFO"]
query = environ["QUERY_STRING"]
script_name = environ["SCRIPT_NAME"]
content_length = environ.get("CONTENT_LENGTH")
address = environ.get("REMOTE_ADDR")
input = environ.get("wsgi.input")
scheme = environ.get("wsgi.url_scheme")
# in case the current executing environment is python 3
# compliant a set of extra operations must be applied to
# both the path and the script name so that they are
# properly encoded under the current environment
if legacy.PYTHON_3: path = legacy.bytes(path).decode("utf-8")
if legacy.PYTHON_3: script_name = legacy.bytes(script_name).decode("utf-8")
# converts the received content length (string value) into
# the appropriate integer representation so that it's possible
# to use it in the reading of the provided input stream
content_length_i = int(content_length) if content_length else -1
# creates the proper prefix value for the request from
# the script name field and taking into account that this
# value may be an empty or invalid value
prefix = script_name if script_name.endswith("/") else script_name + "/"
# creates the initial request object to be used in the
# handling of the data has been received not that this
# request object is still transient as it does not have
# either the params and the json data set in it
self._request = request.Request(
owner = self,
method = method,
path = path,
prefix = prefix,
query = query,
scheme = scheme,
address = address,
environ = environ,
session_c = self.session_c
)
# sets the original (unset) context for the request handling
# note that the original (own) context is considered to be the
# current instance as it's the base for the context retrieval
self._own = self
# parses the provided query string creating a map of
# parameters that will be used in the request handling
# and then sets it in the request
params = legacy.parse_qs(query, keep_blank_values = True)
params = util.decode_params(params)
self.request.set_params(params)
# reads the data from the input stream file and then tries
# to load the data appropriately handling all normal cases
# (eg json, form data, etc.)
data = None if method in BODYLESS_METHODS else input.read(content_length_i)
self.request.set_data(data)
self.request.load_base()
self.request.load_locale(self.locales)
# resolves the secret based params so that their content
# is correctly decrypted according to the currently set secret
self.request.resolve_params()
# sets the global (operative system) locale for according to the
# current value of the request, this value should be set while
# the request is being handled after that it should be restored
# back to the original (unset) value
self._set_locale()
# calls the before request handler method, indicating that the
# request is going to be handled in the next few logic steps
self.before_request()
try:
# handles the currently defined request and in case there's an
# exception triggered by the underlying action methods, handles
# it with the proper error handler so that a proper result value
# is returned indicating the exception
result = self.handle()
# "extracts" the data type for the result value coming from the handle
# method, in case the value is a generator extracts the first value from
# it so that it may be used for length evaluation (protocol definition)
# at this stage it's possible to have an exception raised for a non
# existent file or any other pre validation based problem
is_generator = legacy.is_generator(result)
if is_generator: first = next(result)
else: first = None
except BaseException as exception:
# resets the values associated with the generator based strategy so
# that the error/exception is handled in the proper (non generator)
# way and no interference exists for such situation, otherwise some
# compatibility problems would occur
is_generator = False
first = None
# verifies if the current error to be handled is a soft one (not severe)
# meaning that it's expected under some circumstances, for that kind of
# situations a less verbose logging operation should be performed
is_soft = type(exception) in (exceptions.NotFoundError,)
# handles the raised exception with the proper behavior so that the
# resulting value represents the exception with either a map or a
# string based value (properly encoded with the default encoding)
result = self.handle_error(exception)
if is_soft: self.log_warning(exception)
else: self.log_error(exception)
finally:
# performs the flush operation in the request so that all the
# stream oriented operation are completely performed, this should
# include things like session flushing (into cookie)
self.request.flush()
# resets the locale so that the value gets restored to the original
# value as it is expected by the current systems behavior, note that
# this is only done in case the safe flag is active (would create some
# serious performance problems otherwise)
if self.safe: self._reset_locale()
# in case the current method required empty responses/result the result
# is "forced" to be empty so that no specification is
if method in EMPTY_METHODS: result = ""
# re-retrieves the data type for the result value, this is required
# as it may have been changed by an exception handling, failing to do
# this would create series handling problems (stalled connection)
result_t = type(result)
# verifies that the type of the result is a dictionary and if
# that's the case verifies if it's considered to be empty, for
# such situations the single result value is set with success
is_map = result_t == dict
is_list = result_t in (list, tuple)
if is_map and not result: result["result"] = "success"
# retrieves the complete set of warning "posted" during the handling
# of the current request and in case thre's at least one warning message
# contained in it sets the warnings in the result
warnings = self.request.get_warnings()
if is_map and warnings: result["warnings"] = warnings
# retrieves any pending set cookie directive from the request and
# uses it to update the set cookie header if it exists
set_cookie = self.request.get_set_cookie()
if set_cookie: self.request.set_header("Set-Cookie", set_cookie)
# verifies if the current response is meant to be serialized as a json message
# this is the case for both the map type of response and the list type type
# of response as both of them represent a json message to be serialized
is_json = is_map or is_list
# retrieves the name of the encoding that is going to be used in case the
# the resulting data need to be converted from unicode
encoding = self.request.get_encoding()
# dumps the result using the json serializer and retrieves the resulting
# string value from it as the final message to be sent to the client, then
# validates that the value is a string value in case it's not casts it as
# a string using the default "serializer" structure
result_s = json.dumps(result) if is_json else result
result_t = type(result_s)
if result_t == legacy.UNICODE: result_s = result_s.encode(encoding)
elif not result_t == legacy.BYTES: result_s = legacy.bytes(str(result_s))
# calculates the final size of the resulting message in bytes so that
# it may be used in the content length header, note that a different
# approach is taken when the returned value is a generator, where it's
# expected that the first yield result is the total size of the message
result_l = first if is_generator else len(result_s)
is_empty = self.request.is_empty() and result_l == 0
# tries to determine if the length of the payload to be sent should be
# set as part of the headers for the response
set_length = not is_empty and not result_l in (None, -1)
# sets the "target" content type taking into account the if the value is
# set and if the current structure is a map or not
default_content_type = is_json and "application/json" or "text/plain"
self.request.default_content_type(default_content_type)
# calls the after request handler that is meant to defined the end of the
# processing of the request, this creates an extension point for final
# modifications on the request/response to be sent to the client