recal
This commit is contained in:
@@ -18,46 +18,55 @@
|
|||||||
import re
|
import re
|
||||||
from typing import Dict, List, Union, Any
|
from typing import Dict, List, Union, Any
|
||||||
|
|
||||||
class bellande_format:
|
class Bellande_Format:
|
||||||
def parse_bellande(self, file_path: str) -> Dict:
|
def parse_bellande(self, file_path: str) -> str:
|
||||||
with open(file_path, 'r') as file:
|
with open(file_path, 'r') as file:
|
||||||
lines = file.readlines()
|
lines = file.readlines()
|
||||||
return self.parse_lines(lines)
|
parsed_data = self.parse_lines(lines)
|
||||||
|
return self.to_string_representation(parsed_data)
|
||||||
|
|
||||||
def parse_lines(self, lines: List[str]) -> Dict:
|
def parse_lines(self, lines: List[str]) -> Union[Dict, List]:
|
||||||
result = {}
|
result = {}
|
||||||
stack = [(-1, result)]
|
current_key = None
|
||||||
|
current_list = None
|
||||||
|
indent_stack = [(-1, result)]
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
stripped = line.strip()
|
stripped = line.strip()
|
||||||
if not stripped or stripped.startswith('#'):
|
if not stripped or stripped.startswith('#'):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
indent = len(line) - len(line.lstrip())
|
indent = len(line) - len(line.lstrip())
|
||||||
while stack and indent <= stack[-1][0]:
|
|
||||||
stack.pop()
|
while indent_stack and indent <= indent_stack[-1][0]:
|
||||||
|
popped = indent_stack.pop()
|
||||||
parent = stack[-1][1]
|
if isinstance(popped[1], list):
|
||||||
|
current_list = None
|
||||||
|
|
||||||
if ':' in stripped:
|
if ':' in stripped:
|
||||||
key, value = map(str.strip, stripped.split(':', 1))
|
key, value = map(str.strip, stripped.split(':', 1))
|
||||||
|
current_key = key
|
||||||
if value:
|
if value:
|
||||||
parent[key] = self.parse_value(value)
|
result[key] = self.parse_value(value)
|
||||||
else:
|
else:
|
||||||
new_dict = {}
|
result[key] = []
|
||||||
parent[key] = new_dict
|
current_list = result[key]
|
||||||
stack.append((indent, new_dict))
|
indent_stack.append((indent, current_list))
|
||||||
elif stripped.startswith('-'):
|
elif stripped.startswith('-'):
|
||||||
value = stripped[1:].strip()
|
value = stripped[1:].strip()
|
||||||
|
parsed_value = self.parse_value(value)
|
||||||
if isinstance(parent, list):
|
if current_list is not None:
|
||||||
parent.append(self.parse_value(value))
|
current_list.append(parsed_value)
|
||||||
else:
|
else:
|
||||||
new_list = [self.parse_value(value)]
|
if not result: # If result is empty, start a root-level list
|
||||||
last_key = list(parent.keys())[-1]
|
result = [parsed_value]
|
||||||
parent[last_key] = new_list
|
current_list = result
|
||||||
stack.append((indent, new_list))
|
indent_stack = [(-1, result)]
|
||||||
|
else:
|
||||||
|
result[current_key] = [parsed_value]
|
||||||
|
current_list = result[current_key]
|
||||||
|
indent_stack.append((indent, current_list))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def parse_value(self, value: str) -> Union[str, int, float, bool, None]:
|
def parse_value(self, value: str) -> Union[str, int, float, bool, None]:
|
||||||
@@ -76,6 +85,24 @@ class bellande_format:
|
|||||||
else:
|
else:
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def to_string_representation(self, data: Any) -> str:
|
||||||
|
if isinstance(data, dict):
|
||||||
|
items = [f'"{k}": {self.to_string_representation(v)}' for k, v in data.items()]
|
||||||
|
return '{' + ', '.join(items) + '}'
|
||||||
|
elif isinstance(data, list):
|
||||||
|
items = [self.to_string_representation(item) for item in data]
|
||||||
|
return '[' + ', '.join(items) + ']'
|
||||||
|
elif isinstance(data, str):
|
||||||
|
return f'"{data}"'
|
||||||
|
elif isinstance(data, (int, float)):
|
||||||
|
return str(data)
|
||||||
|
elif data is None:
|
||||||
|
return 'null'
|
||||||
|
elif isinstance(data, bool):
|
||||||
|
return str(data).lower()
|
||||||
|
else:
|
||||||
|
return str(data)
|
||||||
|
|
||||||
def write_bellande(self, data: Any, file_path: str):
|
def write_bellande(self, data: Any, file_path: str):
|
||||||
with open(file_path, 'w') as file:
|
with open(file_path, 'w') as file:
|
||||||
file.write(self.to_bellande_string(data))
|
file.write(self.to_bellande_string(data))
|
||||||
@@ -93,7 +120,7 @@ class bellande_format:
|
|||||||
elif isinstance(data, list):
|
elif isinstance(data, list):
|
||||||
lines = []
|
lines = []
|
||||||
for item in data:
|
for item in data:
|
||||||
if isinstance(item, (dict, list)):
|
if isinstance(item, dict):
|
||||||
lines.append(f"{' ' * indent}-")
|
lines.append(f"{' ' * indent}-")
|
||||||
lines.append(self.to_bellande_string(item, indent + 4))
|
lines.append(self.to_bellande_string(item, indent + 4))
|
||||||
else:
|
else:
|
||||||
@@ -104,7 +131,7 @@ class bellande_format:
|
|||||||
|
|
||||||
def format_value(self, value: Any) -> str:
|
def format_value(self, value: Any) -> str:
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
if ' ' in value or ':' in value:
|
if ' ' in value or ':' in value or value.lower() in ['true', 'false', 'null']:
|
||||||
return f'"{value}"'
|
return f'"{value}"'
|
||||||
return value
|
return value
|
||||||
elif isinstance(value, bool):
|
elif isinstance(value, bool):
|
||||||
|
Reference in New Issue
Block a user