1from __future__ import print_function, absolute_import 

2 

3import logging 

4 

5import glob 

6import os.path as op 

7 

8from shutil import copytree 

9 

10from grond.meta import GrondError 

11 

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

13km = 1e3 

14 

15 

16class GrondInit(object): 

17 

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

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

20 

21 def __init__(self): 

22 pass 

23 

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 } 

29 

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 } 

35 

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)] 

40 

41 @property 

42 def section_files(self): 

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

44 

45 @property 

46 def snippet_files(self): 

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

48 

49 def _get_snippet_files(self, name): 

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

51 files.sort() 

52 return files 

53 

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!' 

61 

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!' 

69 

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] 

77 

78 @staticmethod 

79 def filename_to_abbrv(filename): 

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

81 

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

83 

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) 

90 

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) 

94 

95 copytree(example_dir, path) 

96 

97 def abbrv_to_filename(self, abbrv): 

98 ext = '.gronf' 

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

100 

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

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

103 return fn 

104 

105 def abbrv_to_example_dir(self, abbrv): 

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

107 

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 

114 

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

116 return f.read() 

117 

118 def get_content_snippet(self, abbrv): 

119 try: 

120 fn = self.abbrv_to_filename(abbrv) 

121 except OSError: 

122 return False 

123 

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

125 return f.read()