Skip to content

Commit 8d85753

Browse files
authored
Merge pull request #17 from Deric-W/dev
Dev
2 parents 5b50350 + 91d5643 commit 8d85753

File tree

5 files changed

+208
-83
lines changed

5 files changed

+208
-83
lines changed

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ The script is called either by the configuration of the web server or a shebang
1616
- Python code is contained within the `<?pyhp` and `?>` tags (like PHP)
1717
- the Script is called like a interpreter, with the filepath as cli parameter
1818
- if no filepath is given, the script is reading from stdin
19-
- if "-c" is given, the file will be processed an cached in /etc/pyhp/relative/to/document root/filename.cache
19+
- if "-c" is given, the file will be processed an cached in cache_path/absolute/path/filename.cache
2020
(the file is also loaded or renewed with this option)
21-
- python code can be away from the left site of the file for better optics --> Test4.pyhp
21+
- python code can be away from the left site of the file for better optics --> Test4.pyhp, fib.pyhp
2222
- the following PHP features are available as part of the `pyhp` class:
2323
- `$_REQUEST` as REQUEST
2424
- `$_GET`as GET
@@ -30,12 +30,25 @@ The script is called either by the configuration of the web server or a shebang
3030
- `header`
3131
- `header_remove`
3232
- `headers_sent`
33+
- `header_register_callback`
3334
- `setrawcookie`
3435
- `setcookie`
35-
- automatic sending of headers with fallback: `Content-Type: text/html`
36+
- `register_shutdown_function`
37+
- automatic sending of headers with fallback: `Content-Type: text/html`
38+
39+
## Cache Handlers
40+
- are responsible for saveing/loading/renewing caches
41+
- are python scripts with the following contents:
42+
- the `handler` class, wich takes the cache path and absolute file path as initialization parameters
43+
- the method `is_outdated`, wich returns True or False
44+
- the method `save`, wich returns nothing and saves the boolean code_at_begin and preprocessed code
45+
- the method `load`, wich returns a tuble with the boolean code_at_begin and the code saved by `save`
46+
- the method `close`, wich does cleanup tasks
3647

3748
## Installation
3849
1. enable CGI for your web server
3950
2. drop pyhp.py somewhere and mark it as executable (make sure Python 3.4+ is installed)
40-
3. to enable the support for caching, create the directory `/etc/pyhp` and give the Webserver permission to read/write
41-
4. Done! you can now use `.pyhp` files by adding a Shebang
51+
3. create /etc/pyhp.conf
52+
4. create the directories listed in pyhp.conf and drop the choosen cache handler (and maybe others) in the cache handler directory
53+
54+
Done! you can now use `.pyhp` files by adding a Shebang

TODO

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
session_start
2+
session_abort
3+
session_write_close(session_commit)
4+
session_create_id
5+
session_destroy
6+
session_get_cookie_params
7+
session_id
8+
session_name
9+
session_regenerate_id
10+
session_reset
11+
session_save_path
12+
session_set_cookie_params
13+
session_set_save_handler
14+
session_status
15+
session_gc
16+
session_encode
17+
session_decode
18+
$_SESSION

cache_handlers/files_mtime.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/python3
2+
3+
"""PyHP cache handler (files with modification time)"""
4+
5+
import marshal
6+
import os.path
7+
from os import makedirs
8+
9+
class handler:
10+
def __init__(self, cache_path, file_path):
11+
self.cache_path = os.path.join(cache_path, file_path.strip(os.path.sep) + ".cache") # use full path to allow indentical named files in different directories with cache_path as root
12+
self.file_path = file_path
13+
14+
def is_outdated(self): # return True if cache is not created or needs refresh
15+
if not os.path.isfile(self.cache_path) or os.path.getmtime(self.cache_path) < os.path.getmtime(self.file_path):
16+
return True
17+
else:
18+
return False
19+
20+
def load(self):
21+
with open(self.cache_path, "rb") as cache:
22+
cache_content = marshal.load(cache)
23+
if len(cache_content) != 2:
24+
raise ValueError("corrupted cache at " + self.cache_path)
25+
else:
26+
return cache_content[0], cache_content[1] # file_content, code_at_begin
27+
28+
def save(self, file_content, code_at_begin):
29+
if not os.path.isdir(os.path.dirname(self.cache_path)): # directories not already created
30+
os.makedirs(os.path.dirname(self.cache_path), exist_ok=True)
31+
with open(self.cache_path, "wb") as cache:
32+
marshal.dump([file_content, code_at_begin], cache)
33+
34+
def close(self):
35+
pass

pyhp.conf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Configuration for the PyHP Interpreter (https://github.com/Deric-W/PyHP-Interpreter)
2+
# This file uses the INI syntax
3+
4+
[parser]
5+
opening_tag = \<\?pyhp[\n \t] # regex
6+
closing_tag = [\n \t]\?\> # regex
7+
8+
[caching]
9+
allow_caching = True
10+
auto_caching = False # ignore -c arg
11+
cache_path = /var/cache/pyhp # path to use
12+
handler = files_mtime
13+
handler_path = /lib/pyhp/cache_handlers # directory containing the handler

0 commit comments

Comments
 (0)