Skip to content

Commit b3ab2d4

Browse files
committed
fix: now macos opening file will not trigger _log
1 parent 288560d commit b3ab2d4

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ build
1111
.env
1212
venv
1313
*/log.txt
14+
*/log.txt.1
1415
*_tmp*
1516
.DS_Store
1617
.TempDir

resbibman/core/fileTools.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
The tools that deals with files in the database
33
"""
44
from pathlib import Path
5-
import os, shutil, json, platform, typing, uuid, time
5+
import os, shutil, json, platform, typing, uuid, time, sys
66
from typing import List, Union, TypedDict
77
import warnings
8+
import threading
9+
810
from . import globalVar as G
911
from .utils import openFile, TimeUtils
1012
from ..confReader import getConf, VERSION, getConfV
@@ -199,6 +201,10 @@ def __init__(self, data_path):
199201
self.path = data_path
200202
self.base_name: str = os.path.split(data_path)[-1]
201203
self._file_extension: str = "" # extension of the main document, "" for undefined / No file
204+
205+
# a switch to trigger the logging of file modification time
206+
self._enable_log_modification_timestamp = True
207+
202208
self.init()
203209

204210
def init(self):
@@ -464,8 +470,20 @@ def openFile(self):
464470
if self.file_extension == ".hpack":
465471
openTmp_hpack(self.file_p)
466472
else:
473+
# Open file in MacOS will be treated as a file modification
474+
# so temporarily ban file observer log modification time
475+
if sys.platform == "darwin":
476+
self._enable_log_modification_timestamp = False
477+
467478
openFile(self.file_p)
468-
# self._log() # change time modified.
479+
480+
# maybe re-enable file observer log modification time
481+
def _restartLoggingModification():
482+
# relax some time to open the file
483+
time.sleep(3)
484+
self._enable_log_modification_timestamp = True
485+
if sys.platform == "darwin":
486+
threading.Thread(target=_restartLoggingModification, args=(), daemon=True).start()
469487
return True
470488

471489
def openMiscDir(self):
@@ -499,7 +517,9 @@ def setWatch(self, status: bool = False):
499517
self.logger.debug(f"setWatch (fm): Started file observer for {self.uuid}")
500518

501519
def _log(self) -> bool:
502-
"""log the modification info info file"""
520+
"""log the modification time to the info file"""
521+
if not self._enable_log_modification_timestamp:
522+
return False
503523
time_now = time.time()
504524
if time_now - self._time_last_log < self.LOG_TOLERANCE_INTERVAL:
505525
# Prevent multiple log at same time

0 commit comments

Comments
 (0)