~jasper/live_doc

3975e6e0b3cf2d11f8c57c8bcd2c65c4a089a860 — Jasper den Ouden 2 years ago 37378c5
Default root dir `~/` and allow permitting certain directories.
5 files changed, 22 insertions(+), 11 deletions(-)

M README.md
M assets/top.htm
M live_doc/cmdline_args.py
M live_doc/http_main.py
M live_doc/main.py
M README.md => README.md +2 -0
@@ 29,6 29,8 @@ Configuration also selects wrappers and code highlighters the same way. Wrappers

* Saving to `.html` and/or `.zip` with the output files in it. (TODO suppose may want input ones too?)

* Server NOT intended to face public networks. It has basic cookie authentication, but for defense in deph. (Also you edit using your own editor, shouldn't be possible to do arbitrary things via http-calling the server right now.)

* TODO document the code.

### Current interpreters:

M assets/top.htm => assets/top.htm +1 -1
@@ 8,7 8,7 @@
    <script src="/:asset:/live.js"></script>
</head>
<body>
<nav><a href="./">Browse directory</a> <a href="/:doc:/">docs</a></nav>
<nav><a href="./">Browse directory</a> <a href="/:live_doc:/doc">docs</a></nav>
<div class=options>
<form id=save_nb action="/:save_nb:{file}" method=POST>
<input type=submit value="💾 Save">

M live_doc/cmdline_args.py => live_doc/cmdline_args.py +6 -2
@@ 14,8 14,12 @@ def def_args():
        help="""Run as server. (after other processing)
Follow with port, defaults 4000.""", nargs='?', default='no')

    args.add_argument('-dir',
        help="Directory it will start looking for the files. Defaultly $PDW")
    args.add_argument('-root',
        help="Directory it will start looking for the files. Defaultly $HOME")

    args.add_argument('-permit',
        help="Directories it permits access to.",
        action='append')

    args.add_argument('-output-dir',
        help="""Directory where to keep the files generated inside.

M live_doc/http_main.py => live_doc/http_main.py +5 -4
@@ 12,8 12,6 @@ from live_doc.cmdline_args import def_args
args = def_args().parse_args()

import os
if isinstance(args.dir, str):  # Change directory if asked.
    os.chdir(args.dir)

from live_doc.util.ConAuth import ConAuth  # Some authentication setup.
auth = ConAuth.cls_args_read(args)


@@ 21,7 19,7 @@ auth = ConAuth.cls_args_read(args)
from live_doc.main import Main

main = Main([os.getenv("HOME") + "/.config/live_doc/", "/etc/live_doc/"],
            args)
            args, permit_dirs=args.permit or [])
assets = main.assets

for file in args.save or []:


@@ 32,6 30,9 @@ for file in args.save_zip or []:

# -------- Server part.

# Select directory.
os.chdir(args.root if isinstance(args.root, str) else os.getenv('HOME'))

if args.serve == 'no':  # -serve not in there, or `no`
    exit(0)  # Don't want the server.



@@ 259,7 260,7 @@ Will probably redirect to a picture after.</div>\n"""

        path = self.path.split('?', 1)[0]
        slash, *rest = path.split(':', 2)
        if slash == '/' and len(rest) == 2 and rest[0]!='doc':
        if slash == '/' and len(rest) == 2 and rest[0]!='live_doc':
            # Remote Procedure Calls. (/assets)
            cmd, inp = rest
            rpc_fun = getattr(self, f"do_GET_{cmd}")

M live_doc/main.py => live_doc/main.py +8 -4
@@ 145,7 145,7 @@ highlighter=highlighters.bat_aha:highlighters.vimcat_aha:highlighters.script_bat
colorer=highlighters.aha:highlighters.plain
""")  # NOTE: wrap is used straight-up right now.

    def __init__(self, cfg_dirs, args, file_ok=None):
    def __init__(self, cfg_dirs, args, file_ok=None, permit_dirs=None):
        self.assets = AssetsMem([d + "/assets/" for d in cfg_dirs]
                                + [f"{module_dir}/assets/"])
        self.configs = Assets([d + "/cfg/" for d in cfg_dirs])


@@ 173,11 173,15 @@ colorer=highlighters.aha:highlighters.plain
        self.file_ok = set() if file_ok is None else file_ok
        self.notify_file = self.file_ok.add

        self.permit_dirs = permit_dirs if len(permit_dirs)>0 else ['']

    def mangle_filename(self, filename):
        if filename.startswith(":doc:"):  # Documents translated to document dir.
            return module_dir + "/doc" + self[5:]
        if filename.startswith(":live_doc:"):
            return module_dir + filename[10:]
        elif not filename.startswith('/') and filename.find("..") == -1:
            return filename
            # Must be in one of the permitted directories. (defaultly all)
            if any(map(filename.startswith, self.permit_dirs)):
                return filename
        # else  Otherwise not permitted.

    cls = MainFileHandler