blob: d09d8bd55406377b21fc492605697eec0624667b [file] [log] [blame]
Norman James8b2b7222015-10-08 07:01:38 -05001"""This module holds all objects shared by all other modules in tftpy."""
2
3import logging
4
5LOG_LEVEL = logging.NOTSET
6MIN_BLKSIZE = 8
7DEF_BLKSIZE = 512
8MAX_BLKSIZE = 65536
9SOCK_TIMEOUT = 5
10MAX_DUPS = 20
11TIMEOUT_RETRIES = 5
12DEF_TFTP_PORT = 69
13
14# A hook for deliberately introducing delay in testing.
15DELAY_BLOCK = 0
16
17# Initialize the logger.
18logging.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.
21log = logging.getLogger('tftpy')
22
23def 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
31def 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
38class 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
51class TftpException(Exception):
52 """This class is the parent class of all exceptions regarding the handling
53 of the TFTP protocol."""
54 pass
55
56class TftpTimeout(TftpException):
57 """This class represents a timeout error waiting for a response from the
58 other end."""
59 pass