#NEWLINE# def update( globalvars ) : #NEWLINE# #INDENT# '\n    Update the profile\n    ' #NEWLINE# global config #NEWLINE# profileini = getprofileini ( ) #NEWLINE# config = configparser . ConfigParser ( ) #NEWLINE# config . read ( profileini ) #NEWLINE# defaults = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# if ( globalvars is not None ) : #NEWLINE# #INDENT# defaults = { a [ 0 ] : a [ 1 ] for a in globalvars } #NEWLINE# #UNINDENT# generic_configs = [ { #NEWLINE# #INDENT# 'name' : 'User' , #NEWLINE# 'nature' : 'generic' , #NEWLINE# 'description' : 'General information' , #NEWLINE# 'variables' : [ 'user.email' , 'user.name' , 'user.fullname' ] , #NEWLINE# 'defaults' : { #NEWLINE# #INDENT# 'user.email' : { #NEWLINE# #INDENT# 'value' : defaults . get ( 'user.email' , '' ) , #NEWLINE# 'description' : 'Email address' , #NEWLINE# 'validator' : EmailValidator ( ) , #NEWLINE# #UNINDENT# } , #NEWLINE# 'user.fullname' : { #NEWLINE# #INDENT# 'value' : defaults . get ( 'user.fullname' , '' ) , #NEWLINE# 'description' : 'Full Name' , #NEWLINE# 'validator' : NonEmptyValidator ( ) , #NEWLINE# #UNINDENT# } , #NEWLINE# 'user.name' : { #NEWLINE# #INDENT# 'value' : defaults . get ( 'user.name' , getpass . getuser ( ) ) , #NEWLINE# 'description' : 'Name' , #NEWLINE# 'validator' : NonEmptyValidator ( ) , #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } ] #NEWLINE# mgr = plugins_get_mgr ( ) #NEWLINE# extra_configs = mgr . gather_configs ( ) #NEWLINE# allconfigs = ( generic_configs + extra_configs ) #NEWLINE# for c in allconfigs : #NEWLINE# #INDENT# name = c [ 'name' ] #NEWLINE# for v in c [ 'variables' ] : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# c [ 'defaults' ] [ v ] [ 'value' ] = config [ name ] [ v ] #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for c in allconfigs : #NEWLINE# #INDENT# print ( '' ) #NEWLINE# print ( c [ 'description' ] ) #NEWLINE# print ( '==================' ) #NEWLINE# if ( len ( c [ 'variables' ] ) == 0 ) : #NEWLINE# #INDENT# print ( 'Nothing to do. Enabled by default' ) #NEWLINE# continue #NEWLINE# #UNINDENT# name = c [ 'name' ] #NEWLINE# config [ name ] = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# config [ name ] [ 'nature' ] = c [ 'nature' ] #NEWLINE# for v in c [ 'variables' ] : #NEWLINE# #INDENT# value = '' #NEWLINE# description = ( v + ' ' ) #NEWLINE# helptext = '' #NEWLINE# validator = None #NEWLINE# if ( v in c [ 'defaults' ] ) : #NEWLINE# #INDENT# value = c [ 'defaults' ] [ v ] . get ( 'value' , '' ) #NEWLINE# helptext = c [ 'defaults' ] [ v ] . get ( 'description' , '' ) #NEWLINE# validator = c [ 'defaults' ] [ v ] . get ( 'validator' , None ) #NEWLINE# #UNINDENT# if ( helptext != '' ) : #NEWLINE# #INDENT# description += ( ( '(' + helptext ) + ')' ) #NEWLINE# #UNINDENT# while True : #NEWLINE# #INDENT# choice = input_with_default ( description , value ) #NEWLINE# if ( validator is not None ) : #NEWLINE# #INDENT# if validator . is_valid ( choice ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print ( 'Invalid input. Expected input is {}' . format ( validator . message ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# config [ name ] [ v ] = choice #NEWLINE# if ( ( v == 'enable' ) and ( choice == 'n' ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# with open ( profileini , 'w' ) as fd : #NEWLINE# #INDENT# config . write ( fd ) #NEWLINE# #UNINDENT# print ( 'Updated profile file:' , config )