[Git][NTPsec/ntpsec][master] 2 commits: Reorganization and cleanup of pylib.

Eric S. Raymond gitlab at mg.gitlab.com
Tue Oct 18 09:03:09 UTC 2016


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
cec49098 by Eric S. Raymond at 2016-10-18T04:55:25-04:00
Reorganization and cleanup of pylib.

- - - - -
dd165e1c by Eric S. Raymond at 2016-10-18T04:57:03-04:00
Remove obsolete debugging stuff.

- - - - -


2 changed files:

- pylib/__init__.py
- pylib/packet.py


Changes:

=====================================
pylib/__init__.py
=====================================
--- a/pylib/__init__.py
+++ b/pylib/__init__.py
@@ -4,7 +4,5 @@
 # Preserve this property!
 from __future__ import absolute_import  # Ensure Python2 behaves like Python 3
 
-from .ntp_control import *
-
 api_major_version = 1   # bumped on incompatible changes
 api_minor_version = 0   # bumped on compatible changes


=====================================
pylib/packet.py
=====================================
--- a/pylib/packet.py
+++ b/pylib/packet.py
@@ -195,6 +195,10 @@ def dump_hex_printable(xdata):
         sys.stdout.write("\n")
         llen -= rowlen
 
+class Mode6Exception(BaseException):
+    def __init__(self, message):
+        self.message = message
+
 class Mode6Session:
     "A session to a host"
 
@@ -355,7 +359,7 @@ class Mode6Session:
             bail += 1
             if bail >= (2*MAXFRAGS):
                 warn("too many packets in response; bailing out\n")
-                return SERR_TOOMUCH
+                raise Mode6Exception(SERR_TOOMUCH)
 
             if len(fragments) == 0:
                 tvo = self.primary_timeout / 1000
@@ -366,14 +370,14 @@ class Mode6Session:
                 (rd, _, _) = select.select([self.sock], [], [], tvo)
             except select.error as msg:
                 warn("select failed: %s\n" % msg[1])
-                return SERR_SELECT
+                raise Mode6Exception(SERR_SELECT)
 
             if not rd:
                 # Timed out.  Return what we have
                 if len(fragments) == 0:
                     if timeo:
                         warn("%s: timed out, nothing received\n" % self.name)
-                        return SERR_TIMEOUT
+                        raise Mode6Exception(SERR_TIMEOUT)
                 if timeo:
                     warn("%s: timed out with incomplete data\n" % self.name)
                     if self.debug:
@@ -382,7 +386,7 @@ class Mode6Session:
                             sys.stderr.write("%d: %s" % (i+1, frag.stats()))
                         sys.stderr.write("last fragment %sreceived\n",
                                     ("not " "", )[seenlastfrag])
-                    return SERR_INCOMPLETE
+                    raise Mode6Exception(SERR_INCOMPLETE)
 
             rawdata = self.sock.recv(4096)
             if self.debug:
@@ -392,7 +396,7 @@ class Mode6Session:
                 rpkt.analyze(rawdata)
             except struct.error as reason:
                 warn("packet analysis failed: %s\n" % reason)
-                return SERR_UNSPEC
+                raise Mode6Exception(SERR_UNSPEC)
 
             if rpkt.version() > NTP_VERSION or rpkt.version() < NTP_OLDVERSION:
                 if self.debug:
@@ -491,36 +495,35 @@ class Mode6Session:
                         dump_hex_printable(self.response)
                     return None
 
-    def doquery(self, opcode, associd=0, qdata="", auth=False, quiet=False):
+    def doquery(self, opcode, associd=0, qdata="", auth=False):
         "send a request and save the response"
         if not self.havehost():
-            return SERR_NOHOST
-        done = False
+            raise Mode6Exception(SERR_NOHOST)
+        retry = True
         while True:
             # Ship the request
             res = self.sendrequest(opcode, associd, qdata, auth)
             if res is not None:
                 return res
             # Get the response.
-            res = self.getresponse(opcode, associd, done)
-            if res:
-                if not quiet:
-                    if isinstance(res, int):
-                        sys.stderr.write("***Packet error %d" % res)
-                    else:
-                        sys.stderr.write(res.format(associd))
-                if not done and res in (SERR_TIMEOUT,SERR_INCOMPLETE):
-                    done = True
+            try:
+                res = self.getresponse(opcode, associd, not retry)
+            except Mode6Exception as e:
+                if retry and e.message in (SERR_TIMEOUT,SERR_INCOMPLETE):
+                    retry = False
                     continue
+                else:
+                    raise e
             break
-        # Return None on success, otherwise an error string
+        # Return data on success
         return res
 
     def readstat(self, associd=0):
         "Read peer status."
-        self.doquery(opcode=CTL_OP_READSTAT, associd=associd, quiet=True)
-        if self.response.startswith("*"):
-            return self.response
+        try:
+            self.doquery(opcode=CTL_OP_READSTAT, associd=associd)
+        except Mode6Exception as e:
+            return e.message
         idlist = []
         if associd == 0:
             for i in range(len(self.response)//4):
@@ -535,29 +538,29 @@ class Mode6Session:
             qdata = ""
         else:
             qdata = ",".join(varlist)
-        self.doquery(opcode=CTL_OP_READVAR, associd=associd, qdata=qdata, quiet=True)
-        if self.response.startswith("*"):
-            return self.response
-        else:
-            response = self.response
-            # Trim trailing NULs from the text
-            while response.endswith("\x00"):
-                response = response[:-1]
-            response = response.rstrip()
-            items = []
-            if response:
-                for pair in response.split(","):
-                    (var, val) = pair.split("=")
-                    var = var.strip()
-                    val = val.strip()
+        try:
+            self.doquery(opcode=CTL_OP_READVAR, associd=associd, qdata=qdata)
+        except Mode6Exception as e:
+            return e.message
+        response = self.response
+        # Trim trailing NULs from the text
+        while response.endswith("\x00"):
+            response = response[:-1]
+        response = response.rstrip()
+        items = []
+        if response:
+            for pair in response.split(","):
+                (var, val) = pair.split("=")
+                var = var.strip()
+                val = val.strip()
+                try:
+                    val = int(val)
+                except ValueError:
                     try:
-                        val = int(val)
+                        val = float(val)
                     except ValueError:
-                        try:
-                            val = float(val)
-                        except ValueError:
-                            if val[0] == '"' and val[-1] == '"':
-                                val = val[1:-1]
-                    items.append((var, val))
-            return dict(items)
+                        if val[0] == '"' and val[-1] == '"':
+                            val = val[1:-1]
+                items.append((var, val))
+        return dict(items)
 # end



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/c70facd79932f8934ded65048f1b203521a17d43...dd165e1c4f4004be7790cbe52b4a46ffa7a5148c
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161018/22371d5b/attachment.html>


More information about the vc mailing list