|
@@ -2,40 +2,16 @@ import textwrap
|
|
|
|
|
|
|
|
from .exception import pini_exception
|
|
from .exception import pini_exception
|
|
|
from .key import key
|
|
from .key import key
|
|
|
|
|
+from .line import line
|
|
|
|
|
|
|
|
-class section:
|
|
|
|
|
|
|
+class section(line):
|
|
|
def __init__(self):
|
|
def __init__(self):
|
|
|
- self.__name: str | None = None
|
|
|
|
|
- self.__comment: str | None = None
|
|
|
|
|
|
|
+ super().__init__()
|
|
|
self.__keys: list[key] = list()
|
|
self.__keys: list[key] = list()
|
|
|
|
|
|
|
|
@property
|
|
@property
|
|
|
def is_global(self) -> bool:
|
|
def is_global(self) -> bool:
|
|
|
- return self.__name is None
|
|
|
|
|
-
|
|
|
|
|
- @property
|
|
|
|
|
- def name(self) -> str | None:
|
|
|
|
|
- return self.__name
|
|
|
|
|
-
|
|
|
|
|
- @name.setter
|
|
|
|
|
- def name(self, target: str | None) -> None:
|
|
|
|
|
- if type(target) is str:
|
|
|
|
|
- target = target.strip()
|
|
|
|
|
-
|
|
|
|
|
- if type(target) is not str and target is not None:
|
|
|
|
|
- raise TypeError("Section name must be str, or None for global.")
|
|
|
|
|
-
|
|
|
|
|
- self.__name = target
|
|
|
|
|
-
|
|
|
|
|
- @property
|
|
|
|
|
- def comment(self) -> str | None:
|
|
|
|
|
- return self.__comment
|
|
|
|
|
-
|
|
|
|
|
- def add_comment(self, content: str) -> None:
|
|
|
|
|
- if self.__comment is None:
|
|
|
|
|
- self.__comment = ""
|
|
|
|
|
-
|
|
|
|
|
- self.__comment = self.__comment + content
|
|
|
|
|
|
|
+ return self.name is None
|
|
|
|
|
|
|
|
def get(self, target: str) -> key | None:
|
|
def get(self, target: str) -> key | None:
|
|
|
if type(target) is not str:
|
|
if type(target) is not str:
|
|
@@ -44,7 +20,7 @@ class section:
|
|
|
target.strip()
|
|
target.strip()
|
|
|
|
|
|
|
|
if len(target) == 0:
|
|
if len(target) == 0:
|
|
|
- raise TypeError("Ket name to get can not be empty.")
|
|
|
|
|
|
|
+ raise TypeError("Key name to get can not be empty.")
|
|
|
|
|
|
|
|
for count in self.__keys:
|
|
for count in self.__keys:
|
|
|
if count.name == target:
|
|
if count.name == target:
|
|
@@ -55,7 +31,21 @@ class section:
|
|
|
def exists(self, target: str) -> bool:
|
|
def exists(self, target: str) -> bool:
|
|
|
return self.get(target) is not None
|
|
return self.get(target) is not None
|
|
|
|
|
|
|
|
- def add_key(self, target: key) -> None:
|
|
|
|
|
|
|
+ def drop(self, target: key | str) -> key | None:
|
|
|
|
|
+ if type(target) is key:
|
|
|
|
|
+ target = target.name
|
|
|
|
|
+
|
|
|
|
|
+ index = -1
|
|
|
|
|
+
|
|
|
|
|
+ for count in range(len(self.__keys)):
|
|
|
|
|
+ if self.__keys[count].name == target:
|
|
|
|
|
+ index = count
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ if index != -1:
|
|
|
|
|
+ return self.__keys.pop(index)
|
|
|
|
|
+
|
|
|
|
|
+ def add(self, target: key) -> None:
|
|
|
if type(target) is not key:
|
|
if type(target) is not key:
|
|
|
raise TypeError("New key must be instance of key.")
|
|
raise TypeError("New key must be instance of key.")
|
|
|
|
|
|
|
@@ -66,17 +56,17 @@ class section:
|
|
|
|
|
|
|
|
raise pini_exception(error)
|
|
raise pini_exception(error)
|
|
|
|
|
|
|
|
- self.__keys.append(target)
|
|
|
|
|
|
|
+ self.__keys.append(target.clone())
|
|
|
|
|
|
|
|
- def __str__(self) -> str:
|
|
|
|
|
|
|
+ def render(self) -> str:
|
|
|
content = ""
|
|
content = ""
|
|
|
comments = list()
|
|
comments = list()
|
|
|
|
|
|
|
|
- if self.__name is not None:
|
|
|
|
|
- content = "[" + self.__name + "]\n"
|
|
|
|
|
|
|
+ if self.name is not None:
|
|
|
|
|
+ content = "[" + self.name + "]\n"
|
|
|
|
|
|
|
|
- if self.__comment is not None:
|
|
|
|
|
- comments = textwrap.wrap(self.__comment, 77)
|
|
|
|
|
|
|
+ if self.comment is not None:
|
|
|
|
|
+ comments = textwrap.wrap(self.comment, 77)
|
|
|
|
|
|
|
|
for count in comments:
|
|
for count in comments:
|
|
|
content = content + "# " + count + "\n"
|
|
content = content + "# " + count + "\n"
|