Util.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 format_elapsed_hr(t,now=None,cached={}):
  33. now = now or time.time()
  34. e = int(now - t)
  35. if not e in cached:
  36. h = abs(e) // 3600
  37. m = abs(e) // 60 % 60
  38. cached[e] = '{a}{b} minute{c} {d}'.format(
  39. a = '{} hour{}, '.format(h,suf(h)) if h else '',
  40. b = m,
  41. c = suf(m),
  42. d = 'ago' if e > 0 else 'in the future' ) if (h or m) else 'just now'
  43. return cached[e]
  44. def do_system(cmd,testing=False,shell=False):
  45. if testing:
  46. from mmgen.util import msg
  47. msg("Would execute: '%s'" % cmd)
  48. return True
  49. else:
  50. import subprocess
  51. return subprocess.call((cmd if shell else cmd.split()),shell,stderr=subprocess.PIPE)
  52. def get_url(url,gzip_ok=False,proxy=None,timeout=60,verbose=False,debug=False):
  53. if debug:
  54. print('get_url():')
  55. print(' url', url)
  56. print(' gzip_ok:',gzip_ok, 'proxy:',proxy, 'timeout:',timeout, 'verbose:',verbose)
  57. import pycurl,io
  58. c = pycurl.Curl()
  59. c_out = io.StringIO()
  60. c.setopt(pycurl.WRITEFUNCTION,c_out.write)
  61. c.setopt(pycurl.TIMEOUT,timeout)
  62. c.setopt(pycurl.FOLLOWLOCATION,True)
  63. c.setopt(pycurl.COOKIEFILE,'')
  64. c.setopt(pycurl.VERBOSE,verbose)
  65. if gzip_ok:
  66. c.setopt(pycurl.USERAGENT,'Lynx/2.8.9dev.8 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/3.4.9')
  67. c.setopt(pycurl.HTTPHEADER, [
  68. 'Accept: text/html, text/plain, text/sgml, text/css, application/xhtml+xml, */*;q=0.01',
  69. 'Accept-Encoding: gzip',
  70. 'Accept-Language: en']
  71. )
  72. if proxy:
  73. c.setopt(pycurl.PROXY,proxy)
  74. c.setopt(pycurl.URL,url)
  75. c.perform()
  76. text = c_out.getvalue()
  77. if text[:2] == '\x1f\x8b': # gzip magic number
  78. c_out.seek(0,0)
  79. import gzip
  80. with gzip.GzipFile(fileobj=c_out) as f:
  81. text = f.read()
  82. c_out.close()
  83. c.close()
  84. return text
  85. # big_digits = """
  86. # ███ █ ███ ███ █ █████ ███ █████ ███ ███
  87. # █ █ ██ █ █ █ ██ █ █ █ █ █ █ █
  88. # █ █ █ ██ ██ █ █ ████ ████ █ ███ ████
  89. # █ █ █ █ █ ████ █ █ █ █ █ █ █
  90. # ███ █ █████ ███ █ ████ ███ █ ███ ███ ██
  91. #
  92. # """
  93. big_digits = {
  94. 'w': 7, 'h': 6, 'n': 10, 'nums': """
  95. ████ █ ████ ████ █ █████ ████ ██████ ████ ████
  96. █ █ ██ █ █ █ ██ █ █ █ █ █ █ █
  97. █ █ █ █ ███ █ █ ████ █████ █ ████ █████
  98. █ █ █ ██ █ █ █ █ █ █ █ █ █ █
  99. █ █ █ █ █ █████ █ █ █ █ █ █ █
  100. ████ █ ██████ ████ █ ████ ████ █ ████ ████
  101. """,
  102. 'pw': 5, 'pn': 2, 'punc': """
  103. ██
  104. ██
  105. ██
  106. """
  107. }
  108. _bnums_c,_bpunc_c = [[l.strip('\n') + ' ' * (big_digits[m]*big_digits['n'])
  109. for l in big_digits[k][1:].split('\n')]
  110. for k,m in (('nums','w'),('punc','pw'))]
  111. _bnums_n,_bpunc_n = [[[l[0+(j*w):w+(j*w)] for l in i]
  112. for j in range(big_digits[n])] for n,w,i in
  113. (('n',big_digits['w'],_bnums_c),('pn',big_digits['pw'],_bpunc_c))]
  114. def display_big_digits(s,pre='',suf=''):
  115. s = [int((d,10,11)[(d in '.:')+(d==':')]) for d in s]
  116. return pre + ('\n'+pre).join(
  117. [''.join([(_bnums_n+_bpunc_n)[d][l] for d in s]) + suf for l in range(big_digits['h'])]
  118. )
  119. if __name__ == '__main__':
  120. num = '2345.17'
  121. print(display_big_digits(num,pre='+ ',suf=' +'))