Skip to content

Commit

Permalink
passing Request to Dynamic?
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotakj committed Jul 29, 2019
1 parent 0c03219 commit 41bf875
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 54 deletions.
3 changes: 1 addition & 2 deletions Mamlambo/Cache.py → Mamlambo/Core/Cache.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
from . import Session as session
from . import Singleton
from Mamlambo.Core import Singleton


@Singleton.singleton_object
Expand Down
4 changes: 2 additions & 2 deletions Mamlambo/Configuration.py → Mamlambo/Core/Configuration.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import yaml
from .MamlamboException import MamlamboException
from . import Singleton
from .Response import Response
from Mamlambo.Core import Singleton
from Mamlambo.Response import Response
import re
from urllib.parse import urlparse

Expand Down
4 changes: 2 additions & 2 deletions Mamlambo/MamlamboException.py → Mamlambo/Core/MamlamboException.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import sys
import Mamlambo
from . import Response
from . import Configuration
from Mamlambo.Core import Configuration
from Mamlambo import Response


class MamlamboException:
Expand Down
File renamed without changes.
Empty file added Mamlambo/Core/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions Mamlambo/RendererDefault.py → Mamlambo/Renderer/Default.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# -*- coding: utf-8 -*-

import os
from .MamlamboException import MamlamboException
from Mamlambo.Core.MamlamboException import MamlamboException


class RendererDefault:
class Default:

def __init__(self, config, request, response):

Expand Down
33 changes: 22 additions & 11 deletions Mamlambo/RendererDynamic.py → Mamlambo/Renderer/Dynamic.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import copy
import json
from contextlib import redirect_stdout
from .Configuration import Configuration
from .MamlamboException import MamlamboException
from Mamlambo.Core.Configuration import Configuration
from Mamlambo.Core.MamlamboException import MamlamboException


class RendererDynamic:
class Dynamic:
__page_raw = None
__page_result = None
__page_template = None
Expand All @@ -28,13 +28,14 @@ class RendererDynamic:
__directive_attributes = None

__is_nested_call = None
__verbose = True
__verbose = False

def verbose(self, text):
if self.__verbose:
print(text)

def __init__(self, file_name, is_fragment=False, is_nested_call=0):
def __init__(self, request, file_name, is_fragment=False, is_nested_call=0):
self.__request = request
self.__page_mime = "text/html"
self.__http_code = 200
self.__page_filename = file_name
Expand Down Expand Up @@ -313,7 +314,7 @@ def process_directive_page(self, matches):
# print('-- using master page')
page_master_file_name = Configuration().map_path(path_info=self.__page_master)
self.verbose("!RendererDynamic('" + page_master_file_name + "')")
masterpage = RendererDynamic(page_master_file_name, is_nested_call=1)
masterpage = Dynamic(self.__request, page_master_file_name, is_nested_call=1)
self.verbose("!After RendererDynamic of master:" + str(masterpage))
# print('-- master info')
# print("master_placeholderse: " + str(masterpage.master_placeholders))
Expand Down Expand Up @@ -403,9 +404,6 @@ def process_directive_page(self, matches):
# if page_code include the code.py
regex_doctype = r"<[^\!].*[\^>]*>" # tags not starting with <! like DOCTYPE
matches = re.finditer(regex_doctype, self.__page_raw, re.MULTILINE)
self.verbose('~~~~~~~~')
self.verbose('self.__page_code_source: ' + str(self.__page_code_source))
self.verbose('~~~~~~~~')
found = False
for matches_count, match in enumerate(matches, start=0):
found = True
Expand All @@ -416,13 +414,26 @@ def process_directive_page(self, matches):
else:
include_master_page_source_code = ""

inject_request = """
REQUEST = 'a'
print(dir())
globals()["REQUEST"] = 'b'
print(dir(globals()))
\n"""

if self.__page_code_source:
self.__page_raw = self.insert_str(
self.__page_raw,
"\n<?python:\n" + include_master_page_source_code + "\n" + self.__page_code_source + "\n?>",
"\n<?python:\n" + inject_request + include_master_page_source_code + "\n" + self.__page_code_source + "\n?>",
match.end())
break

self.verbose('~~~~~~~~')
self.verbose('self.__page_raw: ' + str(self.__page_raw))
self.verbose('~~~~~~~~')


if not found:
try:
with io.StringIO() as buf, redirect_stdout(buf):
Expand Down Expand Up @@ -526,7 +537,7 @@ class RendererMain:
# def __init__(self, file_name, is_fragment=False):
def __init__(self, request, response):
file_name = Configuration().map_path(path_info=request.path_info)
markdown = RendererDynamic(file_name, is_fragment=False, is_nested_call=0)
markdown = Dynamic(request, file_name, is_fragment=False, is_nested_call=0)
response.mime = markdown.page_mime
response.content_str = markdown.page_result
response.code = markdown.http_code
Expand Down
6 changes: 1 addition & 5 deletions Mamlambo/RendererRoutes.py → Mamlambo/Renderer/Routes.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
# -*- coding: utf-8 -*-

import re
import os
from . import Configuration
from . import Request
from . import Response

from enum import Enum

Expand All @@ -15,7 +11,7 @@ class Mode(Enum):
route = 'route'


class RendererRoutes:
class Routes:

# mode = 'redirection' adds header Location and response.end
# mode = 'route' changes url
Expand Down
4 changes: 2 additions & 2 deletions Mamlambo/RendererStatic.py → Mamlambo/Renderer/Static.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# -*- coding: utf-8 -*-

import os
from .MamlamboException import MamlamboException
from Mamlambo.Core.MamlamboException import MamlamboException


class RendererStatic:
class Static:
def __init__(self, config, request, response):
response.complete = False

Expand Down
Empty file added Mamlambo/Renderer/__init__.py
Empty file.
32 changes: 28 additions & 4 deletions Mamlambo/Request.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
from . import Session as session
from . import Singleton
from urllib.parse import urlparse

from Mamlambo.Session import Session
import inspect

import os

REQUEST = None

class Request():
__headers = []
Expand All @@ -19,7 +24,26 @@ class Request():

__master = None

def __init__(self, headers=None):
def __init__(self, headers=None, ):
global REQUEST

print(REQUEST)
#print(str(frame.f_globals))
#print(str(frame.f_locals))

#print("================================")
#print("== LOCALS ==============================")
#print(frame.f_back.f_locals)
#print("================================")
#print(frame.f_back.f_locals)

#print(str(dir(self)))
#print(str(globals()))
#print(str(locals()))
#print(str(globals()["request"]))
#self.__uri = request.uri
#o = urlparse(self.__uri)
#self.__path_info = o.path
if headers:
self.__headers = headers

Expand All @@ -38,7 +62,7 @@ def headers(self, value):
self.__headers = value

def session_start(self):
self.__session = session.Session()
self.__session = Session.Session()
self.__session.start()

@property
Expand Down
Empty file modified Mamlambo/Response.py
100644 → 100755
Empty file.
35 changes: 14 additions & 21 deletions Mamlambo/Router.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import Mamlambo
from . import RendererDynamic
from .RendererRoutes import RendererRoutes
from .RendererRoutes import Mode
from . import RendererStatic
from . import RendererDefault
from .Configuration import Configuration
from .Response import Response
from . import Request
from .MamlamboException import MamlamboException
from . import Singleton
from .Cache import Cache
from Mamlambo.Renderer import Dynamic, Default, Static
from Mamlambo.Renderer.Routes import Routes
from Mamlambo.Renderer.Routes import Mode
from Mamlambo.Core.Configuration import Configuration
from Mamlambo.Response import Response
from Mamlambo import Request
from Mamlambo.Core.MamlamboException import MamlamboException
from Mamlambo.Core.Cache import Cache


class Router:
Expand All @@ -22,6 +18,7 @@ class Router:
__status = None

def __init__(self, env, path_to_configuration=None):

response = Response() # instantiate empty singleton
response.complete = False
# Singleton.Singleton.reset(response)
Expand Down Expand Up @@ -61,7 +58,7 @@ def __init__(self, env, path_to_configuration=None):

# process request ---------------------------------------------------------------------------------------
# 1) check redirections
RendererRoutes(config.redirections, request, response, Mode.redirection)
Routes(config.redirections, request, response, Mode.redirection)
if response.complete:
self.__content = [b'']
self.__headers = response.headers
Expand All @@ -72,10 +69,10 @@ def __init__(self, env, path_to_configuration=None):
return

# 2) check routes
RendererRoutes(config.routes, request, response, Mode.route)
Routes(config.routes, request, response, Mode.route)

# 3) check default document
RendererDefault.RendererDefault(config, request, response)
Default.Default(config, request, response)
if response.complete:
self.__content = [response.content_bytes]
self.__headers = response.headers
Expand All @@ -86,7 +83,7 @@ def __init__(self, env, path_to_configuration=None):
return

# 4) check static content
RendererStatic.RendererStatic(config, request, response)
Static.Static(config, request, response)
if response.complete:
self.__content = [response.content_bytes]
self.__headers = response.headers
Expand All @@ -101,7 +98,7 @@ def __init__(self, env, path_to_configuration=None):
print("response.complete=" + str(response.complete))
print("response.status=" + str(response.status))

RendererDynamic.RendererMain(request, response)
Dynamic.RendererMain(request, response)
if response.complete:
self.__content = [response.content_bytes]
self.__headers = response.headers
Expand All @@ -119,10 +116,6 @@ def __init__(self, env, path_to_configuration=None):

return





# properties to pass data to WSGI
@property
def headers(self):
Expand Down
Empty file modified Mamlambo/Session.py
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion Mamlambo/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

version_info = (0, 3, 0)
__version__ = ".".join([str(v) for v in version_info])
__name__ = "Mamlambo"

ENV = None
6 changes: 6 additions & 0 deletions demo/html/dynamic/use_master.pyhtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@

<py:placeholder id="x">
33333333

query: <span py:content="q" /><br />
u: <span py:content="u" /><br />

Powered by Mamlambo: <span py:content="version" />

</py:placeholder>
12 changes: 10 additions & 2 deletions demo/html/dynamic/use_master.pyhtml.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import json
from Mamlambo.Request import Request
import Mamlambo

logo = "@LOGO"

version = Mamlambo.__version__

request = Request()
q = request.query_string
u = REQUEST

logo = "@LOGO"

0 comments on commit 41bf875

Please sign in to comment.