Coverage for cli/src/xyfigure/xybase.py: 68%

44 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-18 01:09 +0000

1# https://www.python.org/dev/peps/pep-0008/#imports 

2# standard library imports 

3import os 

4from pathlib import Path 

5import sys 

6from abc import ABC 

7 

8# related third-party imports 

9 

10# local application/library specific imports 

11 

12 

13# Helper functions 

14def absolute_path(folder): 

15 """ 

16 Makes certain the path to the folder for pending serialization exists. 

17 If it doesn't exist, ask the user if the folder should be created or not. 

18 Print the full path to the command line. 

19 Returns the absolute path, possibly for pending serialization. 

20 """ 

21 run_path = Path.cwd() 

22 abs_path = run_path.joinpath(folder) 

23 # abs_path = os.path.join(os.getcwd(), folder) 

24 if not os.path.isdir(abs_path): 

25 print(f'Folder needed but not found: "{abs_path}"') 

26 val = input("Create folder? [y]es or [n]o : ") 

27 if val == "y": 

28 os.mkdir(folder) 

29 print(f'Created folder: "{folder}"') 

30 else: 

31 print("Check accuracy of folders in database.") 

32 print("Abnormal script termination.") 

33 sys.exit("Folder misspecified.") 

34 return abs_path 

35 

36 

37# Abstract Base Class 

38class XYBase(ABC): 

39 """ 

40 Base class to collect all data and methods common to XYBase descendants. 

41 """ 

42 

43 def __init__(self, guid, **kwargs): 

44 

45 self._guid = guid 

46 

47 self._verbose = kwargs.get("verbose", True) 

48 

49 # moved up from XYView 

50 self._serialize = kwargs.get("serialize", False) 

51 

52 default_folder = "." 

53 self._folder = kwargs.get("folder", default_folder) 

54 self._folder_pathlib = Path(self._folder).expanduser() 

55 if not self._folder_pathlib.is_dir(): 

56 print('Error: keyword "folder" has a value (e.g., a folder path)') 

57 print("that cannot be found as specified:") 

58 print(self._folder_pathlib) 

59 raise KeyError("folder not found") 

60 

61 self._file = kwargs.get("file", None) 

62 

63 if self._file is None: 

64 print('Error: keyword "file" not found.') 

65 sys.exit("Abnormal termination.") 

66 

67 # abs_path = absolute_path(self._folder) 

68 

69 # self._path_file_input = os.path.join(abs_path, self._file) 

70 self._file_pathlib = self._folder_pathlib.joinpath(self._file) 

71 self._path_file_input = str(self._file_pathlib) 

72 self._path_file_output = None 

73 

74 @property 

75 def guid(self): 

76 return self._guid 

77 

78 def serialize(self, folder, filename): 

79 """ 

80 Writes our data, to be extended by descendants. 

81 """ 

82 abs_path = absolute_path(folder) 

83 self._path_file_output = os.path.join(abs_path, filename) 

84 

85 

86""" 

87Copyright 2023 Sandia National Laboratories 

88 

89Notice: This computer software was prepared by National Technology and Engineering Solutions of 

90Sandia, LLC, hereinafter the Contractor, under Contract DE-NA0003525 with the Department of Energy 

91(DOE). All rights in the computer software are reserved by DOE on behalf of the United States 

92Government and the Contractor as provided in the Contract. You are authorized to use this computer 

93software for Governmental purposes but it is not to be released or distributed to the public. 

94NEITHER THE U.S. GOVERNMENT NOR THE CONTRACTOR MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES 

95ANY LIABILITY FOR THE USE OF THIS SOFTWARE. This notice including this sentence must appear on any 

96copies of this computer software. Export of this data may require a license from the United States 

97Government. 

98"""