mirror of
https://github.com/erkin/ponysay.git
synced 2025-03-29 14:57:43 +01:00
minor bug fix in ponysay-tool + more flexible code
Signed-off-by: Mattias Andrée <maandree@operamail.com>
This commit is contained in:
parent
8db29c00b6
commit
5456154b80
2 changed files with 110 additions and 92 deletions
|
@ -89,7 +89,10 @@ class PonysayTool():
|
||||||
cmd %= ('-echo -icanon -echo -isig -ixoff -ixon', os.path.realpath('/dev/stdout'))
|
cmd %= ('-echo -icanon -echo -isig -ixoff -ixon', os.path.realpath('/dev/stdout'))
|
||||||
Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).wait()
|
Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).wait()
|
||||||
print('\033[?25l', end='') # hide cursor
|
print('\033[?25l', end='') # hide cursor
|
||||||
self.browse(opts['-b'][0], opts['-r'])
|
dir = opts['-b'][0]
|
||||||
|
if not dir.endswith(os.sep):
|
||||||
|
dir += os.sep
|
||||||
|
self.browse(dir, opts['-r'])
|
||||||
finally:
|
finally:
|
||||||
print('\033[?25h', end='') # show cursor
|
print('\033[?25h', end='') # show cursor
|
||||||
cmd = 'stty %s < %s > /dev/null 2> /dev/null'
|
cmd = 'stty %s < %s > /dev/null 2> /dev/null'
|
||||||
|
|
197
src/ponysay.py
197
src/ponysay.py
|
@ -272,10 +272,10 @@ class Ponysay():
|
||||||
## Run modes
|
## Run modes
|
||||||
if test('-h'): args.help()
|
if test('-h'): args.help()
|
||||||
elif test('-v'): self.version()
|
elif test('-v'): self.version()
|
||||||
elif test('--quoters'): self.quoters()
|
elif test('--quoters'): self.quoters(True, False)
|
||||||
elif test('--Onelist', ('--onelist', '++onelist')): self.fullonelist()
|
elif test('--Onelist', ('--onelist', '++onelist')): self.onelist(True, True)
|
||||||
elif test('--onelist'): self.onelist()
|
elif test('--onelist'): self.onelist(True, False)
|
||||||
elif test('++onelist'): self.__extraponies(); self.onelist()
|
elif test('++onelist'): self.onelist(False, True)
|
||||||
elif test('+A', ('-L', '+L')): self.linklist(); self.__extraponies(); self.linklist()
|
elif test('+A', ('-L', '+L')): self.linklist(); self.__extraponies(); self.linklist()
|
||||||
elif test('-A', ('-l', '+l')): self.list(); self.__extraponies(); self.list()
|
elif test('-A', ('-l', '+l')): self.list(); self.__extraponies(); self.list()
|
||||||
elif test('-L'): self.linklist()
|
elif test('-L'): self.linklist()
|
||||||
|
@ -335,7 +335,7 @@ class Ponysay():
|
||||||
'''
|
'''
|
||||||
Use extra ponies
|
Use extra ponies
|
||||||
|
|
||||||
@param args:ArgParser Parsed command line arguments, may be `None`
|
@param args:ArgParser Parsed command line arguments, `None` to force rather than by reading arguments
|
||||||
'''
|
'''
|
||||||
def __extraponies(self, args = None):
|
def __extraponies(self, args = None):
|
||||||
## If extraponies are used, change ponydir to extraponydir
|
## If extraponies are used, change ponydir to extraponydir
|
||||||
|
@ -349,12 +349,15 @@ class Ponysay():
|
||||||
'''
|
'''
|
||||||
Use best.pony if nothing else is set
|
Use best.pony if nothing else is set
|
||||||
|
|
||||||
@param args:ArgParser Parsed command line arguments
|
@param args:ArgParser Parsed command line arguments
|
||||||
|
@param ponydirs:itr<str> Pony directories to use
|
||||||
'''
|
'''
|
||||||
def __bestpony(self, args):
|
def __bestpony(self, args, ponydirs = None):
|
||||||
|
if ponydirs is None: ponydirs = self.ponydirs
|
||||||
|
|
||||||
## Set best.pony as the pony to display if none is selected
|
## Set best.pony as the pony to display if none is selected
|
||||||
if (args.opts['-f'] is None) or (args.opts['-q'] is None) or (len(args.opts['-q']) == 0):
|
if (args.opts['-f'] is None) or (args.opts['-q'] is None) or (len(args.opts['-q']) == 0):
|
||||||
for ponydir in self.ponydirs:
|
for ponydir in ponydirs:
|
||||||
if os.path.isfile(ponydir + 'best.pony') or os.path.islink(ponydir + 'best.pony'):
|
if os.path.isfile(ponydir + 'best.pony') or os.path.islink(ponydir + 'best.pony'):
|
||||||
pony = os.path.realpath(ponydir + 'best.pony') # Canonical path
|
pony = os.path.realpath(ponydir + 'best.pony') # Canonical path
|
||||||
args.opts['-f' if args.opts['-q'] is None else '-q'] = [pony]
|
args.opts['-f' if args.opts['-q'] is None else '-q'] = [pony]
|
||||||
|
@ -456,15 +459,17 @@ class Ponysay():
|
||||||
'''
|
'''
|
||||||
Returns one file with full path, names is filter for names, also accepts filepaths
|
Returns one file with full path, names is filter for names, also accepts filepaths
|
||||||
|
|
||||||
@param names:list<str> Ponies to choose from, may be `None`
|
@param names:list<str> Ponies to choose from, may be `None`
|
||||||
@param alt:bool For method internal use...
|
@param alt:bool For method internal use...
|
||||||
@return :str The file name of a pony
|
@param ponydirs:itr<str> The pony directories to use
|
||||||
|
@return :str The file name of a pony
|
||||||
'''
|
'''
|
||||||
def __getponypath(self, names = None, alt = False):
|
def __getponypath(self, names = None, alt = False, ponydirs = None):
|
||||||
|
if ponydirs is None: ponydirs = self.ponydirs
|
||||||
ponies = {}
|
ponies = {}
|
||||||
|
|
||||||
## List all pony files, without the .pony ending
|
## List all pony files, without the .pony ending
|
||||||
for ponydir in self.ponydirs:
|
for ponydir in ponydirs:
|
||||||
for ponyfile in os.listdir(ponydir):
|
for ponyfile in os.listdir(ponydir):
|
||||||
if endswith(ponyfile, '.pony'):
|
if endswith(ponyfile, '.pony'):
|
||||||
pony = ponyfile[:-5]
|
pony = ponyfile[:-5]
|
||||||
|
@ -532,7 +537,7 @@ class Ponysay():
|
||||||
if self.restriction is not None:
|
if self.restriction is not None:
|
||||||
logic = Ponysay.makeRestrictionLogic(self.restriction)
|
logic = Ponysay.makeRestrictionLogic(self.restriction)
|
||||||
ponies = {}
|
ponies = {}
|
||||||
for ponydir in self.ponydirs:
|
for ponydir in ponydirs:
|
||||||
for pony in Ponysay.restrictedPonies(ponydir, logic):
|
for pony in Ponysay.restrictedPonies(ponydir, logic):
|
||||||
if (pony not in passed) and (pony in oldponies):
|
if (pony not in passed) and (pony in oldponies):
|
||||||
ponyfile = ponydir + pony + '.pony'
|
ponyfile = ponydir + pony + '.pony'
|
||||||
|
@ -541,7 +546,7 @@ class Ponysay():
|
||||||
oldponies = ponies
|
oldponies = ponies
|
||||||
ponies = {}
|
ponies = {}
|
||||||
(termh, termw) = self.__gettermsize()
|
(termh, termw) = self.__gettermsize()
|
||||||
for ponydir in self.ponydirs:
|
for ponydir in ponydirs:
|
||||||
(fitw, fith) = (None, None)
|
(fitw, fith) = (None, None)
|
||||||
if os.path.exists(ponydir + 'widths'):
|
if os.path.exists(ponydir + 'widths'):
|
||||||
fitw = set()
|
fitw = set()
|
||||||
|
@ -570,7 +575,7 @@ class Ponysay():
|
||||||
pony = names[random.randrange(0, len(names))]
|
pony = names[random.randrange(0, len(names))]
|
||||||
if pony not in ponies:
|
if pony not in ponies:
|
||||||
if not alt:
|
if not alt:
|
||||||
autocorrect = SpelloCorrecter(self.ponydirs, '.pony')
|
autocorrect = SpelloCorrecter(ponydirs, '.pony')
|
||||||
(alternatives, dist) = autocorrect.correct(pony)
|
(alternatives, dist) = autocorrect.correct(pony)
|
||||||
limit = os.environ['PONYSAY_TYPO_LIMIT'] if 'PONYSAY_TYPO_LIMIT' in os.environ else ''
|
limit = os.environ['PONYSAY_TYPO_LIMIT'] if 'PONYSAY_TYPO_LIMIT' in os.environ else ''
|
||||||
limit = 5 if len(limit) == 0 else int(dist)
|
limit = 5 if len(limit) == 0 else int(dist)
|
||||||
|
@ -650,7 +655,7 @@ class Ponysay():
|
||||||
'''
|
'''
|
||||||
Get ponies that pass restriction
|
Get ponies that pass restriction
|
||||||
|
|
||||||
@param ponydir:str Pony directory
|
@param ponydir:str Pony directory, must end with `os.sep`
|
||||||
@param logic:dict<str, str>→bool Restriction test functor
|
@param logic:dict<str, str>→bool Restriction test functor
|
||||||
@return :list<str> Passed ponies
|
@return :list<str> Passed ponies
|
||||||
'''
|
'''
|
||||||
|
@ -672,14 +677,19 @@ class Ponysay():
|
||||||
'''
|
'''
|
||||||
Returns a set with all ponies that have quotes and are displayable
|
Returns a set with all ponies that have quotes and are displayable
|
||||||
|
|
||||||
@return :set<str> All ponies that have quotes and are displayable
|
@param ponydirs:itr<str> The pony directories to use
|
||||||
|
@param quotedirs:itr<str> The quote directories to use
|
||||||
|
@return :set<str> All ponies that have quotes and are displayable
|
||||||
'''
|
'''
|
||||||
def __quoters(self):
|
def __quoters(self, ponydirs = None, quotedirs = None):
|
||||||
|
if ponydirs is None: ponydirs = self.ponydirs
|
||||||
|
if quotedirs is None: quotedirs = self.quotedirs
|
||||||
|
|
||||||
## List all unique quote files
|
## List all unique quote files
|
||||||
quotes = []
|
quotes = []
|
||||||
quoteshash = set()
|
quoteshash = set()
|
||||||
_quotes = []
|
_quotes = []
|
||||||
for quotedir in self.quotedirs:
|
for quotedir in quotedirs:
|
||||||
_quotes += [item[:item.index('.')] for item in os.listdir(quotedir)]
|
_quotes += [item[:item.index('.')] for item in os.listdir(quotedir)]
|
||||||
for quote in _quotes:
|
for quote in _quotes:
|
||||||
if not quote == '':
|
if not quote == '':
|
||||||
|
@ -689,7 +699,7 @@ class Ponysay():
|
||||||
|
|
||||||
## Create a set of all ponyes that have quotes
|
## Create a set of all ponyes that have quotes
|
||||||
ponies = set()
|
ponies = set()
|
||||||
for ponydir in self.ponydirs:
|
for ponydir in ponydirs:
|
||||||
for pony in os.listdir(ponydir):
|
for pony in os.listdir(ponydir):
|
||||||
if not pony[0] == '.':
|
if not pony[0] == '.':
|
||||||
p = pony[:-5] # remove .pony
|
p = pony[:-5] # remove .pony
|
||||||
|
@ -704,17 +714,22 @@ class Ponysay():
|
||||||
'''
|
'''
|
||||||
Returns a list with all (pony, quote file) pairs
|
Returns a list with all (pony, quote file) pairs
|
||||||
|
|
||||||
|
@param ponydirs:itr<str> The pony directories to use
|
||||||
|
@param quotedirs:itr<str> The quote directories to use
|
||||||
@return (pony, quote):(str, str) All ponies–quote file-pairs
|
@return (pony, quote):(str, str) All ponies–quote file-pairs
|
||||||
'''
|
'''
|
||||||
def __quotes(self):
|
def __quotes(self, ponydirs = None, quotedirs = None):
|
||||||
|
if ponydirs is None: ponydirs = self.ponydirs
|
||||||
|
if quotedirs is None: quotedirs = self.quotedirs
|
||||||
|
|
||||||
## Get all ponyquote files
|
## Get all ponyquote files
|
||||||
quotes = []
|
quotes = []
|
||||||
for quotedir in self.quotedirs:
|
for quotedir in quotedirs:
|
||||||
quotes += [quotedir + item for item in os.listdir(quotedir)]
|
quotes += [quotedir + item for item in os.listdir(quotedir)]
|
||||||
|
|
||||||
## Create list of all pony–quote file-pairs
|
## Create list of all pony–quote file-pairs
|
||||||
rc = []
|
rc = []
|
||||||
for ponydir in self.ponydirs:
|
for ponydir in ponydirs:
|
||||||
for pony in os.listdir(ponydir):
|
for pony in os.listdir(ponydir):
|
||||||
if not pony[0] == '.':
|
if not pony[0] == '.':
|
||||||
p = pony[:-5] # remove .pony
|
p = pony[:-5] # remove .pony
|
||||||
|
@ -803,12 +818,16 @@ class Ponysay():
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Lists the available ponies
|
Lists the available ponies
|
||||||
|
|
||||||
|
@param ponydirs:itr<str> The pony directories to use
|
||||||
'''
|
'''
|
||||||
def list(self):
|
def list(self, ponydirs = None):
|
||||||
|
if ponydirs is None: ponydirs = self.ponydirs
|
||||||
|
|
||||||
## Get all quoters
|
## Get all quoters
|
||||||
quoters = self.__quoters()
|
quoters = self.__quoters()
|
||||||
|
|
||||||
for ponydir in self.ponydirs: # Loop ponydirs
|
for ponydir in ponydirs: # Loop ponydirs
|
||||||
## Get all ponies in the directory
|
## Get all ponies in the directory
|
||||||
_ponies = os.listdir(ponydir)
|
_ponies = os.listdir(ponydir)
|
||||||
|
|
||||||
|
@ -830,13 +849,17 @@ class Ponysay():
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Lists the available ponies with alternatives inside brackets
|
Lists the available ponies with alternatives inside brackets
|
||||||
|
|
||||||
|
@param ponydirs:itr<str> The pony directories to use
|
||||||
'''
|
'''
|
||||||
def linklist(self):
|
def linklist(self, ponydirs = None):
|
||||||
|
if ponydirs is None: ponydirs = self.ponydirs
|
||||||
|
|
||||||
## Get the size of the terminal and all ponies with quotes
|
## Get the size of the terminal and all ponies with quotes
|
||||||
termsize = self.__gettermsize()
|
termsize = self.__gettermsize()
|
||||||
quoters = self.__quoters()
|
quoters = self.__quoters()
|
||||||
|
|
||||||
for ponydir in self.ponydirs: # Loop ponydirs
|
for ponydir in ponydirs: # Loop ponydirs
|
||||||
## Get all pony files in the directory
|
## Get all pony files in the directory
|
||||||
_ponies = os.listdir(ponydir)
|
_ponies = os.listdir(ponydir)
|
||||||
|
|
||||||
|
@ -903,18 +926,28 @@ class Ponysay():
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Lists with all ponies that have quotes and are displayable, on one column without anything bold or otherwise formated
|
Lists with all ponies that have quotes and are displayable, on one column without anything bold or otherwise formated
|
||||||
|
|
||||||
|
@param standard:bool Include standard ponies
|
||||||
|
@param extra:bool Include extra ponies
|
||||||
'''
|
'''
|
||||||
def quoters(self):
|
def quoters(self, standard = True, extra = False):
|
||||||
## Get all quoters
|
## Get all quoters
|
||||||
ponies = self.__quoters()
|
ponies = list(self.__quoters()) if standard else []
|
||||||
|
|
||||||
## UCS:ise and sort
|
if standard:
|
||||||
self.__ucsise(ponies)
|
## UCS:ise
|
||||||
ponies = list(ponies)
|
self.__ucsise(ponies)
|
||||||
ponies.sort()
|
|
||||||
|
## And now the extra ponies
|
||||||
|
if extra:
|
||||||
|
self.__extraponies()
|
||||||
|
xponies = list(self.__quoters())
|
||||||
|
self.__ucsise(xponies)
|
||||||
|
ponies += xponies
|
||||||
|
|
||||||
## Print each one on a seperate line, but skip duplicates
|
## Print each one on a seperate line, but skip duplicates
|
||||||
last = ''
|
last = ''
|
||||||
|
ponies.sort()
|
||||||
for pony in ponies:
|
for pony in ponies:
|
||||||
if not pony == last:
|
if not pony == last:
|
||||||
last = pony
|
last = pony
|
||||||
|
@ -923,69 +956,51 @@ class Ponysay():
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Lists the available ponies on one column without anything bold or otherwise formated
|
Lists the available ponies on one column without anything bold or otherwise formated
|
||||||
|
|
||||||
|
@param standard:bool Include standard ponies
|
||||||
|
@param extra:bool Include extra ponies
|
||||||
'''
|
'''
|
||||||
def onelist(self):
|
def onelist(self, standard = True, extra = False):
|
||||||
## Get all pony files
|
## All ponies
|
||||||
_ponies = []
|
|
||||||
for ponydir in self.ponydirs: # Loop ponydirs
|
|
||||||
_ponies += os.listdir(ponydir)
|
|
||||||
|
|
||||||
## Remove .pony from all files and skip those that does not have .pony
|
|
||||||
ponies = []
|
ponies = []
|
||||||
for pony in _ponies:
|
|
||||||
if endswith(pony, '.pony'):
|
|
||||||
ponies.append(pony[:-5])
|
|
||||||
|
|
||||||
## UCS:ise and sort
|
if standard:
|
||||||
self.__ucsise(ponies)
|
## Get all pony files
|
||||||
ponies.sort()
|
_ponies = []
|
||||||
|
for ponydir in self.ponydirs: # Loop ponydirs
|
||||||
|
_ponies += os.listdir(ponydir)
|
||||||
|
|
||||||
|
## Remove .pony from all files and skip those that does not have .pony
|
||||||
|
for pony in _ponies:
|
||||||
|
if endswith(pony, '.pony'):
|
||||||
|
ponies.append(pony[:-5])
|
||||||
|
|
||||||
|
## UCS:ise
|
||||||
|
self.__ucsise(ponies)
|
||||||
|
|
||||||
|
if extra:
|
||||||
|
## Swap to extra ponies
|
||||||
|
self.__extraponies()
|
||||||
|
|
||||||
|
## Get all pony files
|
||||||
|
_ponies = []
|
||||||
|
for ponydir in self.ponydirs: # Loop ponydirs
|
||||||
|
_ponies += os.listdir(ponydir)
|
||||||
|
|
||||||
|
## Remove .pony from all files and skip those that does not have .pony
|
||||||
|
xponies = []
|
||||||
|
for pony in _ponies:
|
||||||
|
if endswith(pony, '.pony'):
|
||||||
|
xponies.append(pony[:-5])
|
||||||
|
|
||||||
|
## UCS:ise
|
||||||
|
self.__ucsise(xponies)
|
||||||
|
|
||||||
|
## Add found extra ponies
|
||||||
|
ponies += xponies
|
||||||
|
|
||||||
## Print each one on a seperate line, but skip duplicates
|
## Print each one on a seperate line, but skip duplicates
|
||||||
last = ''
|
last = ''
|
||||||
for pony in ponies:
|
|
||||||
if not pony == last:
|
|
||||||
last = pony
|
|
||||||
print(pony)
|
|
||||||
|
|
||||||
|
|
||||||
'''
|
|
||||||
Lists the available ponies on one column without anything bold or otherwise formated, both standard ponies and extra ponies
|
|
||||||
'''
|
|
||||||
def fullonelist(self):
|
|
||||||
## Get all pony files
|
|
||||||
_ponies = []
|
|
||||||
for ponydir in self.ponydirs: # Loop ponydirs
|
|
||||||
_ponies += os.listdir(ponydir)
|
|
||||||
|
|
||||||
## Remove .pony from all files and skip those that does not have .pony
|
|
||||||
ponies = []
|
|
||||||
for pony in _ponies:
|
|
||||||
if endswith(pony, '.pony'):
|
|
||||||
ponies.append(pony[:-5])
|
|
||||||
|
|
||||||
## UCS:ise
|
|
||||||
self.__ucsise(ponies)
|
|
||||||
|
|
||||||
## Swap to extra ponies
|
|
||||||
self.__extraponies()
|
|
||||||
|
|
||||||
## Get all pony files
|
|
||||||
_ponies = []
|
|
||||||
for ponydir in self.ponydirs: # Loop ponydirs
|
|
||||||
_ponies += os.listdir(ponydir)
|
|
||||||
|
|
||||||
## Remove .pony from all files and skip those that does not have .pony
|
|
||||||
xponies = []
|
|
||||||
for pony in _ponies:
|
|
||||||
if endswith(pony, '.pony'):
|
|
||||||
xponies.append(pony[:-5])
|
|
||||||
|
|
||||||
## UCS:ise
|
|
||||||
self.__ucsise(xponies)
|
|
||||||
|
|
||||||
## Print each one on a seperate line, but skip duplicates
|
|
||||||
last = ''
|
|
||||||
ponies += xponies
|
|
||||||
ponies.sort()
|
ponies.sort()
|
||||||
for pony in ponies:
|
for pony in ponies:
|
||||||
if not pony == last:
|
if not pony == last:
|
||||||
|
|
Loading…
Add table
Reference in a new issue