Merge pull request #22 from tildetown/gopher

adding gopher support features from ~vilmibm
master
hvincent 2017-12-04 19:33:29 -05:00 committed by GitHub
commit a65dd546a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 127 additions and 25 deletions

View File

@ -73,6 +73,28 @@ def reload_ttbprc(ttbprc={}):
SETTINGS = ttbprc SETTINGS = ttbprc
def get_files():
"""Returns a list of user's feels."""
files = []
for filename in os.listdir(config.USER_DATA):
if nopub(filename):
link = os.path.join(config.WWW,
os.path.splitext(
os.path.basename(filename))[0]+".html")
if os.path.exists(link):
subprocess.call(["rm", link])
continue
filename = os.path.join(config.USER_DATA, filename)
if os.path.isfile(filename) and valid(filename):
files.append(filename)
files.sort()
files.reverse()
return files
def load_files(): def load_files():
''' '''
file loader file loader
@ -83,20 +105,7 @@ def load_files():
global FILES global FILES
FILES = [] FILES = get_files()
for filename in os.listdir(config.USER_DATA):
if nopub(filename):
link = os.path.join(config.WWW, os.path.splitext(os.path.basename(filename))[0]+".html")
if os.path.exists(link):
subprocess.call(["rm", link])
continue
filename = os.path.join(config.USER_DATA, filename)
if os.path.isfile(filename) and valid(filename):
FILES.append(filename)
FILES.sort()
FILES.reverse()
## html outputting ## html outputting

85
ttbp/gopher.py 100644
View File

@ -0,0 +1,85 @@
"""
This module contains gopher-related stuff.
"""
import getpass
import os
from . import util
GOPHER_PROMPT = """
Would you like to publish your feels to gopher?
gopher is a pre-web technology that is text-oriented and primarily used to
share folders of your files with the world.
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 feels on gopher.
.:: .::
.: .::
.:.: .: .:: .:: .:: .::::
.:: .: .:: .: .:: .::.::
.:: .::::: .::.::::: .:: .:: .:::
.:: .: .: .:: .::
.:: .:::: .:::: .:::.:: .::
this file was created on their behalf by ttbp.
"""
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
entry_filenames.reverse()
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.')
return
with open(os.path.join(ttbp_gopher, 'gophermap'), 'w') as gophermap:
gophermap.write(GOPHERMAP_HEADER.format(
user=getpass.getuser()))
for entry_filename in entry_filenames:
filename = os.path.basename(entry_filename)
with open(os.path.join(ttbp_gopher, filename), 'w') as gopher_entry:
with open(entry_filename, 'r') as source_entry:
gopher_entry.write(source_entry.read())
gophermap.write('0{file_label}\t{filename}'.format(
file_label=os.path.basename(entry_filename),
filename=filename))
def setup_gopher(gopher_path):
"""Given a path relative to ~/public_gopher, this function:
- creates a directory under public_gopher
- creates a landing page
It doesn't create a gophermap as that is left to the publish_gopher
function.
"""
public_gopher = os.path.expanduser('~/public_gopher')
if not os.path.isdir(public_gopher):
print("\n\tERROR: you don't seem to have gopher set up (no public_gopher directory)")
return
ttbp_gopher = os.path.join(public_gopher, gopher_path)
if os.path.isdir(ttbp_gopher):
print("\n\tERROR: gopher path is already set up. quitting so we don't overwrite anything.")
return
os.makedirs(ttbp_gopher)

View File

@ -34,13 +34,11 @@ https://github.com/modgethanc/ttbp
from __future__ import absolute_import from __future__ import absolute_import
import os import os
import random
import tempfile import tempfile
import subprocess import subprocess
import time import time
import json import json
from email.mime.text import MIMEText; from email.mime.text import MIMEText
import re
import datetime import datetime
import inflect import inflect
@ -48,6 +46,7 @@ import inflect
from . import config from . import config
from . import core from . import core
from . import chatter from . import chatter
from . import gopher
from . import util from . import util
__version__ = "0.9.3" __version__ = "0.9.3"
@ -58,7 +57,8 @@ p = inflect.engine()
## user globals ## user globals
SETTINGS = { SETTINGS = {
"editor": "none", "editor": "none",
"publish dir": False "publish dir": False,
"gopher": False,
} }
## ui globals ## ui globals
@ -72,7 +72,7 @@ RAINBOW = False
## ref ## ref
EDITORS = ["vim", "vi", "emacs", "pico", "nano", "ed"] EDITORS = ["vim", "vi", "emacs", "pico", "nano", "ed", "micro"]
SUBJECTS = ["help request", "bug report", "feature suggestion", "general comment"] SUBJECTS = ["help request", "bug report", "feature suggestion", "general comment"]
## ttbp specific utilities ## ttbp specific utilities
@ -388,7 +388,9 @@ def setup():
print("\n\ttext editor:\t" +SETTINGS.get("editor")) print("\n\ttext editor:\t" +SETTINGS.get("editor"))
if core.publishing(): if core.publishing():
print("\tpublish dir:\t" +os.path.join(config.PUBLIC, str(SETTINGS.get("publish dir")))) print("\tpublish dir:\t" +os.path.join(config.PUBLIC, str(SETTINGS.get("publish dir"))))
print("\tpubishing:\t"+str(SETTINGS.get("publishing"))+"\n") print("\tpublishing:\t"+str(SETTINGS.get("publishing")))
print("\tgopher:\t"+str(SETTINGS.get('gopher')))
print("")
# editor selection # editor selection
SETTINGS.update({"editor": select_editor()}) SETTINGS.update({"editor": select_editor()})
@ -403,6 +405,13 @@ def setup():
if core.publishing(): if core.publishing():
print("publish directory: ~"+config.USER+"/public_html/"+SETTINGS.get("publish dir")) print("publish directory: ~"+config.USER+"/public_html/"+SETTINGS.get("publish dir"))
# gopher opt-in
SETTINGS.update({'gopher': gopher.select_gopher()})
redraw('opting into gopher: ' + str(SETTINGS['gopher']))
# TODO for now i'm hardcoding where people's gopher stuff is generated. if
# there is demand for this to be configurable we can expose that.
gopher.setup_gopher('feels')
# save settings # save settings
ttbprc = open(config.TTBPRC, "w") ttbprc = open(config.TTBPRC, "w")
ttbprc.write(json.dumps(SETTINGS, sort_keys=True, indent=2, separators=(',',':'))) ttbprc.write(json.dumps(SETTINGS, sort_keys=True, indent=2, separators=(',',':')))
@ -705,6 +714,9 @@ editor.
core.load_files() core.load_files()
core.write("index.html") core.write("index.html")
left = "posted to "+config.LIVE+config.USER+"/"+str(SETTINGS.get("publish dir"))+"/index.html\n\n>" left = "posted to "+config.LIVE+config.USER+"/"+str(SETTINGS.get("publish dir"))+"/index.html\n\n>"
if SETTINGS['gopher']:
gopher.publish_gopher('feels', core.get_files())
redraw(left + " thanks for sharing your feels!") redraw(left + " thanks for sharing your feels!")
return return

View File

@ -212,8 +212,4 @@ def input_yn(query):
while ans not in ["y", "n"]: while ans not in ["y", "n"]:
ans = raw_input("'y' or 'n' please: ") ans = raw_input("'y' or 'n' please: ")
if ans == "y": return ans == "y"
return True
else:
return False