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:
parent
baf412ea5b
commit
0a4f9f12ed
1 changed files with 110 additions and 70 deletions
|
|
@ -128,31 +128,47 @@ def rgb_rainbow( x, freq = 1.0/(256.0/math.pi) ):
|
|||
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_dir=os.path.dirname(os.path.realpath(__file__))
|
||||
os.chdir( themes_dir )
|
||||
|
||||
# load available themes
|
||||
for f in glob.iglob("colout_*.py"):
|
||||
module = ".".join(f.split(".")[:-1]) # remove extension
|
||||
name = "_".join(module.split("_")[1:]) # remove the prefix
|
||||
themes[name] = importlib.import_module(module)
|
||||
|
||||
# load available colormaps (GIMP palettes format)
|
||||
|
||||
def load_palettes( palettes_dir ):
|
||||
global colormaps
|
||||
colormaps = {}
|
||||
os.chdir( palettes_dir )
|
||||
|
||||
# load available colormaps (GIMP palettes format)
|
||||
for p in glob.iglob("*.gpl"):
|
||||
name,palette = parse_gimp_palette(p)
|
||||
if name in colormaps:
|
||||
raise Exception('Duplicated palette filename: %s' % name)
|
||||
raise DuplicatedPalette(name)
|
||||
# Convert the palette to ANSI
|
||||
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
|
||||
colormaps[name] = uniq( ansi_palette )
|
||||
|
||||
|
||||
def load_lexers():
|
||||
global lexers
|
||||
# load available pygments lexers
|
||||
lexers = []
|
||||
try:
|
||||
|
|
@ -172,6 +188,12 @@ else:
|
|||
lexers.sort()
|
||||
|
||||
|
||||
def load_resources( themes_dir, palettes_dir ):
|
||||
load_themes( themes_dir )
|
||||
load_palettes( palettes_dir )
|
||||
load_lexers()
|
||||
|
||||
|
||||
###############################################################################
|
||||
# Library
|
||||
###############################################################################
|
||||
|
|
@ -316,7 +338,7 @@ def colorin(text, color="red", style="normal"):
|
|||
|
||||
# unrecognized
|
||||
else:
|
||||
raise Exception('Unrecognized color %s' % color)
|
||||
raise UnknownColor(color)
|
||||
|
||||
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__":
|
||||
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."
|
||||
|
||||
|
|
@ -611,6 +646,7 @@ if __name__ == "__main__":
|
|||
pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, myscale \
|
||||
= __args_parse__(sys.argv, usage)
|
||||
|
||||
try:
|
||||
if myscale:
|
||||
scale = map(int,myscale.split(","))
|
||||
|
||||
|
|
@ -644,3 +680,7 @@ if __name__ == "__main__":
|
|||
else:
|
||||
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"] )
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue