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 <dev@austreelis.net>
This commit is contained in:
Austreelis 2023-09-08 16:01:02 +02:00 committed by Lionel Dricot
parent 5f3d8d69be
commit bc43e3150b
8 changed files with 19 additions and 26 deletions

9
offpunk.py → offpunk/__init__.py Executable file → Normal file
View File

@ -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")

11
ansicat.py → offpunk/ansicat.py Executable file → Normal file
View File

@ -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

9
netcache.py → offpunk/netcache.py Executable file → Normal file
View File

@ -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))

8
opnk.py → offpunk/opnk.py Executable file → Normal file
View File

@ -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 its 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:

View File

@ -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

View File

@ -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"]