-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
440 lines (364 loc) · 12.9 KB
/
Copy pathcli.py
File metadata and controls
440 lines (364 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
"""CLI commands for config-utils."""
import os
import sys
import yaml
import subprocess
import json
from pathlib import Path
from typing import Dict, Any, Set, Tuple
import click
def flatten_dict(data: Dict[str, Any], depth: int, parent_key: str = '', sep: str = '.') -> Dict[str, Any]:
"""
Flatten a nested dictionary to a specified depth.
Args:
data: The dictionary to flatten
depth: How many levels deep to flatten (0 = unlimited, 1 = root only, no flattening)
parent_key: The parent key for recursion
sep: The separator for nested keys
Returns:
Flattened dictionary
"""
# Depth 1 means no flattening - just return the dict as is
if depth == 1 and not parent_key:
return data.copy()
items = {}
current_depth = len(parent_key.split(sep)) if parent_key else 0
for key, value in data.items():
new_key = f"{parent_key}{sep}{key}" if parent_key else key
# Calculate the depth of the new key
new_depth = len(new_key.split(sep))
# If we've reached the depth limit, don't flatten further
if depth > 0 and new_depth >= depth:
items[new_key] = value
elif isinstance(value, dict) and value:
# Continue flattening
items.update(flatten_dict(value, depth, new_key, sep=sep))
else:
items[new_key] = value
return items
def unflatten_dict(data: Dict[str, Any], sep: str = '.') -> Dict[str, Any]:
"""
Unflatten a dictionary with dot-separated keys back to nested structure.
Args:
data: The flattened dictionary
sep: The separator used in keys
Returns:
Nested dictionary
"""
result = {}
for key, value in data.items():
parts = key.split(sep)
current = result
for part in parts[:-1]:
if part not in current:
current[part] = {}
current = current[part]
current[parts[-1]] = value
return result
def load_yaml_file(file_path: str) -> Dict[str, Any]:
"""
Load a YAML file and return its contents.
Args:
file_path: Path to the YAML file
Returns:
Dictionary containing YAML data
Raises:
SystemExit: If file not found or invalid YAML
"""
try:
with open(file_path, 'r') as f:
data = yaml.safe_load(f) or {}
if not isinstance(data, dict):
click.echo(f"Error: Root must be a YAML mapping in {file_path}", err=True)
sys.exit(1)
return data
except FileNotFoundError:
click.echo(f"Error: File not found: {file_path}", err=True)
sys.exit(1)
except yaml.YAMLError as e:
click.echo(f"Error: Invalid YAML in {file_path}: {e}", err=True)
sys.exit(1)
def make_hashable(value: Any) -> Any:
"""
Convert a value to a hashable type for set operations.
Args:
value: The value to convert
Returns:
A hashable representation of the value
"""
if isinstance(value, dict):
return tuple(sorted((k, make_hashable(v)) for k, v in value.items()))
elif isinstance(value, list):
return tuple(make_hashable(item) for item in value)
elif isinstance(value, set):
return frozenset(make_hashable(item) for item in value)
else:
return value
def perform_set_operation(
file1_data: Dict[str, Any],
file2_data: Dict[str, Any],
operation: str,
compare_mode: str,
depth: int
) -> Dict[str, Any]:
"""
Perform set operations on two dictionaries.
Args:
file1_data: First file data
file2_data: Second file data
operation: One of 'union', 'intersect', 'diff', 'rdiff', 'symdiff'
compare_mode: Either 'keys' or 'kv' (key-value)
depth: Depth level for comparison (0 = unlimited)
Returns:
Dictionary with the result of the set operation
"""
# Flatten both dictionaries
flat1 = flatten_dict(file1_data, depth)
flat2 = flatten_dict(file2_data, depth)
if compare_mode == 'keys':
# Compare only keys
keys1 = set(flat1.keys())
keys2 = set(flat2.keys())
if operation == 'union':
result_keys = keys1 | keys2
elif operation == 'intersect':
result_keys = keys1 & keys2
elif operation == 'diff':
result_keys = keys1 - keys2
elif operation == 'rdiff':
result_keys = keys2 - keys1
elif operation == 'symdiff':
result_keys = keys1 ^ keys2
else:
result_keys = set()
# Build result dictionary, preferring values from file1
result = {}
for key in result_keys:
if key in flat1:
result[key] = flat1[key]
else:
result[key] = flat2[key]
else: # compare_mode == 'kv'
# Compare key-value pairs using hashable representations
# Create sets of (key, hashable_value) tuples
items1 = set((k, make_hashable(v)) for k, v in flat1.items())
items2 = set((k, make_hashable(v)) for k, v in flat2.items())
if operation == 'union':
# For union, we need to handle conflicts - file1 takes precedence
result_items = items1 | items2
result = {}
for key, _ in result_items:
# Prefer file1 values
if key in flat1:
result[key] = flat1[key]
else:
result[key] = flat2[key]
elif operation == 'intersect':
result_items = items1 & items2
result = {k: flat1[k] for k, _ in result_items}
elif operation == 'diff':
result_items = items1 - items2
result = {k: flat1[k] for k, _ in result_items}
elif operation == 'rdiff':
result_items = items2 - items1
result = {k: flat2[k] for k, _ in result_items}
elif operation == 'symdiff':
result_items = items1 ^ items2
result = {}
for key, _ in result_items:
if key in flat1:
result[key] = flat1[key]
else:
result[key] = flat2[key]
else:
result = {}
# Unflatten the result if depth > 1
# For depth=1, we didn't flatten, so no need to unflatten
# For depth>1, we need to unflatten back to nested structure
if depth > 1:
result = unflatten_dict(result)
return result
@click.group()
@click.version_option()
def main():
"""config-utils: Capture environment variables, Django settings, and perform YAML set operations."""
pass
@main.command()
@click.option(
'--output',
'-o',
default='env_config.yaml',
help='Output file path (default: env_config.yaml)',
type=click.Path(),
)
@click.option(
'--format',
'-f',
type=click.Choice(['yaml', 'yml'], case_sensitive=False),
default='yaml',
help='Output format (default: yaml)',
)
def capture_env(output, format):
"""Capture all environment variables and store them in YAML format."""
try:
# Get all environment variables
env_vars = dict(os.environ)
# Ensure output path is Path object
output_path = Path(output)
# Write to YAML file
with open(output_path, 'w') as f:
yaml.dump(env_vars, f, default_flow_style=False, sort_keys=True)
click.echo(f"✓ Captured {len(env_vars)} environment variables to {output_path}")
except Exception as e:
click.echo(f"✗ Error: {str(e)}", err=True)
sys.exit(1)
@main.command()
@click.option(
'--output',
'-o',
default='django_settings.yaml',
help='Output file path (default: django_settings.yaml)',
type=click.Path(),
)
@click.option(
'--format',
'-f',
type=click.Choice(['yaml', 'yml'], case_sensitive=False),
default='yaml',
help='Output format (default: yaml)',
)
@click.option(
'--manage-py',
'-m',
default='manage.py',
help='Path to manage.py (default: manage.py)',
type=click.Path(exists=True),
)
@click.option(
'--settings',
'-s',
help='Django settings module (e.g., myproject.settings)',
envvar='DJANGO_SETTINGS_MODULE',
)
def capture_django_settings(output, format, manage_py, settings):
"""Capture Django settings and store them in YAML format.
Uses 'python manage.py shell' to access Django settings.
Requires manage.py to be present in the current directory or specify path with --manage-py.
"""
try:
# Check if manage.py exists
manage_path = Path(manage_py)
if not manage_path.exists():
click.echo(
f"✗ Error: manage.py not found at {manage_path}. "
"Run this command from your Django project root or use --manage-py to specify the path.",
err=True
)
sys.exit(1)
# Python script to run in Django shell
django_script = """
import json
from django.conf import settings
settings_dict = {}
for setting in dir(settings):
if setting.isupper():
try:
value = getattr(settings, setting)
# Convert non-serializable types to strings
if not isinstance(value, (str, int, float, bool, list, dict, type(None))):
value = str(value)
settings_dict[setting] = value
except Exception as e:
settings_dict[setting] = f"<Error retrieving value: {str(e)}>"
print(json.dumps(settings_dict))
"""
# Prepare environment variables
env = os.environ.copy()
if settings:
env['DJANGO_SETTINGS_MODULE'] = settings
# Run manage.py shell with the script
result = subprocess.run(
['python', str(manage_path), 'shell'],
input=django_script,
capture_output=True,
text=True,
env=env,
timeout=30
)
if result.returncode != 0:
click.echo(f"✗ Error running Django shell:", err=True)
click.echo(result.stderr, err=True)
sys.exit(1)
# Parse JSON output
try:
settings_dict = json.loads(result.stdout.strip())
except json.JSONDecodeError:
click.echo(f"✗ Error: Could not parse Django settings output", err=True)
click.echo(f"Output: {result.stdout}", err=True)
sys.exit(1)
# Ensure output path is Path object
output_path = Path(output)
# Write to YAML file
with open(output_path, 'w') as f:
yaml.dump(settings_dict, f, default_flow_style=False, sort_keys=True)
click.echo(f"✓ Captured {len(settings_dict)} Django settings to {output_path}")
except subprocess.TimeoutExpired:
click.echo("✗ Error: Django shell command timed out", err=True)
sys.exit(1)
except Exception as e:
click.echo(f"✗ Error: {str(e)}", err=True)
sys.exit(1)
# Set operation commands
def create_set_operation_command(operation: str, description: str):
"""Factory function to create set operation commands."""
@main.command(name=operation, help=description)
@click.argument('file1', type=click.Path(exists=True))
@click.argument('file2', type=click.Path(exists=True))
@click.option(
'--compare',
type=click.Choice(['keys', 'kv'], case_sensitive=False),
default='kv',
help='Comparison mode: keys or kv (key-values). Default: kv',
)
@click.option(
'--depth',
type=int,
default=1,
help='How many levels deep to compare. 1 = root keys only, 0 = unlimited (full depth). Default: 1',
)
def command(file1, file2, compare, depth):
try:
# Load YAML files
file1_data = load_yaml_file(file1)
file2_data = load_yaml_file(file2)
# Perform set operation
result = perform_set_operation(file1_data, file2_data, operation, compare, depth)
# Output result as YAML to stdout
yaml.dump(result, sys.stdout, default_flow_style=False, sort_keys=False, allow_unicode=True)
except Exception as e:
click.echo(f"Error: {str(e)}", err=True)
sys.exit(1)
return command
# Create all set operation commands
union_cmd = create_set_operation_command(
'union',
'Returns all keys (or key-value pairs) present in either file.'
)
intersect_cmd = create_set_operation_command(
'intersect',
'Returns only keys (or key-value pairs) present in both files.'
)
diff_cmd = create_set_operation_command(
'diff',
'Returns keys (or key-value pairs) in file1 but not in file2 (A - B).'
)
rdiff_cmd = create_set_operation_command(
'rdiff',
'Returns keys (or key-value pairs) in file2 but not in file1 (B - A).'
)
symdiff_cmd = create_set_operation_command(
'symdiff',
'Returns keys (or key-value pairs) in either file but not in both (symmetric difference).'
)
if __name__ == '__main__':
main()