Browse Source

Convert runserver.py into a generated nilmdb-server script

tags/nilmdb-1.0
Jim Paris 9 years ago
parent
commit
5c531d8273
3 changed files with 79 additions and 35 deletions
  1. +78
    -0
      nilmdb/scripts/nilmdb_server.py
  2. +0
    -35
      runserver.py
  3. +1
    -0
      setup.py

+ 78
- 0
nilmdb/scripts/nilmdb_server.py View File

@@ -0,0 +1,78 @@
#!/usr/bin/python

import nilmdb.server
import argparse
import os
import socket

def main():
"""Main entry point for the 'nilmdb-server' command line script"""

parser = argparse.ArgumentParser(
description = 'Run the NilmDB server',
formatter_class = argparse.ArgumentDefaultsHelpFormatter)

group = parser.add_argument_group("Standard options")
group.add_argument('-a', '--address',
help = 'Only listen on the given address',
default = '0.0.0.0')
group.add_argument('-p', '--port', help = 'Listen on the given port',
type = int, default = 12380)
group.add_argument('-d', '--database', help = 'Database directory',
default = os.path.join(os.getcwd(), "db"))
group.add_argument('-q', '--quiet', help = 'Silence output',
action = 'store_true')

group = parser.add_argument_group("Debug options")
group.add_argument('-y', '--yappi', help = 'Run under yappi profiler and '
'invoke interactive shell afterwards',
action = 'store_true')

args = parser.parse_args()

# Create database object
db = nilmdb.server.NilmDB(args.database)

# Configure the server
if args.quiet:
embedded = True
else:
embedded = False
server = nilmdb.server.Server(db,
host = args.address,
port = args.port,
embedded = embedded)

# Print info
if not args.quiet:
print "Database: %s" % (os.path.realpath(args.database))
if args.address == '0.0.0.0' or args.address == '::':
host = socket.getfqdn()
else:
host = args.address
print "Server URL: http://%s:%d/" % ( host, args.port)
print "----"

# Run it
if args.yappi:
print "Running in yappi"
try:
import yappi
yappi.start()
server.start(blocking = True)
finally:
yappi.stop()
yappi.print_stats(sort_type = yappi.SORTTYPE_TTOT, limit = 50)
from IPython import embed
embed(header = "Use the yappi object to explore further, "
"quit to exit")
else:
server.start(blocking = True)

# Clean up
if not args.quiet:
print "Closing database"
db.close()

if __name__ == "__main__":
main()

+ 0
- 35
runserver.py View File

@@ -1,35 +0,0 @@
#!/usr/bin/python

import nilmdb
import argparse

formatter = argparse.ArgumentDefaultsHelpFormatter
parser = argparse.ArgumentParser(description='Run the NILM server',
formatter_class = formatter)
parser.add_argument('-p', '--port', help='Port number', type=int, default=12380)
parser.add_argument('-d', '--database', help='Database directory', default="db")
parser.add_argument('-y', '--yappi', help='Run with yappi profiler',
action='store_true')
args = parser.parse_args()

# Start web app on a custom port
db = nilmdb.NilmDB(args.database)
server = nilmdb.Server(db, host = "127.0.0.1",
port = args.port,
embedded = False)


if args.yappi:
print "Running in yappi"
try:
import yappi
yappi.start()
server.start(blocking = True)
finally:
yappi.stop()
print "Try: yappi.print_stats(sort_type=yappi.SORTTYPE_TTOT,limit=50)"
from IPython import embed
embed()
else:
server.start(blocking = True)
db.close()

+ 1
- 0
setup.py View File

@@ -48,6 +48,7 @@ setup(name='nilmdb',
entry_points = {
'console_scripts': [
'nilmtool = nilmdb.scripts.nilmtool:main',
'nilmdb-server = nilmdb.scripts.nilmdb_server:main',
],
},
ext_modules = cython_modules,


Loading…
Cancel
Save