/usr/share/pyshared/pwman/ui/tools.py is in pwman3 0.4.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | #============================================================================
# This file is part of Pwman3.
#
# Pwman3 is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2
# as published by the Free Software Foundation;
#
# Pwman3 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pwman3; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#============================================================================
# Copyright (C) 2013 Oz Nahum <nahumoz@gmail.com>
#============================================================================
"""
Define the CLI interface for pwman3 and the helper functions
"""
from pwman.util.callback import Callback
import pwman.util.config as config
import subprocess as sp
import getpass
import sys
import struct
import os
import colorama
from pwman.data.tags import TagNew as Tag
if sys.platform != 'win32':
import termios
import fcntl
import tty
try:
import pyreadline as readline
_readline_available = True
except ImportError:
_readline_available = False
# raise ImportError("You need 'pyreadline' on Windows")
else:
try:
import readline
_readline_available = True
except ImportError, e:
_readline_available = False
_defaultwidth = 10
class ANSI(object):
"""
ANSI Colors
"""
Reset = 0
Bold = 1
Underscore = 2
Black = 30
Red = 31
Green = 32
Yellow = 33
Blue = 34
Magenta = 35
Cyan = 36
White = 37
def typeset(text, color, bold=False, underline=False):
"""
print colored strings using colorama
"""
if not config.get_value("Global", "colors") == 'yes':
return text
if bold:
text = colorama.Style.BRIGHT + text
if underline and not 'win32' in sys.platform:
text = ANSI.Underscore + text
return color+text+colorama.Style.RESET_ALL
def select(question, possible):
"""
select input from user
"""
for i in range(0, len(possible)):
print ("%d - %-"+str(_defaultwidth)+"s") % (i+1, possible[i])
while 1:
uinput = getonechar(question)
if uinput.isdigit() and int(uinput) in range(1, len(possible)+1):
return possible[int(uinput)-1]
def text_to_clipboards(text):
"""
copy text to clipboard
credit:
https://pythonadventures.wordpress.com/tag/xclip/
"""
# "primary":
try:
xsel_proc = sp.Popen(['xsel', '-pi'], stdin=sp.PIPE)
xsel_proc.communicate(text)
# "clipboard":
xsel_proc = sp.Popen(['xsel', '-bi'], stdin=sp.PIPE)
xsel_proc.communicate(text)
except OSError, e:
print e, "\nExecuting xsel failed, is it installed ?\n \
please check your configuration file ... "
def text_to_mcclipboard(text):
"""
copy text to mac os x clip board
credit:
https://pythonadventures.wordpress.com/tag/xclip/
"""
# "primary":
try:
pbcopy_proc = sp.Popen(['pbcopy'], stdin=sp.PIPE)
pbcopy_proc.communicate(text)
except OSError, e:
print e, "\nExecuting pbcoy failed..."
def open_url(link, macosx=False):
"""
launch xdg-open or open in MacOSX with url
"""
uopen = "xdg-open"
if macosx:
uopen = "open"
try:
sp.Popen([uopen, link], stdin=sp.PIPE)
except OSError, e:
print "Executing open_url failed with:\n", e
def getpassword(question, width=_defaultwidth, echo=False):
if echo:
print question.ljust(width),
return sys.stdin.readline().rstrip()
else:
while 1:
a1 = getpass.getpass(question.ljust(width))
if len(a1) == 0:
return a1
a2 = getpass.getpass("[Repeat] %s" % (question.ljust(width)))
if a1 == a2:
return a1
else:
print "Passwords don't match. Try again."
def gettermsize():
s = struct.pack("HHHH", 0, 0, 0, 0)
f = sys.stdout.fileno()
x = fcntl.ioctl(f, termios.TIOCGWINSZ, s)
rows, cols, width, height = struct.unpack("HHHH", x)
return rows, cols
def getinput(question, default="", completer=None, width=_defaultwidth):
if not _readline_available:
return raw_input(question.ljust(width))
else:
def defaulter():
"""define default behavior startup"""
if _readline_available:
readline.insert_text(default)
readline.set_startup_hook(defaulter)
oldcompleter = readline.get_completer()
readline.set_completer(completer)
x = raw_input(question.ljust(width))
readline.set_completer(completer)
readline.set_startup_hook()
return x
def getyesno(question, defaultyes=False, width=_defaultwidth):
if (defaultyes):
default = "[Y/n]"
else:
default = "[y/N]"
ch = getonechar("%s %s" % (question, default), width)
if (ch == '\n'):
if (defaultyes):
return True
else:
return False
elif (ch == 'y' or ch == 'Y'):
return True
elif (ch == 'n' or ch == 'N'):
return False
else:
return getyesno(question, defaultyes, width)
class CliMenu(object):
def __init__(self):
self.items = []
def add(self, item):
if (isinstance(item, CliMenuItem)):
self.items.append(item)
else:
print item.__class__
def run(self):
while True:
i = 0
for x in self.items:
i = i + 1
# don't break compatability with old db
try:
current = x.getter()
except TypeError:
current = x.getter
currentstr = ''
if type(current) == list:
for c in current:
currentstr += ("%s " % (c))
else:
currentstr = current
print ("%d - %-"+str(_defaultwidth)
+ "s %s") % (i, x.name+":",
currentstr)
print "%c - Finish editing" % ('X')
option = getonechar("Enter your choice:")
try:
print "selection, ", option
# substract 1 because array subscripts start at 0
selection = int(option) - 1
# new value is created by calling the editor with the
# previous value as a parameter
# TODO: enable overriding password policy as if new node
# is created.
if selection == 1: # for password
value = self.items[selection].editor(0)
else:
try:
edit = self.items[selection].getter()
value = self.items[selection].editor(edit)
self.items[selection].setter(value)
except TypeError:
edit = self.items[selection].getter
value = self.items[selection].editor(edit)
self.items[selection].setter = value
except (ValueError, IndexError):
if (option.upper() == 'X'):
break
print "Invalid selection"
def runner(self, new_node):
while True:
i = 0
for x in self.items:
i = i + 1
# don't break compatability with old db
try:
current = x.getter()
except TypeError:
current = x.getter
except AttributeError:
current = x
currentstr = ''
if type(current) == list:
for c in current:
try:
currentstr += ' '+c
except TypeError:
currentstr += ' '+c._name
else:
currentstr = current
print ("%d - %-"+str(_defaultwidth)
+ "s %s") % (i, x.name+":",
currentstr)
print "%c - Finish editing" % ('X')
option = getonechar("Enter your choice:")
try:
print "selection, ", option
# substract 1 because array subscripts start at 0
selection = int(option) - 1
# new value is created by calling the editor with the
# previous value as a parameter
# TODO: enable overriding password policy as if new node
# is created.
if selection == 0:
new_node.username = getinput("Username:")
self.items[2].getter = new_node.username
elif selection == 1: # for password
value = self.items[selection].editor(0)
new_node.password = value
self.items[2].getter = new_node.password
elif selection == 2:
new_node.notes = getinput("Url:")
self.items[2].getter = new_node.url
elif selection == 3: # for notes
new_node.notes = getinput("Notes:")
self.items[3].getter = new_node.notes
self.items[3].setter = new_node.notes
elif selection == 4:
taglist = getinput("Tags:")
tagstrings = taglist.split()
tags = [Tag(tn) for tn in tagstrings]
new_node.tags = tags
self.items[4].setter = new_node.tags
self.items[4].getter = new_node.tags
except (ValueError, IndexError):
if (option.upper() == 'X'):
break
print "Invalid selection"
def getonechar(question, width=_defaultwidth):
question = "%s " % (question)
print question.ljust(width),
sys.stdout.flush()
fd = sys.stdin.fileno()
tty_mode = tty.tcgetattr(fd)
tty.setcbreak(fd)
try:
ch = os.read(fd, 1)
finally:
tty.tcsetattr(fd, tty.TCSAFLUSH, tty_mode)
print ch
return ch
class CliMenuItem(object):
def __init__(self, name, editor, getter, setter):
self.name = name
self.editor = editor
self.getter = getter
self.setter = setter
class CLICallback(Callback):
def getinput(self, question):
return raw_input(question)
def getsecret(self, question):
return getpass.getpass(question + ":")
|