Signed-off-by: Mattias Andrée <maandree@operamail.com>
This commit is contained in:
Mattias Andrée 2013-08-12 09:53:01 +02:00
parent f44a77924d
commit 90028ec636

View file

@ -56,7 +56,70 @@ class Ponysay():
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
@ -90,63 +153,35 @@ class Ponysay():
return file
## Change system enviroment variables with ponysayrc
for file in ('$XDG_CONFIG_HOME/ponysay/ponysayrc', '$HOME/.config/ponysay/ponysayrc', '$HOME/.ponysayrc', '/etc/ponysayrc'):
file = 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
def __getShareDirectories(directory):
'''
Gets existing unique /share directories
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('~')
@param directory:str The directory base name
@return :list<str> Absolute directory names
'''
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()
self.unrecognised = False
def __share(file):
'''
Gets /share files
# 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 = 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):
@param file:str The file base name
@return :list<str> Absolute file names
'''
def cat(a, b):
if a is None:
return None
return a + b
return [cat(parsefile(item), file) for item in [
return [cat(Ponysay.__parseFile(item), file) for item in [
'./',
'$XDG_DATA_HOME/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
appendset = set()
self.xponydirs = []
_ponydirs = share('ponies/')
for ponydir in _ponydirs:
if (ponydir is not None) and os.path.isdir(ponydir) and (ponydir not in appendset):
self.xponydirs.append(ponydir)
appendset.add(ponydir)
appendset = set()
self.vtponydirs = []
_ponydirs = share('ttyponies/')
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)
def __isPonythink():
'''
Check if ponythink is executed
'''
isthink = sys.argv[0]
if os.sep in isthink:
isthink = isthink[isthink.rfind(os.sep) + 1:]
if os.extsep in isthink:
isthink = isthink[:isthink.find(os.extsep)]
isthink = isthink.endswith('think')
return isthink
@ -308,7 +292,7 @@ class Ponysay():
## The stuff
if not self.unrecognised:
self.print_pony(args)
self.printPony(args)
else:
args.help()
exit(255)
@ -439,7 +423,7 @@ class Ponysay():
ascii = line[s + 1:].strip(stripset)
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:
for pony in ponies:
if pony in map:
@ -814,7 +798,7 @@ class Ponysay():
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.