Skip to content

Commit

Permalink
Merge pull request #17 from Deric-W/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Deric-W committed Aug 3, 2019
2 parents 5b50350 + 91d5643 commit 8d85753
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 83 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ The script is called either by the configuration of the web server or a shebang
- Python code is contained within the `<?pyhp` and `?>` tags (like PHP)
- the Script is called like a interpreter, with the filepath as cli parameter
- if no filepath is given, the script is reading from stdin
- if "-c" is given, the file will be processed an cached in /etc/pyhp/relative/to/document root/filename.cache
- if "-c" is given, the file will be processed an cached in cache_path/absolute/path/filename.cache
(the file is also loaded or renewed with this option)
- python code can be away from the left site of the file for better optics --> Test4.pyhp
- python code can be away from the left site of the file for better optics --> Test4.pyhp, fib.pyhp
- the following PHP features are available as part of the `pyhp` class:
- `$_REQUEST` as REQUEST
- `$_GET`as GET
Expand All @@ -30,12 +30,25 @@ The script is called either by the configuration of the web server or a shebang
- `header`
- `header_remove`
- `headers_sent`
- `header_register_callback`
- `setrawcookie`
- `setcookie`
- automatic sending of headers with fallback: `Content-Type: text/html`
- `register_shutdown_function`
- automatic sending of headers with fallback: `Content-Type: text/html`

## Cache Handlers
- are responsible for saveing/loading/renewing caches
- are python scripts with the following contents:
- the `handler` class, wich takes the cache path and absolute file path as initialization parameters
- the method `is_outdated`, wich returns True or False
- the method `save`, wich returns nothing and saves the boolean code_at_begin and preprocessed code
- the method `load`, wich returns a tuble with the boolean code_at_begin and the code saved by `save`
- the method `close`, wich does cleanup tasks

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

Done! you can now use `.pyhp` files by adding a Shebang
18 changes: 18 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
session_start
session_abort
session_write_close(session_commit)
session_create_id
session_destroy
session_get_cookie_params
session_id
session_name
session_regenerate_id
session_reset
session_save_path
session_set_cookie_params
session_set_save_handler
session_status
session_gc
session_encode
session_decode
$_SESSION
35 changes: 35 additions & 0 deletions cache_handlers/files_mtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/python3

"""PyHP cache handler (files with modification time)"""

import marshal
import os.path
from os import makedirs

class handler:
def __init__(self, cache_path, file_path):
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
self.file_path = file_path

def is_outdated(self): # return True if cache is not created or needs refresh
if not os.path.isfile(self.cache_path) or os.path.getmtime(self.cache_path) < os.path.getmtime(self.file_path):
return True
else:
return False

def load(self):
with open(self.cache_path, "rb") as cache:
cache_content = marshal.load(cache)
if len(cache_content) != 2:
raise ValueError("corrupted cache at " + self.cache_path)
else:
return cache_content[0], cache_content[1] # file_content, code_at_begin

def save(self, file_content, code_at_begin):
if not os.path.isdir(os.path.dirname(self.cache_path)): # directories not already created
os.makedirs(os.path.dirname(self.cache_path), exist_ok=True)
with open(self.cache_path, "wb") as cache:
marshal.dump([file_content, code_at_begin], cache)

def close(self):
pass
13 changes: 13 additions & 0 deletions pyhp.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Configuration for the PyHP Interpreter (https://github.com/Deric-W/PyHP-Interpreter)
# This file uses the INI syntax

[parser]
opening_tag = \<\?pyhp[\n \t] # regex
closing_tag = [\n \t]\?\> # regex

[caching]
allow_caching = True
auto_caching = False # ignore -c arg
cache_path = /var/cache/pyhp # path to use
handler = files_mtime
handler_path = /lib/pyhp/cache_handlers # directory containing the handler
Loading

0 comments on commit 8d85753

Please sign in to comment.