Changeset: 811ec4cbbf29 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=811ec4cbbf29
Modified Files:
        clients/iotapi/documentation/api_server_arguments.rst
        clients/iotapi/requirements.txt
        clients/iotapi/src/Settings/filesystem.py
        clients/iotapi/src/Settings/iotlogger.py
        clients/iotapi/src/Streams/streams.py
        clients/iotapi/src/WebSockets/websockets.py
        clients/iotapi/src/main.py
        clients/iotclient/documentation/iot_server_arguments.rst
        clients/iotclient/requirements.txt
        clients/iotclient/src/Settings/filesystem.py
        clients/iotclient/src/Settings/iotlogger.py
        clients/iotclient/src/main.py
Branch: iot
Log Message:

Improved arguments parsing


diffs (truncated from 681 to 300 lines):

diff --git a/clients/iotapi/documentation/api_server_arguments.rst 
b/clients/iotapi/documentation/api_server_arguments.rst
--- a/clients/iotapi/documentation/api_server_arguments.rst
+++ b/clients/iotapi/documentation/api_server_arguments.rst
@@ -24,31 +24,30 @@ Paths
 
 .. important:: Make sure the current user has permissions on the locations 
provided.
 
-**-f - -filesystem=**
+**-f - -filesystem**
 
 Set the filesystem directory where the baskets will be created. By default in 
UNIX systems is on :code:`/etc/iotapi` directory, while on Windows is on the 
directory where the :code:`main.py` script was invoked.
 
-**-l  - -log=**
+**-l  - -log**
 
 Location of logfile. On the logfile is reported when streams are created or 
removed, when tuples are inserted and when the baskets are flushed. By default 
in UNIX systems is :code:`/var/log/iot/iotapi.log`, while on Windows is the 
:code:`iotapi.log` on the directory where the :code:`main.py` script was called.
 
-
 Web API Listening and Behavior
 ------------------------------
 
 Customize the Web API server parameters and behavior.
 
-**-sh  - -shost=**
+**-sh  - -shost**
 
 Listening host of the application (IOT) server. By default is on 
:code:`0.0.0.0`.
 
-**-sp  - -sport=**
+**-sp  - -sport**
 
 Listening port of the application (IOT) server. By default is on port 
:code:`8002`.
 
-**-pi  - -polling=**
+**-pi  - -polling**
 
-Set the polling interval to MonetDB database for updates. By default is 
:code:`60` seconds.
+Set the polling interval in seconds to MonetDB database for streams updates. 
By default is :code:`60` seconds.
 
 Database Connection
 -------------------
@@ -57,18 +56,25 @@ Credentials for the MAPI connection to M
 
 .. note:: The user's password will be prompted during the initialization of 
the server.
 
-**-d  - -host=**
+**-d  - -host**
 
 Listening host of MonetDB database. By default is on :code:`127.0.0.1`.
 
-**-d  - -port=**
+**-d  - -port**
 
 Listening port of MonetDB database. By default is on port :code:`50000`.
 
-**-u  - -user=**
+**-u  - -user**
 
 Name of the user to authenticate. By default is user :code:`monetdb`.
 
-**-d  - -database=**
+**-d  - -database**
 
 Name of database to use. By default is :code:`iotdb` database.
+
+Help
+----
+
+**- -help**
+
+Display arguments help.
diff --git a/clients/iotapi/requirements.txt b/clients/iotapi/requirements.txt
--- a/clients/iotapi/requirements.txt
+++ b/clients/iotapi/requirements.txt
@@ -1,4 +1,5 @@
 git+https://github.com/dpallot/simple-websocket-server.git
+IPy==0.83
 jsonschema==2.5.1
 pymonetdb==0.1.1
 python-dateutil==2.5.3
diff --git a/clients/iotapi/src/Settings/filesystem.py 
b/clients/iotapi/src/Settings/filesystem.py
--- a/clients/iotapi/src/Settings/filesystem.py
+++ b/clients/iotapi/src/Settings/filesystem.py
@@ -1,22 +1,19 @@
+import os
 import sys
-import os
 
 from iotlogger import add_log
 
 Baskets_Location = None
 
+if sys.platform in ("linux", "linux2", "darwin"):
+    DEFAULT_FILESYSTEM = '/etc/iotapi'
+elif sys.platform == "win32":
+    DEFAULT_FILESYSTEM = os.path.join(os.path.dirname(__file__), os.pardir)
+
 
 def init_file_system(new_location=None):
     global Baskets_Location
 
-    if new_location is None:
-        if sys.platform in ("linux", "linux2", "darwin"):
-            new_location = '/etc/iotapi'
-        elif sys.platform == "win32":
-            new_location = os.path.join(os.path.dirname(__file__), os.pardir)
-    else:
-        new_location = new_location
-
     try:
         Baskets_Location = os.path.join(new_location, "baskets")
         if not os.path.exists(Baskets_Location):
diff --git a/clients/iotapi/src/Settings/iotlogger.py 
b/clients/iotapi/src/Settings/iotlogger.py
--- a/clients/iotapi/src/Settings/iotlogger.py
+++ b/clients/iotapi/src/Settings/iotlogger.py
@@ -1,35 +1,27 @@
 import logging
+import os
 import sys
 
-import os
-
 Logger = logging.getLogger("IOTAPILog")
 
+if sys.platform in ("linux", "linux2", "darwin"):
+    DEFAULT_LOGGING = '/var/log/iot/iotapi.log'
+elif sys.platform == "win32":
+    DEFAULT_LOGGING = os.path.join(os.path.dirname(__file__), os.pardir, 
'iotapi.log')
 
-def init_logging(new_location):
-    global Logger
 
-    if new_location is None:
-        if sys.platform in ("linux", "linux2", "darwin"):
-            logging_location = '/var/log/iot/iotapi.log'
-        elif sys.platform == "win32":
-            logging_location = os.path.join(os.path.dirname(__file__), 
os.pardir, 'iotapi.log')
-    else:
-        logging_location = new_location
-
+def init_logging(logging_location):
     try:
-        logger = logging.getLogger("IOTAPILog")
-        logger.setLevel(logging.DEBUG)
+        Logger.setLevel(logging.DEBUG)
         formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s 
- %(message)s')
-
         logging_path = os.path.dirname(logging_location)
         if not os.path.exists(logging_path):
             os.makedirs(logging_path)
         log_handler = logging.FileHandler(logging_location, mode='a+')
         log_handler.setFormatter(formatter)
-        logger.addHandler(log_handler)
+        Logger.addHandler(log_handler)
     except (Exception, OSError) as ex:
-        print >> sys.stdout, ex
+        print ex
         sys.exit(1)
 
 
diff --git a/clients/iotapi/src/Streams/streams.py 
b/clients/iotapi/src/Streams/streams.py
--- a/clients/iotapi/src/Streams/streams.py
+++ b/clients/iotapi/src/Streams/streams.py
@@ -50,6 +50,8 @@ class IOTStream(object):
         self._base_path = os.path.join(get_baskets_base_location(), 
schema_name, stream_name)
         self._baskets_lock = RWLock()
         self._baskets = {}  # dictionary of basket_number -> total_tuples
+        if not os.path.exists(self._base_path):
+            os.makedirs(self._base_path)
         for name in os.listdir(self._base_path):
             self.append_basket(name)
         self._observer = Observer()
diff --git a/clients/iotapi/src/WebSockets/websockets.py 
b/clients/iotapi/src/WebSockets/websockets.py
--- a/clients/iotapi/src/WebSockets/websockets.py
+++ b/clients/iotapi/src/WebSockets/websockets.py
@@ -137,6 +137,7 @@ def init_websockets(host, port):
     global WebSocketServer
     try:
         WebSocketServer = SimpleWebSocketServer(host, port, IOTAPI)
+        print ''.join(['Starting Websockets server: ws://', host, ':', 
str(port), "/"])
         WebSocketServer.serveforever()
     except (BaseException, OSError) as ex:
         print ex
diff --git a/clients/iotapi/src/main.py b/clients/iotapi/src/main.py
--- a/clients/iotapi/src/main.py
+++ b/clients/iotapi/src/main.py
@@ -1,27 +1,29 @@
-import getopt
+import argparse
 import getpass
+import os
 import signal
 import sys
 
+from IPy import IP
 from multiprocessing import Process
 from threading import Thread
-from Settings.filesystem import init_file_system
-from Settings.iotlogger import init_logging, add_log
-from Settings.mapiconnection import init_monetdb_connection, 
close_monetdb_connection
+from Settings.filesystem import init_file_system, DEFAULT_FILESYSTEM
+from Settings.iotlogger import init_logging, add_log, DEFAULT_LOGGING
+from Settings.mapiconnection import init_monetdb_connection
 from Streams.streampolling import init_stream_polling_thread
-from WebSockets.websockets import init_websockets, terminate_websockets
+from WebSockets.websockets import init_websockets
 
 subprocess = None
 
 
 def signal_handler(signal, frame):
     subprocess.terminate()
+    add_log(20, 'Stopped IOT API Server')
 
 
-def start_process(polling_interval, filesystem_location, logging_location, 
sockets_host, sockets_port,
-                  connection_hostname, con_port, con_user, con_password, 
con_database):
+def start_process(polling_interval, filesystem_location, sockets_host, 
sockets_port, connection_hostname, con_port,
+                  con_user, con_password, con_database):
     # WARNING The initiation order must be this!!!
-    init_logging(logging_location)  # init logging context
     init_file_system(filesystem_location)  # init filesystem
     # init mapi connection
     init_monetdb_connection(connection_hostname, con_port, con_user, 
con_password, con_database)
@@ -32,60 +34,69 @@ def start_process(polling_interval, file
     add_log(20, 'Started IOT API Server')
     thread1.join()
 
-    terminate_websockets()
-    close_monetdb_connection()
-    add_log(20, 'Stopped IOT API Server')
 
+def check_path(value):
+    if not os.path.isabs(value):
+        raise argparse.ArgumentTypeError("%s is an invalid path" % value)
+    return value
 
-def main(argv):
+
+def check_positive_int(value):
+    ivalue = int(value)
+    if ivalue <= 0:
+        raise argparse.ArgumentTypeError("%s is an invalid positive int value" 
% value)
+    return ivalue
+
+
+def check_ipv4_address(value):
+    try:
+        IP(value)
+    except:
+        raise argparse.ArgumentTypeError("%s is an invalid IPv4 address" % 
value)
+    return value
+
+
+def main():
     global subprocess
 
+    parser = argparse.ArgumentParser(description='IOT Web API for MonetDB', 
epilog="There might exist bugs!",
+                                     add_help=False)
+    parser.add_argument('-f', '--filesystem', type=check_path, nargs='?', 
default=DEFAULT_FILESYSTEM,
+                        help='Baskets\' location directory (default: %s)' % 
DEFAULT_FILESYSTEM)
+    parser.add_argument('-l', '--log', type=check_path, nargs='?', 
default=DEFAULT_LOGGING,
+                        help='Logging file location (default: %s)' % 
DEFAULT_LOGGING)
+    parser.add_argument('-pi', '--polling', type=check_positive_int, 
nargs='?', default=60,
+                        help='Polling interval in seconds to the database for 
streams updates (default: 60)')
+    parser.add_argument('-sh', '--shost', type=check_ipv4_address, nargs='?', 
default='0.0.0.0',
+                        help='Web API server host (default: 0.0.0.0)')
+    parser.add_argument('-sp', '--sport', type=check_positive_int, nargs='?', 
default=8002,
+                        help='Web API server port (default: 8002)')
+    parser.add_argument('-h', '--host', nargs='?', default='127.0.0.1',
+                        help='MonetDB database host (default: 127.0.0.1)')
+    parser.add_argument('-p', '--port', type=check_positive_int, nargs='?', 
default=50000,
+                        help='Database listening port (default: 50000)')
+    parser.add_argument('-d', '--database', nargs='?', default='iotdb', 
help='Database name (default: iotdb)')
+    parser.add_argument('-u', '--user', nargs='?', default='monetdb', 
help='Database user (default: monetdb)')
+    parser.add_argument('--help', action='store_true', help='Display this 
help')
+
     try:
-        opts, args = getopt.getopt(argv[1:], 'pi:f:l:sh:sp:h:p:d:u', 
['polling=', 'filesystem=', 'log=', 'shost=',
-                                                                      
'sport=', 'host=', 'port=', 'database=', 'user='])
-    except getopt.GetoptError:
-        print "Error while parsing the arguments!"
+        args = vars(parser.parse_args())
+    except BaseException as ex:
+        print ex
         sys.exit(1)
 
-    polling_interval = 60
-    filesystem_location = None
-    logging_location = None
-    sockets_host = '0.0.0.0'
-    sockets_port = 8002
+    if args['help']:
+        parser.print_help()
+        sys.exit(0)
 
-    con_hostname = '127.0.0.1'
-    con_port = 50000
-    con_user = 'monetdb'
-    con_database = 'iotdb'
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to