Feature #31: better error handling

Add dedicated exception and exit on specific error code.
Also put resource loading in separated functions.
This commit is contained in:
Johann Dreo 2013-05-26 17:21:36 +02:00
commit 0a4f9f12ed

View file

@ -128,31 +128,47 @@ def rgb_rainbow( x, freq = 1.0/(256.0/math.pi) ):
return ( red, green, blue ) return ( red, green, blue )
class UnknownColor(Exception):
pass
class DuplicatedPalette(Exception):
pass
############################################################################### ###############################################################################
# Load available extern ressources # Load available extern resources
############################################################################### ###############################################################################
# load available themes def load_themes( themes_dir):
global themes
themes = {} themes = {}
themes_dir=os.path.dirname(os.path.realpath(__file__))
os.chdir( themes_dir ) os.chdir( themes_dir )
# load available themes
for f in glob.iglob("colout_*.py"): for f in glob.iglob("colout_*.py"):
module = ".".join(f.split(".")[:-1]) # remove extension module = ".".join(f.split(".")[:-1]) # remove extension
name = "_".join(module.split("_")[1:]) # remove the prefix name = "_".join(module.split("_")[1:]) # remove the prefix
themes[name] = importlib.import_module(module) themes[name] = importlib.import_module(module)
# load available colormaps (GIMP palettes format)
def load_palettes( palettes_dir ):
global colormaps
colormaps = {} colormaps = {}
os.chdir( palettes_dir )
# load available colormaps (GIMP palettes format)
for p in glob.iglob("*.gpl"): for p in glob.iglob("*.gpl"):
name,palette = parse_gimp_palette(p) name,palette = parse_gimp_palette(p)
if name in colormaps: if name in colormaps:
raise Exception('Duplicated palette filename: %s' % name) raise DuplicatedPalette(name)
# Convert the palette to ANSI # Convert the palette to ANSI
ansi_palette = [ rgb_to_ansi(r,g,b) for r,g,b in palette ] ansi_palette = [ rgb_to_ansi(r,g,b) for r,g,b in palette ]
# Compress it so that there isn't two consecutive identical colors # Compress it so that there isn't two consecutive identical colors
colormaps[name] = uniq( ansi_palette ) colormaps[name] = uniq( ansi_palette )
def load_lexers():
global lexers
# load available pygments lexers # load available pygments lexers
lexers = [] lexers = []
try: try:
@ -172,6 +188,12 @@ else:
lexers.sort() lexers.sort()
def load_resources( themes_dir, palettes_dir ):
load_themes( themes_dir )
load_palettes( palettes_dir )
load_lexers()
############################################################################### ###############################################################################
# Library # Library
############################################################################### ###############################################################################
@ -316,7 +338,7 @@ def colorin(text, color="red", style="normal"):
# unrecognized # unrecognized
else: else:
raise Exception('Unrecognized color %s' % color) raise UnknownColor(color)
return start + style_code + endmarks[mode] + color_code + "m" + text + stop return start + style_code + endmarks[mode] + color_code + "m" + text + stop
@ -595,6 +617,19 @@ def write_all( as_all, stream_in, stream_out, function, *args ):
if __name__ == "__main__": if __name__ == "__main__":
error_codes = {"UnknownColor":1, "DuplicatedPalette":2}
try:
# Search for available resources files (themes, palettes)
# in the same dir as the colout.py script
res_dir = os.path.dirname(os.path.realpath(__file__))
# this must be called before args parsing, because the help can list available resources
load_resources( res_dir, res_dir )
except DuplicatedPalette as e:
print( "ERROR in colout, duplicated palette file name: %s" % e )
sys.exit( error_codes["DuplicatedPalette"] )
usage = "A regular expression based formatter that color up an arbitrary text stream." usage = "A regular expression based formatter that color up an arbitrary text stream."
@ -611,6 +646,7 @@ if __name__ == "__main__":
pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, myscale \ pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, myscale \
= __args_parse__(sys.argv, usage) = __args_parse__(sys.argv, usage)
try:
if myscale: if myscale:
scale = map(int,myscale.split(",")) scale = map(int,myscale.split(","))
@ -644,3 +680,7 @@ if __name__ == "__main__":
else: else:
write_all( as_all, sys.stdin, sys.stdout, colorup, pattern, color, style, on_groups ) write_all( as_all, sys.stdin, sys.stdout, colorup, pattern, color, style, on_groups )
except UnknownColor as e:
print("ERROR in colout, unknown color: %s" % e )
sys.exit( error_codes["UnknownColor"] )