ttbp/ttbp/gopher.py

119 lines
3.8 KiB
Python
Raw Normal View History

2017-12-04 03:37:43 +00:00
"""
This module contains gopher-related stuff.
"""
import getpass
import os
2017-12-05 00:18:43 +00:00
import time
import subprocess
2017-12-04 03:37:43 +00:00
from . import util
from . import config
#from .core import parse_date
2017-12-04 03:37:43 +00:00
GOPHER_PROMPT = """
GOPHER SETUP
2017-12-04 03:37:43 +00:00
gopher is a pre-web technology that is text-oriented and primarily used to
share folders of your files with the world.
Would you like to publish your feels to gopher?
Entries you write will automatically get linked to ~/public_gopher/feels/,
including gophermap generation. Files you manually delete will no longer be
visible from your gopherhole, and will be purged from your gophermap on your
next entry update.
2017-12-04 03:37:43 +00:00
If you don't know what it is or don't want it that is totally ok!
You can always change this later.""".lstrip()
GOPHERMAP_HEADER = """
welcome to {user}'s gopherfeels.
2017-12-04 03:37:43 +00:00
2017-12-04 04:04:46 +00:00
.:: .::
.: .::
.:.: .: .:: .:: .:: .::::
.:: .: .:: .: .:: .::.::
.:: .::::: .::.::::: .:: .:: .:::
.:: .: .: .:: .::
.:: .:::: .:::: .:::.:: .::
2017-12-04 03:37:43 +00:00
this file is automatically generated by ttbp.
2017-12-04 03:37:43 +00:00
2017-12-05 00:18:43 +00:00
0(about ttbp)\t/~endorphant/ttbp.txt\ttilde.town\t70
1(back to user's home)\t/~{user}
entries:
2017-12-05 00:18:43 +00:00
2017-12-04 03:37:43 +00:00
"""
def select_gopher():
return util.input_yn(GOPHER_PROMPT)
def publish_gopher(gopher_path, entry_filenames):
"""This function (re)generates a user's list of feels posts in their gopher
directory and their gophermap."""
entry_filenames = entry_filenames[:] # force a copy since this might be shared state in core.py
ttbp_gopher = os.path.join(
os.path.expanduser('~/public_gopher'),
gopher_path)
if not os.path.isdir(ttbp_gopher):
print('\n\tERROR: something is wrong. your gopher directory is missing. re-enable gopher publishing from the settings menu to fix this up!')
2017-12-04 03:37:43 +00:00
return
2017-12-05 00:18:43 +00:00
with open(os.path.join(ttbp_gopher, 'gophermap'), 'w') as gophermap:
2017-12-04 03:37:43 +00:00
gophermap.write(GOPHERMAP_HEADER.format(
user=getpass.getuser()))
for entry_filename in entry_filenames:
2017-12-04 04:04:46 +00:00
filename = os.path.basename(entry_filename)
2017-12-06 03:50:45 +00:00
gopher_entry_symlink = os.path.join(ttbp_gopher, os.path.basename(entry_filename))
if not os.path.exists(gopher_entry_symlink):
subprocess.call(["ln", "-s", entry_filename, gopher_entry_symlink])
2017-12-05 00:18:43 +00:00
label = "-".join(util.parse_date(entry_filename))
2017-12-05 00:18:43 +00:00
gophermap.write('0{file_label}\t{filename}\n'.format(
file_label=label,
2017-12-04 04:04:46 +00:00
filename=filename))
2017-12-04 03:37:43 +00:00
def setup_gopher(gopher_path):
"""Given a path relative to ~/public_gopher, this function:
- creates a directory ~/.ttbp/gopher
- symlinks that directory to ~/public_gopher/{gopher_path}
2017-12-04 03:37:43 +00:00
It doesn't create a gophermap as that is left to the publish_gopher
function.
"""
public_gopher = os.path.expanduser('~/public_gopher')
2017-12-04 04:04:46 +00:00
if not os.path.isdir(public_gopher):
"""
2017-12-04 03:37:43 +00:00
print("\n\tERROR: you don't seem to have gopher set up (no public_gopher directory)")
return
"""
os.makedirs(public_gopher)
2017-12-04 03:37:43 +00:00
ttbp_gopher = os.path.join(public_gopher, gopher_path)
2017-12-04 04:04:46 +00:00
if os.path.isdir(ttbp_gopher):
print("\n\tERROR: gopher path is already set up. quitting so we don't overwrite anything.")
return
gopher_entries = os.path.join(os.path.expanduser("~/.ttbp"), "gopher")
2018-01-03 05:38:16 +00:00
if not os.path.isdir(gopher_entries):
os.makedirs(gopher_entries)
subprocess.call(["ln", "-s", gopher_entries, ttbp_gopher])
def unpublish():
"""blanks all gopher things and recreates the directories."""
subprocess.call(["rm", "-rf", config.GOPHER_PATH])
subprocess.call(["rm", "-rf", config.GOPHER_ENTRIES])
os.mkdir(config.GOPHER_ENTRIES)
subprocess.call(["ln", "-s", config.GOPHER_ENTRIES, config.GOPHER_PATH])