From 5fd6a6c710d3666d8df6c4291b21a4f03e88be74 Mon Sep 17 00:00:00 2001 From: "Schimon Jehudah, Adv." Date: Wed, 20 Nov 2024 17:21:26 +0200 Subject: [PATCH] Automate installation process; Add command line prompt to set configurations; Update document README. --- README.md | 16 +--- pyproject.toml | 3 +- rivista/__main__.py | 104 +++++++++++++++++++++- rivista/assets/json/README | 1 - rivista/assets/script/postprocess.js | 2 +- rivista/{assets => configs}/settings.toml | 6 +- rivista/utilities.py | 22 +++++ rivista/version.py | 4 +- 8 files changed, 136 insertions(+), 22 deletions(-) delete mode 100644 rivista/assets/json/README rename rivista/{assets => configs}/settings.toml (70%) create mode 100644 rivista/utilities.py diff --git a/README.md b/README.md index 64ac7e7..1ac2399 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,9 @@ PubSub nodes into static XHTML journal sites. Rivista was inspired from Tigase and was motivated by Movim. ## Home site -- https://schapps.woodpeckersnest.eu/rivista/ + - gemini://woodpeckersnest.space/~schapps/rivista.gmi +- https://schapps.woodpeckersnest.eu/rivista/ ## Instances @@ -92,23 +93,14 @@ $ pipx uninstall rivista $ pipx install git+https://git.xmpp-it.net/sch/Rivista ``` -### Configure - -Add account credentials to file `settings.toml`. - -Copy file`settings.toml` to `~/.config/rivista/`. - -Copy directories `css`, `graphics`, `img`, `script`, and `xsl` to -`~/.local/share/rivista/`. - -Copy directory `json` to `~/.cache/rivista/`. - ### Start ``` $ rivista ``` +Open URL http://localhost:8000 + ## Usage It is possible to view a complete node and even a single item, which means, that diff --git a/pyproject.toml b/pyproject.toml index e2947d1..33fc721 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,7 @@ keywords = [ dependencies = [ "beautifulsoup4", "fastapi", + "gmcapsule", "lxml", "markdown", # "markdown-text-clean", @@ -59,4 +60,4 @@ rivista = "rivista.__main__:main" platforms = ["any"] [tool.setuptools.package-data] -"*" = ["*.toml"] +"*" = ["*.css", "*.js", "*.ico", "*.png", "*.svg", "*.toml", "*.xsl"] diff --git a/rivista/__main__.py b/rivista/__main__.py index d642253..b0e5459 100644 --- a/rivista/__main__.py +++ b/rivista/__main__.py @@ -3,12 +3,112 @@ #import importlib.resources +import os +from rivista.config import Cache, Data, Settings +from rivista.utilities import Toml from rivista.http.instance import HttpInstance +import shutil import uvicorn def main(): http_instance = HttpInstance() return http_instance.app -app = main() -uvicorn.run(app, host='localhost', port=8000, reload=False) +if __name__ == 'rivista.__main__': + + directory = os.path.dirname(__file__) + + # Copy data files + # FIXME File settins.toml is copied too. + directory_data = Data.get_directory() + if not os.path.exists(directory_data): + directory_assets = os.path.join(directory, 'assets') + directory_assets_new = shutil.copytree(directory_assets, directory_data) + print(f'Data directory {directory_assets_new} has been created and populated.') + + # Copy settings files + directory_settings = Settings.get_directory() + if not os.path.exists(directory_settings): + directory_configs = os.path.join(directory, 'configs') + directory_settings_new = shutil.copytree(directory_configs, directory_settings) + print(f'Settings directory {directory_settings_new} has been created and populated.') + + # Create cache directories + directory_cache = Cache.get_directory() + if not os.path.exists(directory_cache): + print(f'Creating a cache directory at {directory_cache}.') + os.mkdir(directory_cache) + # TODO Add more cache directories (see JabberCard). + # TODO Remove json (indices is the new name) + for subdirectory in ('json', 'indices', 'pubsub'): + subdirectory_cache = os.path.join(directory_cache, subdirectory) + if not os.path.exists(subdirectory_cache): + print(f'Creating a cache subdirectory at {subdirectory_cache}.') + os.mkdir(subdirectory_cache) + + # Configure settings file + file_settings = os.path.join(directory_settings, 'settings.toml') + if not os.path.exists(file_settings): + directory_configs = os.path.join(directory, 'configs') + file_settings_empty = os.path.join(directory_configs, 'settings.toml') + shutil.copyfile(file_settings_empty, file_settings) + data_settings = Toml.open_file_toml(file_settings) + + # Configure account + data_settings_account = data_settings['account'] + + settings_account = { + 'xmpp': 'Set a Jabber ID', + 'pass': 'Input Password' + } + + for key in settings_account: + data_settings_account_value = data_settings_account[key] + if not data_settings_account_value: + settings_account_message = settings_account[key] + while not data_settings_account_value: + data_settings_account_value = input(f'{settings_account_message}: ') + data_settings_account[key] = data_settings_account_value + + # Configure default PubSub Service and Node Name + data_settings_default = data_settings['default'] + + settings_default_default = { + 'pubsub': 'pubsub.woodpeckersnest.space', + 'nodeid': 'PlanetJabber' + } + + for key in settings_default_default: + data_settings_default_value = data_settings_default[key] + if not data_settings_default_value: + settings_default_default_value = settings_default_default[key] + data_settings_default_value = input(f'Set brand {key} (default: "{settings_default_default_value}"): ') + if not data_settings_default_value: data_settings_default_value = settings_default_default_value + data_settings_default[key] = data_settings_default_value + + # Configure Settings + data_settings_settings = data_settings['settings'] + + data_settings_settings_service = data_settings_settings['service'] + if data_settings_settings_service is '': + while data_settings_settings_service not in ('n', 'N', 'no', 'No', 'y', 'Y', 'yes', 'Yes'): + data_settings_settings_service = input(f'Run as service? [Y/n] ') + if data_settings_settings_service in ('y', 'Y', 'yes', 'Yes'): + data_settings_settings['service'] = 1 + else: + data_settings_settings['service'] = 0 + + data_settings_settings_include = data_settings_settings['include'] + if data_settings_settings_include is '': + data_settings_settings_include = input(f'Limit service to included domains? (comma separated. remain empty for no limit) ') + data_settings_settings['include'] = data_settings_settings_include if data_settings_settings_include else 0 + + data_settings_settings_operator = data_settings_settings['operator'] + if data_settings_settings_operator is '': + data_settings_settings['operator'] = input(f'Set a Jabber ID of an operator to contact with: ') + + Toml.save_to_toml(file_settings, data_settings) + + # Start JabberCard + app = main() + uvicorn.run(app, host='localhost', port=8000, reload=False) diff --git a/rivista/assets/json/README b/rivista/assets/json/README deleted file mode 100644 index 67b9b1a..0000000 --- a/rivista/assets/json/README +++ /dev/null @@ -1 +0,0 @@ -This directory caches lists of PubSub nodes. \ No newline at end of file diff --git a/rivista/assets/script/postprocess.js b/rivista/assets/script/postprocess.js index 1a42590..c428ec5 100644 --- a/rivista/assets/script/postprocess.js +++ b/rivista/assets/script/postprocess.js @@ -239,7 +239,7 @@ window.onload = async function(){ elementDiv.appendChild(elementP2); let elementSpan = document.createElement('span'); elementSpan.id = 'return'; - elementSpan.textContent = 'Return'; + elementSpan.textContent = 'Continue Reading'; elementSpan.addEventListener ('click', function() { document.querySelector('#selection-page').remove(); }); diff --git a/rivista/assets/settings.toml b/rivista/configs/settings.toml similarity index 70% rename from rivista/assets/settings.toml rename to rivista/configs/settings.toml index e99e40a..ce01971 100644 --- a/rivista/assets/settings.toml +++ b/rivista/configs/settings.toml @@ -5,11 +5,11 @@ pass = "" # Password. # A default node, when no arguments are set. [default] -pubsub = "pubsub.woodpeckersnest.space" # Jabber ID. -nodeid = "PlanetJabber" # Node ID. +pubsub = "" # Jabber ID. +nodeid = "" # Node ID. # Settings [settings] -service = 1 # Enable server as a service. +service = "" # Enable server as a service. include = "" # Limit service to a given domain. operator = "" # A Jabber ID to contact with, in case of an error. diff --git a/rivista/utilities.py b/rivista/utilities.py new file mode 100644 index 0000000..86fb425 --- /dev/null +++ b/rivista/utilities.py @@ -0,0 +1,22 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import tomli_w + +try: + import tomllib +except: + import tomli as tomllib + + +class Toml: + + def open_file_toml(filename: str) -> dict: + with open(filename, mode="rb") as fn: + data = tomllib.load(fn) + return data + + def save_to_toml(filename: str, data: dict) -> None: + with open(filename, 'w') as fn: + data_as_string = tomli_w.dumps(data) + fn.write(data_as_string) diff --git a/rivista/version.py b/rivista/version.py index 8cff63c..0e742f9 100644 --- a/rivista/version.py +++ b/rivista/version.py @@ -1,2 +1,2 @@ -__version__ = '1.0' -__version_info__ = (1, 0) +__version__ = '0.1' +__version_info__ = (0, 1)