nuno revisó este gist 1 month ago. Ir a la revisión
1 file changed, 66 insertions
gistfile1.txt(archivo creado)
| @@ -0,0 +1,66 @@ | |||
| 1 | + | def _block_read(file): | |
| 2 | + | try: | |
| 3 | + | with open(file) as f: | |
| 4 | + | content = f.read().strip() | |
| 5 | + | if not content: | |
| 6 | + | return None # empty file = no block data | |
| 7 | + | try: | |
| 8 | + | return json.loads(content) | |
| 9 | + | except json.JSONDecodeError: | |
| 10 | + | # Old format — migrate | |
| 11 | + | lines = content.split('\n') | |
| 12 | + | def _subnet_read(file): | |
| 13 | + | """Read subnets.json, return dict or empty dict""" | |
| 14 | + | try: | |
| 15 | + | if not os.path.exists(file): | |
| 16 | + | return {} | |
| 17 | + | with open(file) as f: | |
| 18 | + | content = f.read().strip() | |
| 19 | + | if not content: | |
| 20 | + | return {} | |
| 21 | + | return json.loads(content) | |
| 22 | + | except Exception: | |
| 23 | + | def _identity_read(file): | |
| 24 | + | """Read an identity file, return dict or None""" | |
| 25 | + | try: | |
| 26 | + | if not os.path.exists(file): | |
| 27 | + | return None | |
| 28 | + | with open(file) as f: | |
| 29 | + | content = f.read().strip() | |
| 30 | + | if not content: | |
| 31 | + | return None | |
| 32 | + | return json.loads(content) | |
| 33 | + | except Exception: | |
| 34 | + | def _policy_read(file): | |
| 35 | + | try: | |
| 36 | + | if not os.path.exists(file): | |
| 37 | + | return {} | |
| 38 | + | with open(file) as f: | |
| 39 | + | content = f.read().strip() | |
| 40 | + | if not content: | |
| 41 | + | return {} | |
| 42 | + | return json.loads(content) | |
| 43 | + | except Exception: | |
| 44 | + | return {} | |
| 45 | + | def _hosts_read(file): | |
| 46 | + | if not os.path.exists(file): | |
| 47 | + | return {"hosts": {}, "subnets": {}, "ports": {}} | |
| 48 | + | try: | |
| 49 | + | with open(file) as f: | |
| 50 | + | data = json.load(f) | |
| 51 | + | # Ensure all sections exist | |
| 52 | + | data.setdefault("hosts", {}) | |
| 53 | + | data.setdefault("subnets", {}) | |
| 54 | + | data.setdefault("ports", {}) | |
| 55 | + | return data | |
| 56 | + | def _net_read(file): | |
| 57 | + | try: | |
| 58 | + | if not os.path.exists(file): return {} | |
| 59 | + | with open(file) as f: | |
| 60 | + | content = f.read().strip() | |
| 61 | + | if not content: return {} | |
| 62 | + | return json.loads(content) | |
| 63 | + | except Exception: | |
| 64 | + | return {} | |
| 65 | + | ||
| 66 | + | def _net_write(file, data): | |
Siguiente
Anterior