2012-08-18 13:15:12 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
'''
|
|
|
|
ponysay.py - POC of ponysay in python
|
2012-08-18 17:34:10 +02:00
|
|
|
Copyright (C) 2012 Erkin Batu Altunbaş
|
|
|
|
|
|
|
|
Authors: Erkin Batu Altunbaş: Project leader, helped write the first implementation
|
|
|
|
Mattias "maandree" Andrée: Major contributor of both implementions
|
|
|
|
Elis "etu" Axelsson Major contributor of current implemention and patcher of first implementation
|
|
|
|
Sven-Hendrik "svenstaro" Haase: Helped write the first implementation
|
|
|
|
Kyah "L-four" Rindlisbacher: Patched the first implementation
|
|
|
|
Jan Alexander "heftig" Steffens: Helped write the first implementation
|
2012-08-18 13:15:12 +02:00
|
|
|
|
|
|
|
License: WTFPL
|
|
|
|
'''
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import random
|
2012-08-18 14:26:53 +02:00
|
|
|
from subprocess import Popen, PIPE
|
2012-08-18 13:15:12 +02:00
|
|
|
|
|
|
|
|
2012-08-18 14:26:53 +02:00
|
|
|
'''
|
|
|
|
The version of ponysay
|
|
|
|
'''
|
2012-08-18 16:46:38 +02:00
|
|
|
VERSION = '2.0-alpha'
|
2012-08-18 14:26:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
The directory where ponysay is installed, this is modified when building with make
|
|
|
|
'''
|
2012-08-18 14:29:57 +02:00
|
|
|
INSTALLDIR = '/usr'
|
2012-08-18 13:15:12 +02:00
|
|
|
|
|
|
|
|
2012-08-18 17:09:52 +02:00
|
|
|
'''
|
|
|
|
The user's home directory
|
|
|
|
'''
|
|
|
|
HOME = os.environ['HOME']
|
|
|
|
|
|
|
|
|
2012-08-18 14:26:53 +02:00
|
|
|
'''
|
|
|
|
The directories where pony files are stored, ttyponies/ are used if the terminal is Linux VT (also known as TTY)
|
|
|
|
'''
|
|
|
|
ponydirs = []
|
2012-08-18 17:03:30 +02:00
|
|
|
if os.environ['TERM'] == 'linux': _ponydirs = [HOME + '/.local/share/ponysay/ttyponies/', INSTALLDIR + '/share/ponysay/ttyponies/']
|
|
|
|
else: _ponydirs = [HOME + '/.local/share/ponysay/ponies/', INSTALLDIR + '/share/ponysay/ponies/' ]
|
2012-08-18 14:26:53 +02:00
|
|
|
for ponydir in _ponydirs:
|
|
|
|
if os.path.isdir(ponydir):
|
|
|
|
ponydirs.append(ponydir)
|
|
|
|
|
2012-08-18 16:55:57 +02:00
|
|
|
|
2012-08-18 16:20:49 +02:00
|
|
|
'''
|
|
|
|
The directories where quotes files are stored
|
|
|
|
'''
|
|
|
|
quotedirs = []
|
2012-08-18 17:03:30 +02:00
|
|
|
_quotedirs = [HOME + '/.local/share/ponysay/quotes/', INSTALLDIR + '/share/ponysay/quotes/']
|
2012-08-18 16:20:49 +02:00
|
|
|
for quotedir in _quotedirs:
|
|
|
|
if os.path.isdir(quotedir):
|
|
|
|
quotedirs.append(quotedir)
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 16:46:38 +02:00
|
|
|
'''
|
|
|
|
Argument parsing
|
|
|
|
'''
|
2012-08-18 13:15:12 +02:00
|
|
|
parser = argparse.ArgumentParser(description = 'Ponysay, like cowsay with ponies')
|
|
|
|
|
2012-08-18 16:46:38 +02:00
|
|
|
parser.add_argument('-v', '--version', action = 'version', version = '%s %s' % ('ponysay', VERSION))
|
2012-08-18 16:08:24 +02:00
|
|
|
parser.add_argument('-l', '--list', action = 'store_true', dest = 'list', help = 'list pony files')
|
|
|
|
parser.add_argument('-L', '--altlist', action = 'store_true', dest = 'linklist', help = 'list pony files with alternatives')
|
2012-08-18 16:43:22 +02:00
|
|
|
parser.add_argument( '--quoters', action = 'store_true', dest = 'quoters', help = 'list ponies with quotes (visible in -l and -L)') # for shell completions
|
|
|
|
parser.add_argument( '--onelist', action = 'store_true', dest = 'onelist', help = 'list pony files in one columns') # for shell completions
|
2012-08-18 16:08:24 +02:00
|
|
|
parser.add_argument('-f', '--pony', action = 'append', dest = 'pony', help = 'select a pony (either a file name or a pony name)')
|
2012-08-18 13:15:12 +02:00
|
|
|
parser.add_argument('message', nargs = '?', help = 'message to ponysay')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
2012-08-18 17:09:52 +02:00
|
|
|
'''
|
|
|
|
This is the mane class of ponysay
|
|
|
|
'''
|
2012-08-18 13:15:12 +02:00
|
|
|
class ponysay():
|
2012-08-18 17:09:52 +02:00
|
|
|
'''
|
|
|
|
Starts the part of the program the arguments indicate
|
|
|
|
'''
|
2012-08-18 13:15:12 +02:00
|
|
|
def __init__(self, args):
|
2012-08-18 16:08:24 +02:00
|
|
|
if args.list: self.list()
|
|
|
|
elif args.linklist: self.linklist()
|
2012-08-18 16:43:22 +02:00
|
|
|
elif args.quoters: self.quoters()
|
|
|
|
elif args.onelist: self.onelist()
|
2012-08-18 16:08:24 +02:00
|
|
|
else: self.print_pony(args)
|
2012-08-18 13:15:12 +02:00
|
|
|
|
2012-08-18 14:26:53 +02:00
|
|
|
|
2012-08-18 17:03:30 +02:00
|
|
|
'''
|
|
|
|
Returns one .pony-file with full path, names is filter for names, also accepts filepaths
|
|
|
|
'''
|
|
|
|
def __getponypath(self, names = None):
|
|
|
|
ponies = {}
|
|
|
|
|
2012-08-18 17:09:52 +02:00
|
|
|
if not names == None:
|
2012-08-18 17:05:13 +02:00
|
|
|
for name in names:
|
|
|
|
if os.path.isfile(name):
|
|
|
|
return name
|
2012-08-18 17:03:30 +02:00
|
|
|
|
|
|
|
for ponydir in ponydirs:
|
|
|
|
for ponyfile in os.listdir(ponydir):
|
2012-08-18 17:14:00 +02:00
|
|
|
pony = ponyfile[:-5]
|
|
|
|
if pony not in ponies:
|
|
|
|
ponies[pony] = ponydir + ponyfile
|
2012-08-18 17:03:30 +02:00
|
|
|
|
2012-08-18 17:05:13 +02:00
|
|
|
if names == None:
|
|
|
|
names = list(ponies.keys())
|
|
|
|
|
2012-08-18 17:03:30 +02:00
|
|
|
return ponies[names[random.randrange(0, len(names))]]
|
|
|
|
|
|
|
|
|
2012-08-18 15:02:34 +02:00
|
|
|
'''
|
2012-08-18 16:43:22 +02:00
|
|
|
Returns a set with all ponies that have quotes and are displayable
|
2012-08-18 15:02:34 +02:00
|
|
|
'''
|
|
|
|
def __quoters(self):
|
|
|
|
quotes = []
|
|
|
|
quoteshash = set()
|
2012-08-18 16:20:49 +02:00
|
|
|
_quotes = []
|
|
|
|
for quotedir in quotedirs:
|
|
|
|
_quotes += [item[:item.index('.')] for item in os.listdir(INSTALLDIR + '/share/ponysay/quotes/')]
|
2012-08-18 15:02:34 +02:00
|
|
|
for quote in _quotes:
|
2012-08-18 15:10:49 +02:00
|
|
|
if not quote == '':
|
2012-08-18 15:02:34 +02:00
|
|
|
if not quote in quoteshash:
|
|
|
|
quoteshash.add(quote)
|
|
|
|
quotes.append(quote)
|
2012-08-18 15:10:49 +02:00
|
|
|
|
2012-08-18 15:02:34 +02:00
|
|
|
ponies = set()
|
|
|
|
for ponydir in ponydirs:
|
|
|
|
for pony in os.listdir(ponydir):
|
|
|
|
if not pony[0] == '.':
|
|
|
|
p = pony[:-5] # remove .pony
|
|
|
|
for quote in quotes:
|
|
|
|
if ('+' + p + '+') in ('+' + quote + '+'):
|
|
|
|
if not p in ponies:
|
|
|
|
ponies.add(p)
|
|
|
|
|
|
|
|
return ponies
|
|
|
|
|
2012-08-18 15:10:49 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
Returns a list with all (pony, quote file) pairs
|
|
|
|
'''
|
|
|
|
def __quotes(self):
|
2012-08-18 16:20:49 +02:00
|
|
|
quotes = []
|
|
|
|
for quotedir in quotedirs:
|
|
|
|
quotes += [quotedir + item for item in os.listdir(quotedir)]
|
2012-08-18 15:10:49 +02:00
|
|
|
rc = []
|
|
|
|
|
|
|
|
for ponydir in ponydirs:
|
|
|
|
for pony in os.listdir(ponydir):
|
|
|
|
if not pony[0] == '.':
|
|
|
|
p = pony[:-5] # remove .pony
|
|
|
|
for quote in quotes:
|
2012-08-18 16:20:49 +02:00
|
|
|
q = quote[quote.rindex('/') + 1:]
|
|
|
|
if ('+' + p + '+') in ('+' + q + '+'):
|
|
|
|
rc.append((p, quote))
|
2012-08-18 15:10:49 +02:00
|
|
|
|
|
|
|
return rc
|
|
|
|
|
|
|
|
|
2012-08-18 14:26:53 +02:00
|
|
|
'''
|
|
|
|
Lists the available ponies
|
|
|
|
'''
|
|
|
|
def list(self):
|
2012-08-18 16:46:38 +02:00
|
|
|
termsize = Popen(['stty', 'size'], stdout=PIPE).communicate()[0].decode('utf8', 'replace')[:-1].split(' ')
|
2012-08-18 14:26:53 +02:00
|
|
|
termsize = [int(item) for item in termsize]
|
2012-08-18 13:15:12 +02:00
|
|
|
|
2012-08-18 15:02:34 +02:00
|
|
|
quoters = self.__quoters()
|
|
|
|
|
2012-08-18 13:15:12 +02:00
|
|
|
for ponydir in ponydirs: # Loop ponydirs
|
2012-08-18 14:26:53 +02:00
|
|
|
print('\033[1mponyfiles located in ' + ponydir + '\033[21m')
|
2012-08-18 13:15:12 +02:00
|
|
|
|
|
|
|
ponies = os.listdir(ponydir)
|
2012-08-18 14:26:53 +02:00
|
|
|
ponies = [item[:-5] for item in ponies] # remove .pony from file name
|
2012-08-18 13:15:12 +02:00
|
|
|
ponies.sort()
|
2012-08-18 14:26:53 +02:00
|
|
|
|
2012-08-18 13:15:12 +02:00
|
|
|
width = len(max(ponies, key = len)) + 2 # Get the longest ponyfilename lenght + 2 spaces
|
|
|
|
|
|
|
|
x = 0
|
|
|
|
for pony in ponies:
|
2012-08-18 15:02:34 +02:00
|
|
|
spacing = ' ' * (width - len(pony))
|
2012-08-18 16:46:38 +02:00
|
|
|
print(('\033[1m' + pony + '\033[21m' if (pony in quoters) else pony) + spacing, end='') # Print ponyfilename
|
2012-08-18 14:29:57 +02:00
|
|
|
x += width
|
2012-08-18 13:15:12 +02:00
|
|
|
if x > (termsize[1] - width): # If too wide, make new line
|
2012-08-18 16:08:24 +02:00
|
|
|
print()
|
2012-08-18 13:15:12 +02:00
|
|
|
x = 0
|
2012-08-18 14:26:53 +02:00
|
|
|
|
2012-08-18 16:46:38 +02:00
|
|
|
print('\n');
|
2012-08-18 15:10:49 +02:00
|
|
|
|
|
|
|
|
2012-08-18 16:08:24 +02:00
|
|
|
'''
|
|
|
|
Lists the available ponies with alternatives inside brackets
|
|
|
|
'''
|
|
|
|
def linklist(self):
|
2012-08-18 16:46:38 +02:00
|
|
|
termsize = Popen(['stty', 'size'], stdout=PIPE).communicate()[0].decode('utf8', 'replace')[:-1].split(' ')
|
2012-08-18 16:08:24 +02:00
|
|
|
termsize = [int(item) for item in termsize]
|
|
|
|
|
|
|
|
quoters = self.__quoters()
|
|
|
|
|
|
|
|
for ponydir in ponydirs: # Loop ponydirs
|
|
|
|
print('\033[1mponyfiles located in ' + ponydir + '\033[21m')
|
|
|
|
|
|
|
|
files = os.listdir(ponydir)
|
|
|
|
files = [item[:-5] for item in files] # remove .pony from file name
|
|
|
|
files.sort()
|
2012-08-18 16:46:38 +02:00
|
|
|
pairs = [(item, os.readlink(ponydir + item + '.pony') if os.path.islink(ponydir + item + '.pony') else '') for item in files]
|
2012-08-18 16:08:24 +02:00
|
|
|
|
|
|
|
ponymap = {}
|
|
|
|
for pair in pairs:
|
2012-08-18 16:46:38 +02:00
|
|
|
if pair[1] == '':
|
2012-08-18 16:08:24 +02:00
|
|
|
if pair[0] not in ponymap:
|
|
|
|
ponymap[pair[0]] = []
|
|
|
|
else:
|
|
|
|
target = pair[1][:-5]
|
|
|
|
if '/' in target:
|
|
|
|
target = target[target.rindex('/') + 1:]
|
|
|
|
if target in ponymap:
|
|
|
|
ponymap[target].append(pair[0])
|
|
|
|
else:
|
|
|
|
ponymap[target] = [pair[0]]
|
|
|
|
|
|
|
|
width = 0
|
|
|
|
ponies = []
|
|
|
|
widths = []
|
|
|
|
for pony in ponymap:
|
|
|
|
w = len(pony)
|
|
|
|
item = '\033[1m' + pony + '\033[21m' if (pony in quoters) else pony
|
|
|
|
syms = ponymap[pony]
|
|
|
|
if len(syms) > 0:
|
|
|
|
w += 2 + len(syms)
|
2012-08-18 16:46:38 +02:00
|
|
|
item += ' ('
|
2012-08-18 16:08:24 +02:00
|
|
|
first = True
|
|
|
|
for sym in syms:
|
|
|
|
w += len(sym)
|
|
|
|
if not first:
|
2012-08-18 16:46:38 +02:00
|
|
|
item += ' '
|
2012-08-18 16:08:24 +02:00
|
|
|
else:
|
|
|
|
first = False
|
|
|
|
item += '\033[1m' + sym + '\033[21m' if (sym in quoters) else sym
|
2012-08-18 16:46:38 +02:00
|
|
|
item += ')'
|
2012-08-18 16:08:24 +02:00
|
|
|
ponies.append(item)
|
|
|
|
widths.append(w)
|
|
|
|
if width < w:
|
|
|
|
width = w
|
|
|
|
|
|
|
|
width += 2;
|
|
|
|
x = 0
|
|
|
|
index = 0
|
|
|
|
for pony in ponies:
|
|
|
|
spacing = ' ' * (width - widths[index])
|
|
|
|
index += 1
|
2012-08-18 16:46:38 +02:00
|
|
|
print(pony + spacing, end='') # Print ponyfilename
|
2012-08-18 16:08:24 +02:00
|
|
|
x += width
|
|
|
|
if x > (termsize[1] - width): # If too wide, make new line
|
|
|
|
print()
|
|
|
|
x = 0
|
|
|
|
|
2012-08-18 16:46:38 +02:00
|
|
|
print('\n');
|
2012-08-18 16:08:24 +02:00
|
|
|
|
|
|
|
|
2012-08-18 16:43:22 +02:00
|
|
|
'''
|
|
|
|
Lists with all ponies that have quotes and are displayable
|
|
|
|
'''
|
|
|
|
def quoters(self):
|
|
|
|
last = ""
|
|
|
|
ponies = []
|
|
|
|
for pony in self.__quoters():
|
|
|
|
ponies.append(pony)
|
|
|
|
ponies.sort()
|
|
|
|
for pony in ponies:
|
|
|
|
if not pony == last:
|
|
|
|
last = pony
|
|
|
|
print(pony)
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
Lists the available ponies one one column without anything bold
|
|
|
|
'''
|
|
|
|
def onelist(self):
|
|
|
|
last = ""
|
|
|
|
ponies = []
|
|
|
|
for ponydir in ponydirs: # Loop ponydirs
|
|
|
|
ponies += os.listdir(ponydir)
|
|
|
|
ponies = [item[:-5] for item in ponies] # remove .pony from file name
|
|
|
|
ponies.sort()
|
|
|
|
for pony in ponies:
|
|
|
|
if not pony == last:
|
|
|
|
last = pony
|
|
|
|
print(pony)
|
|
|
|
|
|
|
|
|
2012-08-18 13:15:12 +02:00
|
|
|
def print_pony(self, args):
|
|
|
|
if args.message == None:
|
2012-08-18 17:02:03 +02:00
|
|
|
msg = sys.stdin.read().strip()
|
2012-08-18 13:15:12 +02:00
|
|
|
else:
|
|
|
|
msg = args.message
|
|
|
|
|
2012-08-18 16:46:38 +02:00
|
|
|
pony = self.__getponypath(args.pony)
|
2012-08-18 13:15:12 +02:00
|
|
|
|
2012-08-18 17:22:22 +02:00
|
|
|
if "think" in __file__: cmd = 'cowthink'
|
|
|
|
else: cmd = 'cowsay'
|
|
|
|
|
|
|
|
os.system(cmd + ' -f ' + pony + ' \'' + msg + '\'')
|
2012-08-18 13:15:12 +02:00
|
|
|
|
|
|
|
|
2012-08-18 14:26:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
Start the program from ponysay.__init__ if this is the executed file
|
|
|
|
'''
|
2012-08-18 13:15:12 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
ponysay(args)
|