Coverage for /usr/local/lib/python3.11/dist-packages/grond/upgrade.py: 0%

43 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-10-28 13:13 +0000

1import sys 

2import copy 

3import difflib 

4from pyrocko import guts_agnostic as aguts 

5from .config import color_diff 

6 

7 

8def rename_attribute(old, new): 

9 def func(path, obj): 

10 if old in obj: 

11 obj.rename_attribute(old, new) 

12 

13 return func 

14 

15 

16def rename_class(new): 

17 def func(path, obj): 

18 obj._tagname = new 

19 

20 return func 

21 

22 

23def drop_attribute(old): 

24 def func(path, obj): 

25 if old in obj: 

26 obj.drop_attribute(old) 

27 

28 return func 

29 

30 

31def set_attribute(k, v): 

32 def func(path, obj): 

33 obj[k] = v 

34 

35 return func 

36 

37 

38def upgrade_config_file(fn, diff=True): 

39 rules = [ 

40 ('grond.HighScoreOptimizerConfig', 

41 rename_class('grond.HighScoreOptimiserConfig')), 

42 ('grond.Config', 

43 rename_attribute('optimizer_config', 'optimiser_config')), 

44 ('grond.CMTProblemConfig', 

45 drop_attribute('apply_balancing_weights')), 

46 ('grond.DoubleDCProblemConfig', 

47 drop_attribute('apply_balancing_weights')), 

48 ('grond.RectangularProblemConfig', 

49 drop_attribute('apply_balancing_weights')), 

50 ('grond.Config', 

51 drop_attribute('analyser_config')), 

52 ('grond.Config', 

53 set_attribute( 

54 'analyser_configs', 

55 aguts.load(string=''' 

56 - !grond.TargetBalancingAnalyserConfig 

57 niterations: 1000 

58 '''))), 

59 

60 ] 

61 

62 def apply_rules(path, obj): 

63 for tagname, func in rules: 

64 if obj._tagname == tagname: 

65 func(path, obj) 

66 

67 t1 = aguts.load(filename=fn) 

68 t2 = copy.deepcopy(t1) 

69 

70 aguts.apply_tree(t2, apply_rules) 

71 

72 s1 = aguts.dump(t1) 

73 s2 = aguts.dump(t2) 

74 

75 if diff: 

76 result = list(difflib.unified_diff( 

77 s1.splitlines(1), s2.splitlines(1), 

78 'normalized old', 'normalized new')) 

79 

80 if sys.stdout.isatty(): 

81 sys.stdout.writelines(color_diff(result)) 

82 else: 

83 sys.stdout.writelines(result) 

84 else: 

85 print(aguts.dump(t2, header=True)) 

86 

87 

88if __name__ == '__main__': 

89 fn = sys.argv[1] 

90 upgrade_config_file(fn)