bugfix: cleanly handle more errors when loading resources
Also print lexers after palettes.
This commit is contained in:
parent
d7dc047a2a
commit
55f18d885e
1 changed files with 39 additions and 14 deletions
|
|
@ -41,11 +41,17 @@ def parse_gimp_palette( filename ):
|
||||||
|
|
||||||
# Then the columns number.
|
# Then the columns number.
|
||||||
# split on colon, take the second argument as an int
|
# split on colon, take the second argument as an int
|
||||||
columns = int( fd.readline().strip().split(":")[1].strip() )
|
line = fd.readline()
|
||||||
|
if "Columns:" in line:
|
||||||
|
columns = int( line.strip().split(":")[1].strip() )
|
||||||
|
lines = fd.readlines()
|
||||||
|
else:
|
||||||
|
columns=3
|
||||||
|
lines = [line] + fd.readlines()
|
||||||
|
|
||||||
# Then the colors themselves.
|
# Then the colors themselves.
|
||||||
palette = []
|
palette = []
|
||||||
for line in fd:
|
for line in lines:
|
||||||
# skip lines with only a comment
|
# skip lines with only a comment
|
||||||
if re.match("^\s*#.*$", line ):
|
if re.match("^\s*#.*$", line ):
|
||||||
continue
|
continue
|
||||||
|
|
@ -122,6 +128,9 @@ endmarks = {8: ";", 256: ";38;5;"}
|
||||||
ansi_min = 16
|
ansi_min = 16
|
||||||
ansi_max = 232
|
ansi_max = 232
|
||||||
|
|
||||||
|
themes = {}
|
||||||
|
colormaps = {}
|
||||||
|
|
||||||
def rgb_rainbow( x, freq = 1.0/(256.0/math.pi) ):
|
def rgb_rainbow( x, freq = 1.0/(256.0/math.pi) ):
|
||||||
"""Analytical expression of a n-colors rainbow colormap"""
|
"""Analytical expression of a n-colors rainbow colormap"""
|
||||||
scope = (ansi_max - ansi_min)/2.0
|
scope = (ansi_max - ansi_min)/2.0
|
||||||
|
|
@ -147,7 +156,7 @@ class DuplicatedTheme(Exception):
|
||||||
|
|
||||||
def load_themes( themes_dir):
|
def load_themes( themes_dir):
|
||||||
global themes
|
global themes
|
||||||
themes = {}
|
logging.debug("search for themes in: %s" % themes_dir)
|
||||||
os.chdir( themes_dir )
|
os.chdir( themes_dir )
|
||||||
|
|
||||||
# load available themes
|
# load available themes
|
||||||
|
|
@ -162,12 +171,16 @@ def load_themes( themes_dir):
|
||||||
|
|
||||||
def load_palettes( palettes_dir ):
|
def load_palettes( palettes_dir ):
|
||||||
global colormaps
|
global colormaps
|
||||||
colormaps = {}
|
logging.debug("search for palettes in: %s" % palettes_dir)
|
||||||
os.chdir( palettes_dir )
|
os.chdir( palettes_dir )
|
||||||
|
|
||||||
# load available colormaps (GIMP palettes format)
|
# load available colormaps (GIMP palettes format)
|
||||||
for p in glob.iglob("*.gpl"):
|
for p in glob.iglob("*.gpl"):
|
||||||
|
try:
|
||||||
name,palette = parse_gimp_palette(p)
|
name,palette = parse_gimp_palette(p)
|
||||||
|
except Exception as e:
|
||||||
|
logging.warning("error while parsing palette %s: %s" % ( p,e ) )
|
||||||
|
continue
|
||||||
if name in colormaps:
|
if name in colormaps:
|
||||||
raise DuplicatedPalette(name)
|
raise DuplicatedPalette(name)
|
||||||
# Convert the palette to ANSI
|
# Convert the palette to ANSI
|
||||||
|
|
@ -605,10 +618,10 @@ def __args_parse__(argv, usage=""):
|
||||||
parser.add_argument("-t", "--theme", action="store_true",
|
parser.add_argument("-t", "--theme", action="store_true",
|
||||||
help="Interpret REGEX as a theme.")
|
help="Interpret REGEX as a theme.")
|
||||||
|
|
||||||
parser.add_argument("-T", "--themes-dir", metavar="DIR", action="append", type=list, default="",
|
parser.add_argument("-T", "--themes-dir", metavar="DIR", action="append",
|
||||||
help="Search for additional themes (colout_*.py files) in this directory")
|
help="Search for additional themes (colout_*.py files) in this directory")
|
||||||
|
|
||||||
parser.add_argument("-P", "--palettes-dir", metavar="DIR", action="append", type=list, default="",
|
parser.add_argument("-P", "--palettes-dir", metavar="DIR", action="append",
|
||||||
help="Search for additional palettes (*.gpl files) in this directory")
|
help="Search for additional palettes (*.gpl files) in this directory")
|
||||||
|
|
||||||
parser.add_argument("-r", "--resources", action="store_true",
|
parser.add_argument("-r", "--resources", action="store_true",
|
||||||
|
|
@ -687,10 +700,22 @@ if __name__ == "__main__":
|
||||||
# try additional directories if asked
|
# try additional directories if asked
|
||||||
if palettes_dirs:
|
if palettes_dirs:
|
||||||
for adir in palettes_dirs:
|
for adir in palettes_dirs:
|
||||||
|
try:
|
||||||
|
os.chdir( adir )
|
||||||
|
except OSError as e:
|
||||||
|
logging.warning("cannot read palettes directory %s, ignore it" % adir)
|
||||||
|
continue
|
||||||
|
else:
|
||||||
load_palettes( adir )
|
load_palettes( adir )
|
||||||
|
|
||||||
if themes_dirs:
|
if themes_dirs:
|
||||||
for adir in themes_dirs:
|
for adir in themes_dirs:
|
||||||
|
try:
|
||||||
|
os.chdir( adir )
|
||||||
|
except OSError as e:
|
||||||
|
logging.warning("cannot read themes directory %s, ignore it" % adir)
|
||||||
|
continue
|
||||||
|
else:
|
||||||
load_themes( adir )
|
load_themes( adir )
|
||||||
|
|
||||||
except DuplicatedPalette as e:
|
except DuplicatedPalette as e:
|
||||||
|
|
@ -708,16 +733,16 @@ if __name__ == "__main__":
|
||||||
else:
|
else:
|
||||||
print("NO THEME")
|
print("NO THEME")
|
||||||
|
|
||||||
if len(lexers) > 0:
|
|
||||||
print("LEXERS: %s" % ", ".join(lexers) )
|
|
||||||
else:
|
|
||||||
print("NO LEXER")
|
|
||||||
|
|
||||||
if len(colormaps) > 0:
|
if len(colormaps) > 0:
|
||||||
print("PALETTES: %s" % ", ".join(colormaps) )
|
print("PALETTES: %s" % ", ".join(colormaps) )
|
||||||
else:
|
else:
|
||||||
print("NO PALETTE")
|
print("NO PALETTE")
|
||||||
|
|
||||||
|
if len(lexers) > 0:
|
||||||
|
print("LEXERS: %s" % ", ".join(lexers) )
|
||||||
|
else:
|
||||||
|
print("NO LEXER")
|
||||||
|
|
||||||
sys.exit(0) # not an error, we asked for help
|
sys.exit(0) # not an error, we asked for help
|
||||||
|
|
||||||
############
|
############
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue