From bc43e3150bc773d06b7fe6267e7c66e7360a1fbf Mon Sep 17 00:00:00 2001 From: Austreelis Date: Fri, 8 Sep 2023 16:01:02 +0200 Subject: [PATCH] Put everything in a common offpunk package This moves around and renames a bunch of code, as an effort toward fixing the flit build process. Simply put, all source code has been moved in an `offpunk` directory, and renamed in ways that (hopefully) make sense. The `pyproject.toml` file has been updated, too. This changes how to import code from offpunk modules: old top-level modules are now under `offpunk` (`import netcache` -> `from offpunk import netcache`), and "off" prefixes have been stripped (`from offutils import run` -> `from offpunk.utils import run`). Note file mode x (execute) was removed on all touched files (if it was set before), meaning they cannot be executed exactly like before this change. Running scripts directly (`./offpunk/netcache.py`) is broken because python doesn't populate parent modules of top-level modules. Running `python -m offpunk.netcache` is broken because running inner python modules as top-level this way *does* populate parent modules (assuming my understanding is correct, but it may very well not be), but it can somehow trigger undefined behavior (!?). Running `python -m offpunk` still works though. Signed-off-by: Austreelis --- offpunk.py => offpunk/__init__.py | 9 +++------ ansicat.py => offpunk/ansicat.py | 11 +++++------ cache_migration.py => offpunk/cache_migration.py | 0 netcache.py => offpunk/netcache.py | 9 ++++----- opnk.py => offpunk/opnk.py | 8 +++----- offthemes.py => offpunk/themes.py | 0 offutils.py => offpunk/utils.py | 2 +- pyproject.toml | 6 +++--- 8 files changed, 19 insertions(+), 26 deletions(-) rename offpunk.py => offpunk/__init__.py (99%) mode change 100755 => 100644 rename ansicat.py => offpunk/ansicat.py (99%) mode change 100755 => 100644 rename cache_migration.py => offpunk/cache_migration.py (100%) rename netcache.py => offpunk/netcache.py (99%) mode change 100755 => 100644 rename opnk.py => offpunk/opnk.py (98%) mode change 100755 => 100644 rename offthemes.py => offpunk/themes.py (100%) rename offutils.py => offpunk/utils.py (99%) diff --git a/offpunk.py b/offpunk/__init__.py old mode 100755 new mode 100644 similarity index 99% rename from offpunk.py rename to offpunk/__init__.py index e0ff04d..705235c --- a/offpunk.py +++ b/offpunk/__init__.py @@ -21,12 +21,9 @@ import sys import time import urllib.parse import subprocess -import netcache -import opnk -import ansicat -import offthemes -from offutils import run,term_width,is_local,mode_url,unmode_url -from offutils import _CONFIG_DIR,_DATA_DIR,_CACHE_PATH +from offpunk import ansicat, netcache, opnk, themes +from offpunk.utils import run,term_width,is_local,mode_url,unmode_url +from offpunk.utils import _CONFIG_DIR,_DATA_DIR,_CACHE_PATH try: import setproctitle setproctitle.setproctitle("offpunk") diff --git a/ansicat.py b/offpunk/ansicat.py old mode 100755 new mode 100644 similarity index 99% rename from ansicat.py rename to offpunk/ansicat.py index 6b6e357..5530e16 --- a/ansicat.py +++ b/offpunk/ansicat.py @@ -10,10 +10,9 @@ import urllib import argparse import mimetypes import fnmatch -import netcache -import offthemes -from offutils import run,term_width,is_local,looks_like_base64 -from offutils import _DATA_DIR +from offpunk import netcache, themes +from offpunk.utils import run,term_width,is_local,looks_like_base64 +from offpunk.utils import _DATA_DIR try: from readability import Document _HAS_READABILITY = True @@ -143,7 +142,7 @@ class AbstractRenderer(): self.temp_files = {} self.center = center self.last_mode = "readable" - self.theme = offthemes.default + self.theme = themes.default def display(self,mode=None,directdisplay=False): wtitle = self.get_formatted_title() @@ -183,7 +182,7 @@ class AbstractRenderer(): self.disabled_indents = None # each color is an [open,close] pair code self.theme = theme - self.colors = offthemes.colors + self.colors = themes.colors def _insert(self,color,open=True): if open: o = 0 diff --git a/cache_migration.py b/offpunk/cache_migration.py similarity index 100% rename from cache_migration.py rename to offpunk/cache_migration.py diff --git a/netcache.py b/offpunk/netcache.py old mode 100755 new mode 100644 similarity index 99% rename from netcache.py rename to offpunk/netcache.py index c325934..1c61f81 --- a/netcache.py +++ b/offpunk/netcache.py @@ -13,9 +13,8 @@ import datetime import hashlib import sqlite3 from ssl import CertificateError -import ansicat -import offutils -from offutils import _CACHE_PATH,_DATA_DIR,_CONFIG_DIR +from offpunk import ansicat, utils +from offpunk.utils import _CACHE_PATH,_DATA_DIR,_CONFIG_DIR import time try: import chardet @@ -98,7 +97,7 @@ def is_cache_valid(url,validity=0): # a cache to be valid (in seconds) # If 0, then any cache is considered as valid # (use validity = 1 if you want to refresh everything) - if offutils.is_local(url): + if utils.is_local(url): return True cache = get_cache_path(url) if cache : @@ -838,7 +837,7 @@ def fetch(url,offline=False,download_image_first=True,images_mode="readable",val #Image should exist, should be an url (not a data image) #and should not be already cached if image and not image.startswith("data:image/") and not is_cache_valid(image): - width = offutils.term_width() - 1 + width = utils.term_width() - 1 toprint = "Downloading %s" %image toprint = toprint[:width] toprint += " "*(width-len(toprint)) diff --git a/opnk.py b/offpunk/opnk.py old mode 100755 new mode 100644 similarity index 98% rename from opnk.py rename to offpunk/opnk.py index 1537255..a7f4f6c --- a/opnk.py +++ b/offpunk/opnk.py @@ -7,13 +7,11 @@ import os import sys import tempfile import argparse -import netcache -import ansicat -import offutils import shutil import time import fnmatch -from offutils import run,term_width,mode_url,unmode_url,is_local +from offpunk import ansicat, netcache, utils +from offpunk.utils import run,term_width,mode_url,unmode_url,is_local _HAS_XDGOPEN = shutil.which('xdg-open') _GREP = "grep --color=auto" @@ -184,7 +182,7 @@ class opencache(): #we immediately fallback to xdg-open. #netcache currently provide the path if it’s a file. #may this should be migrated here. - if not offutils.is_local(inpath): + if not utils.is_local(inpath): kwargs["images_mode"] = mode cachepath = netcache.fetch(inpath,**kwargs) if not cachepath: diff --git a/offthemes.py b/offpunk/themes.py similarity index 100% rename from offthemes.py rename to offpunk/themes.py diff --git a/offutils.py b/offpunk/utils.py similarity index 99% rename from offutils.py rename to offpunk/utils.py index 603901b..cfbbac0 100644 --- a/offutils.py +++ b/offpunk/utils.py @@ -13,7 +13,7 @@ import shutil import shlex import urllib.parse import urllib.parse -import cache_migration +from offpunk import cache_migration CACHE_VERSION = 1 diff --git a/pyproject.toml b/pyproject.toml index 41c4843..eb38241 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,9 +45,9 @@ Source = "https://git.sr.ht/~lioploum/offpunk" [project.scripts] offpunk = "offpunk:main" -netcache = "netcache:main" -ansicat = "ansicat:main" -opnk = "opnk:main" +netcache = "offpunk.netcache:main" +ansicat = "offpunk.ansicat:main" +opnk = "offpunk.opnk:main" [tool.flit.sdist] include = ["doc/", "man/", "CHANGELOG"]