2828 IndexDispersionSum ,
2929)
3030from ..materials import IsotropicMaterial
31+ from ..utils import detect_encoding
3132
3233WavelengthFilterType = Union [
3334 None , float , int , List [Union [float , int ]], Tuple [Union [float , int ]]
@@ -287,18 +288,7 @@ def get_dispersion(
287288 Returns:
288289 Dispersion: A dispersion object containing the tabulated data.
289290 """
290-
291- index = self .catalog .loc [
292- (self .catalog ["book" ] == book ) & (self .catalog ["page" ] == page )
293- ].index
294-
295- if len (index ) != 1 :
296- raise ValueError ("No entry found." )
297-
298- yml_file = yaml .load (
299- self .rii_path .joinpath (self .catalog .loc [index [0 ]]["path" ]).read_text (),
300- yaml .SafeLoader ,
301- )
291+ yml_file = self ._open_file (book , page )
302292
303293 dispersion_list = []
304294 contains_index_dispersion = False
@@ -441,18 +431,7 @@ def get_reference(self, book: str, page: str) -> str:
441431 Returns:
442432 str: Reference information.
443433 """
444-
445- index = self .catalog .loc [
446- (self .catalog ["book" ] == book ) & (self .catalog ["page" ] == page )
447- ].index
448-
449- if len (index ) != 1 :
450- raise ValueError ("No entry found." )
451-
452- yml_file = yaml .load (
453- self .rii_path .joinpath (self .catalog .loc [index [0 ]]["path" ]).read_text (),
454- yaml .SafeLoader ,
455- )
434+ yml_file = self ._open_file (book , page )
456435
457436 return yml_file ["REFERENCES" ]
458437
@@ -466,6 +445,20 @@ def get_comment(self, book: str, page: str) -> str:
466445 Returns:
467446 str: Dispersion information.
468447 """
448+ yml_file = self ._open_file (book , page )
449+
450+ return yml_file ["COMMENTS" ]
451+
452+ def _open_file (self , book : str , page : str ) -> dict :
453+ """Opens the selected dispersion file.
454+
455+ Args:
456+ book (str): Name of the Material, named 'Book' on the website and the database. E.g. 'Au'
457+ page (str): Name of the Source, named 'Page' on the website and the database. E.g. 'Johnson'
458+
459+ Returns:
460+ dict: Content of the YAML file.
461+ """
469462
470463 index = self .catalog .loc [
471464 (self .catalog ["book" ] == book ) & (self .catalog ["page" ] == page )
@@ -474,9 +467,15 @@ def get_comment(self, book: str, page: str) -> str:
474467 if len (index ) != 1 :
475468 raise ValueError ("No entry found." )
476469
477- yml_file = yaml .load (
478- self .rii_path .joinpath (self .catalog .loc [index [0 ]]["path" ]).read_text (),
479- yaml .SafeLoader ,
470+ encoding = detect_encoding (
471+ self .rii_path .joinpath (self .catalog .loc [index [0 ]]["path" ])
480472 )
481473
482- return yml_file ["COMMENTS" ]
474+ with open (
475+ self .rii_path .joinpath (self .catalog .loc [index [0 ]]["path" ]),
476+ "r" ,
477+ encoding = encoding ,
478+ ) as f :
479+ yml_file = yaml .load (f , yaml .SafeLoader )
480+
481+ return yml_file
0 commit comments