Util.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2016 Philemon <mmgen-py@yandex.com>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmgen_node_tools.Util: utility functions for MMGen node tools
  20. """
  21. import time
  22. from mmgen.util import suf
  23. def get_hms(t=None,utc=False,no_secs=False):
  24. secs = t or time.time()
  25. ret = (time.localtime,time.gmtime)[utc](secs)
  26. fs,n = (('{:02}:{:02}:{:02}',6),('{:02}:{:02}',5))[no_secs]
  27. return fs.format(*ret[3:n])
  28. def get_day_hms(t=None,utc=False):
  29. secs = t or time.time()
  30. ret = (time.localtime,time.gmtime)[utc](secs)
  31. return '{:04}-{:02}-{:02} {:02}:{:02}:{:02}'.format(*ret[0:6])
  32. def do_system(cmd,testing=False,shell=False):
  33. if testing:
  34. from mmgen.util import msg
  35. msg("Would execute: '%s'" % cmd)
  36. return True
  37. else:
  38. import subprocess
  39. return subprocess.call((cmd if shell else cmd.split()),shell,stderr=subprocess.PIPE)
  40. def get_url(url,gzip_ok=False,proxy=None,timeout=60,verbose=False,debug=False):
  41. if debug:
  42. print('get_url():')
  43. print(' url', url)
  44. print(' gzip_ok:',gzip_ok, 'proxy:',proxy, 'timeout:',timeout, 'verbose:',verbose)
  45. import pycurl,io
  46. c = pycurl.Curl()
  47. c_out = io.StringIO()
  48. c.setopt(pycurl.WRITEFUNCTION,c_out.write)
  49. c.setopt(pycurl.TIMEOUT,timeout)
  50. c.setopt(pycurl.FOLLOWLOCATION,True)
  51. c.setopt(pycurl.COOKIEFILE,'')
  52. c.setopt(pycurl.VERBOSE,verbose)
  53. if gzip_ok:
  54. c.setopt(pycurl.USERAGENT,'Lynx/2.8.9dev.8 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/3.4.9')
  55. c.setopt(pycurl.HTTPHEADER, [
  56. 'Accept: text/html, text/plain, text/sgml, text/css, application/xhtml+xml, */*;q=0.01',
  57. 'Accept-Encoding: gzip',
  58. 'Accept-Language: en']
  59. )
  60. if proxy:
  61. c.setopt(pycurl.PROXY,proxy)
  62. c.setopt(pycurl.URL,url)
  63. c.perform()
  64. text = c_out.getvalue()
  65. if text[:2] == '\x1f\x8b': # gzip magic number
  66. c_out.seek(0,0)
  67. import gzip
  68. with gzip.GzipFile(fileobj=c_out) as f:
  69. text = f.read()
  70. c_out.close()
  71. c.close()
  72. return text
  73. # big_digits = """
  74. # ███ █ ███ ███ █ █████ ███ █████ ███ ███
  75. # █ █ ██ █ █ █ ██ █ █ █ █ █ █ █
  76. # █ █ █ ██ ██ █ █ ████ ████ █ ███ ████
  77. # █ █ █ █ █ ████ █ █ █ █ █ █ █
  78. # ███ █ █████ ███ █ ████ ███ █ ███ ███ ██
  79. #
  80. # """
  81. big_digits = {
  82. 'w': 7, 'h': 6, 'n': 10, 'nums': """
  83. ████ █ ████ ████ █ █████ ████ ██████ ████ ████
  84. █ █ ██ █ █ █ ██ █ █ █ █ █ █ █
  85. █ █ █ █ ███ █ █ ████ █████ █ ████ █████
  86. █ █ █ ██ █ █ █ █ █ █ █ █ █ █
  87. █ █ █ █ █ █████ █ █ █ █ █ █ █
  88. ████ █ ██████ ████ █ ████ ████ █ ████ ████
  89. """,
  90. 'pw': 5, 'pn': 2, 'punc': """
  91. ██
  92. ██
  93. ██
  94. """
  95. }
  96. _bnums_c,_bpunc_c = [[l.strip('\n') + ' ' * (big_digits[m]*big_digits['n'])
  97. for l in big_digits[k][1:].split('\n')]
  98. for k,m in (('nums','w'),('punc','pw'))]
  99. _bnums_n,_bpunc_n = [[[l[0+(j*w):w+(j*w)] for l in i]
  100. for j in range(big_digits[n])] for n,w,i in
  101. (('n',big_digits['w'],_bnums_c),('pn',big_digits['pw'],_bpunc_c))]
  102. def display_big_digits(s,pre='',suf=''):
  103. s = [int((d,10,11)[(d in '.:')+(d==':')]) for d in s]
  104. return pre + ('\n'+pre).join(
  105. [''.join([(_bnums_n+_bpunc_n)[d][l] for d in s]) + suf for l in range(big_digits['h'])]
  106. )
  107. if __name__ == '__main__':
  108. num = '2345.17'
  109. print(display_big_digits(num,pre='+ ',suf=' +'))