Browse Source

maint: improve code quality

wangweimin 5 năm trước cách đây
mục cha
commit
c4f8eff43a

+ 16 - 0
lgtm.yml

@@ -0,0 +1,16 @@
+##########################################################################################
+# Use the `path_classifiers` block to define changes to the default classification of    #
+# files.                                                                                 #
+##########################################################################################
+
+path_classifiers:
+  test:
+    - exclude: /
+    - test
+    - exclude: test/util.py
+
+  library:
+    - pywebio/html
+    - exclude: pywebio/html/js/pywebio.js
+    - exclude: pywebio/html/css/app.css
+    - exclude: pywebio/html/index.html

+ 2 - 4
pywebio/__init__.py

@@ -10,14 +10,12 @@ from .__version__ import __author__, __author_email__, __license__, __copyright_
 
 # Set default logging handler to avoid "No handler found" warnings.
 import logging
-from logging import NullHandler, StreamHandler
-
-logging.getLogger(__name__).addHandler(NullHandler())
+logging.getLogger(__name__).addHandler(logging.NullHandler())
 
 
 def enable_debug(level=logging.DEBUG):
     """Output PyWebIO logging message to sys.stderr"""
-    ch = StreamHandler()
+    ch = logging.StreamHandler()
     ch.setLevel(level)
     formatter = logging.Formatter('[%(levelname)s %(asctime)s %(module)s:%(lineno)d %(funcName)s] %(message)s',
                                   datefmt='%y%m%d %H:%M:%S')

+ 5 - 25
pywebio/html/js/pywebio.js

@@ -25,15 +25,6 @@
         return blob;
     };
 
-    function extend(Child, Parent) {
-        var F = function () {
-        };
-        F.prototype = Parent.prototype;
-        Child.prototype = new F();
-        Child.prototype.constructor = Child;
-        Child.uber = Parent.prototype;
-    }
-
     function make_set(arr) {
         var set = {};
         for (var idx in arr)
@@ -237,7 +228,7 @@
         if (msg.spec.auto_scroll_bottom !== undefined)
             AutoScrollBottom = msg.spec.auto_scroll_bottom;
         if (msg.spec.set_anchor !== undefined) {
-            this.container_elem.find(`#${msg.spec.set_anchor}`).attr('id', '');
+            this.container_elem.find(`#${msg.spec.set_anchor}`).removeAttr('id');
             this.container_elem.append(`<div id="${msg.spec.set_anchor}"></div>`);
         }
         if (msg.spec.clear_before !== undefined)
@@ -389,17 +380,6 @@
         }
     }
 
-
-    function FormStack() {
-        push();
-        pop();
-        empty();
-
-        show();// 显示栈顶元素
-        hide();// 隐藏栈顶元素
-    }
-
-
     function FormController(webio_session, task_id, spec) {
         this.webio_session = webio_session;
         this.task_id = task_id;
@@ -968,7 +948,7 @@
             this.ws.onclose = this.on_session_close;
             this.ws.onmessage = function (evt) {
                 var msg = JSON.parse(evt.data);
-                if (debug) console.debug('>>>', msg);
+                if (debug) console.info('>>>', msg);
                 this_.on_server_message(msg);
             };
         };
@@ -977,7 +957,7 @@
                 return console.error('WebSocketWebIOSession.ws is null when invoke WebSocketWebIOSession.send_message. ' +
                     'Please call WebSocketWebIOSession.start_session first');
             this.ws.send(JSON.stringify(msg));
-            if (this.debug) console.debug('<<<', msg);
+            if (this.debug) console.info('<<<', msg);
         };
         this.close_session = function () {
             this.on_session_close();
@@ -1003,7 +983,7 @@
 
             for (var idx in data) {
                 var msg = data[idx];
-                if (this_.debug) console.debug('>>>', msg);
+                if (this_.debug) console.info('>>>', msg);
                 this_.on_server_message(msg);
             }
         };
@@ -1031,7 +1011,7 @@
             this.interval_pull_id = setInterval(pull, pull_interval_ms);
         };
         this.send_message = function (msg) {
-            if (this_.debug) console.debug('<<<', msg);
+            if (this_.debug) console.info('<<<', msg);
             $.ajax({
                 type: "POST",
                 url: this.api_url,

+ 0 - 1
pywebio/platform/tornado.py

@@ -11,7 +11,6 @@ from urllib.parse import urlparse
 import tornado
 import tornado.httpserver
 import tornado.ioloop
-import tornado.websocket
 from tornado.web import StaticFileHandler
 from tornado.websocket import WebSocketHandler
 

+ 2 - 2
pywebio/session/coroutinebased.py

@@ -182,7 +182,7 @@ class CoroutineBasedSession(AbstractSession):
         traceback_msg = ''.join(lines)
         try:
             put_markdown("发生错误:\n```\n%s\n```" % traceback_msg)
-        except:
+        except Exception:
             pass
 
     def register_callback(self, callback, mutex_mode=False):
@@ -212,7 +212,7 @@ class CoroutineBasedSession(AbstractSession):
                 else:
                     try:
                         callback(event['data'])
-                    except:
+                    except Exception:
                         self.on_task_exception()
 
                 if coro is not None:

+ 2 - 2
pywebio/session/threadbased.py

@@ -212,7 +212,7 @@ class ThreadBasedSession(AbstractSession):
         traceback_msg = ''.join(lines)
         try:
             put_markdown("发生错误:\n```\n%s\n```" % traceback_msg)
-        except:
+        except Exception:
             pass
 
     def _activate_callback_env(self):
@@ -250,7 +250,7 @@ class ThreadBasedSession(AbstractSession):
             def run(callback):
                 try:
                     callback(event['data'])
-                except:
+                except Exception:
                     # 子类可能会重写 get_current_session ,所以不要用 ThreadBasedSession.get_current_session 来调用
                     self.get_current_session().on_task_exception()
 

+ 2 - 2
pywebio/utils.py

@@ -25,7 +25,7 @@ def catch_exp_call(func, logger):
     """
     try:
         return func()
-    except:
+    except Exception:
         logger.exception("Error when invoke `%s`" % func)
 
 
@@ -110,7 +110,7 @@ async def wait_host_port(host, port, duration=10, delay=2):
             writer.close()
             await writer.wait_closed()
             return True
-        except:
+        except Exception:
             if delay:
                 await asyncio.sleep(delay)
     return False