Norman James | 8b2b722 | 2015-10-08 07:01:38 -0500 | [diff] [blame^] | 1 | """This module holds all objects shared by all other modules in tftpy.""" |
| 2 | |
| 3 | import logging |
| 4 | |
| 5 | LOG_LEVEL = logging.NOTSET |
| 6 | MIN_BLKSIZE = 8 |
| 7 | DEF_BLKSIZE = 512 |
| 8 | MAX_BLKSIZE = 65536 |
| 9 | SOCK_TIMEOUT = 5 |
| 10 | MAX_DUPS = 20 |
| 11 | TIMEOUT_RETRIES = 5 |
| 12 | DEF_TFTP_PORT = 69 |
| 13 | |
| 14 | # A hook for deliberately introducing delay in testing. |
| 15 | DELAY_BLOCK = 0 |
| 16 | |
| 17 | # Initialize the logger. |
| 18 | logging.basicConfig() |
| 19 | # The logger used by this library. Feel free to clobber it with your own, if you like, as |
| 20 | # long as it conforms to Python's logging. |
| 21 | log = logging.getLogger('tftpy') |
| 22 | |
| 23 | def tftpassert(condition, msg): |
| 24 | """This function is a simple utility that will check the condition |
| 25 | passed for a false state. If it finds one, it throws a TftpException |
| 26 | with the message passed. This just makes the code throughout cleaner |
| 27 | by refactoring.""" |
| 28 | if not condition: |
| 29 | raise TftpException, msg |
| 30 | |
| 31 | def setLogLevel(level): |
| 32 | """This function is a utility function for setting the internal log level. |
| 33 | The log level defaults to logging.NOTSET, so unwanted output to stdout is |
| 34 | not created.""" |
| 35 | global log |
| 36 | log.setLevel(level) |
| 37 | |
| 38 | class TftpErrors(object): |
| 39 | """This class is a convenience for defining the common tftp error codes, |
| 40 | and making them more readable in the code.""" |
| 41 | NotDefined = 0 |
| 42 | FileNotFound = 1 |
| 43 | AccessViolation = 2 |
| 44 | DiskFull = 3 |
| 45 | IllegalTftpOp = 4 |
| 46 | UnknownTID = 5 |
| 47 | FileAlreadyExists = 6 |
| 48 | NoSuchUser = 7 |
| 49 | FailedNegotiation = 8 |
| 50 | |
| 51 | class TftpException(Exception): |
| 52 | """This class is the parent class of all exceptions regarding the handling |
| 53 | of the TFTP protocol.""" |
| 54 | pass |
| 55 | |
| 56 | class TftpTimeout(TftpException): |
| 57 | """This class represents a timeout error waiting for a response from the |
| 58 | other end.""" |
| 59 | pass |