anon.hmm.st/utils.py

43 lines
921 B
Python

import sys,os,contextlib,builtins,email,re
NOT_ALPHANUM = re.compile("[^A-Za-z0-9]")
inp = email.message_from_file(sys.stdin)
homedir = os.path.dirname(__file__)
ship = "anon.hmm.st"
def input():
return inp
def setcwd(c):
homedir = c
@contextlib.contextmanager
def open(*args,**kwargs):
args = list(args)
fn = os.path.join(homedir,args.pop(0))
f = builtins.open(fn,*args,**kwargs)
try:
yield f
finally:
f.close()
def getshipdir():
return os.path.join(homedir,"ships",ship)
def get_name_from_title(title):
shipdir = getshipdir()
t = NOT_ALPHANUM.sub("-",title)
t = t[:20]
if os.path.exists(os.path.join(shipdir,t+".txt")):
n = 1
while os.path.exists(os.path.join(shipdir,t+str(n)+".txt")):
n+=1
return os.path.join(shipdir,t+str(n)+".txt")
else: return os.path.join(shipdir,t+".txt")
def write_file(filename,text):
with open(filename,"w") as f:
f.write(text)
os.chmod(filename,0o664)