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,42 +128,58 @@ 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):
|
||||||
themes = {}
|
global themes
|
||||||
themes_dir=os.path.dirname(os.path.realpath(__file__))
|
themes = {}
|
||||||
os.chdir( themes_dir )
|
os.chdir( themes_dir )
|
||||||
|
|
||||||
for f in glob.iglob("colout_*.py"):
|
# load available themes
|
||||||
|
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)
|
|
||||||
colormaps = {}
|
def load_palettes( palettes_dir ):
|
||||||
for p in glob.iglob("*.gpl"):
|
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)
|
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 )
|
||||||
|
|
||||||
# load available pygments lexers
|
|
||||||
lexers = []
|
def load_lexers():
|
||||||
try:
|
global lexers
|
||||||
|
# load available pygments lexers
|
||||||
|
lexers = []
|
||||||
|
try:
|
||||||
from pygments.lexers import get_all_lexers
|
from pygments.lexers import get_all_lexers
|
||||||
from pygments.lexers import get_lexer_by_name
|
from pygments.lexers import get_lexer_by_name
|
||||||
from pygments import highlight
|
from pygments import highlight
|
||||||
from pygments.formatters import Terminal256Formatter
|
from pygments.formatters import Terminal256Formatter
|
||||||
from pygments.formatters import TerminalFormatter
|
from pygments.formatters import TerminalFormatter
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
for lexer in get_all_lexers():
|
for lexer in get_all_lexers():
|
||||||
try:
|
try:
|
||||||
lexers.append(lexer[1][0])
|
lexers.append(lexer[1][0])
|
||||||
|
|
@ -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"] )
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue