Exemplo n.º 1
0
 def writeFileWithOpen(self, data, path='None', mode='w+'):
     """ This method opens a file in write mode and writes
         the specified data into it.
     """
     try:
         with open(path, mode) as writer:
             writer.file.write(data)
     except (ValueError, OSError, IOError) as e:
         ExceptionHandler(e, "writeFileWithOpen")
Exemplo n.º 2
0
 def writeToOpenFile(self, data):
     """ This method writes data to the actually
         handled file.
     """
     if self.file != None:
         try:
             self.file.write(data)
         except (ValueError, OSError, IOError) as e:
             ExceptionHandler(e, "writeToOpenFile")
Exemplo n.º 3
0
 def closeFile(self):
     """ This method closes the actually
         handled file.
     """
     if self.file != None:
         try:
             self.file.close()
         except (ValueError, OSError, IOError) as e:
             ExceptionHandler(e, "closeFile")
Exemplo n.º 4
0
 def openFile(self, path=None, mode="w+"):
     """ This method opens a file at the
         specified path in write mode.
     """
     if self.file == None and path != None:
         try:
             self.file = open(path, mode)
         except (ValueError, OSError, IOError) as e:
             ExceptionHandler(e, "openFile")
Exemplo n.º 5
0
def feed_the_ai():
    """
    Ask for user input via the ExceptionHandler method.
    """

    # AI requests tea, but has a sense of proper taste.
    user_input = ExceptionHandler.query_options(
        query="\nPlease feed me 'green tea' or 'herbal tea': ",
        error_message="\nYuck! That's not what I wanted!",
        options=['green tea', 'herbal tea'],
        ignore_case=True)

    print(f"\n-AI enjoys a cup of {user_input}-", "\nThanks, human!")

    # AI requests integer, any integer.
    ExceptionHandler.query_int(
        query="\nPlease feed me any integer: ",
        error_message="\nYuck! That's not what I wanted!")

    print("\n-AI plays with the integer happily-", "\nThanks, human!")
Exemplo n.º 6
0
 def readFileWithOpen(self, path):
     """ This method opens a file in read mode and returns
         the contained data.
     """
     content = None
     try:
         with open(path, "r") as reader:
             if reader.mode == 'r':
                 content = reader.read()
     except (ValueError, OSError, IOError) as e:
         ExceptionHandler(e, "readFileWithOpen")
     return content
 def readFromSerialWithDelay(self, secondsDelay=None):
     """This method returns read bytes from the connection with a
         specified delay between read attempts.
     """
     data = None
     if self.connection.is_open:
         try:
             self.serialSleep(secondsDelay)
             data = self.connection.read(self.getBytesInWaiting())
         except (KeyboardInterrupt, serial.SerialTimeoutException,
                 serial.SerialException) as e:
             ExceptionHandler(e, "readFromSerial")
     return data
 def writeToSerial(self, data):
     """ This method writes the data to the open
         connection and returns the number of written bytes.
     """
     writtenBytes = None
     if self.connection.is_open:
         try:
             writtenBytes = self.connection.write(data)
         except (KeyboardInterrupt, serial.SerialTimeoutException,
                 serial.SerialException) as e:
             ExceptionHandler(e, "writeToSerial")
         finally:
             self.flushWriteBuffer()
     return writtenBytes
 def openSerial(self):
     """This method opens a connection on the current serial instance."""
     attempts = 0
     # Try open 10 times
     while not self.connection.is_open and attempts < 10:
         try:
             self.connection.open()
         except serial.SerialException:
             # Write attempts to console
             sys.stdout.write("Try to open port\n")
             attempts += 1
             try:
                 # Sleep between attempts and raise exception after 10 times
                 time.sleep(1)
                 if attempts == 10:
                     raise serial.SerialException
             # Catch exceptions
             except (KeyboardInterrupt, serial.SerialException) as e:
                 ExceptionHandler(e, "openSerial")
         except (ValueError, KeyboardInterrupt) as e:
             ExceptionHandler(e, "openSerial")
     # Reset buffers when connection is open
     self.resetConnectionBuffers()
 def readFromSerialUntilExpectWithDelay(self,
                                        expectedEnd='LF',
                                        expectedBytes=None,
                                        secondsDelay=0.1):
     """ This method returns read bytes. The reading stops at an expected point specified by
         'expectedEnd' or 'expectedBytes'. A delay between read attempts can be specified.
     """
     data = None
     if self.connection.is_open:
         try:
             self.serialSleep(secondsDelay)
             data = self.connection.read_until(expectedEnd, expectedBytes)
         except (KeyboardInterrupt, serial.SerialTimeoutException,
                 serial.SerialException) as e:
             ExceptionHandler(e, "readFromSerialUntilExpect")
     return data
Exemplo n.º 11
0
def exctrace(module,type,message,logger,exc_info,exc_trace,time=datetime.now()):
    #ExceptionHandler.logException('PSG','0','PSG:level 0 exception',datetime.now())
    ExceptionHandler.logException(module,type,message,time)
    logger.log(40,exc_info)
    logger.log(20,exc_trace)