Util.py 4.5 KB

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