Reticulum/RNS/__init__.py

89 lines
1.8 KiB
Python
Raw Normal View History

2016-06-03 11:02:02 -06:00
import os
2018-04-16 14:04:28 -06:00
import sys
2016-06-03 11:02:02 -06:00
import glob
2018-03-19 09:39:08 -06:00
import time
import random
2016-06-03 11:02:02 -06:00
from .Reticulum import Reticulum
2018-03-16 03:50:37 -06:00
from .Identity import Identity
2018-04-16 09:13:39 -06:00
from .Link import Link
2018-03-16 03:50:37 -06:00
from .Transport import Transport
2018-03-19 09:39:08 -06:00
from .Destination import Destination
from .Packet import Packet
2018-04-17 09:46:48 -06:00
from .Packet import PacketReceipt
2018-04-18 15:31:17 -06:00
from .Resource import Resource
2018-03-16 03:50:37 -06:00
2016-06-03 11:02:02 -06:00
modules = glob.glob(os.path.dirname(__file__)+"/*.py")
2018-03-19 09:39:08 -06: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
2018-04-23 15:42:16 -06:00
LOG_EXTREME = 7
2018-03-19 09:39:08 -06:00
LOG_STDOUT = 0x91
LOG_FILE = 0x92
loglevel = LOG_NOTICE
logfile = None
logdest = LOG_STDOUT
logtimefmt = "%Y-%m-%d %H:%M:%S"
random.seed(os.urandom(10))
2018-03-19 09:39:08 -06:00
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"
2018-04-25 16:54:48 -06:00
if (level == LOG_EXTREME):
return "Extra"
2018-03-19 09:39:08 -06:00
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 11:11:50 -06:00
def rand():
result = random.random()
return result
2018-03-19 11:11:50 -06:00
def hexrep(data, delimit=True):
delimiter = ":"
if not delimit:
delimiter = ""
hexrep = delimiter.join("{:02x}".format(ord(c)) for c in data)
2018-03-20 05:32:41 -06:00
return hexrep
def prettyhexrep(data):
delimiter = ""
2020-04-22 04:07:13 -06:00
hexrep = "<"+delimiter.join("{:02x}".format(c) for c in data)+">"
2018-04-16 14:04:28 -06:00
return hexrep
def panic():
os._exit(255)