webui.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python
  2. #============================================================================
  3. # This file is part of Pwman3.
  4. #
  5. # Pwman3 is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License, version 2
  7. # as published by the Free Software Foundation;
  8. #
  9. # Pwman3 is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Pwman3; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #============================================================================
  18. # Copyright (C) 2012-2014 Oz Nahum <nahumoz@gmail.com>
  19. #============================================================================
  20. from __future__ import print_function
  21. from bottle import route, run, debug, template, request, get
  22. import os
  23. import sys
  24. import re
  25. import shutil
  26. from pwman import default_config, which
  27. from pwman import parser_options
  28. from pwman.ui import get_ui_platform
  29. from pwman.ui.tools import CLICallback
  30. from pwman.util.crypto import CryptoEngine
  31. import pwman.util.config as config
  32. import pwman.data.factory
  33. tmplt = """
  34. %#template to generate a HTML table from a list of tuples (or list of lists, or tuple of tuples or ...)
  35. <p>The open items are as follows:</p>
  36. <table border="1">
  37. %for row in rows:
  38. <tr>
  39. %for col in row:
  40. <td>{{col}}</td>
  41. %end
  42. </tr>
  43. %end
  44. </table>
  45. """
  46. def get_conf(args):
  47. config_dir = os.path.expanduser("~/.pwman")
  48. if not os.path.isdir(config_dir):
  49. os.mkdir(config_dir)
  50. if not os.path.exists(args.cfile):
  51. config.set_defaults(default_config)
  52. else:
  53. config.load(args.cfile)
  54. return config
  55. def set_xsel(config, OSX):
  56. if not OSX:
  57. xselpath = which("xsel")
  58. config.set_value("Global", "xsel", xselpath)
  59. elif OSX:
  60. pbcopypath = which("pbcopy")
  61. config.set_value("Global", "xsel", pbcopypath)
  62. def set_win_colors(config):
  63. if 'win' in sys.platform:
  64. try:
  65. import colorama
  66. colorama.init()
  67. except ImportError:
  68. config.set_value("Global", "colors", 'no')
  69. def set_umask(config):
  70. # set umask before creating/opening any files
  71. try:
  72. umask = config.get_value("Global", "umask")
  73. if re.search(r'^\d{4}$', umask):
  74. os.umask(int(umask))
  75. else:
  76. raise ValueError
  77. except ValueError:
  78. print("Could not determine umask from config!")
  79. sys.exit(2)
  80. def set_db(args):
  81. if args.dbase:
  82. config.set_value("Database", "filename", args.dbase)
  83. config.set_value("Global", "save", "False")
  84. def set_algorithm(args, config):
  85. if args.algo:
  86. config.set_value("Encryption", "algorithm", args.algo)
  87. config.set_value("Global", "save", "False")
  88. def get_conf_options(args, OSX):
  89. config = get_conf(args)
  90. xselpath = config.get_value("Global", "xsel")
  91. if not xselpath:
  92. set_xsel(config, OSX)
  93. set_win_colors(config)
  94. set_db(args)
  95. set_umask(config)
  96. set_algorithm(args, config)
  97. dbtype = config.get_value("Database", "type")
  98. if not dbtype:
  99. print("Could not read the Database type from the config!")
  100. sys.exit(1)
  101. return xselpath, dbtype
  102. @route('/', method=['GET', 'POST'])
  103. def listnodes():
  104. OSX = False
  105. args = parser_options().parse_args()
  106. xselpath, dbtype = get_conf_options(args, OSX)
  107. dbver = 0.4
  108. db = pwman.data.factory.create(dbtype, dbver)
  109. db.open()
  110. crypto = CryptoEngine.get()
  111. crypto.auth('YOURPASSWORD')
  112. nodeids = db.listnodes()
  113. nodes = db.getnodes(nodeids)
  114. nodesd = [''] * len(nodes)
  115. for idx, node in enumerate(nodes):
  116. tags = node.tags
  117. tags = filter(None, tags)
  118. nodesd[idx]=('@'.join((node.username, node.url)), ','.join(tags))
  119. output = template('make_table', rows=nodesd)
  120. return output
  121. debug(True)
  122. run(reloader=True)