Selaa lähdekoodia

detect encoding on file read (#974)

Dinh Long Nguyen 1 vuosi sitten
vanhempi
säilyke
9285f267ac

+ 1 - 0
Pipfile

@@ -35,6 +35,7 @@ twisted = "==23.8.0"
 tzlocal = "==3.0"
 boto3 = "==1.29.1"
 watchdog = "==4.0.0"
+charset-normalizer = "==3.3.2"
 
 [dev-packages]
 freezegun = "*"

+ 11 - 7
taipy/gui/_renderers/__init__.py

@@ -13,6 +13,10 @@ import typing as t
 from abc import ABC, abstractmethod
 from os import path
 
+from charset_normalizer import detect
+
+from taipy.logger._taipy_logger import _TaipyLogger
+
 from ..page import Page
 from ..utils import _is_in_notebook, _varname_from_content
 from ._html import _TaipyHTMLParser
@@ -64,11 +68,6 @@ class _Renderer(Page, ABC):
             if _is_in_notebook() and self._observer is None:
                 self.__observe_file_change(content)
             return
-        if self._frame is not None:
-            frame_dir_path = path.dirname(path.abspath(self._frame.f_code.co_filename))
-            content_path = path.join(frame_dir_path, content)
-            if path.exists(content_path) and path.isfile(content_path):
-                return self.__parse_file_content(content_path)
         self._content = content
 
     def __observe_file_change(self, file_path: str):
@@ -82,8 +81,13 @@ class _Renderer(Page, ABC):
         self._observer.start()
 
     def __parse_file_content(self, content):
-        with open(t.cast(str, content), "r") as f:
-            self._content = f.read()
+        with open(t.cast(str, content), "rb") as f:
+            file_content = f.read()
+            encoding = "utf-8"
+            if (detected_encoding := detect(file_content)["encoding"]) is not None:
+                encoding = detected_encoding
+                _TaipyLogger._get_logger().info(f"Detected '{encoding}' encoding for file '{content}'.")
+            self._content = file_content.decode(encoding)
             # Save file path for error handling
             self._filepath = content
 

+ 1 - 0
tools/packages/pipfiles/Pipfile3.10.max

@@ -80,3 +80,4 @@ version = "==4.2.13"
 "apispec" = {version="==6.4.0", extras=["yaml"]}
 "apispec-webframeworks" = {version="==1.0.0"}
 "watchdog" = {version="==4.0.0"}
+"charset-normalizer" = {version="==3.3.2"}

+ 1 - 0
tools/packages/pipfiles/Pipfile3.11.max

@@ -80,3 +80,4 @@ version = "==4.2.13"
 "apispec" = {version="==6.4.0", extras=["yaml"]}
 "apispec-webframeworks" = {version="==1.0.0"}
 "watchdog" = {version="==4.0.0"}
+"charset-normalizer" = {version="==3.3.2"}

+ 1 - 0
tools/packages/pipfiles/Pipfile3.12.max

@@ -80,3 +80,4 @@ version = "==4.2.13"
 "apispec" = {version="==6.4.0", extras=["yaml"]}
 "apispec-webframeworks" = {version="==1.0.0"}
 "watchdog" = {version="==4.0.0"}
+"charset-normalizer" = {version="==3.3.2"}

+ 1 - 0
tools/packages/pipfiles/Pipfile3.8.max

@@ -80,3 +80,4 @@ version = "==4.2.13"
 "apispec" = {version="==6.4.0", extras=["yaml"]}
 "apispec-webframeworks" = {version="==1.0.0"}
 "watchdog" = {version="==4.0.0"}
+"charset-normalizer" = {version="==3.3.2"}

+ 1 - 0
tools/packages/pipfiles/Pipfile3.9.max

@@ -80,3 +80,4 @@ version = "==4.2.13"
 "apispec" = {version="==6.4.0", extras=["yaml"]}
 "apispec-webframeworks" = {version="==1.0.0"}
 "watchdog" = {version="==4.0.0"}
+"charset-normalizer" = {version="==3.3.2"}

+ 1 - 0
tools/packages/taipy-gui/setup.requirements.txt

@@ -15,3 +15,4 @@ taipy-config
 twisted>=23.8.0,<=23.10.0
 tzlocal>=3.0,<=5.2
 watchdog>=4.0.0,<=4.0.0
+charset-normalizer>=3.3.2,<=3.3.2