28
28
IndexDispersionSum ,
29
29
)
30
30
from ..materials import IsotropicMaterial
31
+ from ..utils import detect_encoding
31
32
32
33
WavelengthFilterType = Union [
33
34
None , float , int , List [Union [float , int ]], Tuple [Union [float , int ]]
@@ -287,18 +288,7 @@ def get_dispersion(
287
288
Returns:
288
289
Dispersion: A dispersion object containing the tabulated data.
289
290
"""
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 )
302
292
303
293
dispersion_list = []
304
294
contains_index_dispersion = False
@@ -441,18 +431,7 @@ def get_reference(self, book: str, page: str) -> str:
441
431
Returns:
442
432
str: Reference information.
443
433
"""
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 )
456
435
457
436
return yml_file ["REFERENCES" ]
458
437
@@ -466,6 +445,20 @@ def get_comment(self, book: str, page: str) -> str:
466
445
Returns:
467
446
str: Dispersion information.
468
447
"""
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
+ """
469
462
470
463
index = self .catalog .loc [
471
464
(self .catalog ["book" ] == book ) & (self .catalog ["page" ] == page )
@@ -474,9 +467,15 @@ def get_comment(self, book: str, page: str) -> str:
474
467
if len (index ) != 1 :
475
468
raise ValueError ("No entry found." )
476
469
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" ])
480
472
)
481
473
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