1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

import sys 

import copy 

import difflib 

from pyrocko import guts_agnostic as aguts 

from .config import color_diff 

 

 

def rename_attribute(old, new): 

def func(path, obj): 

if old in obj: 

obj.rename_attribute(old, new) 

 

return func 

 

 

def rename_class(new): 

def func(path, obj): 

obj._tagname = new 

 

return func 

 

 

def drop_attribute(old): 

def func(path, obj): 

if old in obj: 

obj.drop_attribute(old) 

 

return func 

 

 

def set_attribute(k, v): 

def func(path, obj): 

obj[k] = v 

 

return func 

 

 

def upgrade_config_file(fn, diff=True): 

rules = [ 

('grond.HighScoreOptimizerConfig', 

rename_class('grond.HighScoreOptimiserConfig')), 

('grond.Config', 

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

('grond.CMTProblemConfig', 

drop_attribute('apply_balancing_weights')), 

('grond.DoubleDCProblemConfig', 

drop_attribute('apply_balancing_weights')), 

('grond.RectangularProblemConfig', 

drop_attribute('apply_balancing_weights')), 

('grond.Config', 

drop_attribute('analyser_config')), 

('grond.Config', 

set_attribute( 

'analyser_configs', 

aguts.load(string=''' 

- !grond.TargetBalancingAnalyserConfig 

niterations: 1000 

'''))), 

 

] 

 

def apply_rules(path, obj): 

for tagname, func in rules: 

if obj._tagname == tagname: 

func(path, obj) 

 

t1 = aguts.load(filename=fn) 

t2 = copy.deepcopy(t1) 

 

aguts.apply_tree(t2, apply_rules) 

 

s1 = aguts.dump(t1) 

s2 = aguts.dump(t2) 

 

if diff: 

result = list(difflib.unified_diff( 

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

'normalized old', 'normalized new')) 

 

if sys.stdout.isatty(): 

sys.stdout.writelines(color_diff(result)) 

else: 

sys.stdout.writelines(result) 

else: 

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

 

 

if __name__ == '__main__': 

fn = sys.argv[1] 

upgrade_config_file(fn)