def create_config_file(filename):
    """
    Create main configuration file if it doesn't exist.
    """
    import textwrap
    from six.moves.urllib import parse

    if not os.path.exists(filename):
        old_default_config_file = os.path.join(os.path.dirname(filename),
                                               '.tksrc')
        if os.path.exists(old_default_config_file):
            upgrade = click.confirm("\n".join(textwrap.wrap(
                "It looks like you recently updated Taxi. Some "
                "configuration changes are required. You can either let "
                "me upgrade your configuration file or do it "
                "manually.")) + "\n\nProceed with automatic configuration "
                "file upgrade?", default=True
            )

            if upgrade:
                settings = Settings(old_default_config_file)
                settings.convert_to_4()
                with open(filename, 'w') as config_file:
                    settings.config.write(config_file)
                os.remove(old_default_config_file)
                return
            else:
                print("Ok then.")
                sys.exit(0)

        welcome_msg = "Welcome to Taxi!"
        click.secho(welcome_msg, fg='green', bold=True)
        click.secho('=' * len(welcome_msg) + '\n', fg='green', bold=True)

        click.echo(click.wrap_text(
            "It looks like this is the first time you run Taxi. You will need "
            "a configuration file ({}) in order to proceed. Please answer a "
            "few questions to create your configuration file.".format(
                filename
            )
        ) + '\n')

        config = pkg_resources.resource_string('taxi', 'etc/taxirc.sample').decode('utf-8')
        context = {}
        available_backends = plugins_registry.get_available_backends()

        context['backend'] = click.prompt(
            "Backend you want to use (choices are %s)" %
            ', '.join(available_backends),
            type=click.Choice(available_backends)
        )
        context['username'] = click.prompt("Username or token")
        context['password'] = parse.quote(
            click.prompt("Password (leave empty if you're using"
                         " a token)", hide_input=True, default=''),
            safe=''
        )
        # Password can be empty in case of token auth so the ':' separator
        # is not included in the template config, so we add it if the user
        # has set a password
        if context['password']:
            context['password'] = ':' + context['password']

        context['hostname'] = click.prompt(
            "Hostname of the backend (eg. timesheets.example.com)",
            type=Hostname()
        )

        editor = Editor().get_editor()
        context['editor'] = click.prompt(
            "Editor command to edit your timesheets", default=editor
        )

        templated_config = config.format(**context)

        directory = os.path.dirname(filename)
        if not os.path.exists(directory):
            os.makedirs(directory)

        with open(filename, 'w') as f:
            f.write(templated_config)
    else:
        settings = Settings(filename)
        conversions = settings.needed_conversions

        if conversions:
            for conversion in conversions:
                conversion()

            settings.write_config()
def create_config_file ( filename ) : import textwrap from six . moves . urllib import parse if not os . path . exists ( filename ) : old_default_config_file = os . path . join ( os . path . dirname ( filename ) , '.tksrc' ) if os . path . exists ( old_default_config_file ) : upgrade = click . confirm ( "\n" . join ( textwrap . wrap ( "It looks like you recently updated Taxi. Some " "configuration changes are required. You can either let " "me upgrade your configuration file or do it " "manually." ) ) + "\n\nProceed with automatic configuration " "file upgrade?" , default = True ) if upgrade : settings = Settings ( old_default_config_file ) settings . convert_to_4 ( ) with open ( filename , 'w' ) as config_file : settings . config . write ( config_file ) os . remove ( old_default_config_file ) return else : print ( "Ok then." ) sys . exit ( 0 ) welcome_msg = "Welcome to Taxi!" click . secho ( welcome_msg , fg = 'green' , bold = True ) click . secho ( '=' * len ( welcome_msg ) + '\n' , fg = 'green' , bold = True ) click . echo ( click . wrap_text ( "It looks like this is the first time you run Taxi. You will need " "a configuration file ({}) in order to proceed. Please answer a " "few questions to create your configuration file." . format ( filename ) ) + '\n' ) config = pkg_resources . resource_string ( 'taxi' , 'etc/taxirc.sample' ) . decode ( 'utf-8' ) context = { } available_backends = plugins_registry . get_available_backends ( ) context [ 'backend' ] = click . prompt ( "Backend you want to use (choices are %s)" % ', ' . join ( available_backends ) , type = click . Choice ( available_backends ) ) context [ 'username' ] = click . prompt ( "Username or token" ) context [ 'password' ] = parse . quote ( click . prompt ( "Password (leave empty if you're using" " a token)" , hide_input = True , default = '' ) , safe = '' ) if context [ 'password' ] : context [ 'password' ] = ':' + context [ 'password' ] context [ 'hostname' ] = click . prompt ( "Hostname of the backend (eg. timesheets.example.com)" , type = Hostname ( ) ) editor = Editor ( ) . get_editor ( ) context [ 'editor' ] = click . prompt ( "Editor command to edit your timesheets" , default = editor ) templated_config = config . format ( ** context ) directory = os . path . dirname ( filename ) if not os . path . exists ( directory ) : os . makedirs ( directory ) with open ( filename , 'w' ) as f : f . write ( templated_config ) else : settings = Settings ( filename ) conversions = settings . needed_conversions if conversions : for conversion in conversions : conversion ( ) settings . write_config ( )
Create main configuration file if it doesn't exist.