mirror of
https://github.com/alexgo-io/stacks-puppet-node.git
synced 2026-04-24 03:45:38 +08:00
clean up imports; move logging into its own file; move unrelated methods into utils
This commit is contained in:
@@ -39,112 +39,9 @@ from binascii import hexlify
|
||||
from ConfigParser import SafeConfigParser
|
||||
|
||||
import virtualchain
|
||||
from .backend.utxo import *
|
||||
from .utxo import *
|
||||
from .constants import *
|
||||
|
||||
|
||||
class NetworkLogFormatter( logging.Formatter ):
|
||||
"""
|
||||
Log formatter for network endpoints, such as Blockstack Portal
|
||||
"""
|
||||
level_names = {
|
||||
logging.DEBUG: 'DEBUG',
|
||||
logging.INFO: 'INFO',
|
||||
logging.WARN: 'WARN',
|
||||
logging.ERROR: 'ERROR',
|
||||
logging.FATAL: 'FATAL'
|
||||
}
|
||||
|
||||
def format(self, record):
|
||||
data = {
|
||||
'time': int(time.time()),
|
||||
'level': NetworkLogFormatter.level_names.get(record.levelno, 'TRACE'),
|
||||
'category': os.path.basename(record.pathname),
|
||||
'message': record.msg,
|
||||
}
|
||||
return data
|
||||
|
||||
|
||||
|
||||
class NetworkLogHandler( logging.Handler ):
|
||||
"""
|
||||
Log handler for network endpoints, such as Blockstack Portal
|
||||
"""
|
||||
def config(self, url, authorization):
|
||||
self.authorization = authorization
|
||||
self.url = url
|
||||
|
||||
def emit(self, record):
|
||||
log_entry = self.format(record)
|
||||
headers = {
|
||||
'Authorization': self.authorization
|
||||
}
|
||||
|
||||
try:
|
||||
requests.post(self.url, json=log_entry, headers=headers, timeout=1.0)
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
|
||||
def get_network_log_handler(api_password=None, name=None, scheme="http", host="localhost", port=LOG_NETWORK_PORT):
|
||||
"""
|
||||
Get a log handler to sending messages over the network.
|
||||
"""
|
||||
|
||||
level = logging.CRITICAL
|
||||
if DEBUG:
|
||||
logging.disable(logging.NOTSET)
|
||||
level = logging.DEBUG
|
||||
|
||||
if name is None:
|
||||
name = "<unknown>"
|
||||
level = logging.CRITICAL
|
||||
|
||||
if api_password is None:
|
||||
api_password = get_secret("BLOCKSTACK_API_PASSWORD")
|
||||
|
||||
if api_password is None:
|
||||
|
||||
# extract...
|
||||
p = SafeConfigParser()
|
||||
try:
|
||||
p.read(CONFIG_PATH)
|
||||
except:
|
||||
return None
|
||||
|
||||
try:
|
||||
if p.has_section('blockstack-client'):
|
||||
if p.get('blockstack-client', 'api_password') is not None:
|
||||
api_password = p.get('blockstack_client', 'api_password')
|
||||
except:
|
||||
return None
|
||||
|
||||
if not api_password:
|
||||
return None
|
||||
|
||||
url = "{}://{}:{}".format(scheme, host, port)
|
||||
authorization = 'bearer {}'.format(api_password)
|
||||
network = NetworkLogHandler()
|
||||
network.config(url, authorization)
|
||||
network.setLevel( level )
|
||||
formatter = NetworkLogFormatter()
|
||||
network.setFormatter(formatter)
|
||||
network.propagate = False
|
||||
|
||||
return network
|
||||
|
||||
|
||||
def get_logger(name="blockstack-client", debug=DEBUG):
|
||||
logger = virtualchain.get_logger(name)
|
||||
logger.setLevel(logging.DEBUG if debug else logging.INFO)
|
||||
|
||||
if not BLOCKSTACK_TEST:
|
||||
network_logger = get_network_log_handler(name=name)
|
||||
if network_logger:
|
||||
logger.addHandler(network_logger)
|
||||
|
||||
return logger
|
||||
|
||||
from .logger import get_logger
|
||||
|
||||
log = get_logger('blockstack-client')
|
||||
|
||||
@@ -167,55 +64,6 @@ def op_get_opcode_name(op_string):
|
||||
return OPCODE_NAMES[op]
|
||||
|
||||
|
||||
def url_to_host_port(url, port=DEFAULT_BLOCKSTACKD_PORT):
|
||||
"""
|
||||
Given a URL, turn it into (host, port).
|
||||
Return (None, None) on invalid URL
|
||||
"""
|
||||
if not url.startswith('http://') or not url.startswith('https://'):
|
||||
url = 'http://' + url
|
||||
|
||||
urlinfo = urllib2.urlparse.urlparse(url)
|
||||
hostport = urlinfo.netloc
|
||||
|
||||
parts = hostport.split('@')
|
||||
if len(parts) > 2:
|
||||
return None, None
|
||||
|
||||
if len(parts) == 2:
|
||||
hostport = parts[1]
|
||||
|
||||
parts = hostport.split(':')
|
||||
if len(parts) > 2:
|
||||
return None, None
|
||||
|
||||
if len(parts) == 2:
|
||||
try:
|
||||
port = int(parts[1])
|
||||
assert 0 < port < 65535, 'Invalid port'
|
||||
except TypeError:
|
||||
return None, None
|
||||
|
||||
return parts[0], port
|
||||
|
||||
|
||||
def atlas_inventory_to_string( inv ):
|
||||
"""
|
||||
Inventory to string (bitwise big-endian)
|
||||
"""
|
||||
ret = ""
|
||||
for i in xrange(0, len(inv)):
|
||||
for j in xrange(0, 8):
|
||||
bit_index = 1 << (7 - j)
|
||||
val = (ord(inv[i]) & bit_index)
|
||||
if val != 0:
|
||||
ret += "1"
|
||||
else:
|
||||
ret += "0"
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def interactive_prompt(message, parameters, default_opts):
|
||||
"""
|
||||
Prompt the user for a series of parameters
|
||||
|
||||
Reference in New Issue
Block a user