KMS.{usingkms => usingKMS} + split up KMS class into more functions

Signed-off-by: Mattias Andrée <maandree@operamail.com>
This commit is contained in:
Mattias Andrée 2013-08-12 08:29:35 +02:00
parent 7b5af293a6
commit f44a77924d
2 changed files with 140 additions and 74 deletions

View file

@ -33,13 +33,20 @@ from common import *
KMS_VERSION = '2'
'''
KMS support version constant
'''
class KMS():
'''
KMS support utilisation
'''
@staticmethod
def usingkms(linuxvt):
def usingKMS(linuxvt):
'''
Identifies whether KMS support is utilised
@ -50,19 +57,137 @@ class KMS():
if not linuxvt:
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
if env_kms is None: env_kms = ''
## If the palette string is empty KMS is not utilised
return KMS.__getKMSPalette() != ''
@staticmethod
def __parseKMSCommand():
'''
Parse the KMS palette command stored in the environment variables
## Read the PONYSAY_KMS_PALETTE_CMD environment variable, and run it
@return :str? The KMS palette, `None` if 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 == ''):
env_kms = Popen(shlex.split(env_kms_cmd), stdout=PIPE, stdin=sys.stderr).communicate()[0].decode('utf8', 'replace')
if env_kms[-1] == '\n':
env_kms = env_kms[:-1]
return env_kms
return None
@staticmethod
def __getKMSPalette():
'''
Get the KMS palette
## If the palette string is empty KMS is not utilised
return env_kms != ''
@return :str The KMS palette
'''
## Read the PONYSAY_KMS_PALETTE environment variable
env_kms = os.environ['PONYSAY_KMS_PALETTE'] if 'PONYSAY_KMS_PALETTE' in os.environ else None
if env_kms is None:
env_kms = ''
## Read the PONYSAY_KMS_PALETTE_CMD environment variable, and run it
env_kms_cmd = KMS.__parseKMSCommand()
if env_kms_cmd is not None:
env_kms = env_kms_cmd
return env_kms
@staticmethod
def __getCacheDirectory(home):
'''
Gets the KMS change directory, and creates it if it does not exist
@param home:str The user's home directory
@return (cachedir, shared):(str, bool) The cache directory and whether it is user shared
'''
cachedir = '/var/cache/ponysay'
shared = True
if not os.path.isdir(cachedir):
cachedir = home + '/.cache/ponysay'
shared = False
if not os.path.isdir(cachedir):
os.makedirs(cachedir)
return (cachedir, shared)
@staticmethod
def __isCacheOld(cachedir):
'''
Gets whether the cache is old
@param cachedir:str The cache directory
@return Whether the cache is old
'''
newversion = False
if not os.path.isfile(cachedir + '/.version'):
newversion = True
else:
with open(cachedir + '/.version', 'rb') as cachev:
if cachev.read().decode('utf8', 'replace').replace('\n', '') != KMS_VERSION:
newversion = True
return newversion
@staticmethod
def __cleanCache(cachedir):
'''
Clean the cache directory
@param cachedir:str The cache directory
'''
for cached in os.listdir(cachedir):
cached = cachedir + '/' + cached
if os.path.isdir(cached) and not os.path.islink(cached):
shutil.rmtree(cached, False)
else:
os.remove(cached)
with open(cachedir + '/.version', 'w+') as cachev:
cachev.write(KMS_VERSION)
if shared:
try:
os.chmod(cachedir + '/.version', 0o7777)
except:
pass
@staticmethod
def __createKMSPony(pony, kmspony, cachedir):
'''
Create KMS pony
@param pony:str Choosen pony file
@param kmspony:str The KMS pony file
@param cachedir:str The cache directory
'''
## kmspony directory
kmsponydir = kmspony[:kmspony.rindex('/')]
## Change file names to be shell friendly
_kmspony = '\'' + kmspony.replace('\'', '\'\\\'\'') + '\''
_pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\''
_cachedir = '\'' + cachedir.replace('\'', '\'\\\'\'') + '\''
## Create kmspony
if not os.path.isdir(kmsponydir):
os.makedirs(kmsponydir)
if shared:
Popen('chmod -R 7777 -- %s/kmsponies' % _cachedir, shell=True).wait()
opts = '--balloon n --left - --right - --top - --bottom -'
ponytoolcmd = 'ponytool --import ponysay --file %%s %s --export ponysay --file %%s --platform linux %s' % (opts, opts)
ponytoolcmd += ' --colourful y --fullcolour y --palette %s'
if not os.system(ponytoolcmd % (_pony, _kmspony, palette)) == 0:
printerr('Unable to run ponytool successfully, you need util-say>=3 for KMS support')
exit(1)
if shared:
try:
os.chmod(kmspony, 0o7777)
except:
pass
@staticmethod
@ -79,19 +204,8 @@ class KMS():
if not linuxvt:
return pony
## KMS support version constant
KMS_VERSION = '2'
## Read the PONYSAY_KMS_PALETTE environment variable
env_kms = os.environ['PONYSAY_KMS_PALETTE'] if 'PONYSAY_KMS_PALETTE' in os.environ else None
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
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')
if env_kms[-1] == '\n':
env_kms = env_kms[:-1]
## Get KMS palette
env_kms = KMS.__getKMSPalette()
## If not using KMS, return the pony as is
if env_kms == '':
@ -101,38 +215,12 @@ class KMS():
palette = env_kms
palettefile = env_kms.replace('\033]P', '')
## Get and in necessary make cache directory
cachedir = '/var/cache/ponysay'
shared = True
if not os.path.isdir(cachedir):
cachedir = home + '/.cache/ponysay'
shared = False
if not os.path.isdir(cachedir):
os.makedirs(cachedir)
_cachedir = '\'' + cachedir.replace('\'', '\'\\\'\'') + '\''
## Get and if necessary make cache directory
(cachedir, share) = KMS.__getCacheDirectory(home)
## KMS support version control, clean everything if not matching
newversion = False
if not os.path.isfile(cachedir + '/.version'):
newversion = True
else:
with open(cachedir + '/.version', 'rb') as cachev:
if cachev.read().decode('utf8', 'replace').replace('\n', '') != KMS_VERSION:
newversion = True
if newversion:
for cached in os.listdir(cachedir):
cached = cachedir + '/' + cached
if os.path.isdir(cached) and not os.path.islink(cached):
shutil.rmtree(cached, False)
else:
os.remove(cached)
with open(cachedir + '/.version', 'w+') as cachev:
cachev.write(KMS_VERSION)
if shared:
try:
os.chmod(cachedir + '/.version', 0o7777)
except:
pass
if KMS.__isCacheOld(cachedir):
KMS.__cleanCache(cachedir)
## Get kmspony directory and kmspony file
kmsponies = cachedir + '/kmsponies/' + palettefile
@ -140,29 +228,7 @@ class KMS():
## If the kmspony is missing, create it
if not os.path.isfile(kmspony):
## kmspony directory
kmsponydir = kmspony[:kmspony.rindex('/')]
## Change file names to be shell friendly
_kmspony = '\'' + kmspony.replace('\'', '\'\\\'\'') + '\''
_pony = '\'' + pony.replace('\'', '\'\\\'\'') + '\''
## Create kmspony
if not os.path.isdir(kmsponydir):
os.makedirs(kmsponydir)
if shared:
Popen('chmod -R 7777 -- %s/kmsponies' % _cachedir, shell=True).wait()
opts = '--balloon n --left - --right - --top - --bottom -'
ponytoolcmd = 'ponytool --import ponysay --file %%s %s --export ponysay --file %%s --platform linux %s' % (opts, opts)
ponytoolcmd += ' --colourful y --fullcolour y --palette %s'
if not os.system(ponytoolcmd % (_pony, _kmspony, palette)) == 0:
printerr('Unable to run ponytool successfully, you need util-say>=3 for KMS support')
exit(1)
if shared:
try:
os.chmod(kmspony, 0o7777)
except:
pass
KMS.__createKMSPony(pony, kmspony, cachedir)
return kmspony

View file

@ -134,7 +134,7 @@ class Ponysay():
# Whether KMS is used
self.usekms = KMS.usingkms(self.linuxvt)
self.usekms = KMS.usingKMS(self.linuxvt)
# Mode string that modifies or adds $ variables in the pony image