support for best.pony

This commit is contained in:
Mattias Andrée 2012-08-21 21:29:24 +02:00
parent 3e686cd2e0
commit 2eb9342b86

30
ponysay
View file

@ -47,6 +47,7 @@ class Ponysay():
args.help() args.help()
return return
if args.opts['-h'] is not None: args.help() if args.opts['-h'] is not None: args.help()
elif args.opts['--quoters'] is not None: self.quoters() elif args.opts['--quoters'] is not None: self.quoters()
elif args.opts['--onelist'] is not None: self.onelist() elif args.opts['--onelist'] is not None: self.onelist()
@ -54,8 +55,17 @@ class Ponysay():
elif args.opts['-l'] is not None: self.list() elif args.opts['-l'] is not None: self.list()
elif args.opts['-L'] is not None: self.linklist() elif args.opts['-L'] is not None: self.linklist()
elif args.opts['-B'] is not None: self.balloonlist() elif args.opts['-B'] is not None: self.balloonlist()
elif args.opts['-q'] is not None: self.quote(args) else:
else: self.print_pony(args) if (args.opts['-f'] is None) or (args.opts['-q'] is None) or (len(args.opts['-q']) == 0):
for ponydir in ponydirs:
if os.path.isfile(ponydir + 'best.pony') or os.path.islink(ponydir + 'best.pony'):
pony = os.path.realpath(ponydir + 'best.pony') # Canonical path
if args.opts['-q'] is not None: args.opts['-q'] = [pony]
else: args.opts['-f'] = [pony]
break
if args.opts['-q'] is not None: self.quote(args)
else: self.print_pony(args)
## ##
@ -480,11 +490,19 @@ class Ponysay():
def quote(self, args): def quote(self, args):
pairs = self.__quotes() pairs = self.__quotes()
if len(args.opts['-q']) > 0: if len(args.opts['-q']) > 0:
ponyset = set(args.opts['-q']) ponyset = {}
for pony in args.opts['-q']:
if (len(pony) > 5) and (pony[-5:] == '.pony'):
ponyname = pony[:-5]
if '/' in ponyname:
ponyname = ponyname[ponyname.rindex('/') + 1:]
ponyset[ponyname] = pony
else:
ponyset[pony] = pony
alts = [] alts = []
for pair in pairs: for pair in pairs:
if pair[0] in ponyset: if pair[0] in ponyset:
alts.append(pair) alts.append((ponyset[pair[0]], pair[1]))
pairs = alts pairs = alts
if not len(pairs) == 0: if not len(pairs) == 0:
@ -498,11 +516,11 @@ class Ponysay():
qfile.close() qfile.close()
args.opts['-f'] = [pair[0]] args.opts['-f'] = [pair[0]]
elif len(args.opts['-q']) == 0: elif len(args.opts['-q']) == 0:
sys.stderr.write('All the ponies are mute! Call the Princess!\n') sys.stderr.write('Princess Celestia! All the ponies are mute!\n')
exit(1) exit(1)
else: else:
args.opts['-f'] = [args.opts['-q'][random.randrange(0, len(args.opts['-q']))]] args.opts['-f'] = [args.opts['-q'][random.randrange(0, len(args.opts['-q']))]]
args.message = 'I got nuthin\' good to say :(' args.message = 'Zecora! Help me, I am mute!'
self.print_pony(args) self.print_pony(args)