mirror of
https://github.com/erkin/ponysay
synced 2024-11-16 00:17:59 +00:00
documenting the code
This commit is contained in:
parent
9919d3e228
commit
19609f75b2
1 changed files with 40 additions and 6 deletions
44
ponysay.py
44
ponysay.py
|
@ -568,18 +568,22 @@ class Ponysay():
|
||||||
Creates the balloon style object
|
Creates the balloon style object
|
||||||
'''
|
'''
|
||||||
def __getballoon(self, balloonfile):
|
def __getballoon(self, balloonfile):
|
||||||
|
## Use default balloon if none is specified
|
||||||
if balloonfile is None:
|
if balloonfile is None:
|
||||||
if isthink:
|
if isthink:
|
||||||
return Balloon('o', 'o', '( ', ' )', [' _'], ['_'], ['_'], ['_'], ['_ '], ' )', ' )', ' )', ['- '], ['-'], ['-'], ['-'], [' -'], '( ', '( ', '( ')
|
return Balloon('o', 'o', '( ', ' )', [' _'], ['_'], ['_'], ['_'], ['_ '], ' )', ' )', ' )', ['- '], ['-'], ['-'], ['-'], [' -'], '( ', '( ', '( ')
|
||||||
return Balloon('\\', '/', '< ', ' >', [' _'], ['_'], ['_'], ['_'], ['_ '], ' \\', ' |', ' /', ['- '], ['-'], ['-'], ['-'], [' -'], '\\ ', '| ', '/ ')
|
return Balloon('\\', '/', '< ', ' >', [' _'], ['_'], ['_'], ['_'], ['_ '], ' \\', ' |', ' /', ['- '], ['-'], ['-'], ['-'], [' -'], '\\ ', '| ', '/ ')
|
||||||
|
|
||||||
|
## Initialise map for balloon parts
|
||||||
map = {}
|
map = {}
|
||||||
for elem in ('\\', '/', 'ww', 'ee', 'nw', 'nnw', 'n', 'nne', 'ne', 'nee', 'e', 'see', 'se', 'sse', 's', 'ssw', 'sw', 'sww', 'w', 'nww'):
|
for elem in ('\\', '/', 'ww', 'ee', 'nw', 'nnw', 'n', 'nne', 'ne', 'nee', 'e', 'see', 'se', 'sse', 's', 'ssw', 'sw', 'sww', 'w', 'nww'):
|
||||||
map[elem] = []
|
map[elem] = []
|
||||||
|
|
||||||
|
## Read all lines in the balloon file
|
||||||
with open(balloonfile, 'rb') as balloonstream:
|
with open(balloonfile, 'rb') as balloonstream:
|
||||||
data = [line.replace('\n', '') for line in balloonstream.read().decode('utf8', 'replace').split('\n')]
|
data = [line.replace('\n', '') for line in balloonstream.read().decode('utf8', 'replace').split('\n')]
|
||||||
|
|
||||||
|
## Parse the balloon file, and fill the map
|
||||||
last = None
|
last = None
|
||||||
for line in data:
|
for line in data:
|
||||||
if len(line) > 0:
|
if len(line) > 0:
|
||||||
|
@ -590,6 +594,7 @@ class Ponysay():
|
||||||
value = line[len(last) + 1:]
|
value = line[len(last) + 1:]
|
||||||
map[last].append(value)
|
map[last].append(value)
|
||||||
|
|
||||||
|
## Return the balloon
|
||||||
return Balloon(map['\\'][0], map['/'][0], map['ww'][0], map['ee'][0], map['nw'], map['nnw'], map['n'],
|
return Balloon(map['\\'][0], map['/'][0], map['ww'][0], map['ee'][0], map['nw'], map['nnw'], map['n'],
|
||||||
map['nne'], map['ne'], map['nee'][0], map['e'][0], map['see'][0], map['se'], map['sse'],
|
map['nne'], map['ne'], map['nee'][0], map['e'][0], map['see'][0], map['se'], map['sse'],
|
||||||
map['s'], map['ssw'], map['sw'], map['sww'][0], map['w'][0], map['nww'][0])
|
map['s'], map['ssw'], map['sw'], map['sww'][0], map['w'][0], map['nww'][0])
|
||||||
|
@ -604,6 +609,7 @@ class Ponysay():
|
||||||
Prints the name of the program and the version of the program
|
Prints the name of the program and the version of the program
|
||||||
'''
|
'''
|
||||||
def version(self):
|
def version(self):
|
||||||
|
## Prints the "ponysay $VERSION", if this is modified, ./dev/dist.sh must be modified accordingly
|
||||||
print('%s %s' % ('ponysay', VERSION))
|
print('%s %s' % ('ponysay', VERSION))
|
||||||
|
|
||||||
|
|
||||||
|
@ -611,12 +617,14 @@ class Ponysay():
|
||||||
Print the pony with a speech or though bubble. message, pony and wrap from args are used.
|
Print the pony with a speech or though bubble. message, pony and wrap from args are used.
|
||||||
'''
|
'''
|
||||||
def print_pony(self, args):
|
def print_pony(self, args):
|
||||||
|
## Get message and remove tailing whitespace from stdin (but not for each line)
|
||||||
if args.message == None:
|
if args.message == None:
|
||||||
msg = ''.join(sys.stdin.readlines()).rstrip()
|
msg = ''.join(sys.stdin.readlines()).rstrip()
|
||||||
else:
|
else:
|
||||||
msg = args.message
|
msg = args.message
|
||||||
|
|
||||||
if args.opts['-c'] is not None: ## This algorithm should give some result as cowsay's (according to tests)
|
## This algorithm should give some result as cowsay's (according to tests)
|
||||||
|
if args.opts['-c'] is not None:
|
||||||
buf = ''
|
buf = ''
|
||||||
last = ' '
|
last = ' '
|
||||||
CHARS = '\t \n'
|
CHARS = '\t \n'
|
||||||
|
@ -636,8 +644,10 @@ class Ponysay():
|
||||||
last = c
|
last = c
|
||||||
msg = buf.replace('\n', '\n\n')
|
msg = buf.replace('\n', '\n\n')
|
||||||
|
|
||||||
|
## Get the pony
|
||||||
pony = self.__getponypath(args.opts['-f'])
|
pony = self.__getponypath(args.opts['-f'])
|
||||||
|
|
||||||
|
## Use PNG file as pony file
|
||||||
if (len(pony) > 4) and (pony[-4:].lower() == '.png'):
|
if (len(pony) > 4) and (pony[-4:].lower() == '.png'):
|
||||||
pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\''
|
pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\''
|
||||||
pngcmd = ('img2ponysay -p -- ' if linuxvt else 'img2ponysay -- ') + pony
|
pngcmd = ('img2ponysay -p -- ' if linuxvt else 'img2ponysay -- ') + pony
|
||||||
|
@ -645,18 +655,23 @@ class Ponysay():
|
||||||
Popen(pngcmd, stdout=os.fdopen(pngpipe[1], 'w'), shell=True).wait()
|
Popen(pngcmd, stdout=os.fdopen(pngpipe[1], 'w'), shell=True).wait()
|
||||||
pony = '/proc/' + str(os.getpid()) + '/fd/' + str(pngpipe[0])
|
pony = '/proc/' + str(os.getpid()) + '/fd/' + str(pngpipe[0])
|
||||||
|
|
||||||
|
## If KMS is utilies, select a KMS pony file and create it if necessary
|
||||||
pony = self.__kms(pony)
|
pony = self.__kms(pony)
|
||||||
|
|
||||||
|
## If in Linux VT clean the terminal (See info/pdf-manual [Printing in TTY with KMS])
|
||||||
if linuxvt:
|
if linuxvt:
|
||||||
print('\033[H\033[2J', end='')
|
print('\033[H\033[2J', end='')
|
||||||
|
|
||||||
|
## Width Get truncation and wrapping
|
||||||
env_width = os.environ['PONYSAY_FULL_WIDTH'] if 'PONYSAY_FULL_WIDTH' in os.environ else None
|
env_width = os.environ['PONYSAY_FULL_WIDTH'] if 'PONYSAY_FULL_WIDTH' in os.environ else None
|
||||||
if env_width is None: env_width = ''
|
if env_width is None: env_width = ''
|
||||||
widthtruncation = self.__gettermsize()[1] if env_width not in ('yes', 'y', '1') else None
|
widthtruncation = self.__gettermsize()[1] if env_width not in ('yes', 'y', '1') else None
|
||||||
messagewrap = int(args.opts['-W'][0]) if args.opts['-W'] is not None else None
|
messagewrap = int(args.opts['-W'][0]) if args.opts['-W'] is not None else None
|
||||||
|
|
||||||
|
## Get balloon object
|
||||||
balloon = self.__getballoon(self.__getballoonpath(args.opts['-b']))
|
balloon = self.__getballoon(self.__getballoonpath(args.opts['-b']))
|
||||||
|
|
||||||
|
## Run cowsay replacement
|
||||||
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
|
||||||
|
@ -664,6 +679,7 @@ class Ponysay():
|
||||||
output = output[:-1]
|
output = output[:-1]
|
||||||
|
|
||||||
|
|
||||||
|
## Load height trunction settings
|
||||||
env_bottom = os.environ['PONYSAY_BOTTOM'] if 'PONYSAY_BOTTOM' in os.environ else None
|
env_bottom = os.environ['PONYSAY_BOTTOM'] if 'PONYSAY_BOTTOM' in os.environ else None
|
||||||
if env_bottom is None: env_bottom = ''
|
if env_bottom is None: env_bottom = ''
|
||||||
|
|
||||||
|
@ -673,9 +689,8 @@ class Ponysay():
|
||||||
env_lines = os.environ['PONYSAY_SHELL_LINES'] if 'PONYSAY_SHELL_LINES' in os.environ else None
|
env_lines = os.environ['PONYSAY_SHELL_LINES'] if 'PONYSAY_SHELL_LINES' in os.environ else None
|
||||||
if (env_lines is None) or (env_lines == ''): env_lines = '2'
|
if (env_lines is None) or (env_lines == ''): env_lines = '2'
|
||||||
|
|
||||||
|
## Print the output, truncated on height is so set
|
||||||
lines = self.__gettermsize()[0] - int(env_lines)
|
lines = self.__gettermsize()[0] - int(env_lines)
|
||||||
|
|
||||||
|
|
||||||
if linuxvt or (env_height is ('yes', 'y', '1')):
|
if linuxvt or (env_height is ('yes', 'y', '1')):
|
||||||
if env_bottom is ('yes', 'y', '1'):
|
if env_bottom is ('yes', 'y', '1'):
|
||||||
for line in output.split('\n')[: -lines]:
|
for line in output.split('\n')[: -lines]:
|
||||||
|
@ -691,6 +706,7 @@ class Ponysay():
|
||||||
Print the pony with a speech or though bubble and a self quote
|
Print the pony with a speech or though bubble and a self quote
|
||||||
'''
|
'''
|
||||||
def quote(self, args):
|
def quote(self, args):
|
||||||
|
## Get all quotes, and if any pony is choosen just keep them
|
||||||
pairs = self.__quotes()
|
pairs = self.__quotes()
|
||||||
if len(args.opts['-q']) > 0:
|
if len(args.opts['-q']) > 0:
|
||||||
ponyset = {}
|
ponyset = {}
|
||||||
|
@ -708,6 +724,7 @@ class Ponysay():
|
||||||
alts.append((ponyset[pair[0]], pair[1]))
|
alts.append((ponyset[pair[0]], pair[1]))
|
||||||
pairs = alts
|
pairs = alts
|
||||||
|
|
||||||
|
## Select a random pony–quote-pair, load it and print it
|
||||||
if not len(pairs) == 0:
|
if not len(pairs) == 0:
|
||||||
pair = pairs[random.randrange(0, len(pairs))]
|
pair = pairs[random.randrange(0, len(pairs))]
|
||||||
with open(pair[1], 'rb') as qfile:
|
with open(pair[1], 'rb') as qfile:
|
||||||
|
@ -724,22 +741,26 @@ class Ponysay():
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Indentifies whether KMS support is utilised
|
Identifies whether KMS support is utilised
|
||||||
'''
|
'''
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def isUsingKMS():
|
def isUsingKMS():
|
||||||
|
## KMS is not utilised if Linux VT is not used
|
||||||
if not linuxvt:
|
if not linuxvt:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
## Read the PONYSAY_KMS_PALETTE environment variable
|
||||||
env_kms = os.environ['PONYSAY_KMS_PALETTE'] if 'PONYSAY_KMS_PALETTE' in os.environ else None
|
env_kms = os.environ['PONYSAY_KMS_PALETTE'] if 'PONYSAY_KMS_PALETTE' in os.environ else None
|
||||||
if env_kms is None: env_kms = ''
|
if env_kms is None: env_kms = ''
|
||||||
|
|
||||||
|
## Read the PONYSAY_KMS_PALETTE_CMD environment variable, and run it
|
||||||
env_kms_cmd = os.environ['PONYSAY_KMS_PALETTE_CMD'] if 'PONYSAY_KMS_PALETTE_CMD' in os.environ else None
|
env_kms_cmd = os.environ['PONYSAY_KMS_PALETTE_CMD'] if 'PONYSAY_KMS_PALETTE_CMD' in os.environ else None
|
||||||
if (env_kms_cmd is not None) and (not env_kms_cmd == ''):
|
if (env_kms_cmd is not None) and (not env_kms_cmd == ''):
|
||||||
env_kms = Popen(shlex.split(env_kms_cmd), stdout=PIPE, stdin=sys.stderr).communicate()[0].decode('utf8', 'replace')
|
env_kms = Popen(shlex.split(env_kms_cmd), stdout=PIPE, stdin=sys.stderr).communicate()[0].decode('utf8', 'replace')
|
||||||
if env_kms[-1] == '\n':
|
if env_kms[-1] == '\n':
|
||||||
env_kms = env_kms[:-1]
|
env_kms = env_kms[:-1]
|
||||||
|
|
||||||
|
## If the palette string is empty KMS is not utilised
|
||||||
return env_kms != ''
|
return env_kms != ''
|
||||||
|
|
||||||
|
|
||||||
|
@ -747,32 +768,40 @@ class Ponysay():
|
||||||
Returns the file name of the input pony converted to a KMS pony, or if KMS is not used, the input pony itself
|
Returns the file name of the input pony converted to a KMS pony, or if KMS is not used, the input pony itself
|
||||||
'''
|
'''
|
||||||
def __kms(self, pony):
|
def __kms(self, pony):
|
||||||
|
## If not in Linux VT, return the pony as is
|
||||||
if not linuxvt:
|
if not linuxvt:
|
||||||
return pony
|
return pony
|
||||||
|
|
||||||
|
## KMS support version constant
|
||||||
KMS_VERSION = '1'
|
KMS_VERSION = '1'
|
||||||
|
|
||||||
|
## Read the PONYSAY_KMS_PALETTE environment variable
|
||||||
env_kms = os.environ['PONYSAY_KMS_PALETTE'] if 'PONYSAY_KMS_PALETTE' in os.environ else None
|
env_kms = os.environ['PONYSAY_KMS_PALETTE'] if 'PONYSAY_KMS_PALETTE' in os.environ else None
|
||||||
if env_kms is None: env_kms = ''
|
if env_kms is None: env_kms = ''
|
||||||
|
|
||||||
|
## Read the PONYSAY_KMS_PALETTE_CMD environment variable, and run it
|
||||||
env_kms_cmd = os.environ['PONYSAY_KMS_PALETTE_CMD'] if 'PONYSAY_KMS_PALETTE_CMD' in os.environ else None
|
env_kms_cmd = os.environ['PONYSAY_KMS_PALETTE_CMD'] if 'PONYSAY_KMS_PALETTE_CMD' in os.environ else None
|
||||||
if (env_kms_cmd is not None) and (not env_kms_cmd == ''):
|
if (env_kms_cmd is not None) and (not env_kms_cmd == ''):
|
||||||
env_kms = Popen(shlex.split(env_kms_cmd), stdout=PIPE, stdin=sys.stderr).communicate()[0].decode('utf8', 'replace')
|
env_kms = Popen(shlex.split(env_kms_cmd), stdout=PIPE, stdin=sys.stderr).communicate()[0].decode('utf8', 'replace')
|
||||||
if env_kms[-1] == '\n':
|
if env_kms[-1] == '\n':
|
||||||
env_kms = env_kms[:-1]
|
env_kms = env_kms[:-1]
|
||||||
|
|
||||||
|
## If not using KMS, return the pony as is
|
||||||
if env_kms == '':
|
if env_kms == '':
|
||||||
return pony
|
return pony
|
||||||
|
|
||||||
|
## Store palette string and a clong with just the essentials
|
||||||
palette = env_kms
|
palette = env_kms
|
||||||
palettefile = env_kms.replace('\033]P', '')
|
palettefile = env_kms.replace('\033]P', '')
|
||||||
|
|
||||||
|
## Get and in necessary make cache directory
|
||||||
cachedir = '/var/cache/ponysay'
|
cachedir = '/var/cache/ponysay'
|
||||||
if not os.path.isdir(cachedir):
|
if not os.path.isdir(cachedir):
|
||||||
cachedir = HOME + '/.cache/ponysay'
|
cachedir = HOME + '/.cache/ponysay'
|
||||||
if not os.path.isdir(cachedir):
|
if not os.path.isdir(cachedir):
|
||||||
os.makedirs(cachedir)
|
os.makedirs(cachedir)
|
||||||
|
|
||||||
|
## KMS support version control, clean everything if not matching
|
||||||
newversion = False
|
newversion = False
|
||||||
if not os.path.isfile(cachedir + '/.version'):
|
if not os.path.isfile(cachedir + '/.version'):
|
||||||
newversion = True
|
newversion = True
|
||||||
|
@ -790,20 +819,24 @@ class Ponysay():
|
||||||
with open(cachedir + '/.version', 'w+') as cachev:
|
with open(cachedir + '/.version', 'w+') as cachev:
|
||||||
cachev.write(KMS_VERSION)
|
cachev.write(KMS_VERSION)
|
||||||
|
|
||||||
|
## Get kmspony directory and kmspony file
|
||||||
kmsponies = cachedir + '/kmsponies/' + palettefile
|
kmsponies = cachedir + '/kmsponies/' + palettefile
|
||||||
kmspony = (kmsponies + pony).replace('//', '/')
|
kmspony = (kmsponies + pony).replace('//', '/')
|
||||||
|
|
||||||
|
## If the kmspony is missing, create it
|
||||||
if not os.path.isfile(kmspony):
|
if not os.path.isfile(kmspony):
|
||||||
|
## Protokmsponies are uncolourful ttyponies
|
||||||
protokmsponies = cachedir + '/protokmsponies/'
|
protokmsponies = cachedir + '/protokmsponies/'
|
||||||
protokmspony = (protokmsponies + pony).replace('//', '/')
|
protokmspony = (protokmsponies + pony).replace('//', '/')
|
||||||
|
|
||||||
protokmsponydir = protokmspony[:protokmspony.rindex('/')]
|
protokmsponydir = protokmspony[:protokmspony.rindex('/')]
|
||||||
kmsponydir = kmspony[: kmspony.rindex('/')]
|
kmsponydir = kmspony[: kmspony.rindex('/')]
|
||||||
|
|
||||||
|
## Change file names to be shell friendly
|
||||||
_protokmspony = '\'' + protokmspony.replace('\'', '\'\\\'\'') + '\''
|
_protokmspony = '\'' + protokmspony.replace('\'', '\'\\\'\'') + '\''
|
||||||
_kmspony = '\'' + kmspony.replace('\'', '\'\\\'\'') + '\''
|
_kmspony = '\'' + kmspony.replace('\'', '\'\\\'\'') + '\''
|
||||||
_pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\''
|
_pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\''
|
||||||
|
|
||||||
|
## Create protokmspony is missing
|
||||||
if not os.path.isfile(protokmspony):
|
if not os.path.isfile(protokmspony):
|
||||||
if not os.path.isdir(protokmsponydir):
|
if not os.path.isdir(protokmsponydir):
|
||||||
os.makedirs(protokmsponydir)
|
os.makedirs(protokmsponydir)
|
||||||
|
@ -811,6 +844,7 @@ class Ponysay():
|
||||||
sys.stderr.write('Unable to run ponysay2ttyponysay successfully, you need util-say for KMS support\n')
|
sys.stderr.write('Unable to run ponysay2ttyponysay successfully, you need util-say for KMS support\n')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
## Create kmspony
|
||||||
if not os.path.isdir(kmsponydir):
|
if not os.path.isdir(kmsponydir):
|
||||||
os.makedirs(kmsponydir)
|
os.makedirs(kmsponydir)
|
||||||
if not os.system('tty2colourfultty -p ' + palette + ' < ' + _protokmspony + ' > ' + _kmspony) == 0:
|
if not os.system('tty2colourfultty -p ' + palette + ' < ' + _protokmspony + ' > ' + _kmspony) == 0:
|
||||||
|
|
Loading…
Reference in a new issue