2 Commits

Author SHA1 Message Date
c36b9b97e0 Add missing MANIFEST.in, update .gitignore 2020-08-03 23:48:07 -04:00
549a27e66c Add flake8 tests and clean up warnings 2020-08-03 23:46:34 -04:00
7 changed files with 97 additions and 74 deletions

2
.gitignore vendored
View File

@@ -3,8 +3,8 @@ build/
*.pyc *.pyc
dist/ dist/
nilmrun.egg-info/ nilmrun.egg-info/
.eggs/
# This gets generated as needed by setup.py # This gets generated as needed by setup.py
MANIFEST.in
MANIFEST MANIFEST

8
MANIFEST.in Normal file
View File

@@ -0,0 +1,8 @@
# Root
include README.md
include setup.py
include versioneer.py
include Makefile
# Version
include nilmrun/_version.py

View File

@@ -1,9 +1,6 @@
# By default, run the tests. # By default, run the tests.
all: test all: test
test2:
nilmrun/trainola.py data.js
version: version:
python3 setup.py version python3 setup.py version
@@ -23,8 +20,11 @@ develop:
docs: docs:
make -C docs make -C docs
ctrl: flake
flake:
flake8 nilmrun
lint: lint:
pylint3 --rcfile=.pylintrc nilmdb pylint3 --rcfile=setup.cfg nilmrun
test: test:
ifneq ($(INSIDE_EMACS),) ifneq ($(INSIDE_EMACS),)
@@ -37,12 +37,13 @@ else
endif endif
clean:: clean::
find . -name '*.pyc' -o -name '__pycache__' -print0 | xargs -0 rm -rf
rm -f .coverage rm -f .coverage
find . -name '*pyc' | xargs rm -f rm -rf nilmrun.egg-info/ build/
rm -rf nilmtools.egg-info/ build/ MANIFEST.in
make -C docs clean make -C docs clean
gitclean:: gitclean::
git clean -dXf git clean -dXf
.PHONY: all version dist sdist install docs lint test clean gitclean .PHONY: all version dist sdist install docs test
.PHONY: ctrl lint flake clean gitclean

View File

@@ -1,5 +1,3 @@
import nilmrun.processmanager
from ._version import get_versions from ._version import get_versions
__version__ = get_versions()['version'] __version__ = get_versions()['version']
del get_versions del get_versions

View File

@@ -1,7 +1,5 @@
#!/usr/bin/python #!/usr/bin/python
from nilmdb.utils.printf import *
import threading import threading
import subprocess import subprocess
import io import io
@@ -15,9 +13,11 @@ import tempfile
import atexit import atexit
import shutil import shutil
class ProcessError(Exception): class ProcessError(Exception):
pass pass
class LogReceiver(object): class LogReceiver(object):
"""Spawn a thread that listens to a pipe for log messages, """Spawn a thread that listens to a pipe for log messages,
and stores them locally.""" and stores them locally."""
@@ -41,6 +41,7 @@ class LogReceiver(object):
def clear(self): def clear(self):
self.log = io.BytesIO() self.log = io.BytesIO()
class Process(object): class Process(object):
"""Spawn and manage a subprocess, and capture its output.""" """Spawn and manage a subprocess, and capture its output."""
def __init__(self, argv, tempfile=None): def __init__(self, argv, tempfile=None):
@@ -180,6 +181,7 @@ class Process(object):
pass pass
return d return d
class ProcessManager(object): class ProcessManager(object):
"""Track and manage a collection of Process objects""" """Track and manage a collection of Process objects"""
def __init__(self): def __init__(self):
@@ -260,7 +262,8 @@ class ProcessManager(object):
def get_info(self): def get_info(self):
"""Get info about all running PIDs""" """Get info about all running PIDs"""
info = { "total" : Process.get_empty_info(), info = {
"total": Process.get_empty_info(),
"pids": {}, "pids": {},
"system": {} "system": {}
} }

View File

@@ -1,18 +1,12 @@
"""CherryPy-based server for running NILM filters via HTTP""" """CherryPy-based server for running NILM filters via HTTP"""
import cherrypy import cherrypy
import sys
import os import os
import socket import socket
import json
import traceback import traceback
import time
import nilmdb from nilmdb.utils.printf import sprintf
from nilmdb.utils.printf import *
from nilmdb.server.serverutil import ( from nilmdb.server.serverutil import (
chunked_response,
response_type,
exception_to_httperror, exception_to_httperror,
CORS_allow, CORS_allow,
json_to_request_params, json_to_request_params,
@@ -23,10 +17,12 @@ from nilmdb.server.serverutil import (
) )
from nilmdb.utils import serializer_proxy from nilmdb.utils import serializer_proxy
import nilmrun import nilmrun
import nilmrun.processmanager
# Add CORS_allow tool # Add CORS_allow tool
cherrypy.tools.CORS_allow = cherrypy.Tool('on_start_resource', CORS_allow) cherrypy.tools.CORS_allow = cherrypy.Tool('on_start_resource', CORS_allow)
# CherryPy apps # CherryPy apps
class App(object): class App(object):
"""Root application for NILM runner""" """Root application for NILM runner"""
@@ -53,6 +49,7 @@ class App(object):
def version(self): def version(self):
return nilmrun.__version__ return nilmrun.__version__
class AppProcess(object): class AppProcess(object):
def __init__(self, manager): def __init__(self, manager):
@@ -116,6 +113,7 @@ class AppProcess(object):
self.manager.remove(pid) self.manager.remove(pid)
return status return status
class AppRun(object): class AppRun(object):
def __init__(self, manager): def __init__(self, manager):
self.manager = manager self.manager = manager
@@ -154,6 +152,7 @@ class AppRun(object):
"args must be a list of strings") "args must be a list of strings")
return self.manager.run_code(code, args) return self.manager.run_code(code, args)
class Server(object): class Server(object):
def __init__(self, host='127.0.0.1', port=8080, def __init__(self, host='127.0.0.1', port=8080,
force_traceback=False, # include traceback in all errors force_traceback=False, # include traceback in all errors
@@ -181,8 +180,10 @@ class Server(object):
# Set up Cross-Origin Resource Sharing (CORS) handler so we # Set up Cross-Origin Resource Sharing (CORS) handler so we
# can correctly respond to browsers' CORS preflight requests. # can correctly respond to browsers' CORS preflight requests.
# This also limits verbs to GET and HEAD by default. # This also limits verbs to GET and HEAD by default.
app_config.update({ 'tools.CORS_allow.on': True, app_config.update({
'tools.CORS_allow.methods': ['GET', 'HEAD'] }) 'tools.CORS_allow.on': True,
'tools.CORS_allow.methods': ['GET', 'HEAD']
})
# Configure the 'json_in' tool to also allow other content-types # Configure the 'json_in' tool to also allow other content-types
# (like x-www-form-urlencoded), and to treat JSON as a dict that # (like x-www-form-urlencoded), and to treat JSON as a dict that
@@ -226,10 +227,13 @@ class Server(object):
def stop(self): def stop(self):
cherrypy_stop() cherrypy_stop()
# Multiple processes and threads should be OK here, but we'll still # Multiple processes and threads should be OK here, but we'll still
# follow the NilmDB approach of having just one globally initialized # follow the NilmDB approach of having just one globally initialized
# copy of the server object. # copy of the server object.
_wsgi_server = None _wsgi_server = None
def wsgi_application(basepath): # pragma: no cover def wsgi_application(basepath): # pragma: no cover
"""Return a WSGI application object. """Return a WSGI application object.
@@ -247,8 +251,7 @@ def wsgi_application(basepath): # pragma: no cover
except Exception: except Exception:
# Build an error message on failure # Build an error message on failure
import pprint import pprint
err = sprintf("Initializing nilmrun failed:\n\n", err = "Initializing nilmrun failed:\n\n"
dbpath)
err += traceback.format_exc() err += traceback.format_exc()
try: try:
import pwd import pwd
@@ -263,8 +266,10 @@ def wsgi_application(basepath): # pragma: no cover
err += sprintf("\nEnvironment:\n%s\n", pprint.pformat(environ)) err += sprintf("\nEnvironment:\n%s\n", pprint.pformat(environ))
if _wsgi_server is None: if _wsgi_server is None:
# Serve up the error with our own mini WSGI app. # Serve up the error with our own mini WSGI app.
headers = [ ('Content-type', 'text/plain'), headers = [
('Content-length', str(len(err))) ] ('Content-type', 'text/plain'),
('Content-length', str(len(err)))
]
start_response("500 Internal Server Error", headers) start_response("500 Internal Server Error", headers)
return [err] return [err]

View File

@@ -28,3 +28,11 @@ versionfile_source=nilmrun/_version.py
versionfile_build=nilmrun/_version.py versionfile_build=nilmrun/_version.py
tag_prefix=nilmrun- tag_prefix=nilmrun-
parentdir_prefix=nilmrun- parentdir_prefix=nilmrun-
[flake8]
exclude=_version.py
extend-ignore=E731
[pylint]
ignore=_version.py
disable=C0103,C0111,R0913,R0914