Преглед на файлове

Merge branch 'master-upstream' of git://github.com/MaStr/cjdns into crashey

Caleb James DeLisle преди 7 години
родител
ревизия
05a29812e7
променени са 2 файла, в които са добавени 40 реда и са изтрити 32 реда
  1. 30 22
      contrib/python/cjdnsadmin/cjdnsadmin.py
  2. 10 10
      contrib/python/dynamicEndpoints.py

+ 30 - 22
contrib/python/cjdnsadmin/cjdnsadmin.py

@@ -173,20 +173,31 @@ def _getMessage(session, txid):
                 print "message with no txid: " + str(next)
 
 
-def _functionFabric(func_name, argList, oargList, password):
+def _functionFabric(func_name, argList, oargs, password):
     """Function fabric for Session class"""
 
     def functionHandler(self, *args, **kwargs):
         call_args = {}
 
-        for (key, value) in oargList.items():
-            call_args[key] = value
-
-        for i, arg in enumerate(argList):
-            if (i < len(args)):
-                call_args[arg] = args[i]
+        pos = 0
+        for value in args:
+            if (pos < len(argList)):
+                call_args[argList[pos]] = value
+                pos += 1
 
         for (key, value) in kwargs.items():
+            if not key in oargs:
+                # probably a positional argument, given a keyword name
+                # that happens in python
+                if pos < len(argList) and argList[pos] == key:
+                    call_args[argList[pos]] = value
+                    pos += 1
+                    continue
+                else:
+                    print("warning: not an argument to this function",func_name,key)
+                    print(oargs)
+            # TODO: check oargs[key] type matches value
+            # warn, if doesn't
             call_args[key] = value
 
         return _callFunc(self, func_name, password, call_args)
@@ -231,25 +242,22 @@ def connect(ipAddr, port, password):
     for (i, func) in availableFunctions.items():
         items = func.items()
 
-        # grab all the required args first
-        # append all the optional args
-        rargList = [arg for arg,atts in items if atts['required']]
-        argList = rargList + [arg for arg,atts in items if not atts['required']]
-
-        # for each optional arg setup a default value with
-        # a type which will be ignored by the core.
-        oargList = {}
+        # required args
+        argList = []
+        # optional args
+        oargs = {}
+        
         for (arg,atts) in items:
-            if not atts['required']:
-                oargList[arg] = (
-                    "''" if (func[arg]['type'] == 'Int')
-                    else "0")
+            if atts['required']:
+                argList.append(arg)
+            else:
+                oargs[arg] = atts['type']
 
         setattr(Session, i, _functionFabric(
-            i, argList, oargList, password))
+            i, argList, oargs, password))
 
-        funcArgs[i] = rargList
-        funcOargs[i] = oargList
+        funcArgs[i] = argList
+        funcOargs[i] = oargs
 
     session = Session(sock)
 

+ 10 - 10
contrib/python/dynamicEndpoints.py

@@ -114,11 +114,6 @@ class DynamicEndpointWatcher(object):
         # unresponsive.
         self.unresponsive = dict()
 
-        # Holds a cjdns log message subscription to messages about unresponsive
-        # nodes.
-        self.sub = self.cjdns.AdminLog_subscribe(MESSAGE_LINE, MESSAGE_FILE,
-            'DEBUG')
-
         # Add nodes from the given ConfigParser parser.
         for section in configuration.sections():
             # Each section is named with a node key, and contains a
@@ -130,16 +125,21 @@ class DynamicEndpointWatcher(object):
             # Add the node
             self.addNode(peerHostname, peerPort, peerPassword, section)
 
+        # Add all the nodes we're supposed to watch.
+        for node in self.nodes.values():
+            self.lookup(node)
+        logging.info("{} peers added!".format(len(self.nodes)))
+        # Holds a cjdns log message subscription to messages about unresponsive
+        # nodes.
+        self.sub = self.cjdns.AdminLog_subscribe(MESSAGE_LINE, MESSAGE_FILE,
+            'DEBUG')
+
         if self.sub['error'] == 'none':
             # We successfully subscribed to messages.
             
             # When we die, try to unsubscribe
             atexit.register(self.stop)
             
-            # Add all the nodes we're supposed to watch.
-            for node in self.nodes.values():
-                self.lookup(node)
-            logging.info("{} peers added!".format(len(self.nodes)))
         else:
             logging.error(self.sub)
 
@@ -346,7 +346,7 @@ def main(argv):
         # Announce we dropped privs
         logging.info("Dropped privileges: running as {}:{}".format(
             pwd.getpwuid(os.getuid())[0], grp.getgrgid(os.getgid())[0]))
-    except OSError:
+    except (OSError,KeyError):
         # Complain we couldn't drop privs right
         logging.warning("Could not drop privileges: running as {}:{}".format(
             pwd.getpwuid(os.getuid())[0], grp.getgrgid(os.getgid())[0]))