From b2b04691d6ba00a472d674fbf6ee02cd37337ba5 Mon Sep 17 00:00:00 2001 From: pictuga Date: Tue, 25 Jan 2022 22:36:34 +0100 Subject: [PATCH] Ability to pass custom data_files location --- README.md | 1 + morss/feeds.py | 3 ++- morss/util.py | 23 ++++++++++++++--------- morss/wsgi.py | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8a663ce..712e094 100644 --- a/README.md +++ b/README.md @@ -474,6 +474,7 @@ debugging. - `IGNORE_SSL=1`: to ignore SSL certs when fetch feeds and articles - `DELAY` (seconds) sets the browser cache delay, only for HTTP clients - `TIMEOUT` (seconds) sets the HTTP timeout when fetching rss feeds and articles +- `DATA_PATH`: to set custom file location for the `www` folder When parsing long feeds, with a lot of items (100+), morss might take a lot of time to parse it, or might even run into a memory overflow on some shared diff --git a/morss/feeds.py b/morss/feeds.py index 3563216..a229521 100644 --- a/morss/feeds.py +++ b/morss/feeds.py @@ -65,7 +65,8 @@ def parse_rules(filename=None): # for each rule if rules[section][arg].startswith('file:'): - file_raw = open(data_path(rules[section][arg][5:])).read() + path = data_path('www', rules[section][arg][5:]) + file_raw = open(path).read() file_clean = re.sub('<[/?]?(xsl|xml)[^>]+?>', '', file_raw) rules[section][arg] = file_clean diff --git a/morss/util.py b/morss/util.py index 5de28e5..2f8a705 100644 --- a/morss/util.py +++ b/morss/util.py @@ -15,32 +15,37 @@ # You should have received a copy of the GNU Affero General Public License along # with this program. If not, see . +import os import os.path import sys -def pkg_path(path=''): - return os.path.join(os.path.dirname(__file__), path) +def pkg_path(*path_elements): + return os.path.join(os.path.dirname(__file__), *path_elements) data_path_base = None -def data_path(path=''): +def data_path(*path_elements): global data_path_base + path = os.path.join(*path_elements) + if data_path_base is not None: return os.path.join(data_path_base, path) bases = [ - os.path.join(sys.prefix, 'share/morss/www'), # when installed as root - pkg_path('../../../share/morss/www'), - pkg_path('../../../../share/morss/www'), - pkg_path('../share/morss/www'), # for `pip install --target=dir morss` - pkg_path('../www'), # when running from source tree - pkg_path('../..'), # when running on `.cgi` subdir on Apache + os.path.join(sys.prefix, 'share/morss'), # when installed as root + pkg_path('../../../share/morss'), + pkg_path('../../../../share/morss'), + pkg_path('../share/morss'), # for `pip install --target=dir morss` + pkg_path('..'), # when running from source tree ] + if 'DATA_PATH' in os.environ: + bases.append(os.environ['DATA_PATH']) + for base in bases: full_path = os.path.join(base, path) diff --git a/morss/wsgi.py b/morss/wsgi.py index ef829e8..ee88803 100644 --- a/morss/wsgi.py +++ b/morss/wsgi.py @@ -169,7 +169,7 @@ def cgi_file_handler(environ, start_response, app): if re.match(r'^/?([a-zA-Z0-9_-][a-zA-Z0-9\._-]+/?)*$', url): # if it is a legitimate url (no funny relative paths) try: - path = data_path(url) + path = data_path('www', url) f = open(path, 'rb') except IOError: