Coverage for khufu/cl_utils.py : 18%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# encoding: utf-8
3"""
4Documentation for khufu can be found here: http://khufu.readthedocs.org
6Usage:
7 khufu init
8 khufu [-s <pathToSettingsFile>]
10Options:
11 init setup the khufu settings file for the first time
12 -h, --help show this help message
13 -v, --version show version
14 -s, --settings <pathToSettingsFile> the settings file
15"""
16import sys
17import os
18os.environ['TERM'] = 'vt100'
19import readline
20import glob
21import pickle
22from docopt import docopt
23from fundamentals import tools, times
24from subprocess import Popen, PIPE, STDOUT
26def tab_complete(text, state):
27 return (glob.glob(text + '*') + [None])[state]
29def main(arguments=None):
30 """
31 *The main function used when `cl_utils.py` is run as a single script from the cl, or when installed as a cl command*
32 """
33 # setup the command-line util settings
34 su = tools(
35 arguments=arguments,
36 docString=__doc__,
37 logLevel="WARNING",
38 options_first=False,
39 projectName="khufu",
40 defaultSettingsFile=True
41 )
42 arguments, settings, log, dbConn = su.setup()
44 # tab completion for raw_input
45 readline.set_completer_delims(' \t\n;')
46 readline.parse_and_bind("tab: complete")
47 readline.set_completer(tab_complete)
49 # UNPACK REMAINING CL ARGUMENTS USING `EXEC` TO SETUP THE VARIABLE NAMES
50 # AUTOMATICALLY
51 a = {}
52 for arg, val in list(arguments.items()):
53 if arg[0] == "-":
54 varname = arg.replace("-", "") + "Flag"
55 else:
56 varname = arg.replace("<", "").replace(">", "")
57 a[varname] = val
58 if arg == "--dbConn":
59 dbConn = val
60 a["dbConn"] = val
61 log.debug('%s = %s' % (varname, val,))
63 ## START LOGGING ##
64 startTime = times.get_now_sql_datetime()
65 log.info(
66 '--- STARTING TO RUN THE cl_utils.py AT %s' %
67 (startTime,))
69 # set options interactively if user requests
70 if "interactiveFlag" in a and a["interactiveFlag"]:
72 # load previous settings
73 moduleDirectory = os.path.dirname(__file__) + "/resources"
74 pathToPickleFile = "%(moduleDirectory)s/previousSettings.p" % locals()
75 try:
76 with open(pathToPickleFile):
77 pass
78 previousSettingsExist = True
79 except:
80 previousSettingsExist = False
81 previousSettings = {}
82 if previousSettingsExist:
83 previousSettings = pickle.load(open(pathToPickleFile, "rb"))
85 # x-raw-input
86 # x-boolean-raw-input
87 # x-raw-input-with-default-value-from-previous-settings
89 # save the most recently used requests
90 pickleMeObjects = []
91 pickleMe = {}
92 theseLocals = locals()
93 for k in pickleMeObjects:
94 pickleMe[k] = theseLocals[k]
95 pickle.dump(pickleMe, open(pathToPickleFile, "wb"))
97 if a["init"]:
98 from os.path import expanduser
99 home = expanduser("~")
100 filepath = home + "/.config/khufu/khufu.yaml"
101 try:
102 cmd = """open %(filepath)s""" % locals()
103 p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
104 except:
105 pass
106 try:
107 cmd = """start %(filepath)s""" % locals()
108 p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
109 except:
110 pass
111 return
113 # CALL FUNCTIONS/OBJECTS
115 if "dbConn" in locals() and dbConn:
116 dbConn.commit()
117 dbConn.close()
118 ## FINISH LOGGING ##
119 endTime = times.get_now_sql_datetime()
120 runningTime = times.calculate_time_difference(startTime, endTime)
121 log.info('-- FINISHED ATTEMPT TO RUN THE cl_utils.py AT %s (RUNTIME: %s) --' %
122 (endTime, runningTime, ))
124 return
126if __name__ == '__main__':
127 main()