<s> from click import confirm , prompt , UsageError <EOL> import xdg <EOL> from functools import partial <EOL> from os . path import expanduser , expandvars , join , normpath , split , exists , isdir <EOL> from os import makedirs <EOL> from datetime import date , datetime <EOL> import sys <EOL> from khal . log import logger <EOL> from . settings import settings <EOL> def validate_int ( input , min_value , max_value ) : <EOL> try : <EOL> number = int ( input ) <EOL> except ValueError : <EOL> raise UsageError ( '<STR_LIT>' ) <EOL> if min_value <= number <= max_value : <EOL> return number <EOL> else : <EOL> raise UsageError ( '<STR_LIT>' . format ( min_value , max_value ) ) <EOL> def choose_datetime_format ( ) : <EOL> while True : <EOL> ordering_choices = [ <EOL> ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> ] <EOL> separator_choices = [ '<STR_LIT:->' , '<STR_LIT:.>' , '<STR_LIT:/>' ] <EOL> validate = partial ( validate_int , min_value = <NUM_LIT:0> , max_value = <NUM_LIT:2> ) <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> print ( '<STR_LIT:\n>' . join ( <EOL> [ '<STR_LIT>' . format ( num , one ) for num , ( one , _ ) in enumerate ( ordering_choices ) ] ) ) <EOL> ordering_no = prompt ( "<STR_LIT>" , value_proc = validate ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( '<STR_LIT:\n>' . join ( [ '<STR_LIT>' . format ( num , one ) for num , one in enumerate ( separator_choices ) ] ) ) <EOL> prompt_text = "<STR_LIT>" <EOL> separator = separator_choices [ prompt ( prompt_text , value_proc = validate ) ] <EOL> dateformat = separator . join ( ordering_choices [ ordering_no ] [ <NUM_LIT:1> ] ) <EOL> today = date . today ( ) <EOL> text = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( dateformat , today . strftime ( dateformat ) ) ) <EOL> if confirm ( text ) : <EOL> break <EOL> return dateformat <EOL> def choose_time_format ( ) : <EOL> choices = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> while True : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> validate = partial ( validate_int , min_value = <NUM_LIT:0> , max_value = <NUM_LIT:1> ) <EOL> prompt_text = "<STR_LIT>" <EOL> timeformat = choices [ prompt ( prompt_text , default = <NUM_LIT:0> , value_proc = validate ) ] <EOL> now = datetime . now ( ) <EOL> text = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( timeformat , now . strftime ( timeformat ) ) ) <EOL> if confirm ( text ) : <EOL> break <EOL> return timeformat <EOL> def find_vdirs ( ) : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not confirm ( "<STR_LIT>" , default = '<STR_LIT:y>' ) : <EOL> return None <EOL> try : <EOL> from vdirsyncer . cli import config <EOL> except ImportError : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return None <EOL> vdir_config = config . load_config ( ) <EOL> vdirs = list ( ) <EOL> for storage in vdir_config . storages . values ( ) : <EOL> if storage [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> vdirs . append ( ( storage [ '<STR_LIT>' ] , storage [ '<STR_LIT:path>' ] , '<STR_LIT>' ) ) <EOL> if vdirs == list ( ) : <EOL> print ( "<STR_LIT>" ) <EOL> return None <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> for name , path , _ in vdirs : <EOL> print ( '<STR_LIT>' . format ( name , path ) ) <EOL> return vdirs <EOL> def create_vdir ( names = [ ] ) : <EOL> if not confirm ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) : <EOL> return None <EOL> name = '<STR_LIT>' <EOL> while True : <EOL> path = join ( xdg . BaseDirectory . xdg_data_home , '<STR_LIT>' , '<STR_LIT>' , name ) <EOL> path = normpath ( expanduser ( expandvars ( path ) ) ) <EOL> if name not in names and not exists ( path ) : <EOL> break <EOL> else : <EOL> name += '<STR_LIT:1>' <EOL> try : <EOL> dirpath = split ( path ) [ <NUM_LIT:0> ] <EOL> if not exists ( dirpath ) and not isdir ( dirpath ) : <EOL> makedirs ( dirpath ) <EOL> except OSError as error : <EOL> print ( "<STR_LIT>" . format ( dirpath , error ) ) <EOL> return None <EOL> else : <EOL> return [ ( name , path , '<STR_LIT>' ) ] <EOL> def configwizard ( dry_run = False ) : <EOL> config_file = settings . find_configuration_file ( ) <EOL> if not dry_run and config_file is not None : <EOL> logger . fatal ( "<STR_LIT>" . format ( config_file ) ) <EOL> logger . fatal ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> dateformat = choose_datetime_format ( ) <EOL> print ( ) <EOL> timeformat = choose_time_format ( ) <EOL> print ( ) <EOL> vdirs = find_vdirs ( ) <EOL> print ( ) <EOL> if not vdirs : <EOL> vdirs = create_vdir ( ) <EOL> print ( ) <EOL> calendars = '<STR_LIT>' <EOL> for name , path , type_ in vdirs : <EOL> calendars += '''<STR_LIT>''' . format ( name = name , path = path , type = type_ ) <EOL> locale = '''<STR_LIT>''' . format ( <EOL> timeformat = timeformat , <EOL> dateformat = dateformat , <EOL> longdateformat = dateformat , <EOL> ) <EOL> config = '<STR_LIT:\n>' . join ( [ calendars , locale ] ) <EOL> config_path = join ( xdg . BaseDirectory . xdg_config_home , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not confirm ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( config_path ) ) : <EOL> print ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if dry_run : <EOL> print ( config ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> with open ( config_path , '<STR_LIT:w>' ) as config_file : <EOL> config_file . write ( config ) <EOL> print ( "<STR_LIT>" . format ( config_path ) ) </s>
