Reticulum/FPE/__init__.py

75 lines
1.6 KiB
Python
Raw Normal View History

2016-06-03 19:02:02 +02:00
import os
import glob
2018-03-19 16:39:08 +01:00
import time
2016-06-03 19:02:02 +02:00
2018-03-16 10:50:37 +01:00
from .FlexPE import FlexPE
from .Identity import Identity
from .Transport import Transport
2018-03-19 16:39:08 +01:00
from .Destination import Destination
from .Packet import Packet
2018-03-16 10:50:37 +01:00
2016-06-03 19:02:02 +02:00
modules = glob.glob(os.path.dirname(__file__)+"/*.py")
2018-03-19 16:39:08 +01:00
__all__ = [ os.path.basename(f)[:-3] for f in modules if not f.endswith('__init__.py')]
LOG_CRITICAL = 0
LOG_ERROR = 1
LOG_WARNING = 2
LOG_NOTICE = 3
LOG_INFO = 4
LOG_VERBOSE = 5
LOG_DEBUG = 6
LOG_STDOUT = 0x91
LOG_FILE = 0x92
loglevel = LOG_NOTICE
logfile = None
logdest = LOG_STDOUT
logtimefmt = "%Y-%m-%d %H:%M:%S"
def loglevelname(level):
if (level == LOG_CRITICAL):
return "Critical"
if (level == LOG_ERROR):
return "Error"
if (level == LOG_WARNING):
return "Warning"
if (level == LOG_NOTICE):
return "Notice"
if (level == LOG_INFO):
return "Info"
if (level == LOG_VERBOSE):
return "Verbose"
if (level == LOG_DEBUG):
return "Debug"
return "Unknown"
def log(msg, level=3):
# TODO: not thread safe
if loglevel >= level:
timestamp = time.time()
logstring = "["+time.strftime(logtimefmt)+"] ["+loglevelname(level)+"] "+msg
if (logdest == LOG_STDOUT):
print(logstring)
if (logdest == LOG_FILE and logfile != None):
file = open(logfile, "a")
file.write(logstring+"\n")
file.close()
2018-03-19 18:11:50 +01:00
def hexprint(data):
print(hexrep(hexrep))
def hexrep(data, delimit=True):
delimiter = ":"
if not delimit:
delimiter = ""
hexrep = delimiter.join("{:02x}".format(ord(c)) for c in data)
2018-03-20 12:32:41 +01:00
return hexrep
def prettyhexrep(data):
delimiter = ""
hexrep = "<"+delimiter.join("{:02x}".format(ord(c)) for c in data)+">"
2018-03-19 18:11:50 +01:00
return hexrep