Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python 

2# coding=utf-8 

3 

4import inspect 

5import logging 

6import shutil 

7 

8import click 

9import pkg_resources 

10 

11from bob.extension.scripts.click_helper import AliasedGroup, verbosity_option 

12 

13logger = logging.getLogger(__name__) 

14 

15 

16@click.group(cls=AliasedGroup) 

17def config(): 

18 """Commands for listing, describing and copying configuration resources""" 

19 pass 

20 

21 

22@config.command( 

23 epilog=""" 

24\b 

25Examples: 

26 

27\b 

28 1. Lists all configuration resources (type: bob.ip.binseg.config) installed: 

29 

30\b 

31 $ bob binseg config list 

32 

33 

34\b 

35 2. Lists all configuration resources and their descriptions (notice this may 

36 be slow as it needs to load all modules once): 

37 

38\b 

39 $ bob binseg config list -v 

40 

41""" 

42) 

43@verbosity_option() 

44def list(verbose): 

45 """Lists configuration files installed""" 

46 

47 entry_points = pkg_resources.iter_entry_points("bob.ip.binseg.config") 

48 entry_points = dict([(k.name, k) for k in entry_points]) 

49 

50 # all modules with configuration resources 

51 modules = set( 

52 k.module_name.rsplit(".", 1)[0] for k in entry_points.values() 

53 ) 

54 keep_modules = [] 

55 for k in sorted(modules): 

56 if k not in keep_modules and not any( 

57 k.startswith(l) for l in keep_modules 

58 ): 

59 keep_modules.append(k) 

60 modules = keep_modules 

61 

62 # sort data entries by originating module 

63 entry_points_by_module = {} 

64 for k in modules: 

65 entry_points_by_module[k] = {} 

66 for name, ep in entry_points.items(): 

67 if ep.module_name.startswith(k): 

68 entry_points_by_module[k][name] = ep 

69 

70 for config_type in sorted(entry_points_by_module): 

71 

72 # calculates the longest config name so we offset the printing 

73 longest_name_length = max( 

74 len(k) for k in entry_points_by_module[config_type].keys() 

75 ) 

76 

77 # set-up printing options 

78 print_string = " %%-%ds %%s" % (longest_name_length,) 

79 # 79 - 4 spaces = 75 (see string above) 

80 description_leftover = 75 - longest_name_length 

81 

82 print("module: %s" % (config_type,)) 

83 for name in sorted(entry_points_by_module[config_type]): 

84 ep = entry_points[name] 

85 

86 if verbose >= 1: 

87 module = ep.load() 

88 doc = inspect.getdoc(module) 

89 if doc is not None: 

90 summary = doc.split("\n\n")[0] 

91 else: 

92 summary = "<DOCSTRING NOT AVAILABLE>" 

93 else: 

94 summary = "" 

95 

96 summary = ( 

97 (summary[: (description_leftover - 3)] + "...") 

98 if len(summary) > (description_leftover - 3) 

99 else summary 

100 ) 

101 

102 print(print_string % (name, summary)) 

103 

104 

105@config.command( 

106 epilog=""" 

107\b 

108Examples: 

109 

110\b 

111 1. Describes the DRIVE (training) dataset configuration: 

112 

113\b 

114 $ bob binseg config describe drive 

115 

116 

117\b 

118 2. Describes the DRIVE (training) dataset configuration and lists its 

119 contents: 

120 

121\b 

122 $ bob binseg config describe drive -v 

123 

124""" 

125) 

126@click.argument( 

127 "name", 

128 required=True, 

129 nargs=-1, 

130) 

131@verbosity_option() 

132def describe(name, verbose): 

133 """Describes a specific configuration file""" 

134 

135 entry_points = pkg_resources.iter_entry_points("bob.ip.binseg.config") 

136 entry_points = dict([(k.name, k) for k in entry_points]) 

137 

138 for k in name: 

139 if k not in entry_points: 

140 logger.error("Cannot find configuration resource '%s'", k) 

141 continue 

142 ep = entry_points[k] 

143 print("Configuration: %s" % (ep.name,)) 

144 print("Python Module: %s" % (ep.module_name,)) 

145 print("") 

146 mod = ep.load() 

147 

148 if verbose >= 1: 

149 fname = inspect.getfile(mod) 

150 print("Contents:") 

151 with open(fname, "r") as f: 

152 print(f.read()) 

153 else: # only output documentation 

154 print("Documentation:") 

155 print(inspect.getdoc(mod)) 

156 

157 

158@config.command( 

159 epilog=""" 

160\b 

161Examples: 

162 

163\b 

164 1. Makes a copy of one of the stock configuration files locally, so it can be 

165 adapted: 

166 

167\b 

168 $ bob binseg config copy drive -vvv newdataset.py 

169 

170 

171""" 

172) 

173@click.argument( 

174 "source", 

175 required=True, 

176 nargs=1, 

177) 

178@click.argument( 

179 "destination", 

180 required=True, 

181 nargs=1, 

182) 

183@verbosity_option() 

184def copy(source, destination, verbose): 

185 """Copies a specific configuration resource so it can be modified locally""" 

186 

187 entry_points = pkg_resources.iter_entry_points("bob.ip.binseg.config") 

188 entry_points = dict([(k.name, k) for k in entry_points]) 

189 

190 if source not in entry_points: 

191 logger.error("Cannot find configuration resource '%s'", source) 

192 return 1 

193 ep = entry_points[source] 

194 mod = ep.load() 

195 src_name = inspect.getfile(mod) 

196 logger.info("cp %s -> %s" % (src_name, destination)) 

197 shutil.copyfile(src_name, destination)