bx.py (2124B)
1 #!/usr/bin/env python3 2 # electrum-obelisk 3 # Copyright (C) 2020-2021 Ivan J. <parazyd@dyne.org> 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU Affero General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU Affero General Public License for more details. 14 # 15 # You should have received a copy of the GNU Affero General Public License 16 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 """ Module for interfacing with the bx/bs command """ 18 import sys 19 from subprocess import run, CalledProcessError 20 from logging import getLogger 21 from json import loads 22 from json.decoder import JSONDecodeError 23 24 25 def bx_json(args): 26 """ Get JSON data using bx """ 27 cmd = ['bx', args[0], '--format', 'json'] 28 cmd.extend(args[1:]) 29 try: 30 output = run(cmd, check=True, capture_output=True) 31 except CalledProcessError: 32 log = getLogger('electrum-obelisk') 33 log.warning('bx_json (%s) failed', cmd) 34 return None 35 try: 36 return loads(output.stdout) 37 except JSONDecodeError: 38 return None 39 40 41 def bx_raw(args): 42 """ Get raw data using bx """ 43 cmd = ['bx'] 44 cmd.extend(args) 45 try: 46 output = run(cmd, check=True, capture_output=True) 47 except CalledProcessError: 48 log = getLogger('electrum-obelisk') 49 log.warning('bx_raw (%s) failed', cmd) 50 return None 51 if output.stdout: 52 return output.stdout[:-1] 53 return None 54 55 56 def bs_version(): 57 """ Get output of bs -v """ 58 try: 59 output = run(['bs', '-v'], capture_output=True, check=False) 60 return output.stdout.decode()[:-1] 61 except FileNotFoundError: 62 log = getLogger('electrum-obelisk') 63 log.error('Error: bs is not installed.') 64 # TODO: Reenable this 65 #sys.exit(1) 66 return ""