]> git.phdru.name Git - bookmarks_db.git/blobdiff - Robots/bkmk_rforking_sub.py
Fix(Py3): Always log in UTF-8
[bookmarks_db.git] / Robots / bkmk_rforking_sub.py
index 7dd1f5fa7b7da797bff902338afe0507665e4335..8bbf68f57028b7c2bfc80c68407ab3d94a9afbe5 100755 (executable)
@@ -1,4 +1,4 @@
-#! /usr/bin/env python
+#! /usr/bin/env python3
 """Subprocess for the forking robot - check URL using bkmk_rurlib robot
 
 This file is a part of Bookmarks database and Internet robot.
@@ -6,44 +6,46 @@ This file is a part of Bookmarks database and Internet robot.
 """
 
 __author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 1999-2014 PhiloSoft Design"
+__copyright__ = "Copyright (C) 1999-2023 PhiloSoft Design"
 __license__ = "GNU GPL"
 
 __all__ = []
 
 
-import sys, os
-
-lib_dir = os.path.normpath(os.path.join(os.path.dirname(sys.argv[0]), os.pardir))
-sys.path.append(lib_dir) # for bkmk_objects.py
+import os
+import sys
 
 try:
-   import cPickle
-   pickle = cPickle
+    import cPickle
+    pickle = cPickle
 except ImportError:
-   import pickle
+    import pickle
+
+lib_dir = os.path.normpath(os.path.dirname(os.path.dirname(sys.argv[0])))
+sys.path.append(lib_dir)  # for bkmk_objects.py
 
-from subproc import RecordFile
+from subproc import RecordFile  # noqa: E402 import not at top of file
 
 
 def run():
-   bkmk_in = RecordFile(sys.stdin)
-   bkmk_out = RecordFile(sys.stdout)
+    bkmk_in = RecordFile(getattr(sys.stdin, 'buffer', None) or sys.stdin)
+    bkmk_out = RecordFile(getattr(sys.stdout, 'buffer', None) or sys.stdout)
 
-   from m_lib.flog import openlog
-   log = openlog("check2.log")
-   from robots import robot
-   robot = robot(log)
+    from m_lib.flog import openlog
+    log = openlog("check2.log")
+    log.outfile.reconfigure(encoding='utf-8')
+    from robots import robot
+    robot = robot(log)
 
-   while 1:
-      bookmark = pickle.loads(bkmk_in.read_record())
-      log(bookmark.href)
-      robot.check_url(bookmark)
-      bkmk_out.write_record(pickle.dumps(bookmark))
-      log.outfile.flush()
+    while 1:
+        bookmark = pickle.loads(bkmk_in.read_record())
+        log(bookmark.href)
+        robot.check_url(bookmark)
+        bkmk_out.write_record(pickle.dumps(bookmark))
+        log.outfile.flush()
 
-   log.close()
+    log.close()
 
 
 if __name__ == '__main__':
-   run()
+    run()