1 | """This module holds all objects shared by all other modules in tftpy.""" |
---|
2 | |
---|
3 | from __future__ import absolute_import, division, print_function, unicode_literals |
---|
4 | import logging |
---|
5 | from logging.handlers import RotatingFileHandler |
---|
6 | |
---|
7 | LOG_LEVEL = logging.NOTSET |
---|
8 | MIN_BLKSIZE = 8 |
---|
9 | DEF_BLKSIZE = 512 |
---|
10 | MAX_BLKSIZE = 65536 |
---|
11 | SOCK_TIMEOUT = 5 |
---|
12 | MAX_DUPS = 20 |
---|
13 | TIMEOUT_RETRIES = 5 |
---|
14 | DEF_TFTP_PORT = 69 |
---|
15 | |
---|
16 | # A hook for deliberately introducing delay in testing. |
---|
17 | DELAY_BLOCK = 0 |
---|
18 | |
---|
19 | # Initialize the logger. |
---|
20 | logging.basicConfig() |
---|
21 | |
---|
22 | # The logger used by this library. Feel free to clobber it with your own, if |
---|
23 | # you like, as long as it conforms to Python's logging. |
---|
24 | log = logging.getLogger('tftpy') |
---|
25 | |
---|
26 | def create_streamhandler(): |
---|
27 | """add create_streamhandler output logging.DEBUG msg to stdout. |
---|
28 | """ |
---|
29 | console = logging.StreamHandler() |
---|
30 | console.setLevel(logging.INFO) |
---|
31 | formatter = logging.Formatter('%(levelname)-8s %(message)s') |
---|
32 | console.setFormatter(formatter) |
---|
33 | return console |
---|
34 | |
---|
35 | def create_rotatingfilehandler(path, maxbytes=10*1024*1024, count=20): |
---|
36 | """ |
---|
37 | add create_rotatingfilehandler record the logging.DEBUG msg to logfile. you can change the maxsize (10*1024*1024) |
---|
38 | and amount of the logfiles |
---|
39 | """ |
---|
40 | Rthandler = RotatingFileHandler(path, maxbytes, count) |
---|
41 | Rthandler.setLevel(logging.INFO) |
---|
42 | formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s') |
---|
43 | Rthandler.setFormatter(formatter) |
---|
44 | return Rthandler |
---|
45 | |
---|
46 | def addHandler(hdlr): |
---|
47 | """add handler methods |
---|
48 | More details see the page: |
---|
49 | https://docs.python.org/2/library/logging.handlers.html#module-logging.handlers |
---|
50 | """ |
---|
51 | log.addHandler(hdlr) |
---|
52 | |
---|
53 | def tftpassert(condition, msg): |
---|
54 | """This function is a simple utility that will check the condition |
---|
55 | passed for a false state. If it finds one, it throws a TftpException |
---|
56 | with the message passed. This just makes the code throughout cleaner |
---|
57 | by refactoring.""" |
---|
58 | if not condition: |
---|
59 | raise TftpException(msg) |
---|
60 | |
---|
61 | def setLogLevel(level): |
---|
62 | """This function is a utility function for setting the internal log level. |
---|
63 | The log level defaults to logging.NOTSET, so unwanted output to stdout is |
---|
64 | not created.""" |
---|
65 | log.setLevel(level) |
---|
66 | |
---|
67 | class TftpErrors(object): |
---|
68 | """This class is a convenience for defining the common tftp error codes, |
---|
69 | and making them more readable in the code.""" |
---|
70 | NotDefined = 0 |
---|
71 | FileNotFound = 1 |
---|
72 | AccessViolation = 2 |
---|
73 | DiskFull = 3 |
---|
74 | IllegalTftpOp = 4 |
---|
75 | UnknownTID = 5 |
---|
76 | FileAlreadyExists = 6 |
---|
77 | NoSuchUser = 7 |
---|
78 | FailedNegotiation = 8 |
---|
79 | |
---|
80 | class TftpException(Exception): |
---|
81 | """This class is the parent class of all exceptions regarding the handling |
---|
82 | of the TFTP protocol.""" |
---|
83 | pass |
---|
84 | |
---|
85 | class TftpTimeout(TftpException): |
---|
86 | """This class represents a timeout error waiting for a response from the |
---|
87 | other end.""" |
---|
88 | pass |
---|