Commits

Gabriel AHTUNE  committed f0b35c7

write main function in client.py and server.py
add __all__ in several places

  • Participants
  • Parent commits f595f92

Comments (0)

Files changed (7)

File secsgem/client.py

 '''
 This Module Implement a SECS client (ie a Host)
 
-Created on 3 juin 2012
+Created on 3 june 2012
 
 '''
 
+__all__ = ['Client']
 __author__ = 'Gabriel AHTUNE'
 __version__ = '0.9'
+__date__ = "4 November 2012"
 
-
+import argparse
 import logging
-import argparse
+import re
 import socket
 import time
 from threading import Timer
+from .abc import SECSGEMEntity
 from .structures import SGMessage
 from .transports import HSMS, STYPE_TABLE
-from .abc import SECSGEMEntity
+import secsgem.secs2 as secs2
+import secsgem.sml as sml
 
 
 class Client(SECSGEMEntity):
         else:
             self.logger.info("Message not sent: {}".format(str(msg)))
 
-if __name__ == '__main__':
-    PARSER = argparse.ArgumentParser(
+
+def main(args):
+    parser = argparse.ArgumentParser(
         description='create a quick SECS Host',
         epilog='Tested on Linux, python 3.2'
     )
-    PARSER.add_argument('--host', type=str, default='127.0.0.1',
-                        help='specify the host to connect to')
-    PARSER.add_argument('--port', type=int, default=5000,
-                        help='specify the server port number')
-    PARSER.add_argument('--heartbeat', type=int, default=0,
-                        help='period of heartbeat in second')
-    ARGS = PARSER.parse_args()
+    parser.add_argument(
+        '--host',
+        type=str,
+        default='127.0.0.1',
+        help='specify the host to connect to'
+    )
+    parser.add_argument(
+        '--port',
+        type=int,
+        default=5000,
+        help='specify the server port number'
+    )
+    parser.add_argument(
+        '--heartbeat',
+        type=int,
+        default=0,
+        help='period of heartbeat in second'
+    )
+    args = parser.parse_args(args)
 
-    CLIENT = Client(ARGS.host, ARGS.port, heartbeat=ARGS.heartbeat)
+    client = Client(args.host, args.port, heartbeat=args.heartbeat)
 
     def print_(string):
         """print for pylint"""
         print(string)
 
-    CLIENT.addlistener(print_)
-    CLIENT.start()
+    client.addlistener(print_)
+    client.start()
     try:
         while True:
-            input('$> ')
+            msg = input('$> ')
+            if re.match(r"S\d+F\d+", msg):
+                print(sml.sml2list(sml.get_msg(msg)))
+                client.send(
+                    SGMessage(
+                        sml.get_stream(msg),
+                        sml.get_function(msg),
+                        sml.get_w_flag(msg),
+                        sml.sml2list(sml.get_msg(msg)),
+                        0
+                    )
+                )
     except KeyboardInterrupt:
-        CLIENT.close()
+        client.close()
         print('\nCtrl+C pressed -- Quitting')
+    return client
+
+
+if __name__ == '__main__':
+    import sys
+    client = main(sys.argv[1:])

File secsgem/equipmentbase.py

 
 __author__ = 'Gabriel AHTUNE'
 __version__ = '2.0'
+__date__ = "4 November 2012"
 
 LOGGER = logging.getLogger("EquipmentBase")
 

File secsgem/secs2.py

 
 __author__ = 'Gabriel AHTUNE'
 __version__ = '1.0'
+__date__ = "4 November 2012"
 
 import logging
 import struct
     'BOOLEAN':'>B'
 }
 
+
 def dumps(smllist):
     """
     This function converts SML object to SECS-II string
 
         return result
 
+
 def loads(bytemsg):
     """
     This function converts SECS-II binary string directly to
                     stack[-1].append(top)
     return stack[0]
 
+
 def _encode_element(elements, element_kind):
     """
     This function encodes in number a value according to its kind
             )
         )
 
+
 def _decode_item(item, element_kind, nbelement):
     """
     This function decodes a value according to its kind

File secsgem/server.py

 '''
 This module implements a SECS SERVER (ie an equipment)
 
-Created on 3 juin 2012
+Created on 3 june 2012
 
 '''
 
+__all__ = ['Server']
 __author__ = 'Gabriel AHTUNE'
 __version__ = '0.9'
+__date__ = "4 November 2012"
 
+import argparse
 import logging
+import re
 import socket
-import argparse
 import time
+from .abc import SECSGEMEntity
+from .structures import SGMessage
 from .transports import HSMS, STYPE_TABLE
-from .structures import SGMessage
-from .abc import SECSGEMEntity
+import secsgem.secs2 as secs2
+import secsgem.sml as sml
 
 
 class Server(SECSGEMEntity):
             self.closed = True
 
 
-if __name__ == "__main__":
-    PARSER = argparse.ArgumentParser(
-            description='Create a quick SECS Equipment connection',
-            epilog='Tested on Linux, python 3.2')
-    PARSER.add_argument('--host', type=str, default='',
-                        help='Specify the host to listen')
-    PARSER.add_argument('--port', type=int, default=5000,
-                        help='Specify the SERVER port number')
-    ARGS = PARSER.parse_args()
+def main(args):
+    parser = argparse.ArgumentParser(
+                description='Create a quick SECS Equipment connection',
+                epilog='Tested on Linux, python 3.2'
+    )
+    parser.add_argument(
+            '--host', 
+            type=str, 
+            default='',
+            help='Specify the host to listen'
+    )
+    parser.add_argument(
+            '--port', 
+            type=int,
+            default=5000,
+            help='Specify the SERVER port number'
+    )
+    args = parser.parse_args(args)
 
-    SERVER = Server(ARGS.host, ARGS.port)
+    server = Server(ARGS.host, ARGS.port)
 
     def print_(string):
         print(string)
-    SERVER.addlistener(print_)
-    SERVER.start()
+
+    server.addlistener(print_)
+    server.start()
 
     try:
         while True:
             msg = input('$> ')
-#            SERVER.send()
+            if re.match(r"S\d+F\d+", msg):
+                print(sml.sml2list(sml.get_msg(msg)))
+                server.send(
+                    SGMessage(
+                        sml.get_stream(msg),
+                        sml.get_function(msg),
+                        sml.get_w_flag(msg),
+                        sml.sml2list(sml.get_msg(msg)),
+                        0
+                    )
+                )
     except KeyboardInterrupt:
-        SERVER.close()
+        server.close()
         print('\nCtrl+C pressed -- Quitting')
+    return server
+
+
+if __name__ == "__main__":
+    import sys
+    server = main(sys.argv[1:])

File secsgem/sml.py

 
 __author__ = 'Gabriel AHTUNE'
 __version__ = '2.0'
+__date__ = "4 November 2012"
 
 
 import re

File secsgem/structures.py

 
 __author__ = 'Gabriel AHTUNE'
 __version__ = '1.0a'
+__date__ = "4 November 2012"
 
 from collections import namedtuple
 import sys

File secsgem/transports.py

 
 __author__ = "Gabriel AHTUNE"
 __version__ = '1.0'
+__date__ = "4 November 2012"
 
 import logging
 import struct
             Text = text
         )
 
-
     def heartbeats(self):
         """
         This method build the Linktest request ''structures.HSMSFrame''