Put config dir discovery/creation in own method, store computed filename of bookmarks.
This commit is contained in:
parent
62972c0228
commit
03b90fcd5e
71
av98.py
71
av98.py
|
@ -213,31 +213,6 @@ class GeminiClient(cmd.Cmd):
|
|||
def __init__(self, restricted=False):
|
||||
cmd.Cmd.__init__(self)
|
||||
|
||||
# Set umask so that nothing we create can be read by anybody else.
|
||||
# The certificate cache and TOFU database contain "browser history"
|
||||
# type sensitivie information.
|
||||
os.umask(0o077)
|
||||
|
||||
# Find config directory
|
||||
## Look for something pre-existing
|
||||
for confdir in ("~/.av98/", "~/.config/av98/"):
|
||||
confdir = os.path.expanduser(confdir)
|
||||
if os.path.exists(confdir):
|
||||
self.config_dir = confdir
|
||||
break
|
||||
## Otherwise, make one in .config if it exists
|
||||
else:
|
||||
if os.path.exists(os.path.expanduser("~/.config/")):
|
||||
self.config_dir = os.path.expanduser("~/.config/av98/")
|
||||
else:
|
||||
self.config_dir = os.path.expanduser("~/.av98/")
|
||||
print("Creating config directory {}".format(self.config_dir))
|
||||
os.makedirs(self.config_dir)
|
||||
|
||||
## Claim two temporary filenames to use as buffers
|
||||
self.raw_file_buffer = tempfile.NamedTemporaryFile(delete=False).name
|
||||
self.rendered_file_buffer = tempfile.NamedTemporaryFile(delete=False).name
|
||||
|
||||
self.no_cert_prompt = "\x1b[38;5;76m" + "AV-98" + "\x1b[38;5;255m" + "> " + "\x1b[0m"
|
||||
self.cert_prompt = "\x1b[38;5;202m" + "AV-98" + "\x1b[38;5;255m" + "+cert> " + "\x1b[0m"
|
||||
self.prompt = self.no_cert_prompt
|
||||
|
@ -284,12 +259,40 @@ class GeminiClient(cmd.Cmd):
|
|||
"redirects_followed": 0
|
||||
}
|
||||
|
||||
self._init_config()
|
||||
ui_out.debug("Raw buffer: ", self.raw_file_buffer)
|
||||
ui_out.debug("Rendered buffer: ", self.rendered_file_buffer)
|
||||
self.tofu_store = TofuStore(self.config_dir)
|
||||
self.client_cert_manager = ClientCertificateManager(self.config_dir)
|
||||
self.cache = Cache()
|
||||
|
||||
ui_out.debug("Raw buffer: ", self.raw_file_buffer)
|
||||
ui_out.debug("Rendered buffer: ", self.rendered_file_buffer)
|
||||
def _init_config(self):
|
||||
# Set umask so that nothing we create can be read by anybody else.
|
||||
# The certificate cache and TOFU database contain "browser history"
|
||||
# type sensitivie information.
|
||||
os.umask(0o077)
|
||||
|
||||
# Find or create config directory
|
||||
## Look for something pre-existing
|
||||
for confdir in ("~/.av98/", "~/.config/av98/"):
|
||||
confdir = os.path.expanduser(confdir)
|
||||
if os.path.exists(confdir):
|
||||
self.config_dir = confdir
|
||||
break
|
||||
## Otherwise, make one in .config if it exists
|
||||
else:
|
||||
if os.path.exists(os.path.expanduser("~/.config/")):
|
||||
self.config_dir = os.path.expanduser("~/.config/av98/")
|
||||
else:
|
||||
self.config_dir = os.path.expanduser("~/.av98/")
|
||||
print("Creating config directory {}".format(self.config_dir))
|
||||
os.makedirs(self.config_dir)
|
||||
|
||||
# Set some filename constants
|
||||
self.bm_file = os.path.join(self.config_dir, "bookmarks.gmi")
|
||||
# Claim two temporary filenames to use as buffers
|
||||
self.raw_file_buffer = tempfile.NamedTemporaryFile(delete=False).name
|
||||
self.rendered_file_buffer = tempfile.NamedTemporaryFile(delete=False).name
|
||||
|
||||
def _go_to_gi(self, gi, update_hist=True, check_cache=True):
|
||||
"""
|
||||
|
@ -837,19 +840,18 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
|
|||
directly at the new address in future.
|
||||
"""
|
||||
# Nothing to do if no bookmarks exist!
|
||||
bm_file = os.path.join(self.config_dir, "bookmarks.gmi")
|
||||
if not os.path.exists(bm_file):
|
||||
if not os.path.exists(self.bm_file):
|
||||
return
|
||||
|
||||
# Backup bookmark file
|
||||
backup_file = tempfile.NamedTemporaryFile(delete=False)
|
||||
backup_file.close()
|
||||
backup_file = backup_file.name
|
||||
shutil.copyfile(bm_file, backup_file)
|
||||
shutil.copyfile(self.bm_file, backup_file)
|
||||
|
||||
# Attempt maintenance, restore backup if anything fails
|
||||
try:
|
||||
with open(backup_file, "r") as fp_old, open(bm_file, "w") as fp_new:
|
||||
with open(backup_file, "r") as fp_old, open(self.bm_file, "w") as fp_new:
|
||||
for line in fp_old:
|
||||
if not line.startswith("=>"):
|
||||
fp_new.write(line)
|
||||
|
@ -862,7 +864,7 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
|
|||
ui_out.debug("Updating old bookmark url {} to {} based on permanent redirect.".format(old_url, url))
|
||||
fp_new.write(line.replace(old_url, url))
|
||||
except Exception as err:
|
||||
shutil.copyfile(backup_file, bm_file)
|
||||
shutil.copyfile(backup_file, self.bm_file)
|
||||
ui_out.debug(traceback.format_exc())
|
||||
finally:
|
||||
os.unlink(backup_file)
|
||||
|
@ -1279,15 +1281,14 @@ Optionally, specify the new name for the bookmark."""
|
|||
'bookmarks' shows all bookmarks.
|
||||
'bookmarks n' navigates immediately to item n in the bookmark menu.
|
||||
Bookmarks are stored using the 'add' command."""
|
||||
bm_file = os.path.join(self.config_dir, "bookmarks.gmi")
|
||||
if not os.path.exists(bm_file):
|
||||
if not os.path.exists(self.bm_file):
|
||||
print("You need to 'add' some bookmarks, first!")
|
||||
return
|
||||
args = line.strip()
|
||||
if len(args.split()) > 1 or (args and not args.isnumeric()):
|
||||
print("bookmarks command takes a single integer argument!")
|
||||
return
|
||||
gi = GeminiItem("file://" + os.path.abspath(bm_file))
|
||||
gi = GeminiItem("file://" + os.path.abspath(self.bm_file))
|
||||
if args:
|
||||
# Semi-sneaky
|
||||
# Parses the bookmark file and modifies self.index so that
|
||||
|
|
Loading…
Reference in New Issue