cleaner code

This commit is contained in:
Mattias Andrée 2012-09-30 02:55:48 +02:00
parent 19609f75b2
commit 1ca607912a

View file

@ -45,6 +45,14 @@ def print(text = '', end = '\n'):
sys.stdout.buffer.write((str(text) + end).encode('utf-8')) sys.stdout.buffer.write((str(text) + end).encode('utf-8'))
'''
Checks whether a text ends with a specific text, but has more
'''
def endswith(text, ending):
return text.endswith(ending) and not (text == ending);
''' '''
This is the mane class of ponysay This is the mane class of ponysay
''' '''
@ -174,7 +182,7 @@ class Ponysay():
map = {} map = {}
stripset = ' \t' # must be string, wtf! and way doesn't python's doc say so stripset = ' \t' # must be string, wtf! and way doesn't python's doc say so
for line in maplines: for line in maplines:
if (len(line) > 0) and not (line[0] == '#'): if not line.startswith('#'):
s = line.index('') s = line.index('')
ucs = line[:s] .strip(stripset) ucs = line[:s] .strip(stripset)
ascii = line[s + 1:].strip(stripset) ascii = line[s + 1:].strip(stripset)
@ -361,7 +369,7 @@ class Ponysay():
## Remove .pony from all files and skip those that does not have .pony ## Remove .pony from all files and skip those that does not have .pony
ponies = [] ponies = []
for pony in _ponies: for pony in _ponies:
if (len(pony) > 5) and (pony[-5:] == '.pony'): if endswith(pony, '.pony'):
ponies.append(pony[:-5]) ponies.append(pony[:-5])
## UCS:ise pony names, they are already sorted ## UCS:ise pony names, they are already sorted
@ -389,7 +397,7 @@ class Ponysay():
## Remove .pony from all files and skip those that does not have .pony ## Remove .pony from all files and skip those that does not have .pony
ponies = [] ponies = []
for pony in _ponies: for pony in _ponies:
if (len(pony) > 5) and (pony[-5:] == '.pony'): if endswith(pony, '.pony'):
ponies.append(pony[:-5]) ponies.append(pony[:-5])
## If there are no ponies in the directory skip to next directory, otherwise, print the directories name ## If there are no ponies in the directory skip to next directory, otherwise, print the directories name
@ -478,7 +486,7 @@ class Ponysay():
## Remove .pony from all files and skip those that does not have .pony ## Remove .pony from all files and skip those that does not have .pony
ponies = [] ponies = []
for pony in _ponies: for pony in _ponies:
if (len(pony) > 5) and (pony[-5:] == '.pony'): if endswith(pony, '.pony'):
ponies.append(pony[:-5]) ponies.append(pony[:-5])
## USC:ise and sort ## USC:ise and sort
@ -510,9 +518,9 @@ class Ponysay():
for balloondir in balloondirs: for balloondir in balloondirs:
for balloon in os.listdir(balloondir): for balloon in os.listdir(balloondir):
## Use .think if running ponythink, otherwise .say ## Use .think if running ponythink, otherwise .say
if isthink and (len(balloon) > 6) and (balloon[-6:] == '.think'): if isthink and endswith(balloon, '.think'):
balloon = balloon[:-6] balloon = balloon[:-6]
elif (not isthink) and (len(balloon) > 4) and (balloon[-4:] == '.say'): elif (not isthink) and endswith(balloon, '.say'):
balloon = balloon[:-4] balloon = balloon[:-4]
else: else:
continue continue
@ -539,9 +547,9 @@ class Ponysay():
for balloon in os.listdir(balloondir): for balloon in os.listdir(balloondir):
balloonfile = balloon balloonfile = balloon
## Use .think if running ponythink, otherwise .say ## Use .think if running ponythink, otherwise .say
if isthink and (len(balloon) > 6) and (balloon[-6:] == '.think'): if isthink and endswith(balloon, '.think'):
balloon = balloon[:-6] balloon = balloon[:-6]
elif (not isthink) and (len(balloon) > 4) and (balloon[-4:] == '.say'): elif (not isthink) and endswith(balloon, '.say'):
balloon = balloon[:-4] balloon = balloon[:-4]
else: else:
continue continue
@ -648,7 +656,7 @@ class Ponysay():
pony = self.__getponypath(args.opts['-f']) pony = self.__getponypath(args.opts['-f'])
## Use PNG file as pony file ## Use PNG file as pony file
if (len(pony) > 4) and (pony[-4:].lower() == '.png'): if endswith(pony.lower(), '.png'):
pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\'' pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\''
pngcmd = ('img2ponysay -p -- ' if linuxvt else 'img2ponysay -- ') + pony pngcmd = ('img2ponysay -p -- ' if linuxvt else 'img2ponysay -- ') + pony
pngpipe = os.pipe() pngpipe = os.pipe()
@ -675,7 +683,7 @@ class Ponysay():
backend = Backend(message = msg, ponyfile = pony, wrapcolumn = messagewrap if messagewrap is not None else 40, width = widthtruncation, balloon = balloon) backend = Backend(message = msg, ponyfile = pony, wrapcolumn = messagewrap if messagewrap is not None else 40, width = widthtruncation, balloon = balloon)
backend.parse() backend.parse()
output = backend.output output = backend.output
if (len(output) > 0) and (output[-1] == '\n'): if output.endswith('\n'):
output = output[:-1] output = output[:-1]
@ -711,7 +719,7 @@ class Ponysay():
if len(args.opts['-q']) > 0: if len(args.opts['-q']) > 0:
ponyset = {} ponyset = {}
for pony in args.opts['-q']: for pony in args.opts['-q']:
if (len(pony) > 5) and (pony[-5:] == '.pony'): if endswith(pony, '.pony'):
ponyname = pony[:-5] ponyname = pony[:-5]
if '/' in ponyname: if '/' in ponyname:
ponyname = ponyname[ponyname.rindex('/') + 1:] ponyname = ponyname[ponyname.rindex('/') + 1:]
@ -1286,7 +1294,7 @@ class Backend():
name = dollar[:find('=')] name = dollar[:find('=')]
value = dollar[find('=') + 1:] value = dollar[find('=') + 1:]
variables[name] = value variables[name] = value
elif (len(dollar) < 7) or not (dollar[:7] == 'balloon'): elif not dollar.startswith('balloon'):
data = variables[dollar].replace('$', '$$') data = variables[dollar].replace('$', '$$')
if data == '$$': # if not handled specially we will get an infinity loop if data == '$$': # if not handled specially we will get an infinity loop
if (skip == 0) or (nonskip > 0): if (skip == 0) or (nonskip > 0):
@ -1819,8 +1827,8 @@ opts.add_argumentless(['-l', '--list'], help =
opts.add_argumentless(['-L', '--symlist', '--altlist'], help = 'List pony names with alternatives.') opts.add_argumentless(['-L', '--symlist', '--altlist'], help = 'List pony names with alternatives.')
opts.add_argumentless(['+l', '++list'], help = 'List non-MLP:FiM pony names.') opts.add_argumentless(['+l', '++list'], help = 'List non-MLP:FiM pony names.')
opts.add_argumentless(['+L', '++symlist', '++altlist'], help = 'List non-MLP:FiM pony names with alternatives.') opts.add_argumentless(['+L', '++symlist', '++altlist'], help = 'List non-MLP:FiM pony names with alternatives.')
opts.add_argumentless(['-A', '--all'], help = 'List MLP:FIM and non-MLP:FiM pony names.') opts.add_argumentless(['-A', '--all'], help = 'List all pony names.')
opts.add_argumentless(['+A', '++all'], help = 'List MLP:FIM and non-MLP:FiM pony names with alternatives.') opts.add_argumentless(['+A', '++all'], help = 'List all pony names with alternatives.')
opts.add_argumentless(['-B', '--bubblelist', '--balloonlist'], help = 'List balloon styles.') opts.add_argumentless(['-B', '--bubblelist', '--balloonlist'], help = 'List balloon styles.')
opts.add_argumentless(['-c', '--compact'], help = 'Compress messages.') opts.add_argumentless(['-c', '--compact'], help = 'Compress messages.')
opts.add_argumented( ['-W', '--wrap'], arg = 'COLUMN', help = 'Specify column where the message should be wrapped.') opts.add_argumented( ['-W', '--wrap'], arg = 'COLUMN', help = 'Specify column where the message should be wrapped.')