11# -*- coding: utf-8 -*-
2+ """This module provides utilities to interact with colormap. It provides functions
3+ to load/save colormaps from/to json files and to build icons representing the
4+ colormaps.
5+ """
26#
37# Licensed under the terms of the BSD 3-Clause
48# (see plotpy/LICENSE for details)
@@ -35,10 +39,10 @@ def load_raw_colormaps_from_json(
3539 Dictionnary of colormaps names -> raw colormap sequences
3640 """
3741 if isinstance (json_path , str ) and os .path .isfile (json_path ):
38- with open (json_path ) as f :
42+ with open (json_path , encoding = "utf-8" ) as f :
3943 try :
4044 return json .load (f )
41- except BaseException as e :
45+ except json . JSONDecodeError as e :
4246 print (e )
4347 return {}
4448 return {}
@@ -71,7 +75,7 @@ def save_colormaps(json_filename: str, colormaps: dict[str, CustomQwtLinearColor
7175 """
7276 raw_colormaps = {name : cmap .to_tuples () for name , cmap in colormaps .items ()}
7377 json_abs_path = CONF .get_path (json_filename )
74- with open (json_abs_path , "w" ) as f :
78+ with open (json_abs_path , "w" , encoding = "utf-8" ) as f :
7579 json .dump (raw_colormaps , f , indent = 4 )
7680
7781
@@ -139,7 +143,7 @@ def get_cmap_path(config_path: str):
139143 )
140144 if os .path .isfile (data_config_path ):
141145 return data_config_path
142- except BaseException as e :
146+ except ( FileNotFoundError , PermissionError , OSError ) as e :
143147 print (e )
144148
145149 user_config_path = CONF .get_path (config_path )
@@ -154,11 +158,17 @@ def get_cmap_path(config_path: str):
154158
155159# Load default colormaps path from the config file
156160DEFAULT_COLORMAPS_PATH = get_cmap_path (
157- CONF .get ("colormaps" , "colormaps/default" , default = "colormaps_default.json" ) # type: ignore
161+ CONF .get (
162+ "colormaps" ,
163+ "colormaps/default" ,
164+ default = "colormaps_default.json" , # type: ignore
165+ )
158166)
159167# Load custom colormaps path from the config file
160168CUSTOM_COLORMAPS_PATH = get_cmap_path (
161- CONF .get ("colormaps" , "colormaps/custom" , default = "colormaps_custom.json" ) # type: ignore
169+ CONF .get (
170+ "colormaps" , "colormaps/custom" , default = "colormaps_custom.json" # type: ignore
171+ )
162172)
163173
164174# Load default and custom colormaps from json files
0 commit comments