tools.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. """
  20. Define the CLI interface for pwman3 and the helper functions
  21. """
  22. import pwman.util.config as config
  23. import subprocess as sp
  24. class ANSI(object):
  25. """
  26. ANSI Colors
  27. """
  28. Reset = 0
  29. Bold = 1
  30. Underscore = 2
  31. Black = 30
  32. Red = 31
  33. Green = 32
  34. Yellow = 33
  35. Blue = 34
  36. Magenta = 35
  37. Cyan = 36
  38. White = 37
  39. def typeset(text, color, bold=False, underline=False):
  40. """print colored strings"""
  41. if not config.get_value("Global", "colors") == 'yes':
  42. return text
  43. if (bold):
  44. bold = "%d ;" % (ANSI.Bold)
  45. else:
  46. bold = ""
  47. if (underline):
  48. underline = "%d;" % (ANSI.Underscore)
  49. else:
  50. underline = ""
  51. return "\033[%s%s%sm%s\033[%sm" % (bold, underline, color,
  52. text, ANSI.Reset)
  53. def select(question, possible):
  54. """
  55. select input from user
  56. """
  57. for i in range(0, len(possible)):
  58. print ("%d - %-"+str(_defaultwidth)+"s") % (i+1, possible[i])
  59. while 1:
  60. uinput = getonechar(question)
  61. if uinput.isdigit() and int(uinput) in range(1, len(possible)+1):
  62. return possible[int(uinput)-1]
  63. def text_to_clipboards(text):
  64. """
  65. copy text to clipboard
  66. credit:
  67. https://pythonadventures.wordpress.com/tag/xclip/
  68. """
  69. # "primary":
  70. try:
  71. xsel_proc = sp.Popen(['xsel', '-pi'], stdin=sp.PIPE)
  72. xsel_proc.communicate(text)
  73. # "clipboard":
  74. xsel_proc = sp.Popen(['xsel', '-bi'], stdin=sp.PIPE)
  75. xsel_proc.communicate(text)
  76. except OSError, e:
  77. print e, "\nExecuting xsel failed, is it installed ?\n \
  78. please check your configuration file ... "
  79. def text_to_mcclipboard(text):
  80. """
  81. copy text to mac os x clip board
  82. credit:
  83. https://pythonadventures.wordpress.com/tag/xclip/
  84. """
  85. # "primary":
  86. try:
  87. pbcopy_proc = sp.Popen(['pbcopy'], stdin=sp.PIPE)
  88. pbcopy_proc.communicate(text)
  89. except OSError, e:
  90. print e, "\nExecuting pbcoy failed..."
  91. def open_url(link, macosx=False):
  92. """
  93. launch xdg-open or open in MacOSX with url
  94. """
  95. uopen = "xdg-open"
  96. if macosx:
  97. uopen = "open"
  98. try:
  99. sp.Popen([uopen, link], stdin=sp.PIPE)
  100. except OSError, e:
  101. print "Executing open_url failed with:\n", e