Test whether an object can be validly converted to a netCDF-3 dimension, variable or attribute name Earlier versions of the netCDF C-library reference implementation enforced a more restricted set of characters in creating new names, but permitted reading names containing arbitrary
(s)
| 140 | |
| 141 | |
| 142 | def is_valid_nc3_name(s): |
| 143 | """Test whether an object can be validly converted to a netCDF-3 |
| 144 | dimension, variable or attribute name |
| 145 | |
| 146 | Earlier versions of the netCDF C-library reference implementation |
| 147 | enforced a more restricted set of characters in creating new names, |
| 148 | but permitted reading names containing arbitrary bytes. This |
| 149 | specification extends the permitted characters in names to include |
| 150 | multi-byte UTF-8 encoded Unicode and additional printing characters |
| 151 | from the US-ASCII alphabet. The first character of a name must be |
| 152 | alphanumeric, a multi-byte UTF-8 character, or '_' (reserved for |
| 153 | special names with meaning to implementations, such as the |
| 154 | "_FillValue" attribute). Subsequent characters may also include |
| 155 | printing special characters, except for '/' which is not allowed in |
| 156 | names. Names that have trailing space characters are also not |
| 157 | permitted. |
| 158 | """ |
| 159 | if not isinstance(s, str): |
| 160 | return False |
| 161 | num_bytes = len(s.encode("utf-8")) |
| 162 | return ( |
| 163 | (unicodedata.normalize("NFC", s) == s) |
| 164 | and (s not in _reserved_names) |
| 165 | and (num_bytes >= 0) |
| 166 | and ("/" not in s) |
| 167 | and (s[-1] != " ") |
| 168 | and (_isalnumMUTF8(s[0]) or (s[0] == "_")) |
| 169 | and all(_isalnumMUTF8(c) or c in _specialchars for c in s) |
| 170 | ) |
no test coverage detected
searching dependent graphs…