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
4import inspect
5import logging
6import shutil
8import click
9import pkg_resources
11from bob.extension.scripts.click_helper import AliasedGroup, verbosity_option
13logger = logging.getLogger(__name__)
16@click.group(cls=AliasedGroup)
17def config():
18 """Commands for listing, describing and copying configuration resources"""
19 pass
22@config.command(
23 epilog="""
24\b
25Examples:
27\b
28 1. Lists all configuration resources (type: bob.ip.binseg.config) installed:
30\b
31 $ bob binseg config list
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):
38\b
39 $ bob binseg config list -v
41"""
42)
43@verbosity_option()
44def list(verbose):
45 """Lists configuration files installed"""
47 entry_points = pkg_resources.iter_entry_points("bob.ip.binseg.config")
48 entry_points = dict([(k.name, k) for k in entry_points])
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
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
70 for config_type in sorted(entry_points_by_module):
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 )
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
82 print("module: %s" % (config_type,))
83 for name in sorted(entry_points_by_module[config_type]):
84 ep = entry_points[name]
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 = ""
96 summary = (
97 (summary[: (description_leftover - 3)] + "...")
98 if len(summary) > (description_leftover - 3)
99 else summary
100 )
102 print(print_string % (name, summary))
105@config.command(
106 epilog="""
107\b
108Examples:
110\b
111 1. Describes the DRIVE (training) dataset configuration:
113\b
114 $ bob binseg config describe drive
117\b
118 2. Describes the DRIVE (training) dataset configuration and lists its
119 contents:
121\b
122 $ bob binseg config describe drive -v
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"""
135 entry_points = pkg_resources.iter_entry_points("bob.ip.binseg.config")
136 entry_points = dict([(k.name, k) for k in entry_points])
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()
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))
158@config.command(
159 epilog="""
160\b
161Examples:
163\b
164 1. Makes a copy of one of the stock configuration files locally, so it can be
165 adapted:
167\b
168 $ bob binseg config copy drive -vvv newdataset.py
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"""
187 entry_points = pkg_resources.iter_entry_points("bob.ip.binseg.config")
188 entry_points = dict([(k.name, k) for k in entry_points])
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)