00001
00002
00003 """
00004 @package configure_slimpy
00005 Generate a script SLIMpy can use to update defaults on import
00006 script must be python and contain:
00007 a dict object named 'DEFAULTS'
00008 an integer RC_VERSION
00009
00010 configure_slimpy takes two optional arguments dest and rc_name
00011 the values are a path and a filename respectively.
00012
00013 On import SLIMpy looks for an environment variable SLIMPY_RC,
00014 a file named .slimpy_rc in the current directory or a file
00015 named .slimpy_rc in the users $HOME directory in that order.
00016 The first file that it finds it will use.
00017
00018 An example of generating a script
00019
00020 @code
00021 [bash]$ python configure_slimpy.py [dest=] [rc_name=] [slimpy options]
00022 Wrote file: '/Users/sean/.slimpy_rc'
00023
00024 <edit your script>
00025 @code
00026
00027 @param dest destination of the .slimpy_rc file may be \a global \a local or a path name.
00028 @param rc_name the name of the .slimpy_rc file defaults to ".slimpy_rc"
00029 """
00030
00031 __copyright__ = """
00032 Copyright 2008 Sean Ross-Ross
00033 """
00034 __license__ = """
00035 This file is part of SLIMpy.
00036
00037 SLIMpy is free software: you can redistribute it and/or modify
00038 it under the terms of the GNU Lesser General Public License as published by
00039 the Free Software Foundation, either version 3 of the License, or
00040 (at your option) any later version.
00041
00042 SLIMpy is distributed in the hope that it will be useful,
00043 but WITHOUT ANY WARRANTY; without even the implied warranty of
00044 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00045 GNU Lesser General Public License for more details.
00046
00047 You should have received a copy of the GNU Lesser General Public License
00048 along with SLIMpy. If not, see <http://www.gnu.org/licenses/>.
00049 """
00050
00051
00052 RC_VERSION = 1
00053
00054 from SLIMpy.setup import *
00055
00056 from SLIMpy import env
00057 from os.path import join, split, isdir,exists
00058 from string import Template
00059 import os
00060
00061
00062 def get_rc_filename(dest,rc_name):
00063 """
00064 @param dest destination of the .slimpy_rc file may be \a global \a local or a path name.
00065 @param rc_name the name of the .slimpy_rc file defaults to ".slimpy_rc"
00066 @return a fully formed path name
00067 """
00068 if dest == 'global':
00069 HOME = os.environ.get('HOME',None)
00070 if not HOME:
00071 option_parser.error('coultd not find home enironment')
00072 slim_rcfile = join(HOME,rc_name)
00073 elif dest == 'local':
00074 slim_rcfile = rc_name
00075 else:
00076 assert isdir(dest)
00077 slim_rcfile = join(dest,rc_name)
00078
00079 return slim_rcfile
00080
00081
00082 def check_exists( slim_rcfile ):
00083 """
00084 checks if the rc file already exists and if so
00085 prompt the user if they truly want to over write it
00086 """
00087 if exists( slim_rcfile ):
00088 while 1:
00089 ans = raw_input( "file '%s' exists.\n"
00090 "Are yout sure you want to overwrite? [Y|n]: " %slim_rcfile)
00091 ans = ans.lower()
00092 if ans in ['no','n']:
00093 raise SystemExit( 'goodbye' )
00094 elif ans in ['yes','y','']:
00095 return
00096 else:
00097 print "sorry need 'y' or 'n' answer"
00098
00099 def create_dict_lines( key ,slimpy_rc):
00100 """
00101 Write the current defaults to the SLIMpy rc file with documentation
00102 """
00103 from SLIMpy.setup.DEFAULTS import SYS_DEFAULTS
00104
00105 slimvars = env[ 'slimvars' ]
00106
00107 djoin = lambda key,value: "%s : %s" %( repr(key), repr(value) )
00108 doc = "# " + slimvars.slimdoc.get(key,"No Doc")
00109 doc = doc.replace('\n','\n# ')
00110 value = slimvars[key]
00111 val2 = SYS_DEFAULTS.get(key,"No Default value")
00112
00113 l1 = djoin(key, val2)
00114 l2 = djoin(key, value)
00115
00116 print >> slimpy_rc
00117 print >> slimpy_rc, doc
00118 print >> slimpy_rc, "#"+"="*79
00119 print >> slimpy_rc, '#',l1,','
00120
00121 if SYS_DEFAULTS.get(key, None) == value:
00122 pass
00123 else:
00124 print >> slimpy_rc, l2,','
00125
00126 print >> slimpy_rc, "#"+"="*79
00127
00128 def write_header( slimpy_rc ):
00129 """
00130 Write the preamble to the rc file
00131 """
00132 import SLIMpy,time
00133 print >> slimpy_rc, "'''"
00134 print >> slimpy_rc, "slimpy_rc file:"
00135 print >> slimpy_rc, "Created on",time.strftime( "%a, %b %d, %Y" )
00136 print >> slimpy_rc
00137 print >> slimpy_rc, SLIMpy.__version__,"-",SLIMpy.__date__
00138 print >> slimpy_rc
00139 print >> slimpy_rc, SLIMpy.__license__
00140 print >> slimpy_rc, "'''"
00141 print >> slimpy_rc
00142 print >> slimpy_rc, "RC_VERSION =", RC_VERSION
00143 print >> slimpy_rc
00144
00145 if __name__ == '__main__':
00146
00147 Parameters( 'dest', 'rc_name')
00148 Defaults( dest='global',rc_name='.slimpy_rc' )
00149
00150
00151
00152 pars = parse_args( )
00153
00154 dest = pars['dest']
00155 rc_name = pars['rc_name']
00156
00157 slim_rcfile = get_rc_filename( dest, rc_name )
00158
00159 check_exists( slim_rcfile )
00160
00161 slimpy_rc = open( slim_rcfile, 'w' )
00162
00163 write_header( slimpy_rc )
00164
00165
00166 print >> slimpy_rc, "DEFAULTS = {"
00167 for key in DEFAULTS.DEFAULTS:
00168 create_dict_lines( key, slimpy_rc)
00169
00170 print >> slimpy_rc
00171 print >> slimpy_rc, "}"
00172
00173 print "Wrote file: '%s'" %slim_rcfile
00174