refactoring write_body
This commit is contained in:
parent
78c2728e96
commit
a79f5da2cc
37
offpunk.py
37
offpunk.py
|
@ -262,18 +262,14 @@ class GeminiItem():
|
||||||
filename = os.path.basename(self._cache_path)
|
filename = os.path.basename(self._cache_path)
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
def write_body(self,body,mode,encoding):
|
def write_body(self,body,mime):
|
||||||
## body is a copy of the raw gemtext
|
## body is a copy of the raw gemtext
|
||||||
## Write_body() also create the cache !
|
## Write_body() also create the cache !
|
||||||
## Tmpf is the temporary cache (historically, the only cache)
|
self.mime = mime
|
||||||
tmpf = tempfile.NamedTemporaryFile(mode, encoding=encoding, delete=False)
|
if self.mime and self.mime.startswith("text/"):
|
||||||
size = tmpf.write(body)
|
mode = "w"
|
||||||
tmpf.close()
|
else:
|
||||||
tmp_filename = tmpf.name
|
mode = "wb"
|
||||||
#self._debug("Wrote %d byte response to %s." % (size, tmp_filename))
|
|
||||||
# Maintain cache and log : FIXME
|
|
||||||
#self._log_visit(gi, address, size)
|
|
||||||
## We create the permanent cache
|
|
||||||
cache_dir = os.path.dirname(self._cache_path)
|
cache_dir = os.path.dirname(self._cache_path)
|
||||||
# If the subdirectory already exists as a file (not a folder)
|
# If the subdirectory already exists as a file (not a folder)
|
||||||
# We remove it (happens when accessing URL/subfolder before
|
# We remove it (happens when accessing URL/subfolder before
|
||||||
|
@ -283,7 +279,9 @@ class GeminiItem():
|
||||||
if os.path.isfile(cache_dir):
|
if os.path.isfile(cache_dir):
|
||||||
os.remove(cache_dir)
|
os.remove(cache_dir)
|
||||||
os.makedirs(cache_dir,exist_ok=True)
|
os.makedirs(cache_dir,exist_ok=True)
|
||||||
shutil.copyfile(tmp_filename,self._cache_path)
|
with open(self._cache_path, mode=mode) as f:
|
||||||
|
f.write(body)
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
def get_mime(self):
|
def get_mime(self):
|
||||||
|
@ -446,7 +444,6 @@ class GeminiClient(cmd.Cmd):
|
||||||
self.synconly = synconly
|
self.synconly = synconly
|
||||||
self.tmp_filename = ""
|
self.tmp_filename = ""
|
||||||
self.visited_hosts = set()
|
self.visited_hosts = set()
|
||||||
#self.waypoints = []
|
|
||||||
self.offline_only = False
|
self.offline_only = False
|
||||||
self.sync_only = False
|
self.sync_only = False
|
||||||
self.tourfile = os.path.join(self.config_dir, "tour")
|
self.tourfile = os.path.join(self.config_dir, "tour")
|
||||||
|
@ -744,7 +741,9 @@ you'll be able to transparently follow links to Gopherspace!""")
|
||||||
# DEFAULT GEMINI MIME
|
# DEFAULT GEMINI MIME
|
||||||
if mime == "":
|
if mime == "":
|
||||||
mime = "text/gemini; charset=utf-8"
|
mime = "text/gemini; charset=utf-8"
|
||||||
gi.mime, mime_options = cgi.parse_header(mime)
|
mime, mime_options = cgi.parse_header(mime)
|
||||||
|
# Trying to not set the mime to see what happens
|
||||||
|
#gi.set_mime(mime)
|
||||||
if "charset" in mime_options:
|
if "charset" in mime_options:
|
||||||
try:
|
try:
|
||||||
codecs.lookup(mime_options["charset"])
|
codecs.lookup(mime_options["charset"])
|
||||||
|
@ -756,18 +755,18 @@ you'll be able to transparently follow links to Gopherspace!""")
|
||||||
|
|
||||||
# Save the result in a temporary file
|
# Save the result in a temporary file
|
||||||
## Set file mode
|
## Set file mode
|
||||||
if gi.get_mime().startswith("text/"):
|
if mime.startswith("text/"):
|
||||||
mode = "w"
|
|
||||||
encoding = mime_options.get("charset", "UTF-8")
|
encoding = mime_options.get("charset", "UTF-8")
|
||||||
try:
|
try:
|
||||||
body = body.decode(encoding)
|
body = body.decode(encoding)
|
||||||
except UnicodeError:
|
except UnicodeError:
|
||||||
raise RuntimeError("Could not decode response body using %s encoding declared in header!" % encoding)
|
raise RuntimeError("Could not decode response body using %s encoding declared in header!" % encoding)
|
||||||
else:
|
# It looks like we don’t really need encoding to write the file to cache
|
||||||
mode = "wb"
|
# Text has been converted to UTF-8 anyway
|
||||||
encoding = None
|
#else:
|
||||||
|
# encoding = None
|
||||||
## body is a copy of the raw gemtext
|
## body is a copy of the raw gemtext
|
||||||
gi.write_body(body,mode,encoding)
|
gi.write_body(body,mime)
|
||||||
return gi
|
return gi
|
||||||
|
|
||||||
def _send_request(self, gi):
|
def _send_request(self, gi):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user