Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
Add pylint linting (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinHjelmare authored Apr 22, 2021
1 parent 5d96c0b commit e6b6670
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 49 deletions.
33 changes: 18 additions & 15 deletions imjoy/__main__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Provide main entrypoint."""
import asyncio
import json
import logging
import os
import re
import sys
import asyncio
from aiohttp import web
import logging
import urllib.request

from aiohttp import web
from imjoy_rpc import default_config

from imjoy.options import parse_cmd_line
Expand All @@ -20,13 +21,13 @@
def load_plugin(plugin_file):
"""Load plugin file."""
try:
import yaml
except Exception:
import yaml # pylint: disable=import-outside-toplevel
except Exception as err:
logger.error(
"It appears that your ImJoy installation is not complete, "
"please reinstall it with 'pip install imjoy[socketio]'"
)
raise SystemExit
raise SystemExit from err
if os.path.isfile(plugin_file):
content = open(plugin_file).read()
elif plugin_file.startswith("http"):
Expand All @@ -40,10 +41,10 @@ def load_plugin(plugin_file):
filename, _ = os.path.splitext(os.path.basename(plugin_file))
default_config["name"] = filename[:32]
try:
exec(content, globals())
exec(content, globals()) # pylint: disable=exec-used
logger.info("Plugin executed")
except Exception as e:
logger.error("Failed to execute plugin %s", e)
except Exception as err: # pylint: disable=broad-except
logger.error("Failed to execute plugin %s", err)

elif plugin_file.endswith(".imjoy.html"):
# load config
Expand All @@ -58,10 +59,10 @@ def load_plugin(plugin_file):
found = re.findall("<script (.*)>\n(.*)</script>", content, re.DOTALL)[0]
if "python" in found[0]:
try:
exec(found[1], globals())
exec(found[1], globals()) # pylint: disable=exec-used
logger.info("Plugin executed")
except Exception as e:
logger.error("Failed to execute plugin %s", e)
except Exception as err: # pylint: disable=broad-except
logger.error("Failed to execute plugin %s", err)
else:
raise Exception(
"Invalid script type ({}) in file {}".format(found[0], plugin_file)
Expand Down Expand Up @@ -92,13 +93,14 @@ async def start_plugin(app):

if opt.serve:
try:
# pylint: disable=import-outside-toplevel
from imjoy.socketio_server import create_socketio_server
except Exception:
except Exception as err:
logger.error(
"It appears that your ImJoy installation is not complete, "
"please reinstall it with 'pip install imjoy[socketio]'"
)
raise SystemExit
raise SystemExit from err
if opt.plugin_server and not opt.plugin_server.endswith(opt.serve):
print(
"WARNING: the specified port ({}) "
Expand All @@ -117,6 +119,7 @@ async def start_plugin(app):
elif opt.jupyter:
sys.argv = sys.argv[:1]
sys.argc = 1
# pylint: disable=import-outside-toplevel
from notebook.notebookapp import NotebookApp

kwargs = {
Expand Down Expand Up @@ -145,7 +148,7 @@ async def start_plugin(app):
if app.port != int(opt.port):
print("\nWARNING: using a different port: {}.\n".format(app.port))
write_token(app.token)
app._token_generated = True
app._token_generated = True # pylint: disable=protected-access
app.start()

elif not opt.legacy:
Expand Down
53 changes: 27 additions & 26 deletions imjoy/socketio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def setup_router(app, static_dir=None):

def setup_socketio(sio):
"""Set up socketio."""
# pylint: disable=unused-variable

@sio.event
async def list_plugins(sid, data):
Expand All @@ -78,8 +79,8 @@ async def register_plugin(sid, config):
plugins[config["id"]] = config
plugin_channel = str(uuid.uuid4())
config["plugin_channel"] = plugin_channel
clients = {}
config["clients"] = clients
plugin_clients = {}
config["clients"] = plugin_clients

# broadcast to the plugin message to all the clients
@sio.on(plugin_channel)
Expand Down Expand Up @@ -115,33 +116,33 @@ async def start_plugin(sid, config):
async def connect_plugin(sid, data):
pid = data.get("id")

if pid in plugins:
plugin_info = plugins[pid]
client_info = {}
if pid not in plugins:
logger.error("Plugin not found %s, requested by client %s", pid, sid)
return {"error": "Plugin not found: " + pid}

# generate a channel and store it to plugin.clients
client_channel = str(uuid.uuid4())
logger.info(f"{client_channel}(sid:{sid}) is connecting to plugin {pid}")
plugin_info["clients"][client_channel] = client_info
client_info["channel"] = client_channel
plugin_info = plugins[pid]
client_info = {}

# listen to the client channel and forward to the plugin
@sio.on(client_channel)
async def on_client_message(sid, data):
await sio.emit(plugin_info["plugin_channel"], data)
# generate a channel and store it to plugin.clients
client_channel = str(uuid.uuid4())
logger.info("%s(sid:%s) is connecting to plugin %s", client_channel, sid, pid)
plugin_info["clients"][client_channel] = client_info
client_info["channel"] = client_channel

# notify the plugin about the new client
await sio.emit(plugin_info["plugin_channel"] + "-new-client", client_info)
# listen to the client channel and forward to the plugin
@sio.on(client_channel)
async def on_client_message(sid, data):
await sio.emit(plugin_info["plugin_channel"], data)

async def finalize():
del plugin_info["clients"][client_channel]
# notify the plugin about the new client
await sio.emit(plugin_info["plugin_channel"] + "-new-client", client_info)

finalizers.append([sid, finalize])
async def finalize():
del plugin_info["clients"][client_channel]

return {"channel": client_channel}
else:
logger.error(f"Plugin not found {pid}, requested by client {sid}")
return {"error": "Plugin not found: " + pid}
finalizers.append([sid, finalize])

return {"channel": client_channel}

@sio.event
async def connect(sid, environ):
Expand All @@ -153,12 +154,12 @@ async def disconnect(sid):
for obj in lst2finalize:
finalize_func = obj[1]
try:
logger.info("Removing " + obj[0])
logger.info("Removing %s", obj[0])
await finalize_func()
finally:
finalizers.remove(obj)


if __name__ == "__main__":
app = create_socketio_server()
web.run_app(app, port=9988)
application = create_socketio_server()
web.run_app(application, port=9988)
14 changes: 8 additions & 6 deletions imjoy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,24 @@ def get_drives():
return drives


_server_thread = None
_SERVER_THREAD = None


def _show_elfinder_colab(root_dir="/content", port=8765, height=600, width="100%"):
# pylint: disable=import-error, import-outside-toplevel, no-name-in-module
from google.colab import output
from imjoy_elfinder.app import main

global _server_thread
if _server_thread is None:
global _SERVER_THREAD # pylint: disable=global-statement
if _SERVER_THREAD is None:

def start_elfinder():
global _server_thread
global _SERVER_THREAD # pylint: disable=global-statement
try:
main(["--root-dir={}".format(root_dir), "--port={}".format(port)])
except OSError:
print("ImJoy-elFinder server already started.")
_server_thread = thread
_SERVER_THREAD = thread

# start imjoy-elfinder server
thread = threading.Thread(target=start_elfinder)
Expand All @@ -50,7 +51,7 @@ def start_elfinder():


def _show_elfinder_jupyter(url="/elfinder", height=600, width="100%"):
from IPython import display
from IPython import display # pylint: disable=import-outside-toplevel

code = """(async (url, width, height, element) => {
element.appendChild(document.createTextNode(''));
Expand All @@ -69,6 +70,7 @@ def _show_elfinder_jupyter(url="/elfinder", height=600, width="100%"):
def show_elfinder(**kwargs):
"""Show elfinder."""
try:
# pylint: disable=import-outside-toplevel, unused-import
from google.colab import output # noqa: F401

is_colab = True
Expand Down
19 changes: 19 additions & 0 deletions pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[MASTER]
reports=no

disable=
locally-disabled,
unused-argument,
fixme,

# Minimum number of public methods for a class (see R0903).
min-public-methods=0

# Maximum number of locals for function / method body
max-locals=25

# Maximum number of branch for function / method body
max-branches=30

# Maximum number of return / yield for function / method body
max-returns=8
1 change: 1 addition & 0 deletions requirements_lint.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
black==19.10b0
flake8==3.9.0
pylint==2.7.4
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Set up the ImJoy-Engine imjoy package."""
import os
from setuptools import setup, find_packages
import json
import os

from setuptools import find_packages, setup

DESCRIPTION = (
"ImJoy Plugin Engine for running Python plugins locally "
Expand All @@ -10,6 +11,7 @@

try:
# for Google Colab
# pylint: disable=unused-import
import google.colab.output # noqa: F401

REQUIREMENTS = [
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ignore_errors = True
commands =
black --check ./
flake8 ./
pylint imjoy setup.py tests
deps =
-rrequirements.txt
-rrequirements_lint.txt
Expand Down

0 comments on commit e6b6670

Please sign in to comment.