mirror of
https://github.com/alexgo-io/stacks-puppet-node.git
synced 2026-04-23 19:31:00 +08:00
add utility methods from blockstack_client, like serializing an atlas zone file inventory message to a string and parsing host:port strings. This cuts dependency on blockstack_client
This commit is contained in:
84
blockstack/lib/util.py
Normal file
84
blockstack/lib/util.py
Normal file
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env python2
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Blockstack
|
||||
~~~~~
|
||||
copyright: (c) 2014-2015 by Halfmoon Labs, Inc.
|
||||
copyright: (c) 2016-2018 by Blockstack.org
|
||||
|
||||
This file is part of Blockstack
|
||||
|
||||
Blockstack is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Blockstack is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Blockstack. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
import urllib2
|
||||
import hashlib
|
||||
import threading
|
||||
import traceback
|
||||
import gc
|
||||
import time
|
||||
from .config import RPC_SERVER_PORT
|
||||
|
||||
def url_to_host_port(url, port=None):
|
||||
"""
|
||||
Given a URL, turn it into (host, port) for a blockstack server.
|
||||
Return (None, None) on invalid URL
|
||||
"""
|
||||
if not url.startswith('http://') or not url.startswith('https://'):
|
||||
url = 'http://' + url
|
||||
|
||||
if not port:
|
||||
port = RPC_SERVER_PORT
|
||||
|
||||
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 range(0, len(inv)):
|
||||
for j in range(0, 8):
|
||||
bit_index = 1 << (7 - j)
|
||||
val = (ord(inv[i]) & bit_index)
|
||||
if val != 0:
|
||||
ret += "1"
|
||||
else:
|
||||
ret += "0"
|
||||
|
||||
return ret
|
||||
Reference in New Issue
Block a user