__init__.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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) 2012 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  20. # ============================================================================
  21. import os
  22. import pkg_resources
  23. import argparse
  24. import sys
  25. import re
  26. import colorama
  27. from pwman.util import config
  28. from pwman.data import factory
  29. appname = "pwman3"
  30. try:
  31. version = pkg_resources.get_distribution('pwman3').version
  32. except pkg_resources.DistributionNotFound: # pragma: no cover
  33. version = "0.5"
  34. website = "http://pwman3.github.io/pwman3/"
  35. author = "Oz Nahum Tiram"
  36. authoremail = "nahumoz@gmail.com"
  37. description = "a command line password management application."
  38. keywords = "password management sqlite crypto"
  39. long_description = (u"Pwman3 aims to provide a simple but powerful command "
  40. "line interface for password management.\nIt allows one "
  41. "to store your password in database locked by "
  42. "a\nmaster password which is AES encrypted.")
  43. _db_warn = (u"pwman3 detected that you are using the old database format"
  44. " which is insecure."
  45. " pwman3 will try to automatically convert the database now."
  46. "\n"
  47. "If you choose not to convert the database, pwman3, will quit."
  48. "\nYou can check the help (pwman3 -h) or look at the manpage how "
  49. "to convert the database manually."
  50. )
  51. def which(cmd): # pragma: no cover
  52. _, cmdname = os.path.split(cmd)
  53. for path in os.environ["PATH"].split(os.pathsep):
  54. cmd = os.path.join(path, cmdname)
  55. if os.path.isfile(cmd) and os.access(cmd, os.X_OK): # pragma: no cover
  56. return cmd
  57. return ''
  58. config_dir = os.path.expanduser("~/.pwman")
  59. def parser_options(formatter_class=argparse.HelpFormatter): # pragma: no cover
  60. parser = argparse.ArgumentParser(prog=appname,
  61. description=description,
  62. formatter_class=formatter_class)
  63. parser.add_argument('-c', '--config', dest='cfile',
  64. default=os.path.expanduser("~/.pwman/config"),
  65. help='cofiguration file to read')
  66. parser.add_argument('-d', '--database', dest='dbase')
  67. parser.add_argument('-i', '--import', dest='import_file',
  68. type=argparse.FileType())
  69. return parser
  70. def get_conf(args):
  71. config_dir = os.path.expanduser("~/.pwman")
  72. if not os.path.isdir(config_dir): # pragma: no cover
  73. os.mkdir(config_dir)
  74. configp = config.Config(args.cfile, config.default_config)
  75. return configp
  76. def set_xsel(configp, OSX):
  77. if not OSX:
  78. xselpath = which("xsel")
  79. configp.set_value("Global", "xsel", xselpath)
  80. elif OSX:
  81. pbcopypath = which("pbcopy")
  82. configp.set_value("Global", "xsel", pbcopypath)
  83. def set_win_colors(config): # pragma: no cover
  84. if 'win' in sys.platform:
  85. colorama.init()
  86. def set_umask(configp):
  87. umask = configp.get_value("Global", "umask")
  88. if re.search(r'^\d{4}$', umask):
  89. os.umask(int(umask))
  90. def set_db(args, configp):
  91. if args.dbase:
  92. configp.set_value("Database", "dburi", args.dbase)
  93. configp.set_value("Global", "save", "False")
  94. def get_conf_options(args, OSX):
  95. configp = get_conf(args)
  96. xselpath = configp.get_value("Global", "xsel")
  97. if not xselpath: # pragma: no cover
  98. set_xsel(configp, OSX)
  99. set_win_colors(configp)
  100. set_db(args, configp)
  101. set_umask(configp)
  102. dburi = configp.get_value("Database", "dburi")
  103. return xselpath, dburi, configp
  104. def get_db_version(config, args):
  105. dbver = factory.check_db_version(config.get_value("Database", "dburi"))
  106. return dbver