Hide keyboard shortcuts

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 fundamentals can be found here: http://fundamentals.readthedocs.org 

5 

6Usage: 

7 fundamentals init 

8 fundamentals [-s <pathToSettingsFile>]  

9 

10Options: 

11 init setup the fundamentals 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 

25 

26 

27def tab_complete(text, state): 

28 return (glob.glob(text + '*') + [None])[state] 

29 

30 

31def main(arguments=None): 

32 """ 

33 *The main function used when `cl_utils.py` is run as a single script from the cl, or when installed as a cl command* 

34 """ 

35 # setup the command-line util settings 

36 su = tools( 

37 arguments=arguments, 

38 docString=__doc__, 

39 logLevel="WARNING", 

40 options_first=False, 

41 projectName="fundamentals", 

42 defaultSettingsFile=True 

43 ) 

44 arguments, settings, log, dbConn = su.setup() 

45 

46 # tab completion for raw_input 

47 readline.set_completer_delims(' \t\n;') 

48 readline.parse_and_bind("tab: complete") 

49 readline.set_completer(tab_complete) 

50 

51 # UNPACK REMAINING CL ARGUMENTS USING `EXEC` TO SETUP THE VARIABLE NAMES 

52 # AUTOMATICALLY 

53 a = {} 

54 for arg, val in list(arguments.items()): 

55 if arg[0] == "-": 

56 varname = arg.replace("-", "") + "Flag" 

57 else: 

58 varname = arg.replace("<", "").replace(">", "") 

59 a[varname] = val 

60 if arg == "--dbConn": 

61 dbConn = val 

62 a["dbConn"] = val 

63 log.debug('%s = %s' % (varname, val,)) 

64 

65 ## START LOGGING ## 

66 startTime = times.get_now_sql_datetime() 

67 log.info( 

68 '--- STARTING TO RUN THE cl_utils.py AT %s' % 

69 (startTime,)) 

70 

71 # set options interactively if user requests 

72 if "interactiveFlag" in a and a["interactiveFlag"]: 

73 

74 # load previous settings 

75 moduleDirectory = os.path.dirname(__file__) + "/resources" 

76 pathToPickleFile = "%(moduleDirectory)s/previousSettings.p" % locals() 

77 try: 

78 with open(pathToPickleFile): 

79 pass 

80 previousSettingsExist = True 

81 except: 

82 previousSettingsExist = False 

83 previousSettings = {} 

84 if previousSettingsExist: 

85 previousSettings = pickle.load(open(pathToPickleFile, "rb")) 

86 

87 # x-raw-input 

88 # x-boolean-raw-input 

89 # x-raw-input-with-default-value-from-previous-settings 

90 

91 # save the most recently used requests 

92 pickleMeObjects = [] 

93 pickleMe = {} 

94 theseLocals = locals() 

95 for k in pickleMeObjects: 

96 pickleMe[k] = theseLocals[k] 

97 pickle.dump(pickleMe, open(pathToPickleFile, "wb")) 

98 

99 if a["init"]: 

100 from os.path import expanduser 

101 home = expanduser("~") 

102 filepath = home + "/.config/fundamentals/fundamentals.yaml" 

103 try: 

104 cmd = """open %(filepath)s""" % locals() 

105 p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) 

106 except: 

107 pass 

108 try: 

109 cmd = """start %(filepath)s""" % locals() 

110 p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) 

111 except: 

112 pass 

113 return 

114 

115 # CALL FUNCTIONS/OBJECTS 

116 

117 if "dbConn" in locals() and dbConn: 

118 dbConn.commit() 

119 dbConn.close() 

120 ## FINISH LOGGING ## 

121 endTime = times.get_now_sql_datetime() 

122 runningTime = times.calculate_time_difference(startTime, endTime) 

123 log.info('-- FINISHED ATTEMPT TO RUN THE cl_utils.py AT %s (RUNTIME: %s) --' % 

124 (endTime, runningTime, )) 

125 

126 return 

127 

128if __name__ == '__main__': 

129 main()