restic2html.py.bak 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/python3
  2. from flask import Flask, render_template
  3. import json
  4. from datetime import datetime, date, timedelta
  5. import re
  6. import os.path, time
  7. app = Flask(__name__)
  8. @app.route('/')
  9. def index():
  10. try:
  11. with open('/tmp/restic_forget.txt', 'r') as f:
  12. content = f.read()
  13. match = re.search(r'{(.+?)}', content)
  14. if match:
  15. elements = match.group(1).split()
  16. forget_list = [str(element) for element in elements]
  17. except:
  18. forget_list = []
  19. with open('/tmp/restic.json') as f:
  20. data = json.load(f)
  21. for item in data:
  22. if item['short_id'] in forget_list:
  23. item['mopped'] = True
  24. else:
  25. item['mopped'] = ''
  26. today = date.today().strftime("%d.%m.%Y")
  27. tp = time.localtime(os.path.getmtime('/tmp/restic.json'))
  28. # convert struct_time to string with strftime, the result looks like "2022-03-30 12:23:55"
  29. tmp_restic_json_date = time.strftime('%d.%m.%Y %H:%M:%S', tp)
  30. try:
  31. tp = time.localtime(os.path.getmtime('/tmp/restic_forget.txt'))
  32. # convert struct_time to string with strftime, the result looks like "2022-03-30 12:23:55"
  33. tmp_restic_forget_date = time.strftime('/tmp/restic_forget.txt vom %d.%m.%Y %H:%M:%S', tp)
  34. except:
  35. tmp_restic_forget_date = ''
  36. return render_template( 'table.html', data=data, today=today,
  37. tmp_restic_json_date=tmp_restic_json_date,
  38. tmp_restic_forget_date=tmp_restic_forget_date )
  39. if __name__ == '__main__':
  40. app.run(port=5001, host='0.0.0.0', FLASK_ENV='development')
  41. # -------------------------------------------------- helper routines ---
  42. def parse_date(date_string):
  43. formats = (
  44. '%Y-%m-%dT%H:%M:%S%z',
  45. '%Y-%m-%dT%H:%M:%S.%f%z',
  46. '%Y-%m-%dT%H:%M:%SZ'
  47. )
  48. if len(date_string) > 26:
  49. tz = 'Z'
  50. m = re.search(r"(\+.*?)$", date_string)
  51. if m:
  52. tz = m[0]
  53. date_string = date_string[0:19] + tz #'Z+0000'
  54. for fmt in formats:
  55. try:
  56. return datetime.strptime(date_string, fmt)
  57. except ValueError:
  58. raise ValueError('no valid date format found for %s' % date_string)
  59. # https://stackoverflow.com/questions/6036082/call-a-python-function-from-jinja2
  60. # colors from
  61. @app.context_processor
  62. def utility_processor():
  63. def time_class(rtime):
  64. date_obj = parse_date(rtime)
  65. diff = date.today() - date_obj.date()
  66. if diff <= timedelta(days=0):
  67. return 'daily1'
  68. elif diff <= timedelta(days=1):
  69. return 'daily2'
  70. elif diff <= timedelta(days=7):
  71. return 'daily3'
  72. elif diff <= timedelta(days=15):
  73. return 'weekly'
  74. elif diff <= timedelta(days=30):
  75. return 'monthly'
  76. elif diff <= timedelta(days=200):
  77. return 'yearly1'
  78. elif diff <= timedelta(days=400):
  79. return 'yearly2'
  80. else:
  81. return 'yearly3'
  82. def clean_date(rtime):
  83. date_obj = parse_date(rtime)
  84. return date_obj.strftime('%d.%m.%Y')
  85. def clean_datetime(rtime):
  86. date_obj = parse_date(rtime)
  87. return date_obj.strftime('%d.%m.%Y %H:%M')
  88. def forget_pattern(mopped):
  89. if mopped:
  90. return 'forget'
  91. else:
  92. return ''
  93. return dict( time_class=time_class, clean_date=clean_date,
  94. clean_datetime=clean_datetime,
  95. forget_pattern=forget_pattern )