Преглед изворни кода

Remove all startup aid functions from cli script

These functions can be used also by other front ends,
hence they are now found in __init__.py
oz123 пре 10 година
родитељ
комит
fb714e6e7e
2 измењених фајлова са 100 додато и 96 уклоњено
  1. 99 0
      pwman/__init__.py
  2. 1 96
      scripts/pwman3

+ 99 - 0
pwman/__init__.py

@@ -21,6 +21,10 @@
 import os
 import os
 import pkg_resources
 import pkg_resources
 import argparse
 import argparse
+from util import config
+import sys
+import re
+import data.factory
 
 
 appname = "Pwman3"
 appname = "Pwman3"
 try:
 try:
@@ -34,6 +38,15 @@ authoremail = "nahumoz@gmail.com"
 description = "Pwman -a command line password management application."
 description = "Pwman -a command line password management application."
 keywords = "password management sqlite crypto"
 keywords = "password management sqlite crypto"
 
 
+_db_warn = ("pwman3 detected that you are using the old database format"
+            " which is insecure."
+            " pwman3 will try to automatically convert the database now."
+            "\n"
+            "If you choose not to convert the database, pwman3, will quit."
+            "\nYou can check the help (pwman3 -h) or look at the manpage how to convert "
+            " the database manually."
+            )
+
 
 
 def which(cmd):
 def which(cmd):
     _, cmdname = os.path.split(cmd)
     _, cmdname = os.path.split(cmd)
@@ -80,3 +93,89 @@ def parser_options():
                         help=("The name of the newly created database after "
                         help=("The name of the newly created database after "
                               "converting."))
                               "converting."))
     return parser
     return parser
+
+
+def get_conf(args):
+    config_dir = os.path.expanduser("~/.pwman")
+
+    if not os.path.isdir(config_dir):
+        os.mkdir(config_dir)
+
+    if not os.path.exists(args.cfile):
+        config.set_defaults(default_config)
+    else:
+        config.load(args.cfile)
+
+    return config
+
+
+def set_xsel(config, OSX):
+    if not OSX:
+        xselpath = which("xsel")
+        config.set_value("Global", "xsel", xselpath)
+    elif OSX:
+        pbcopypath = which("pbcopy")
+        config.set_value("Global", "xsel", pbcopypath)
+
+
+def set_win_colors(config):
+    if 'win' in sys.platform:
+        try:
+            import colorama
+            colorama.init()
+        except ImportError:
+            config.set_value("Global", "colors", 'no')
+
+
+def set_umask(config):
+    # set umask before creating/opening any files
+    try:
+        umask = config.get_value("Global", "umask")
+        if re.search(r'^\d{4}$', umask):
+            os.umask(int(umask))
+        else:
+            raise ValueError
+    except ValueError:
+        print("Could not determine umask from config!")
+        sys.exit(2)
+
+
+
+def set_db(args):
+    if args.dbase:
+        config.set_value("Database", "filename", args.dbase)
+        config.set_value("Global", "save", "False")
+
+
+def set_algorithm(args, config):
+    if args.algo:
+        config.set_value("Encryption", "algorithm", args.algo)
+        config.set_value("Global", "save", "False")
+
+def get_conf_options(args, OSX):
+
+    config = get_conf(args)
+    xselpath = config.get_value("Global", "xsel")
+    if not xselpath:
+        set_xsel(config, OSX)
+
+    set_win_colors(config)
+    set_db(args)
+    set_umask(config)
+    set_algorithm(args, config)
+    dbtype = config.get_value("Database", "type")
+    if not dbtype:
+        print("Could not read the Database type from the config!")
+        sys.exit(1)
+
+    return xselpath, dbtype
+
+
+def get_db_version(config, dbtype, args):
+    if os.path.exists(config.get_value("Database", "filename")):
+        dbver = data.factory.check_db_version(dbtype)
+        if dbver < 0.4 and not args.dbconvert:
+            print(_db_warn)
+    else:
+        dbver = 0.4
+    return dbver

+ 1 - 96
scripts/pwman3

@@ -22,9 +22,8 @@
 from __future__ import print_function
 from __future__ import print_function
 import os
 import os
 import sys
 import sys
-import re
 import shutil
 import shutil
-from pwman import default_config, which
+from pwman import get_conf_options, get_db_version
 from pwman import parser_options
 from pwman import parser_options
 from pwman.ui import get_ui_platform
 from pwman.ui import get_ui_platform
 from pwman.ui.tools import CLICallback
 from pwman.ui.tools import CLICallback
@@ -33,100 +32,6 @@ import pwman.data.factory
 from pwman.data.convertdb import PwmanConvertDB
 from pwman.data.convertdb import PwmanConvertDB
 from pwman.util.crypto import CryptoEngine
 from pwman.util.crypto import CryptoEngine
 
 
-_db_warn = ("pwman3 detected that you are using the old database format"
-            " which is insecure."
-            " pwman3 will try to automatically convert the database now."
-            "\n"
-            "If you choose not to convert the database, pwman3, will quit."
-            "\nYou can check the help (pwman3 -h) or look at the manpage how to convert "
-            " the database manually."
-            )
-
-
-def get_conf(args):
-    config_dir = os.path.expanduser("~/.pwman")
-
-    if not os.path.isdir(config_dir):
-        os.mkdir(config_dir)
-
-    if not os.path.exists(args.cfile):
-        config.set_defaults(default_config)
-    else:
-        config.load(args.cfile)
-
-    return config
-
-
-def set_xsel(config, OSX):
-    if not OSX:
-        xselpath = which("xsel")
-        config.set_value("Global", "xsel", xselpath)
-    elif OSX:
-        pbcopypath = which("pbcopy")
-        config.set_value("Global", "xsel", pbcopypath)
-
-
-def set_win_colors(config):
-    if 'win' in sys.platform:
-        try:
-            import colorama
-            colorama.init()
-        except ImportError:
-            config.set_value("Global", "colors", 'no')
-
-
-def set_umask(config):
-    # set umask before creating/opening any files
-    try:
-        umask = config.get_value("Global", "umask")
-        if re.search(r'^\d{4}$', umask):
-            os.umask(int(umask))
-        else:
-            raise ValueError
-    except ValueError:
-        print("Could not determine umask from config!")
-        sys.exit(2)
-
-
-def set_db(args):
-    if args.dbase:
-        config.set_value("Database", "filename", args.dbase)
-        config.set_value("Global", "save", "False")
-
-
-def set_algorithm(args, config):
-    if args.algo:
-        config.set_value("Encryption", "algorithm", args.algo)
-        config.set_value("Global", "save", "False")
-
-def get_conf_options(args, OSX):
-
-    config = get_conf(args)
-    xselpath = config.get_value("Global", "xsel")
-    if not xselpath:
-        set_xsel(config, OSX)
-
-    set_win_colors(config)
-    set_db(args)
-    set_umask(config)
-    set_algorithm(args, config)
-    dbtype = config.get_value("Database", "type")
-    if not dbtype:
-        print("Could not read the Database type from the config!")
-        sys.exit(1)
-
-    return xselpath, dbtype
-
-
-def get_db_version(config, dbtype, args):
-    if os.path.exists(config.get_value("Database", "filename")):
-        dbver = pwman.data.factory.check_db_version(dbtype)
-        if dbver < 0.4 and not args.dbconvert:
-            print(_db_warn)
-    else:
-        dbver = 0.4
-    return dbver
-
 
 
 def auto_convert():
 def auto_convert():
     try:
     try: