Coverage for /usr/local/lib/python3.11/dist-packages/grond/apps/cmd_init.py: 77%
87 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-26 16:25 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-26 16:25 +0000
1from __future__ import print_function, absolute_import
3import logging
5import glob
6import os.path as op
8from shutil import copytree
10from grond.meta import GrondError
12logger = logging.getLogger('grond.init')
13km = 1e3
16class GrondInit(object):
18 snippet_path = op.join(op.dirname(__file__), '..', 'data', 'snippets')
19 example_path = op.join(op.dirname(__file__), '..', 'data', 'examples')
21 def __init__(self):
22 pass
24 def get_examples(self):
25 return {
26 self.filename_to_abbrv(fn): self._get_example_description(fn)
27 for fn in self.example_dirs
28 }
30 def get_sections(self):
31 return {
32 self.filename_to_abbrv(fn): self._get_description(fn)
33 for fn in self.snippet_files
34 }
36 @property
37 def example_dirs(self):
38 return [path for path in glob.glob(op.join(self.example_path, '*'))
39 if op.isdir(path)]
41 @property
42 def section_files(self):
43 return self._get_snippet_files('section_*.gronf')
45 @property
46 def snippet_files(self):
47 return self._get_snippet_files('*.gronf')
49 def _get_snippet_files(self, name):
50 files = glob.glob(op.join(self.snippet_path, name))
51 files.sort()
52 return files
54 @staticmethod
55 def _get_description(filename):
56 with open(filename, 'rt') as f:
57 for ln in f.readlines():
58 if ln.startswith('#'):
59 return ln.split(':')[-1].strip('# \n')
60 return 'No description!'
62 def _get_example_description(self, example_dir):
63 config_file = self._get_example_config(example_dir)
64 with open(config_file, 'rt') as f:
65 for ln in f.readlines():
66 if ln.startswith('#'):
67 return ln.split(':')[-1].strip('# \n')
68 return 'No description!'
70 @staticmethod
71 def _get_example_config(example_dir):
72 fpath_template = op.join(example_dir, 'config', '*.gronf')
73 config_file = glob.glob(fpath_template)
74 if len(config_file) == 0:
75 raise OSError('No example config file found: %s' % fpath_template)
76 return config_file[0]
78 @staticmethod
79 def filename_to_abbrv(filename):
80 return op.basename(filename).split('.')[0]
82 def init_example(self, abbrv, path, force=False):
84 path = op.abspath(path)
85 if op.exists(path) and not force:
86 raise OSError('Directory already exists: %s' % op.basename(path))
87 elif op.exists(path) and force:
88 pass
89 example_dir = self.abbrv_to_example_dir(abbrv)
91 logger.info('Initialising example "%s" in "%s".', abbrv, path)
92 if not op.exists(example_dir):
93 raise GrondError('Example not found: %s' % abbrv)
95 copytree(example_dir, path)
97 def abbrv_to_filename(self, abbrv):
98 ext = '.gronf'
99 fn = op.join(self.snippet_path, abbrv + ext)
101 if fn not in self._get_snippet_files('*.gronf'):
102 raise OSError('File not found: %s' % fn)
103 return fn
105 def abbrv_to_example_dir(self, abbrv):
106 return op.join(self.example_path, abbrv)
108 def get_content_example(self, abbrv):
109 try:
110 fn = self._get_example_config(
111 self.abbrv_to_example_dir(abbrv))
112 except OSError:
113 return False
115 with open(fn, 'r') as f:
116 return f.read()
118 def get_content_snippet(self, abbrv):
119 try:
120 fn = self.abbrv_to_filename(abbrv)
121 except OSError:
122 return False
124 with open(fn, 'r') as f:
125 return f.read()