From 25b20f6cc198e6d9c7987ec2d63f19573ed05fcc Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Tue, 16 Feb 2016 18:57:50 +0100 Subject: [PATCH] Python3 grammar. + Use `future` library + httplib-->http.client. + Use relative imports wherever. + print-function. + except Exception as ex. --- pybitcoin/__init__.py | 5 ++--- pybitcoin/rpc/namecoind_client.py | 28 +++++++++++++-------------- pybitcoin/rpc/namecoind_cluster.py | 13 ++++++------- pybitcoin/services/__init__.py | 16 +++++++-------- pybitcoin/services/bitcoind.py | 2 +- pybitcoin/services/blockchain_info.py | 6 +++--- pybitcoin/services/chain_com.py | 6 +++--- pybitcoin/transactions/__init__.py | 2 +- setup.py | 3 ++- unit_tests.py | 4 ++-- 10 files changed, 42 insertions(+), 43 deletions(-) diff --git a/pybitcoin/__init__.py b/pybitcoin/__init__.py index 4a9c2d4..25fb2c7 100644 --- a/pybitcoin/__init__.py +++ b/pybitcoin/__init__.py @@ -7,13 +7,12 @@ :license: MIT, see LICENSE for more details. """ -import services from .services import * -import transactions +from . import transactions from .transactions import * -import passphrases +from . import passphrases from .passphrases import create_passphrase from .passphrases.legacy import ( random_160bit_passphrase, random_256bit_passphrase diff --git a/pybitcoin/rpc/namecoind_client.py b/pybitcoin/rpc/namecoind_client.py index 2919fa1..84847b1 100644 --- a/pybitcoin/rpc/namecoind_client.py +++ b/pybitcoin/rpc/namecoind_client.py @@ -16,15 +16,15 @@ from .config import NAMECOIND_USE_HTTPS, VALUE_MAX_LIMIT import ssl -import httplib +import http.client as httplib -create_ssl_authproxy = False +create_ssl_authproxy = False do_wrap_socket = False if hasattr( ssl, "_create_unverified_context" ): #opt-out for verifying self-signed certificates (typically used in namecoin/bitcoind) ssl._create_default_https_context = ssl._create_unverified_context - create_ssl_authproxy = True + create_ssl_authproxy = True if not hasattr( ssl, "create_default_context" ): create_ssl_authproxy = False @@ -37,17 +37,17 @@ class NamecoindConnection( httplib.HTTPSConnection ): """ def __init__(self, host, port, timeout=None ): - + httplib.HTTPSConnection.__init__(self, host, port ) self.timeout = timeout - + def connect( self ): - + sock = socket.create_connection((self.host, self.port), self.timeout) if self._tunnel_host: self.sock = sock self._tunnel() - + self.sock = ssl.wrap_socket( sock, cert_reqs=ssl.CERT_NONE ) @@ -59,7 +59,7 @@ def __init__(self, server=NAMECOIND_SERVER, port=NAMECOIND_PORT, passphrase=NAMECOIND_WALLET_PASSPHRASE): global create_ssl_authproxy, do_wrap_socket - + if use_https: http_string = 'https://' else: @@ -69,19 +69,19 @@ def __init__(self, server=NAMECOIND_SERVER, port=NAMECOIND_PORT, self.passphrase = passphrase self.server = server - + if do_wrap_socket: # ssl._create_unverified_context and ssl.create_default_context are not supported. - # wrap the socket directly + # wrap the socket directly connection = NamecoindConnection( server, int(port) ) self.obj = AuthServiceProxy(authproxy_config_uri, connection=connection) - + elif create_ssl_authproxy: - # ssl has _create_unverified_context, so we're good to go + # ssl has _create_unverified_context, so we're good to go self.obj = AuthServiceProxy(authproxy_config_uri) - + else: - # have to set up an unverified context ourselves + # have to set up an unverified context ourselves ssl_ctx = ssl.create_default_context() ssl_ctx.check_hostname = False ssl_ctx.verify_mode = ssl.CERT_NONE diff --git a/pybitcoin/rpc/namecoind_cluster.py b/pybitcoin/rpc/namecoind_cluster.py index ce67893..050f6b0 100644 --- a/pybitcoin/rpc/namecoind_cluster.py +++ b/pybitcoin/rpc/namecoind_cluster.py @@ -6,7 +6,7 @@ :copyright: (c) 2015 by Halfmoon Labs :license: MIT, see LICENSE for more details. """ - +from __future__ import print_function import os import sys import json @@ -146,7 +146,7 @@ def rebroadcast_tx(server, raw_tx): namecoind = NamecoindClient(server) - print namecoind.sendrawtransaction(raw_tx) + print(namecoind.sendrawtransaction(raw_tx)) # ----------------------------------- @@ -186,7 +186,7 @@ def get_receiver_address(server): if info['ismine'] is not True: msg = "something went wrong" - print msg + print(msg) reply['error'] = msg else: reply['address'] = address @@ -205,7 +205,7 @@ def check_if_needs_reload(server, min_balance=MIN_BALANCE): balance = float(info['balance']) if balance < min_balance: - print "%s needs reloading" % server + print("%s needs reloading" % server) return True @@ -218,7 +218,7 @@ def send_payment(server, payments): namecoind.unlock_wallet(NAMECOIND_WALLET_PASSPHRASE) for payment in payments: - print namecoind.sendtoaddress(payment['address'], payment['amount']) + print(namecoind.sendtoaddress(payment['address'], payment['amount'])) # ----------------------------------- @@ -227,7 +227,7 @@ def reload_wallets(main_server, slave_servers=LOAD_SERVERS): payments = [] for server in LOAD_SERVERS: - #print get_receiver_address(server) + #print(get_receiver_address(server)) if check_if_needs_reload(server): reload_tx = {} reload_tx['amount'] = RELOAD_AMOUNT @@ -248,4 +248,3 @@ def reload_wallets(main_server, slave_servers=LOAD_SERVERS): pass check_servers(LOAD_SERVERS, clean=False) - \ No newline at end of file diff --git a/pybitcoin/services/__init__.py b/pybitcoin/services/__init__.py index c23214e..8ac2896 100644 --- a/pybitcoin/services/__init__.py +++ b/pybitcoin/services/__init__.py @@ -8,12 +8,12 @@ """ from .blockchain_client import BlockchainClient -from blockcypher import BlockcypherClient -from blockchain_info import BlockchainInfoClient -from chain_com import ChainComClient -from bitcoind import BitcoindClient, create_bitcoind_service_proxy +from .blockcypher import BlockcypherClient +from .blockchain_info import BlockchainInfoClient +from .chain_com import ChainComClient +from .bitcoind import BitcoindClient, create_bitcoind_service_proxy -import blockcypher -import blockchain_info -import chain_com -import bitcoind +from . import blockcypher +from . import blockchain_info +from . import chain_com +from . import bitcoind diff --git a/pybitcoin/services/bitcoind.py b/pybitcoin/services/bitcoind.py index 7841c49..443463b 100644 --- a/pybitcoin/services/bitcoind.py +++ b/pybitcoin/services/bitcoind.py @@ -7,7 +7,7 @@ :license: MIT, see LICENSE for more details. """ -import httplib +import http.client as httplib from bitcoinrpc.authproxy import AuthServiceProxy diff --git a/pybitcoin/services/blockchain_info.py b/pybitcoin/services/blockchain_info.py index c99b149..05cbdd2 100644 --- a/pybitcoin/services/blockchain_info.py +++ b/pybitcoin/services/blockchain_info.py @@ -49,9 +49,9 @@ def get_unspents(address, blockchain_client=BlockchainInfoClient()): r = requests.get(url, auth=auth) try: unspents = r.json()["unspent_outputs"] - except ValueError, e: + except ValueError as e: raise Exception('Invalid response from blockchain.info.') - + return format_unspents(unspents) def broadcast_transaction(hex_tx, blockchain_client=BlockchainInfoClient()): @@ -60,7 +60,7 @@ def broadcast_transaction(hex_tx, blockchain_client=BlockchainInfoClient()): url = BLOCKCHAIN_API_BASE_URL + '/pushtx' payload = {'tx': hex_tx} r = requests.post(url, data=payload, auth=blockchain_client.auth) - + if 'submitted' in r.text.lower(): return {'success': True} else: diff --git a/pybitcoin/services/chain_com.py b/pybitcoin/services/chain_com.py index 9690319..132bbe4 100644 --- a/pybitcoin/services/chain_com.py +++ b/pybitcoin/services/chain_com.py @@ -51,9 +51,9 @@ def get_unspents(address, blockchain_client=ChainComClient()): try: unspents = r.json() - except ValueError, e: + except ValueError as e: raise Exception('Received non-JSON response from chain.com.') - + return format_unspents(unspents) def broadcast_transaction(hex_tx, blockchain_client): @@ -72,7 +72,7 @@ def broadcast_transaction(hex_tx, blockchain_client): try: data = r.json() - except ValueError, e: + except ValueError as e: raise Exception('Received non-JSON from chain.com.') if 'transaction_hash' in data: diff --git a/pybitcoin/transactions/__init__.py b/pybitcoin/transactions/__init__.py index f402ec2..8c706cc 100644 --- a/pybitcoin/transactions/__init__.py +++ b/pybitcoin/transactions/__init__.py @@ -7,7 +7,7 @@ :license: MIT, see LICENSE for more details. """ -import opcodes +from . import opcodes from .network import broadcast_transaction, send_to_address, get_unspents, \ embed_data_in_blockchain, make_send_to_address_tx, make_op_return_tx, \ diff --git a/setup.py b/setup.py index aef3d68..619b407 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,8 @@ 'pybitcointools==1.1.15', 'python-bitcoinrpc>=0.1', 'keychain>=0.1.4', - 'bitcoin>=1.1.39' + 'bitcoin>=1.1.39', + 'future', ], classifiers=[ 'Intended Audience :: Developers', diff --git a/unit_tests.py b/unit_tests.py index 2dfcc46..ad10258 100644 --- a/unit_tests.py +++ b/unit_tests.py @@ -568,7 +568,7 @@ def test_send_transaction_bitcoind(self): def test_build_transaction(self): signed_tx = make_send_to_address_tx( recipient_address, 1000, private_key, client) - #print signed_tx + #print(signed_tx) self.assertTrue(True) """ @@ -615,7 +615,7 @@ def test_hex_op_return_tx(self): def test_short_hex_op_return_tx(self): resp = embed_data('0'*2) self.assertTrue(resp.get('success')) - + def test_bin_op_return_tx(self): resp = embed_data("Hello, Blockchain!") self.assertTrue(resp.get('success'))