# pytest framework ---------------------------------------------
import requests
from aquests.lib import siesta
from aquests.lib.pmaster import Puppet, processutil
import subprocess
import time
import sys
import os

class launch:
    def __init__ (self, script, port = 5000, ssl = False, silent = True):
        self.__script = script
        endpoint = "http{}://127.0.0.1".format (ssl and "s" or "")
        if port:
            endpoint += ":{}".format (port)
        self.__endpoint = endpoint
        self.__silent = silent
        self.__p = Puppet (communicate = False)        
        self.__requests = Requests (endpoint)
        self.__api = siesta.API (endpoint)
        self.__start ()
    
    def __enter__ (self):
        return self
        
    def __exit__ (self, type, value, tb):
        self._close ()

    def __getattr__ (self, attr):
        if attr in ("get", "post", "put", "patch", "delete", "head", "options"):
            return getattr (self.__requests, attr)
        else:
            return getattr (self.__api, attr)
        
    def __start (self):
        if self.__silent:
            self.__p.start ([sys.executable, self.__script, "-d"])
            self.__wait_until ("running")
        else:
            self.__p.start ([sys.executable, self.__script, "-v"])
            time.sleep (3)
                    
    def __wait_until (self, status, timeout = 10):
        for i in range (timeout):
            command = "{} {} status".format (sys.executable, self.__script)
            res = subprocess.run (command, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)    
            out, err = res.stdout.decode ("utf8"), res.stderr.decode ("utf8")
            if out.find (status) != -1:
                break
            time.sleep (1) 
    
    def _close (self):
        if self.__silent:
            self.__p.start ([sys.executable, self.__script, "stop"])
            self.__wait_until ("stopped")
        else:    
            self.__p.kill ()
            time.sleep (3)
                    

class Requests:
    def __init__ (self, endpoint):
        self.endpoint = endpoint
        self.s = requests.Session ()
    
    def resolve (self, url):
        if url.startswith ("http://") or url.startswith ("https://"):
            return url
        else:
            return self.endpoint + url 
        
    def get (self, url, *args, **karg):
        return self.s.get (self.resolve (url), *args, **karg)
        
    def post (self, url, *args, **karg):
        return self.s.post (self.resolve (url), *args, **karg)
    
    def put (self, url, *args, **karg):
        return self.s.put (self.resolve (url), *args, **karg)
    
    def patch (self, url, *args, **karg):
        return self.s.patch (self.resolve (url), *args, **karg)
    
    def delete (self, url, *args, **karg):
        return self.s.delete (self.resolve (url), *args, **karg)
    
    def head (self, url, *args, **karg):
        return self.s.head (self.resolve (url), *args, **karg)
                
    def options (self, url, *args, **karg):
        return self.s.options (self.resolve (url), *args, **karg)
          
@@ -522,16 +522,49 @@
  silent%0A
+        self.__servicing = False%0A
         
@@ -1127,32 +1127,135 @@
  self.__silent:%0A
+            if self.__is_running ():%0A                self.__servicing = True%0A            else:    %0A    
             self
@@ -1301,24 +1301,28 @@
 ipt, %22-d%22%5D)%0A
+    
             
@@ -1455,32 +1455,372 @@
 e.sleep (3)%0A    
+%0A    def __is_running (self):%0A        command = %22%7B%7D %7B%7D status%22.format (sys.executable, self.__script)%0A        res = subprocess.run (command, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)    %0A        out, err = res.stdout.decode (%22utf8%22), res.stderr.decode (%22utf8%22)%0A        return out.find (%22running%22) != -1%0A             
                 
@@ -2232,13 +2232,19 @@
     
-b
 re
-ak
+turn True
 %0A   
@@ -2268,16 +2268,41 @@
 ep (1) %0A
+        return False    %0A
     %0A   
@@ -2337,32 +2337,57 @@
 if self.__silent
+ and not self.__servicing
 :%0A            se

fix launch: check daemon weather it is running