소스 검색

Change handler type.

Cixo Develop 4 주 전
부모
커밋
69cb8cec1b
5개의 변경된 파일68개의 추가작업 그리고 112개의 파일을 삭제
  1. 3 2
      source/__init__.py
  2. 31 0
      source/file_handler.py
  3. 13 110
      source/handler.py
  4. 11 0
      source/stderr_handler.py
  5. 10 0
      source/stdout_handler.py

+ 3 - 2
source/__init__.py

@@ -1,3 +1,4 @@
-from .levels import levels
-from .logger import logger
 from .handler import handler
+from .stdout_handler import stdout_handler
+from .stderr_handler import stderr_handler
+from .file_handler import file_handler

+ 31 - 0
source/file_handler.py

@@ -0,0 +1,31 @@
+import pathlib
+import os
+
+from .handler import handler
+
+class file_handler(handler):    
+    def __init__(self, target: pathlib.Path) -> None:
+        self.__target = target
+        self.__handler = None
+
+    def add(self, content: str) -> None:
+        if not self.is_ready:
+            self.open()
+        
+        self.__handler.write(content + os.linesep)
+
+    @property
+    def is_ready(self) -> bool: 
+        return self.__handler is not None and not self.__handler.closed
+
+    def open(self) -> None:
+        if self.__handler is None or self.__handler.closed:
+            self.__handler = self.__target.open("a")
+
+    def clean(self) -> None:
+        if self.__handler is None or self.__handler.closed:
+            return
+
+        self.__handler.close()
+
+

+ 13 - 110
source/handler.py

@@ -1,118 +1,21 @@
-import os
-import sys
-import pathlib
 import asyncio
 
 class handler:
-    """
-    That is used to appending data into log files, stdout and stderr.
-    It requires file which is used to store logs.
-
-    Methods
-    -------
-    async _to_file(content: str) : None
-        That add content, as new line, to the file.
-
-    async _to_stdout(content: str) : None
-        That add content, as new line, to the stdout.
-
-    async _to_stderr(content: str) : None
-        That add content, as new line, to the stderr.
-    """
-
-    def __init__(self, target: pathlib.Path | None) -> None:
-        """
-        That create required locks, and log file when it is not exists.
-
-        Parameters
-        ----------
-        target : pathlib.Path | None
-            File which would be used to store logs. If it is set to None, then
-            only stdout and stderr could be used.
-        """
-
-        self.__file_lock = asyncio.Lock()
-        self.__stderr_lock = asyncio.Lock()
-        self.__stdout_lock = asyncio.Lock()
-
-        self.__target = target
-        self.__pipe = None
-        
-        if target is not None and not target.exists():
-            target.touch()
-
-    async def _to_file(self, content: str) -> None:
-        """
-        That adding given content as new line in the file.
-
-        Parameters
-        ----------
-        content : str
-            Content which would be added as new line in the file.
-        """
-
-        if self.__target is None:
-            return
-
-        async with self.__file_lock:
-            await asyncio.to_thread(self.__add_file, content)
-
-    def __add_file(self, content: str) -> None:
-        """
-        That adding given content as new line to the file. It is synchronized
-        function, running in new thread. When file is not open, that open it.
-        
-        Parameters
-        ----------
-        content : str
-            Content to append into file as new line.
-        """
-
-        if self.__pipe is None or self.__pipe.closed:
-            self.__pipe = self.__target.open("a")
-
-        self.__pipe.write(content + os.linesep)
-
-    async def _to_stdout(self, content: str) -> None:
-        """
-        That add content to stdout fifo as new line.
-
-        Parameters
-        ----------
-        content : str
-            New content which would be add to stdout.
-        """
-
-        content = content + os.linesep
-
-        async with self.__stdout_lock:
-            await asyncio.to_thread(sys.stdout.write, content)
-
-    async def _to_stderr(self, content: str) -> None:
-        """
-        That add content to stderr fifo as new line.
-
-        Parameters
-        ----------
-        content : str
-            New content, which would be add to stderr.
-        """
-
-        content = content + os.linesep
-
-        async with self.__stderr_lock:
-            await asyncio.to_thread(sys.stderr.write, content)
+    def __init__(self) -> None:
+        self.__lock = asyncio.Lock()
 
     def __del__(self) -> None:
-        """
-        That is object destructor, which close log file, when it is not 
-        closed yet.
-        """
+        self.clean()
+
+    def open(self) -> None:
+        pass
 
-        if self.__pipe is None:
-            return
+    async def adding(self, content: str) -> None:
+        async with self.__lock:
+            await asyncio.to_thread(self.add, content)
 
-        if self.__pipe.closed:
-            return 
+    def add(self, content: str) -> None:
+        raise NotImplementedError()
 
-        self.__pipe.close()
+    def clean(self) -> None:
+        pass

+ 11 - 0
source/stderr_handler.py

@@ -0,0 +1,11 @@
+import sys
+import os
+
+from .handler import handler
+
+class stderr_handler(handler):
+    def add(self, content: str) -> None:
+        sys.stderr.write(content + os.linesep)
+
+
+

+ 10 - 0
source/stdout_handler.py

@@ -0,0 +1,10 @@
+import sys
+import os
+
+from .handler import handler
+
+class stdout_handler(handler):
+    def add(self, content: str) -> None:
+        sys.stdout.write(content + os.linesep)
+
+