Coverage for frankenstein/cl_utils.py : 54%

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 frankenstein can be found here: http://frankenstein.readthedocs.org
6Usage:
7 frankenstein init
8 frankenstein <pathToTemplate> <pathToDestination> [-s <pathToSettingsFile>]
9 frankenstein -l <pathToTemplate> [-s <pathToSettingsFile>]
11Options:
12 init setup the frankenstein settings file for the first time
13 <pathToTemplate> path the to folder hosting the template files/folders
14 <pathToDestination> path the to directory to write customised template files/folders to
16 -l, --list list the remaining placeholders required by the template after dynamic and settings file placeholders
17 -h, --help show this help message
18 -v, --version show version
19 -s, --settings <pathToSettingsFile> the settings file
20"""
21from __future__ import print_function
22import sys
23import os
24os.environ['TERM'] = 'vt100'
25import readline
26import glob
27import pickle
28from docopt import docopt
29from fundamentals import tools, times
30from subprocess import Popen, PIPE, STDOUT
32def tab_complete(text, state):
33 return (glob.glob(text + '*') + [None])[state]
35def main(arguments=None):
36 """
37 *The main function used when `cl_utils.py` is run as a single script from the cl, or when installed as a cl command*
38 """
39 # setup the command-line util settings
40 su = tools(
41 arguments=arguments,
42 docString=__doc__,
43 logLevel="WARNING",
44 options_first=False,
45 projectName="frankenstein",
46 defaultSettingsFile=True
47 )
48 arguments, settings, log, dbConn = su.setup()
50 # tab completion for raw_input
51 readline.set_completer_delims(' \t\n;')
52 readline.parse_and_bind("tab: complete")
53 readline.set_completer(tab_complete)
55 # UNPACK REMAINING CL ARGUMENTS USING `EXEC` TO SETUP THE VARIABLE NAMES
56 # AUTOMATICALLY
57 a = {}
58 for arg, val in list(arguments.items()):
59 if arg[0] == "-":
60 varname = arg.replace("-", "") + "Flag"
61 else:
62 varname = arg.replace("<", "").replace(">", "")
63 a[varname] = val
64 if arg == "--dbConn":
65 dbConn = val
66 a["dbConn"] = val
67 log.debug('%s = %s' % (varname, val,))
69 listFlag = a["listFlag"]
70 pathToTemplate = a["pathToTemplate"]
71 pathToDestination = a["pathToDestination"]
73 ## START LOGGING ##
74 startTime = times.get_now_sql_datetime()
75 log.info(
76 '--- STARTING TO RUN THE cl_utils.py AT %s' %
77 (startTime,))
79 # set options interactively if user requests
80 if "interactiveFlag" in a and a["interactiveFlag"]:
82 # load previous settings
83 moduleDirectory = os.path.dirname(__file__) + "/resources"
84 pathToPickleFile = "%(moduleDirectory)s/previousSettings.p" % locals()
85 try:
86 with open(pathToPickleFile):
87 pass
88 previousSettingsExist = True
89 except:
90 previousSettingsExist = False
91 previousSettings = {}
92 if previousSettingsExist:
93 previousSettings = pickle.load(open(pathToPickleFile, "rb"))
95 # x-raw-input
96 # x-boolean-raw-input
97 # x-raw-input-with-default-value-from-previous-settings
99 # save the most recently used requests
100 pickleMeObjects = []
101 pickleMe = {}
102 theseLocals = locals()
103 for k in pickleMeObjects:
104 pickleMe[k] = theseLocals[k]
105 pickle.dump(pickleMe, open(pathToPickleFile, "wb"))
107 if a["init"]:
108 from os.path import expanduser
109 home = expanduser("~")
110 filepath = home + "/.config/frankenstein/frankenstein.yaml"
111 try:
112 cmd = """open %(filepath)s""" % locals()
113 p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
114 except:
115 pass
116 try:
117 cmd = """start %(filepath)s""" % locals()
118 p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
119 except:
120 pass
121 return
123 # CALL FUNCTIONS/OBJECTS
124 if not listFlag:
125 electric.electric(
126 log=log,
127 pathToTemplate=pathToTemplate,
128 pathToDestination=pathToDestination,
129 settings=settings
130 ).get()
131 if listFlag:
132 placeHolders = electric.electric(
133 log=log,
134 pathToTemplate=pathToTemplate,
135 pathToDestination=pathToDestination,
136 settings=settings
137 ).list_placeholders()
138 placeHolders = ("\n").join(placeHolders)
139 print(placeHolders)
141 if "dbConn" in locals() and dbConn:
142 dbConn.commit()
143 dbConn.close()
144 ## FINISH LOGGING ##
145 endTime = times.get_now_sql_datetime()
146 runningTime = times.calculate_time_difference(startTime, endTime)
147 log.info('-- FINISHED ATTEMPT TO RUN THE cl_utils.py AT %s (RUNTIME: %s) --' %
148 (endTime, runningTime, ))
150 return
152if __name__ == '__main__':
153 main()