__init__.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 re
  24. import string
  25. import pkg_resources
  26. from pwman.util import config
  27. from pwman.data.factory import check_db_version
  28. try:
  29. import cryptography
  30. has_cryptography = True
  31. except ImportError:
  32. has_cryptography = False
  33. appname = "pwman3"
  34. try:
  35. version = pkg_resources.get_distribution('pwman3').version
  36. except pkg_resources.DistributionNotFound: # pragma: no cover
  37. version = "0.8.1"
  38. class PkgMetadata(object):
  39. def __init__(self):
  40. p = pkg_resources.get_distribution('pwman3')
  41. f = open(os.path.join(p.location+'-info','PKG-INFO'))
  42. lines = f.readlines()
  43. self.summary = lines[3].split(':')[-1].strip()
  44. self.description = ''.join(map(string.strip, lines[9:14]))
  45. self.author_email = lines[6].split(':')[-1].strip()
  46. self.author = lines[5].split(':')[-1].strip()
  47. self.home_page = lines[4].split(':')[-1].strip()
  48. try:
  49. pkg_meta = PkgMetadata()
  50. website = pkg_meta.home_page
  51. author = pkg_meta.author
  52. authoremail = pkg_meta.author_email
  53. description = pkg_meta.summary
  54. long_description = pkg_meta.description
  55. except IOError as E:
  56. # this should only happen once when installing the package
  57. description = "a command line password manager with support for multiple databases."
  58. website = 'http://pwman3.github.io/pwman3/'
  59. def which(cmd): # pragma: no cover
  60. _, cmdname = os.path.split(cmd)
  61. for path in os.environ["PATH"].split(os.pathsep):
  62. cmd = os.path.join(path, cmdname)
  63. if os.path.isfile(cmd) and os.access(cmd, os.X_OK): # pragma: no cover
  64. return cmd
  65. return ''
  66. config_dir = os.path.expanduser("~/.pwman")
  67. def parser_options(formatter_class=argparse.HelpFormatter): # pragma: no cover
  68. parser = argparse.ArgumentParser(
  69. prog='pwman3',
  70. description=description,
  71. formatter_class=formatter_class)
  72. parser.add_argument('-c', '--config', dest='cfile',
  73. default=os.path.expanduser("~/.pwman/config"),
  74. help='cofiguration file to read')
  75. parser.add_argument('-d', '--database', dest='dbase')
  76. parser.add_argument('-i', '--import', nargs=2, dest='file_delim',
  77. help="Specify the file name and the delimeter type")
  78. return parser
  79. def get_conf(args):
  80. config_dir = os.path.expanduser("~/.pwman")
  81. if not os.path.isdir(config_dir): # pragma: no cover
  82. os.mkdir(config_dir)
  83. configp = config.Config(args.cfile, config.default_config)
  84. return configp
  85. def set_xsel(configp, OSX):
  86. if not OSX:
  87. xselpath = which("xsel")
  88. configp.set_value("Global", "xsel", xselpath)
  89. elif OSX:
  90. pbcopypath = which("pbcopy")
  91. configp.set_value("Global", "xsel", pbcopypath)
  92. #def set_win_colors(config): # pragma: no cover
  93. # try:
  94. # if sys.platform.startswith('win'):
  95. # colorama.init()
  96. # except ImportError: # when installing colorama is still not there
  97. # pass
  98. def set_umask(configp):
  99. umask = configp.get_value("Global", "umask")
  100. if re.search(r'^\d{4}$', umask):
  101. os.umask(int(umask))
  102. def set_db(args, configp):
  103. if args.dbase:
  104. configp.set_value("Database", "dburi", args.dbase)
  105. configp.set_value("Global", "save", "False")
  106. def get_conf_options(args, OSX):
  107. configp = get_conf(args)
  108. xselpath = configp.get_value("Global", "xsel")
  109. if not xselpath: # pragma: no cover
  110. set_xsel(configp, OSX)
  111. #set_win_colors(configp)
  112. set_db(args, configp)
  113. set_umask(configp)
  114. dburi = configp.get_value("Database", "dburi")
  115. return xselpath, dburi, configp
  116. def get_db_version(config, args):
  117. dburi = check_db_version(config.get_value("Database", "dburi"))
  118. return dburi