__init__.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 argparse
  23. import sys
  24. import re
  25. import colorama
  26. from pwman.util import config
  27. from pwman.data.factory import check_db_version
  28. appname = "pwman3"
  29. version = "0.7"
  30. website = "http://pwman3.github.io/pwman3/"
  31. author = "Oz Nahum Tiram"
  32. authoremail = "nahumoz@gmail.com"
  33. description = "a command line password manager with support for multiple databases."
  34. keywords = "password management sqlite crypto"
  35. long_description = u"""
  36. Pwman3 aims to provide a simple but powerful commandline interface for
  37. password management.
  38. It allows one to store passwords in database locked by master password which
  39. is AES encrypted.
  40. Pwman3 supports MySQL, Postgresql and SQLite"""
  41. def which(cmd): # pragma: no cover
  42. _, cmdname = os.path.split(cmd)
  43. for path in os.environ["PATH"].split(os.pathsep):
  44. cmd = os.path.join(path, cmdname)
  45. if os.path.isfile(cmd) and os.access(cmd, os.X_OK): # pragma: no cover
  46. return cmd
  47. return ''
  48. config_dir = os.path.expanduser("~/.pwman")
  49. def parser_options(formatter_class=argparse.HelpFormatter): # pragma: no cover
  50. parser = argparse.ArgumentParser(prog=appname,
  51. description=description,
  52. formatter_class=formatter_class)
  53. parser.add_argument('-c', '--config', dest='cfile',
  54. default=os.path.expanduser("~/.pwman/config"),
  55. help='cofiguration file to read')
  56. parser.add_argument('-d', '--database', dest='dbase')
  57. parser.add_argument('-i', '--import', dest='import_file',
  58. type=argparse.FileType())
  59. return parser
  60. def get_conf(args):
  61. config_dir = os.path.expanduser("~/.pwman")
  62. if not os.path.isdir(config_dir): # pragma: no cover
  63. os.mkdir(config_dir)
  64. configp = config.Config(args.cfile, config.default_config)
  65. return configp
  66. def set_xsel(configp, OSX):
  67. if not OSX:
  68. xselpath = which("xsel")
  69. configp.set_value("Global", "xsel", xselpath)
  70. elif OSX:
  71. pbcopypath = which("pbcopy")
  72. configp.set_value("Global", "xsel", pbcopypath)
  73. def set_win_colors(config): # pragma: no cover
  74. if 'win' in sys.platform:
  75. colorama.init()
  76. def set_umask(configp):
  77. umask = configp.get_value("Global", "umask")
  78. if re.search(r'^\d{4}$', umask):
  79. os.umask(int(umask))
  80. def set_db(args, configp):
  81. if args.dbase:
  82. configp.set_value("Database", "dburi", args.dbase)
  83. configp.set_value("Global", "save", "False")
  84. def get_conf_options(args, OSX):
  85. configp = get_conf(args)
  86. xselpath = configp.get_value("Global", "xsel")
  87. if not xselpath: # pragma: no cover
  88. set_xsel(configp, OSX)
  89. set_win_colors(configp)
  90. set_db(args, configp)
  91. set_umask(configp)
  92. dburi = configp.get_value("Database", "dburi")
  93. return xselpath, dburi, configp
  94. def get_db_version(config, args):
  95. dburi = check_db_version(config.get_value("Database", "dburi"))
  96. return dburi