base.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. #============================================================================
  17. # Copyright (C) 2013 Oz Nahum <nahumoz@gmail.com>
  18. #============================================================================
  19. # pylint: disable=I0011
  20. """
  21. Define the base CLI interface for pwman3
  22. """
  23. from __future__ import print_function
  24. class HelpUI(object):
  25. """
  26. this class holds all the UI help functionality.
  27. in PwmanCliNew. The later inherits from this class
  28. and allows it to print help messages to the console.
  29. """
  30. def usage(self, string):
  31. print ("Usage: %s" % (string))
  32. def help_open(self):
  33. self.usage("open <ID>")
  34. print ("Launch default browser with 'xdg-open url',\n",
  35. "the url must contain http:// or https://.")
  36. def help_o(self):
  37. self.help_open()
  38. def help_copy(self):
  39. self.usage("copy <ID>")
  40. print ("Copy password to X clipboard (xsel required)")
  41. def help_cp(self):
  42. self.help_copy()
  43. def help_cls(self):
  44. self.usage("cls")
  45. print ("Clear the Screen from information.")
  46. def help_list(self):
  47. self.usage("list <tag> ...")
  48. print ("List nodes that match current or specified filter.",
  49. " l is an alias.")
  50. def help_EOF(self):
  51. self.help_exit()
  52. def help_delete(self):
  53. self.usage("delete <ID|tag> ...")
  54. print ("Deletes nodes. rm is an alias.")
  55. self._mult_id_help()
  56. def help_h(self):
  57. self.help_help()
  58. def help_help(self):
  59. self.usage("help [topic]")
  60. print ("Prints a help message for a command.")
  61. def help_e(self):
  62. self.help_edit()
  63. def help_n(self):
  64. self.help_new()
  65. def help_p(self):
  66. self.help_print()
  67. def help_l(self):
  68. self.help_list()
  69. def help_edit(self):
  70. self.usage("edit <ID|tag> ... ")
  71. print ("Edits a nodes.")
  72. self._mult_id_help()
  73. def help_import(self):
  74. self.usage("import [filename] ...")
  75. print ("Imports a nodes from a file.")
  76. def help_export(self):
  77. self.usage("export <ID|tag> ... ")
  78. print ("Exports a list of ids to an external format. If no IDs or",
  79. " tags are specified, then all nodes under the current",
  80. " filter are exported.")
  81. self._mult_id_help()
  82. def help_new(self):
  83. self.usage("new")
  84. print ("Creates a new node.,",
  85. "You can override default config settings the following way:\n",
  86. "pwman> n {'leetify':False, 'numerics':True}")
  87. def help_rm(self):
  88. self.help_delete()
  89. def help_print(self):
  90. self.usage("print <ID|tag> ...")
  91. print ("Displays a node. ")
  92. self._mult_id_help()
  93. def _mult_id_help(self):
  94. print("Multiple ids and nodes can be specified, separated by a space.",
  95. " A range of ids can be specified in the format n-N. e.g. ",
  96. " '10-20' would specify all nodes having ids from 10 to 20 ",
  97. " inclusive. Tags are considered one-by-one. e.g. 'foo 2 bar'",
  98. " would yield to all nodes with tag 'foo', node 2 and all ",
  99. " nodes with tag 'bar'.")
  100. def help_exit(self):
  101. self.usage("exit")
  102. print("Exits the application.")
  103. def help_save(self):
  104. self.usage("save [filename]")
  105. print("Saves the current configuration to [filename]. If no filename ",
  106. "is given, the configuration is saved to the file from which ",
  107. "the initial configuration was loaded.")
  108. def help_set(self):
  109. self.usage("set [configoption] [value]")
  110. print("Sets a configuration option. If no value is specified, the ",
  111. "current value for [configoption] is output. If neither ",
  112. "[configoption] nor [value] are specified, the whole current ",
  113. "configuration is output. [configoption] must be of the ",
  114. "format <section>.<option>")
  115. def help_passwd(self):
  116. self.usage("passwd")
  117. print("Changes the password on the database. ")
  118. def help_forget(self):
  119. self.usage("forget")
  120. print("Forgets the database password. Your password will need to ",
  121. "be reentered before accessing the database again.")
  122. def help_clear(self):
  123. self.usage("clear")
  124. print("Clears the filter criteria. ")
  125. def help_filter(self):
  126. self.usage("filter <tag> ...")
  127. print("Filters nodes on tag. Arguments can be zero or more tags. ",
  128. "Displays current tags if called without arguments.")
  129. def help_tags(self):
  130. self.usage("tags")
  131. print("Displays all tags in used in the database.")
  132. class BaseUI(object):
  133. """
  134. this class holds all the UI functionality
  135. in PwmanCliNew. The later inherits from this class
  136. and allows it to print messages to the console.
  137. """
  138. def _do_filter(self, args):
  139. pass
  140. def _tags(self, enc):
  141. """
  142. read tags from TAGS table in DB,
  143. this method has a working unittest
  144. """
  145. tags = self._db.listtags()
  146. if tags:
  147. _tags = [''] * len(tags)
  148. for t in tags:
  149. try:
  150. _tags.append(enc.decrypt(t))
  151. except (ValueError, Exception), e:
  152. _tags.append(t)
  153. del(e)
  154. _tags = filter(None, _tags)
  155. return _tags