Refactor _scan_pages method and enhance file reading logic in PageNode class
- Updated docstring for _scan_pages to clarify exclusion of .allowed files. - Improved file reading logic to handle script detection and content retrieval more efficiently. - Refined error handling during the announce process to catch specific exceptions.
This commit is contained in:
@@ -171,7 +171,7 @@ class PageNode:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def _scan_pages(self, base):
|
def _scan_pages(self, base):
|
||||||
"""Return a list of .mu page paths under the given directory."""
|
"""Return a list of page paths under the given directory, excluding .allowed files."""
|
||||||
base_path = Path(base)
|
base_path = Path(base)
|
||||||
if not base_path.exists():
|
if not base_path.exists():
|
||||||
return []
|
return []
|
||||||
@@ -228,13 +228,16 @@ class PageNode:
|
|||||||
|
|
||||||
if not str(file_path).startswith(str(pagespath)):
|
if not str(file_path).startswith(str(pagespath)):
|
||||||
return DEFAULT_NOTALLOWED.encode("utf-8")
|
return DEFAULT_NOTALLOWED.encode("utf-8")
|
||||||
|
is_script = False
|
||||||
|
file_content = None
|
||||||
try:
|
try:
|
||||||
with file_path.open("rb") as file_handle:
|
with file_path.open("rb") as file_handle:
|
||||||
first_line = file_handle.readline()
|
first_line = file_handle.readline()
|
||||||
is_script = first_line.startswith(b"#!")
|
is_script = first_line.startswith(b"#!")
|
||||||
|
file_handle.seek(0)
|
||||||
if not is_script:
|
if not is_script:
|
||||||
file_handle.seek(0)
|
|
||||||
return file_handle.read()
|
return file_handle.read()
|
||||||
|
file_content = file_handle.read()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return DEFAULT_NOTALLOWED.encode("utf-8")
|
return DEFAULT_NOTALLOWED.encode("utf-8")
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
@@ -266,6 +269,8 @@ class PageNode:
|
|||||||
return result.stdout
|
return result.stdout
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log(f"Error executing script page: {e}", RNS.LOG_ERROR)
|
RNS.log(f"Error executing script page: {e}", RNS.LOG_ERROR)
|
||||||
|
if file_content is not None:
|
||||||
|
return file_content
|
||||||
try:
|
try:
|
||||||
return self._read_file_bytes(file_path)
|
return self._read_file_bytes(file_path)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
@@ -323,9 +328,10 @@ class PageNode:
|
|||||||
else:
|
else:
|
||||||
self.destination.announce()
|
self.destination.announce()
|
||||||
self.last_announce = time.time()
|
self.last_announce = time.time()
|
||||||
except Exception as announce_error:
|
except (TypeError, ValueError) as announce_error:
|
||||||
RNS.log(
|
RNS.log(
|
||||||
f"Error during announce: {announce_error}", RNS.LOG_ERROR,
|
f"Error during announce: {announce_error}",
|
||||||
|
RNS.LOG_ERROR,
|
||||||
)
|
)
|
||||||
wait_time = max(
|
wait_time = max(
|
||||||
(self.last_announce + interval_seconds) - time.time()
|
(self.last_announce + interval_seconds) - time.time()
|
||||||
|
|||||||
Reference in New Issue
Block a user