mirror of
https://github.com/erkin/ponysay.git
synced 2025-02-19 19:04:23 +01:00
misc
Signed-off-by: Mattias Andrée <maandree@operamail.com>
This commit is contained in:
parent
f44a77924d
commit
90028ec636
1 changed files with 94 additions and 110 deletions
214
src/ponysay.py
214
src/ponysay.py
|
@ -56,7 +56,70 @@ class Ponysay():
|
||||||
os.environ['HOME'] = self.HOME = os.path.expanduser('~')
|
os.environ['HOME'] = self.HOME = os.path.expanduser('~')
|
||||||
|
|
||||||
|
|
||||||
def parsefile(file):
|
## Change system enviroment variables with ponysayrc
|
||||||
|
for file in ('$XDG_CONFIG_HOME/ponysay/ponysayrc', '$HOME/.config/ponysay/ponysayrc', '$HOME/.ponysayrc', '/etc/ponysayrc'):
|
||||||
|
file = Ponysay.__parseFile(file)
|
||||||
|
if (file is not None) and os.path.exists(file):
|
||||||
|
with open(file, 'rb') as ponysayrc:
|
||||||
|
code = ponysayrc.read().decode('utf8', 'replace') + '\n'
|
||||||
|
env = os.environ
|
||||||
|
code = compile(code, file, 'exec')
|
||||||
|
exec(code)
|
||||||
|
break
|
||||||
|
|
||||||
|
self.HOME = os.environ['HOME'] if 'HOME' in os.environ else '' # in case ~/.ponysayrc changes it
|
||||||
|
if len(self.HOME) == 0:
|
||||||
|
os.environ['HOME'] = self.HOME = os.path.expanduser('~')
|
||||||
|
|
||||||
|
|
||||||
|
# Whether any unrecognised options was parsed, this should be set by the invoker before run()
|
||||||
|
self.unrecognised = False
|
||||||
|
|
||||||
|
|
||||||
|
# Whether the program is execute in Linux VT (TTY)
|
||||||
|
self.linuxvt = ('TERM' in os.environ) and (os.environ['TERM'] == 'linux')
|
||||||
|
|
||||||
|
# Whether the script is executed as ponythink
|
||||||
|
self.isthink = Ponysay.__isPonythink()
|
||||||
|
|
||||||
|
|
||||||
|
# Whether stdin is piped
|
||||||
|
self.pipelinein = not sys.stdin.isatty()
|
||||||
|
|
||||||
|
# Whether stdout is piped
|
||||||
|
self.pipelineout = not sys.stdout.isatty()
|
||||||
|
|
||||||
|
# Whether stderr is piped
|
||||||
|
self.pipelineerr = not sys.stderr.isatty()
|
||||||
|
|
||||||
|
|
||||||
|
# Whether KMS is used
|
||||||
|
self.usekms = KMS.usingKMS(self.linuxvt)
|
||||||
|
|
||||||
|
|
||||||
|
# Mode string that modifies or adds $ variables in the pony image
|
||||||
|
self.mode = ''
|
||||||
|
|
||||||
|
|
||||||
|
# The directories where pony files are stored, ttyponies/ are used if the terminal is Linux VT (also known as TTY) and not with KMS
|
||||||
|
self.xponydirs = Ponysay.__getShareDirectories('ponies/'):
|
||||||
|
self.vtponydirs = Ponysay.__getShareDirectories('ttyponies/'):
|
||||||
|
|
||||||
|
# The directories where pony files are stored, extrattyponies/ are used if the terminal is Linux VT (also known as TTY) and not with KMS
|
||||||
|
self.extraxponydirs = Ponysay.__getShareDirectories('extraponies/'):
|
||||||
|
self.extravtponydirs = Ponysay.__getShareDirectories('extrattyponies/'):
|
||||||
|
|
||||||
|
# The directories where quotes files are stored
|
||||||
|
self.quotedirs = Ponysay.__getShareDirectories('quotes/'):
|
||||||
|
|
||||||
|
# The directories where balloon style files are stored
|
||||||
|
self.balloondirs = Ponysay.__getShareDirectories('balloons/'):
|
||||||
|
|
||||||
|
# ucsmap files
|
||||||
|
self.ucsmaps = Ponysay.__getShareDirectories('ucsmap/'):
|
||||||
|
|
||||||
|
|
||||||
|
def __parseFile(file):
|
||||||
'''
|
'''
|
||||||
Parse a file name encoded with environment variables
|
Parse a file name encoded with environment variables
|
||||||
|
|
||||||
|
@ -90,63 +153,35 @@ class Ponysay():
|
||||||
return file
|
return file
|
||||||
|
|
||||||
|
|
||||||
## Change system enviroment variables with ponysayrc
|
def __getShareDirectories(directory):
|
||||||
for file in ('$XDG_CONFIG_HOME/ponysay/ponysayrc', '$HOME/.config/ponysay/ponysayrc', '$HOME/.ponysayrc', '/etc/ponysayrc'):
|
'''
|
||||||
file = parsefile(file)
|
Gets existing unique /share directories
|
||||||
if (file is not None) and os.path.exists(file):
|
|
||||||
with open(file, 'rb') as ponysayrc:
|
|
||||||
code = ponysayrc.read().decode('utf8', 'replace') + '\n'
|
|
||||||
env = os.environ
|
|
||||||
code = compile(code, file, 'exec')
|
|
||||||
exec(code)
|
|
||||||
break
|
|
||||||
|
|
||||||
self.HOME = os.environ['HOME'] if 'HOME' in os.environ else '' # in case ~/.ponysayrc changes it
|
@param directory:str The directory base name
|
||||||
if len(self.HOME) == 0:
|
@return :list<str> Absolute directory names
|
||||||
os.environ['HOME'] = self.HOME = os.path.expanduser('~')
|
'''
|
||||||
|
appendset = set()
|
||||||
|
rc = []
|
||||||
|
_ponydirs = Ponysay.__share(directory)
|
||||||
|
for ponydir in _ponydirs:
|
||||||
|
if (ponydir is not None) and os.path.isdir(ponydir) and (ponydir not in appendset):
|
||||||
|
rc.append(ponydir)
|
||||||
|
appendset.add(ponydir)
|
||||||
|
return rc
|
||||||
|
|
||||||
|
|
||||||
# Whether any unrecognised options was parsed, this should be set by the invoker before run()
|
def __share(file):
|
||||||
self.unrecognised = False
|
'''
|
||||||
|
Gets /share files
|
||||||
|
|
||||||
|
@param file:str The file base name
|
||||||
# Whether the program is execute in Linux VT (TTY)
|
@return :list<str> Absolute file names
|
||||||
self.linuxvt = ('TERM' in os.environ) and (os.environ['TERM'] == 'linux')
|
'''
|
||||||
|
|
||||||
|
|
||||||
# Whether the script is executed as ponythink
|
|
||||||
self.isthink = sys.argv[0]
|
|
||||||
if os.sep in self.isthink:
|
|
||||||
self.isthink = self.isthink[self.isthink.rfind(os.sep) + 1:]
|
|
||||||
if os.extsep in self.isthink:
|
|
||||||
self.isthink = self.isthink[:self.isthink.find(os.extsep)]
|
|
||||||
self.isthink = self.isthink.endswith('think')
|
|
||||||
|
|
||||||
|
|
||||||
# Whether stdin is piped
|
|
||||||
self.pipelinein = not sys.stdin.isatty()
|
|
||||||
|
|
||||||
# Whether stdout is piped
|
|
||||||
self.pipelineout = not sys.stdout.isatty()
|
|
||||||
|
|
||||||
# Whether stderr is piped
|
|
||||||
self.pipelineerr = not sys.stderr.isatty()
|
|
||||||
|
|
||||||
|
|
||||||
# Whether KMS is used
|
|
||||||
self.usekms = KMS.usingKMS(self.linuxvt)
|
|
||||||
|
|
||||||
|
|
||||||
# Mode string that modifies or adds $ variables in the pony image
|
|
||||||
self.mode = ''
|
|
||||||
|
|
||||||
|
|
||||||
def share(file):
|
|
||||||
def cat(a, b):
|
def cat(a, b):
|
||||||
if a is None:
|
if a is None:
|
||||||
return None
|
return None
|
||||||
return a + b
|
return a + b
|
||||||
return [cat(parsefile(item), file) for item in [
|
return [cat(Ponysay.__parseFile(item), file) for item in [
|
||||||
'./',
|
'./',
|
||||||
'$XDG_DATA_HOME/ponysay/',
|
'$XDG_DATA_HOME/ponysay/',
|
||||||
'$HOME/.local/share/ponysay/',
|
'$HOME/.local/share/ponysay/',
|
||||||
|
@ -154,68 +189,17 @@ class Ponysay():
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
|
||||||
# The directories where pony files are stored, ttyponies/ are used if the terminal is Linux VT (also known as TTY) and not with KMS
|
def __isPonythink():
|
||||||
appendset = set()
|
'''
|
||||||
self.xponydirs = []
|
Check if ponythink is executed
|
||||||
_ponydirs = share('ponies/')
|
'''
|
||||||
for ponydir in _ponydirs:
|
isthink = sys.argv[0]
|
||||||
if (ponydir is not None) and os.path.isdir(ponydir) and (ponydir not in appendset):
|
if os.sep in isthink:
|
||||||
self.xponydirs.append(ponydir)
|
isthink = isthink[isthink.rfind(os.sep) + 1:]
|
||||||
appendset.add(ponydir)
|
if os.extsep in isthink:
|
||||||
appendset = set()
|
isthink = isthink[:isthink.find(os.extsep)]
|
||||||
self.vtponydirs = []
|
isthink = isthink.endswith('think')
|
||||||
_ponydirs = share('ttyponies/')
|
return isthink
|
||||||
for ponydir in _ponydirs:
|
|
||||||
if (ponydir is not None) and os.path.isdir(ponydir) and (ponydir not in appendset):
|
|
||||||
self.vtponydirs.append(ponydir)
|
|
||||||
appendset.add(ponydir)
|
|
||||||
|
|
||||||
|
|
||||||
# The directories where pony files are stored, extrattyponies/ are used if the terminal is Linux VT (also known as TTY) and not with KMS
|
|
||||||
appendset = set()
|
|
||||||
self.extraxponydirs = []
|
|
||||||
_extraponydirs = share('extraponies/')
|
|
||||||
for extraponydir in _extraponydirs:
|
|
||||||
if (extraponydir is not None) and os.path.isdir(extraponydir) and (extraponydir not in appendset):
|
|
||||||
self.extraxponydirs.append(extraponydir)
|
|
||||||
appendset.add(extraponydir)
|
|
||||||
appendset = set()
|
|
||||||
self.extravtponydirs = []
|
|
||||||
_extraponydirs = share('extrattyponies/')
|
|
||||||
for extraponydir in _extraponydirs:
|
|
||||||
if (extraponydir is not None) and os.path.isdir(extraponydir) and (extraponydir not in appendset):
|
|
||||||
self.extravtponydirs.append(extraponydir)
|
|
||||||
appendset.add(extraponydir)
|
|
||||||
|
|
||||||
|
|
||||||
# The directories where quotes files are stored
|
|
||||||
appendset = set()
|
|
||||||
self.quotedirs = []
|
|
||||||
_quotedirs = share('quotes/')
|
|
||||||
for quotedir in _quotedirs:
|
|
||||||
if (quotedir is not None) and os.path.isdir(quotedir) and (quotedir not in appendset):
|
|
||||||
self.quotedirs.append(quotedir)
|
|
||||||
appendset.add(quotedir)
|
|
||||||
|
|
||||||
|
|
||||||
# The directories where balloon style files are stored
|
|
||||||
appendset = set()
|
|
||||||
self.balloondirs = []
|
|
||||||
_balloondirs = share('balloons/')
|
|
||||||
for balloondir in _balloondirs:
|
|
||||||
if (balloondir is not None) and os.path.isdir(balloondir) and (balloondir not in appendset):
|
|
||||||
self.balloondirs.append(balloondir)
|
|
||||||
appendset.add(balloondir)
|
|
||||||
|
|
||||||
|
|
||||||
# ucsmap files
|
|
||||||
appendset = set()
|
|
||||||
self.ucsmaps = []
|
|
||||||
_ucsmaps = share('ucsmap/')
|
|
||||||
for ucsmap in _ucsmaps:
|
|
||||||
if (ucsmap is not None) and os.path.isdir(ucsmap) and (ucsmap not in appendset):
|
|
||||||
self.ucsmaps.append(ucsmap)
|
|
||||||
appendset.add(ucsmap)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -308,7 +292,7 @@ class Ponysay():
|
||||||
|
|
||||||
## The stuff
|
## The stuff
|
||||||
if not self.unrecognised:
|
if not self.unrecognised:
|
||||||
self.print_pony(args)
|
self.printPony(args)
|
||||||
else:
|
else:
|
||||||
args.help()
|
args.help()
|
||||||
exit(255)
|
exit(255)
|
||||||
|
@ -439,7 +423,7 @@ class Ponysay():
|
||||||
ascii = line[s + 1:].strip(stripset)
|
ascii = line[s + 1:].strip(stripset)
|
||||||
map[ascii] = ucs
|
map[ascii] = ucs
|
||||||
|
|
||||||
## Apply UCS → ACII mapping to ponies, by alias if weak settings
|
## Apply UCS → ASCII mapping to ponies, by alias if weak settings
|
||||||
if ucs_conf == 1:
|
if ucs_conf == 1:
|
||||||
for pony in ponies:
|
for pony in ponies:
|
||||||
if pony in map:
|
if pony in map:
|
||||||
|
@ -814,7 +798,7 @@ class Ponysay():
|
||||||
print('%s %s' % ('ponysay', VERSION))
|
print('%s %s' % ('ponysay', VERSION))
|
||||||
|
|
||||||
|
|
||||||
def print_pony(self, args):
|
def printPony(self, args):
|
||||||
'''
|
'''
|
||||||
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.
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue