gistfile1.txt
· 1.9 KiB · Text
Eredeti
def _block_read(file):
try:
with open(file) as f:
content = f.read().strip()
if not content:
return None # empty file = no block data
try:
return json.loads(content)
except json.JSONDecodeError:
# Old format — migrate
lines = content.split('\n')
def _subnet_read(file):
"""Read subnets.json, return dict or empty dict"""
try:
if not os.path.exists(file):
return {}
with open(file) as f:
content = f.read().strip()
if not content:
return {}
return json.loads(content)
except Exception:
def _identity_read(file):
"""Read an identity file, return dict or None"""
try:
if not os.path.exists(file):
return None
with open(file) as f:
content = f.read().strip()
if not content:
return None
return json.loads(content)
except Exception:
def _policy_read(file):
try:
if not os.path.exists(file):
return {}
with open(file) as f:
content = f.read().strip()
if not content:
return {}
return json.loads(content)
except Exception:
return {}
def _hosts_read(file):
if not os.path.exists(file):
return {"hosts": {}, "subnets": {}, "ports": {}}
try:
with open(file) as f:
data = json.load(f)
# Ensure all sections exist
data.setdefault("hosts", {})
data.setdefault("subnets", {})
data.setdefault("ports", {})
return data
def _net_read(file):
try:
if not os.path.exists(file): return {}
with open(file) as f:
content = f.read().strip()
if not content: return {}
return json.loads(content)
except Exception:
return {}
def _net_write(file, data):
| 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): |
| 67 |