- make python-requests optional again
- reimplement --disable-http which had no effect
This commit is contained in:
Ploum 2023-11-08 16:37:13 +01:00
parent 8d082cb2df
commit ac78e85d04
3 changed files with 22 additions and 13 deletions

View File

@ -1,5 +1,10 @@
# Offpunk History
## 2.0 (beta3 ?) - Unreleased
Changes since beta2:
- bug #25 : makes python-requests optional again
- --disable-http had no effect: reimplemented
## 2.0-beta2 - November 8th 2023
Changes since beta1
- IMPORTANT: migrating from flit to hatchling (patch by Jean Abou Samra)

View File

@ -3,7 +3,6 @@ import os
import sys
import urllib.parse
import argparse
import requests
import codecs
import getpass
import socket
@ -30,6 +29,11 @@ try:
_BACKEND = default_backend()
except(ModuleNotFoundError,ImportError):
_HAS_CRYPTOGRAPHY = False
try:
import requests
_DO_HTTP = True
except (ModuleNotFoundError,ImportError):
_DO_HTTP = False
if not os.path.exists(_CACHE_PATH):
print("Creating cache directory {}".format(_CACHE_PATH))
@ -290,6 +294,7 @@ def set_error(url,err):
return cache
def _fetch_http(url,max_size=None,timeout=DEFAULT_TIMEOUT,accept_bad_ssl_certificates=False,**kwargs):
if not _DO_HTTP: return None
def too_large_error(url,length,max_size):
err = "Size of %s is %s Mo\n"%(url,length)
err += "Offpunk only download automatically content under %s Mo\n" %(max_size/1000000)
@ -782,7 +787,10 @@ def fetch(url,offline=False,download_image_first=True,images_mode="readable",val
print("%s is not a supported protocol"%scheme)
path = None
elif scheme in ("http","https"):
path=_fetch_http(url,**kwargs)
if _DO_HTTP:
path=_fetch_http(url,**kwargs)
else:
print("HTTP requires python-requests")
elif scheme == "gopher":
path=_fetch_gopher(url,**kwargs)
elif scheme == "finger":
@ -815,13 +823,13 @@ def fetch(url,offline=False,download_image_first=True,images_mode="readable",val
print("""ERROR5: Trying to create a directory which already exists
in the cache : """)
print(err)
elif isinstance(err,requests.exceptions.SSLError):
elif _DO_HTTP and isinstance(err,requests.exceptions.SSLError):
if print_error:
print("""ERROR6: Bad SSL certificate:\n""")
print(err)
print("""\n If you know what you are doing, you can try to accept bad certificates with the following command:\n""")
print("""set accept_bad_ssl_certificates True""")
elif isinstance(err,requests.exceptions.ConnectionError):
elif _DO_HTTP and isinstance(err,requests.exceptions.ConnectionError):
if print_error:
print("""ERROR7: Cannot connect to URL:\n""")
print(str(err))

View File

@ -36,11 +36,6 @@ try:
except ModuleNotFoundError:
_HAS_SETPROCTITLE = False
_HAS_XSEL = shutil.which('xsel')
try:
import requests
_DO_HTTP = True
except ModuleNotFoundError:
_DO_HTTP = False
## }}} end of imports
# Command abbreviations
@ -152,7 +147,7 @@ class GeminiClient(cmd.Cmd):
# Sync-only mode is restriced by design
self.offline_only = False
self.sync_only = False
self.support_http = _DO_HTTP
self.support_http = netcache._DO_HTTP
self.automatic_choice = "n"
self.client_certs = {
"active": None
@ -349,7 +344,8 @@ class GeminiClient(cmd.Cmd):
self._update_history(modedurl)
else:
#we are asked not to handle or in sync_only mode
netcache.fetch(url,**params)
if self.support_http or not parsed.scheme in ["http","https"] :
netcache.fetch(url,**params)
@needs_gi
def _show_lookup(self, offset=0, end=None, show_url=False):
@ -877,7 +873,7 @@ Marks are temporary until shutdown (not saved to disk)."""
output += " - python-cryptography : " + has(netcache._HAS_CRYPTOGRAPHY)
output += " - xdg-open : " + has(opnk._HAS_XDGOPEN)
output += "\nWeb browsing:\n"
output += " - python-requests : " + has(_DO_HTTP)
output += " - python-requests : " + has(netcache._DO_HTTP)
output += " - python-feedparser : " + has(ansicat._DO_FEED)
output += " - python-bs4 : " + has(ansicat._HAS_SOUP)
output += " - python-readability : " + has(ansicat._HAS_READABILITY)
@ -898,7 +894,7 @@ Marks are temporary until shutdown (not saved to disk)."""
output += " - Render images (python-pil, chafa or timg) : " + has(ansicat._RENDER_IMAGE)
output += " - Render HTML (bs4, readability) : " + has(ansicat._DO_HTML)
output += " - Render Atom/RSS feeds (feedparser) : " + has(ansicat._DO_FEED)
output += " - Connect to http/https (requests) : " + has(_DO_HTTP)
output += " - Connect to http/https (requests) : " + has(netcache._DO_HTTP)
output += " - Detect text encoding (python-chardet) : " + has(netcache._HAS_CHARDET)
output += " - copy to/from clipboard (xsel) : " + has(_HAS_XSEL)
output += " - restore last position (less 572+) : " + has(opnk._LESS_RESTORE_POSITION)