Coverage for /usr/local/lib/python3.11/dist-packages/grond/apps/cmd_init.py: 77%

86 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2025-04-03 09:31 +0000

1# https://pyrocko.org/grond - GPLv3 

2# 

3# The Grond Developers, 21st Century 

4import logging 

5 

6import glob 

7import os.path as op 

8 

9from shutil import copytree, copyfile 

10 

11from grond.meta import GrondError 

12 

13logger = logging.getLogger('grond.init') 

14km = 1e3 

15 

16 

17class GrondInit(object): 

18 

19 snippet_path = op.join(op.dirname(__file__), '..', 'data', 'snippets') 

20 example_path = op.join(op.dirname(__file__), '..', 'data', 'examples') 

21 

22 def __init__(self): 

23 pass 

24 

25 def get_examples(self): 

26 return { 

27 self.filename_to_abbrv(fn): self._get_example_description(fn) 

28 for fn in self.example_dirs 

29 } 

30 

31 def get_sections(self): 

32 return { 

33 self.filename_to_abbrv(fn): self._get_description(fn) 

34 for fn in self.snippet_files 

35 } 

36 

37 @property 

38 def example_dirs(self): 

39 return [path for path in glob.glob(op.join(self.example_path, '*')) 

40 if op.isdir(path)] 

41 

42 @property 

43 def section_files(self): 

44 return self._get_snippet_files('section_*.gronf') 

45 

46 @property 

47 def snippet_files(self): 

48 return self._get_snippet_files('*.gronf') 

49 

50 def _get_snippet_files(self, name): 

51 files = glob.glob(op.join(self.snippet_path, name)) 

52 files.sort() 

53 return files 

54 

55 @staticmethod 

56 def _get_description(filename): 

57 with open(filename, 'rt') as f: 

58 for ln in f.readlines(): 

59 if ln.startswith('#'): 

60 return ln.split(':')[-1].strip('# \n') 

61 return 'No description!' 

62 

63 def _get_example_description(self, example_dir): 

64 config_file = self._get_example_config(example_dir) 

65 with open(config_file, 'rt') as f: 

66 for ln in f.readlines(): 

67 if ln.startswith('#'): 

68 return ln.split(':')[-1].strip('# \n') 

69 return 'No description!' 

70 

71 @staticmethod 

72 def _get_example_config(example_dir): 

73 fpath_template = op.join(example_dir, 'config', '*.gronf') 

74 config_file = glob.glob(fpath_template) 

75 if len(config_file) == 0: 

76 raise OSError('No example config file found: %s' % fpath_template) 

77 return config_file[0] 

78 

79 @staticmethod 

80 def filename_to_abbrv(filename): 

81 return op.basename(filename).split('.')[0] 

82 

83 def init_example(self, abbrv, path, force=False): 

84 

85 path = op.abspath(path) 

86 if op.exists(path) and not force: 

87 raise OSError('Directory already exists: %s' % op.basename(path)) 

88 elif op.exists(path) and force: 

89 pass 

90 example_dir = self.abbrv_to_example_dir(abbrv) 

91 

92 logger.info('Initialising example "%s" in "%s".', abbrv, path) 

93 if not op.exists(example_dir): 

94 raise GrondError('Example not found: %s' % abbrv) 

95 

96 copytree(example_dir, path, copy_function=copyfile) 

97 

98 def abbrv_to_filename(self, abbrv): 

99 ext = '.gronf' 

100 fn = op.join(self.snippet_path, abbrv + ext) 

101 

102 if fn not in self._get_snippet_files('*.gronf'): 

103 raise OSError('File not found: %s' % fn) 

104 return fn 

105 

106 def abbrv_to_example_dir(self, abbrv): 

107 return op.join(self.example_path, abbrv) 

108 

109 def get_content_example(self, abbrv): 

110 try: 

111 fn = self._get_example_config( 

112 self.abbrv_to_example_dir(abbrv)) 

113 except OSError: 

114 return False 

115 

116 with open(fn, 'r') as f: 

117 return f.read() 

118 

119 def get_content_snippet(self, abbrv): 

120 try: 

121 fn = self.abbrv_to_filename(abbrv) 

122 except OSError: 

123 return False 

124 

125 with open(fn, 'r') as f: 

126 return f.read()