Construct a `RcParams` instance from file *fname*. Unlike `rc_params_from_file`, the configuration class only contains the parameters specified in the file (i.e. default values are not filled in). Parameters ---------- fname : path-like The loaded file. transfo
(fname, transform=lambda x: x, fail_on_error=False)
| 879 | |
| 880 | |
| 881 | def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False): |
| 882 | """ |
| 883 | Construct a `RcParams` instance from file *fname*. |
| 884 | |
| 885 | Unlike `rc_params_from_file`, the configuration class only contains the |
| 886 | parameters specified in the file (i.e. default values are not filled in). |
| 887 | |
| 888 | Parameters |
| 889 | ---------- |
| 890 | fname : path-like |
| 891 | The loaded file. |
| 892 | transform : callable, default: the identity function |
| 893 | A function called on each individual line of the file to transform it, |
| 894 | before further parsing. |
| 895 | fail_on_error : bool, default: False |
| 896 | Whether invalid entries should result in an exception or a warning. |
| 897 | """ |
| 898 | import matplotlib as mpl |
| 899 | rc_temp = {} |
| 900 | with _open_file_or_url(fname) as fd: |
| 901 | try: |
| 902 | for line_no, line in enumerate(fd, 1): |
| 903 | line = transform(line) |
| 904 | strippedline = cbook._strip_comment(line) |
| 905 | if not strippedline: |
| 906 | continue |
| 907 | tup = strippedline.split(':', 1) |
| 908 | if len(tup) != 2: |
| 909 | _log.warning('Missing colon in file %r, line %d (%r)', |
| 910 | fname, line_no, line.rstrip('\n')) |
| 911 | continue |
| 912 | key, val = tup |
| 913 | key = key.strip() |
| 914 | val = val.strip() |
| 915 | if val.startswith('"') and val.endswith('"'): |
| 916 | val = val[1:-1] # strip double quotes |
| 917 | if key in rc_temp: |
| 918 | _log.warning('Duplicate key in file %r, line %d (%r)', |
| 919 | fname, line_no, line.rstrip('\n')) |
| 920 | rc_temp[key] = (val, line, line_no) |
| 921 | except UnicodeDecodeError: |
| 922 | _log.warning('Cannot decode configuration file %r as utf-8.', |
| 923 | fname) |
| 924 | raise |
| 925 | |
| 926 | config = RcParams() |
| 927 | |
| 928 | for key, (val, line, line_no) in rc_temp.items(): |
| 929 | if key in rcsetup._validators: |
| 930 | if fail_on_error: |
| 931 | config[key] = val # try to convert to proper type or raise |
| 932 | else: |
| 933 | try: |
| 934 | config[key] = val # try to convert to proper type or skip |
| 935 | except Exception as msg: |
| 936 | _log.warning('Bad value in file %r, line %d (%r): %s', |
| 937 | fname, line_no, line.rstrip('\n'), msg) |
| 938 | else: |
no test coverage detected
searching dependent graphs…