Coverage for cli/src/xyfigure/client.py: 86%

44 statements  

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

1#!/usr/bin/env python 

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

3# standard library imports 

4import sys 

5import json 

6 

7# related third-party imports 

8 

9# local application/library specific imports 

10# from xyfigure.factory import XYFactory 

11# from xyfigure.code.factory import XYFactory 

12from xyfigure.factory import XYFactory 

13 

14# from xyfigure.xymodel import XYModel 

15# from xyfigure.code.xymodel import XYModel 

16from xyfigure.xymodel import XYModel, XYModelAbaqus 

17 

18# from xyfigure.xyview import XYView 

19# from xyfigure.code.xyview import XYView 

20from xyfigure.xyview import XYView, XYViewAbaqus 

21 

22 

23def main(argv): 

24 """Client to generate a XYFigure from an 'input_file.json' file. 

25 

26 Preconditions: 

27 $ conda activate siblenv 

28 $ ~/sibl/cli/io/example/input_file.json # recipie to create XYFigure object(s) 

29 

30 Use: 

31 $ cd ~/sibl/cli/io/example/ 

32 $ python ~/sibl/cli/src/xyfigure/client.py input_file.json 

33 """ 

34 

35 # to do, add argparse with verbose flag, for now, hard code 

36 # verbose = False 

37 verbose = True 

38 

39 help_string = "$ python ~/sibl/cli/src/xyfigure/client.py input_file.json" 

40 try: 

41 input_file = argv[0] 

42 except IndexError as error: 

43 print(f"Error: {error}.") 

44 print("check script pattern: " + help_string) 

45 print("Abnormal script termination.") 

46 sys.exit("No input file specified.") 

47 

48 with open(input_file) as f: 

49 database = json.load(f) 

50 

51 items = [] # cart of items is empty, fill from factory 

52 # factory = XYFactory() # it's static! 

53 

54 for item in database: 

55 kwargs = database[item] 

56 i = XYFactory.create(item, **kwargs) 

57 if i: 

58 items.append(i) 

59 else: 

60 print("Item is None from factory, nothing added to Client items.") 

61 

62 models = [i for i in items if isinstance(i, (XYModel, XYModelAbaqus))] 

63 views = [i for i in items if isinstance(i, (XYView, XYViewAbaqus))] 

64 

65 for view in views: 

66 if verbose: 

67 print(f'Creating view with guid = "{view.guid}"') 

68 

69 if view.model_keys: # register only selected models with current view 

70 if verbose: 

71 print(f" Adding {view.model_keys} model(s) to current view.") 

72 view.models = [m for m in models if m.guid in view.model_keys] 

73 view.figure() # must be within this subset scope 

74 else: 

75 if verbose: 

76 print(" Adding all models to current view.") 

77 view.models = models # register all models with current view 

78 view.figure() # must be within this subset scope 

79 

80 if verbose: 

81 print("====================================") 

82 print("End of xyfigure/client.py execution.") 

83 

84 

85if __name__ == "__main__": 

86 main(sys.argv[1:]) 

87 

88 

89""" 

90Copyright 2023 Sandia National Laboratories 

91 

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

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

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

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

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

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

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

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

100Government. 

101"""