|
10 | 10 | import projspec.proj |
11 | 11 |
|
12 | 12 |
|
13 | | -@click.command() |
14 | | -@click.argument("path", default=".") |
| 13 | +# global runtime config |
| 14 | +context = {} |
| 15 | + |
| 16 | + |
| 17 | +@click.group() |
| 18 | +def main(): |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +@main.command("make") |
| 23 | +@click.argument("artifact", type=str) |
| 24 | +@click.argument("path", default=".", type=str) |
| 25 | +@click.option( |
| 26 | + "--storage_options", |
| 27 | + default="", |
| 28 | + help="storage options dict for the given URL, as JSON", |
| 29 | +) |
15 | 30 | @click.option( |
16 | 31 | "--types", |
17 | 32 | default="ALL", |
|
22 | 37 | default="NONE", |
23 | 38 | help="List of spec types to ignore (comma-separated list in camel or snake case)", |
24 | 39 | ) |
25 | | -@click.option("--walk", is_flag=True, help="To descend into all child directories") |
26 | | -@click.option("--summary", is_flag=True, help="Show abbreviated output") |
27 | | -@click.option("--version", is_flag=True, default=False, help="Print version and quit") |
| 40 | +def make(artifact, path, storage_options, types, xtypes): |
| 41 | + """Make the given artifact in the project at the given path. |
| 42 | +
|
| 43 | + artifact: str , of the form [<spec>.]<artifact-type>[.<name>] |
| 44 | +
|
| 45 | + path: str, path to the project directory, defaults to "." |
| 46 | + """ |
| 47 | + if types in {"ALL", ""}: |
| 48 | + types = None |
| 49 | + else: |
| 50 | + types = types.split(",") |
| 51 | + proj = projspec.Project( |
| 52 | + path, storage_options=storage_options, types=types, xtypes=xtypes |
| 53 | + ) |
| 54 | + proj.make(artifact) |
| 55 | + |
| 56 | + |
| 57 | +@main.command() |
| 58 | +def version(): |
| 59 | + print(f"projspec version: {projspec.__version__}") |
| 60 | + |
| 61 | + |
| 62 | +@main.command("scan") |
| 63 | +@click.argument("path", default=".") |
28 | 64 | @click.option( |
29 | | - "--json-out", is_flag=True, default=False, help="JSON output, for projects only" |
| 65 | + "--storage_options", |
| 66 | + default="", |
| 67 | + help="storage options dict for the given URL, as JSON", |
30 | 68 | ) |
31 | 69 | @click.option( |
32 | | - "--html-out", is_flag=True, default=False, help="HTML output, for projects only" |
| 70 | + "--types", |
| 71 | + default="ALL", |
| 72 | + help='Type names to scan for (comma-separated list in camel or snake case); defaults to "ALL"', |
33 | 73 | ) |
34 | 74 | @click.option( |
35 | | - "--make", |
36 | | - help="(Re)Create the first artifact found matching this type name; matches [spec.]artifact[.name]", |
| 75 | + "--xtypes", |
| 76 | + default="NONE", |
| 77 | + help="List of spec types to ignore (comma-separated list in camel or snake case)", |
37 | 78 | ) |
38 | 79 | @click.option( |
39 | | - "--info", |
40 | | - help="Give information about a names entity type (spec, contents or artifact)", |
| 80 | + "--json-out", is_flag=True, default=False, help="JSON output, for projects only" |
41 | 81 | ) |
42 | 82 | @click.option( |
43 | | - "--storage_options", |
44 | | - default="", |
45 | | - help="storage options dict for the given URL, as JSON", |
| 83 | + "--html-out", is_flag=True, default=False, help="HTML output, for projects only" |
46 | 84 | ) |
47 | | -def main( |
48 | | - path, |
49 | | - types, |
50 | | - xtypes, |
51 | | - walk, |
52 | | - summary, |
53 | | - version, |
54 | | - json_out, |
55 | | - html_out, |
56 | | - make, |
57 | | - info, |
58 | | - storage_options, |
| 85 | +@click.option("--walk", is_flag=True, help="To descend into all child directories") |
| 86 | +@click.option("--summary", is_flag=True, help="Show abbreviated output") |
| 87 | +@click.option("--library", is_flag=True, help="Add to library") |
| 88 | +def scan( |
| 89 | + path, storage_options, types, xtypes, json_out, html_out, walk, summary, library |
59 | 90 | ): |
60 | | - if version: |
61 | | - print(projspec.__version__) |
62 | | - return |
| 91 | + """Scan the given path for projects, and display |
| 92 | +
|
| 93 | + path: str, path to the project directory, defaults to "." |
| 94 | + """ |
63 | 95 | if types in {"ALL", ""}: |
64 | 96 | types = None |
65 | 97 | else: |
66 | 98 | types = types.split(",") |
67 | | - if info: |
68 | | - info = projspec.utils.camel_to_snake(info) |
| 99 | + proj = projspec.Project( |
| 100 | + path, storage_options=storage_options, types=types, xtypes=xtypes, walk=walk |
| 101 | + ) |
| 102 | + if summary: |
| 103 | + print(proj.text_summary()) |
| 104 | + else: |
| 105 | + if json_out: |
| 106 | + print(json.dumps(proj.to_dict(compact=True))) |
| 107 | + elif html_out: |
| 108 | + print(proj._repr_html_()) |
| 109 | + else: |
| 110 | + print(proj) |
| 111 | + if library: |
| 112 | + proj.add_to_library() |
| 113 | + |
| 114 | + |
| 115 | +@main.command("info") |
| 116 | +@click.argument( |
| 117 | + "types", |
| 118 | + default="ALL", |
| 119 | +) |
| 120 | +def info(types=None): |
| 121 | + if types in {"ALL", "", None}: |
| 122 | + from projspec.utils import class_infos |
| 123 | + |
| 124 | + print(json.dumps(class_infos())) |
| 125 | + else: |
| 126 | + name = projspec.utils.camel_to_snake(types) |
69 | 127 | cls = ( |
70 | | - projspec.proj.base.registry.get(info) |
71 | | - or projspec.content.base.registry.get(info) |
72 | | - or projspec.artifact.base.registry.get(info) |
| 128 | + projspec.proj.base.registry.get(name) |
| 129 | + or projspec.content.base.registry.get(name) |
| 130 | + or projspec.artifact.base.registry.get(name) |
73 | 131 | ) |
74 | 132 | if cls: |
75 | 133 | pydoc.doc(cls, output=sys.stdout) |
76 | 134 | else: |
77 | 135 | print("Name not found") |
78 | | - return |
79 | | - if xtypes in {"NONE", ""}: |
80 | | - xtypes = None |
81 | | - else: |
82 | | - xtypes = xtypes.split(",") |
83 | | - if storage_options: |
84 | | - storage_options = json.loads(storage_options) |
85 | | - else: |
86 | | - storage_options = None |
87 | | - proj = projspec.Project( |
88 | | - path, storage_options=storage_options, types=types, xtypes=xtypes, walk=walk |
89 | | - ) |
90 | | - if make: |
91 | | - proj.make(make) |
92 | | - elif summary: |
93 | | - print(proj.text_summary()) |
94 | | - elif json_out: |
95 | | - print(json.dumps(proj.to_dict(compact=True))) |
96 | | - elif html_out: |
97 | | - print(proj._repr_html_()) |
| 136 | + |
| 137 | + |
| 138 | +@main.group("library") |
| 139 | +def library(): |
| 140 | + """Interact with the project library. |
| 141 | +
|
| 142 | + Library file location is defined by config value "library_path". |
| 143 | + """ |
| 144 | + |
| 145 | + |
| 146 | +@library.command("list") |
| 147 | +@click.option( |
| 148 | + "--json-out", is_flag=True, default=False, help="JSON output, for projects only" |
| 149 | +) |
| 150 | +def list(json_out): |
| 151 | + from projspec.library import ProjectLibrary |
| 152 | + |
| 153 | + library = ProjectLibrary() |
| 154 | + if json_out: |
| 155 | + print(json.dumps({k: v.to_dict() for k, v in library.entries.items()})) |
98 | 156 | else: |
99 | | - print(proj) |
| 157 | + for url, proj in library.entries.items(): |
| 158 | + print(f"{proj.text_summary(bare=True)}") |
| 159 | + |
| 160 | + |
| 161 | +@library.command("delete") |
| 162 | +@click.argument("url") |
| 163 | +def delete(url): |
| 164 | + from projspec.library import ProjectLibrary |
| 165 | + |
| 166 | + library = ProjectLibrary() |
| 167 | + library.entries.pop(url) |
| 168 | + library.save() |
| 169 | + |
| 170 | + |
| 171 | +@main.group("config") |
| 172 | +def config(): |
| 173 | + """Interact with the projspec config.""" |
| 174 | + pass |
| 175 | + |
| 176 | + |
| 177 | +@config.command("get") |
| 178 | +@click.argument("key") |
| 179 | +def get(key): |
| 180 | + from projspec.config import get_conf |
| 181 | + |
| 182 | + print(get_conf(key)) |
| 183 | + |
| 184 | + |
| 185 | +@config.command("show") |
| 186 | +def show(): |
| 187 | + from projspec.config import conf |
| 188 | + |
| 189 | + # TODO: show docs and defaults for each key, from projspec.config.config_doc? |
| 190 | + # TODO: allow JSON output |
| 191 | + print(conf) |
| 192 | + |
| 193 | + |
| 194 | +@config.command("unset") |
| 195 | +@click.argument("key") |
| 196 | +def unset(key): |
| 197 | + from projspec.config import set_conf |
| 198 | + |
| 199 | + set_conf(key, None) |
| 200 | + |
| 201 | + |
| 202 | +@config.command("set") |
| 203 | +@click.argument("key") |
| 204 | +@click.argument("value") |
| 205 | +def set_(key, value): |
| 206 | + from projspec.config import set_conf |
| 207 | + |
| 208 | + set_conf(key, value) |
100 | 209 |
|
101 | 210 |
|
102 | 211 | if __name__ == "__main__": |
|
0 commit comments