Exemple #1
0
def cmd_config(argv, path_to_tx, is_legacy=False):
    """Add local or remote files under Transifex"""
    from_wizard = False
    if len(argv) == 0:
        # since interactive wizard should be equivalent to auto-local
        # subcommand there are some default options that need to be set
        default_options = {
            'execute': True,
            'subcommand': MAPPING,
            'minimum_perc': 0,
            'mode': None,
            'expression_legacy': None,
        }
        try:
            # Run the wizard and configure parse with the wizard inputs
            wizard_options = Wizard(path_to_tx).run()
            wizard_options.update(default_options)
            options = Namespace(**wizard_options)
            parser = set_parser(is_legacy=is_legacy)
            from_wizard = True
        except SystemExit:
            print("\n")
            sys.exit(1)
    else:
        # hack to support backwards compatibility
        # for --auto-local and --auto-remote options
        if '--auto-local' in argv:
            argv.pop(argv.index('--auto-local'))
            argv.insert(0, MAPPING)
        if '--auto-remote' in argv:
            argv.pop(argv.index('--auto-remote'))
            argv.insert(0, MAPPINGREMOTE)

        is_subcommand = argv[0] in CONFIG_SUBCOMMANDS.keys()
        parser = set_parser(subparser=is_subcommand, is_legacy=is_legacy)
        options = parser.parse_args(argv)

    _validate_set_arguments(parser, options)

    if not hasattr(options, 'subcommand'):
        bare_set(path_to_tx, options)
    else:
        try:
            CONFIG_SUBCOMMANDS[options.subcommand](
                path_to_tx, options, from_wizard=from_wizard
            )
        except KeyError:
            parser.print_help()
            sys.exit(2)
def cmd_set(argv, path_to_tx):
    "Add local or remote files under transifex"
    parser = set_parser()
    (options, args) = parser.parse_args(argv)

    # Implement options/args checks
    # TODO !!!!!!!
    if options.local:
        try:
            expression = args[0]
        except IndexError:
            parser.error("Please specify an expression.")
        if not options.resource:
            parser.error("Please specify a resource")
        if not options.source_language:
            parser.error("Please specify a source language.")
        if not '<lang>' in expression:
            parser.error("The expression you have provided is not valid.")
        if not utils.valid_slug(options.resource):
            parser.error("Invalid resource slug. The format is <project_slug>"\
                ".<resource_slug> and the valid characters include [_-\w].")
        _auto_local(path_to_tx, options.resource,
            source_language=options.source_language,
            expression = expression, source_file=options.source_file,
            execute=options.execute, regex=False)
        if options.execute:
            _set_minimum_perc(options.resource, options.minimum_perc, path_to_tx)
            _set_mode(options.resource, options.mode, path_to_tx)
            _set_type(options.resource, options.i18n_type, path_to_tx)
        return

    if options.remote:
        try:
            url = args[0]
        except IndexError:
            parser.error("Please specify an remote url")
        _auto_remote(path_to_tx, url)
        _set_minimum_perc(options.resource, options.minimum_perc, path_to_tx)
        _set_mode(options.resource, options.mode, path_to_tx)
        return

    if options.is_source:
        resource = options.resource
        if not resource:
            parser.error("You must specify a resource name with the"
                " -r|--resource flag.")

        lang = options.language
        if not lang:
            parser.error("Please specify a source language.")

        if len(args) != 1:
            parser.error("Please specify a file.")

        if not utils.valid_slug(resource):
            parser.error("Invalid resource slug. The format is <project_slug>"\
                ".<resource_slug> and the valid characters include [_-\w].")

        file = args[0]
        # Calculate relative path
        path_to_file = relpath(file, path_to_tx)
        _set_source_file(path_to_tx, resource, options.language, path_to_file)
    elif options.resource or options.language:
        resource = options.resource
        lang = options.language

        if len(args) != 1:
            parser.error("Please specify a file")

        # Calculate relative path
        path_to_file = relpath(args[0], path_to_tx)

        try:
            _go_to_dir(path_to_tx)
        except UnInitializedError, e:
            utils.logger.error(e)
            return

        if not utils.valid_slug(resource):
            parser.error("Invalid resource slug. The format is <project_slug>"\
                ".<resource_slug> and the valid characters include [_-\w].")
        _set_translation(path_to_tx, resource, lang, path_to_file)
Exemple #3
0
def cmd_set(argv, path_to_tx):
    "Add local or remote files under transifex"
    parser = set_parser()
    (options, args) = parser.parse_args(argv)

    # Implement options/args checks
    # TODO !!!!!!!
    if options.local:
        try:
            expression = args[0]
        except IndexError:
            parser.error("Please specify an expression.")
        if not options.resource:
            parser.error("Please specify a resource")
        if not options.source_language:
            parser.error("Please specify a source language.")
        if not '<lang>' in expression:
            parser.error("The expression you have provided is not valid.")
        if not utils.valid_slug(options.resource):
            parser.error("Invalid resource slug. The format is <project_slug>"\
                ".<resource_slug> and the valid characters include [_-\w].")
        _auto_local(path_to_tx,
                    options.resource,
                    source_language=options.source_language,
                    expression=expression,
                    source_file=options.source_file,
                    execute=options.execute,
                    regex=False)
        if options.execute:
            _set_minimum_perc(options.resource, options.minimum_perc,
                              path_to_tx)
            _set_mode(options.resource, options.mode, path_to_tx)
            _set_type(options.resource, options.i18n_type, path_to_tx)
        return

    if options.remote:
        try:
            url = args[0]
        except IndexError:
            parser.error("Please specify an remote url")
        _auto_remote(path_to_tx, url)
        _set_minimum_perc(options.resource, options.minimum_perc, path_to_tx)
        _set_mode(options.resource, options.mode, path_to_tx)
        return

    if options.is_source:
        resource = options.resource
        if not resource:
            parser.error("You must specify a resource name with the"
                         " -r|--resource flag.")

        lang = options.language
        if not lang:
            parser.error("Please specify a source language.")

        if len(args) != 1:
            parser.error("Please specify a file.")

        if not utils.valid_slug(resource):
            parser.error("Invalid resource slug. The format is <project_slug>"\
                ".<resource_slug> and the valid characters include [_-\w].")

        file = args[0]
        # Calculate relative path
        path_to_file = relpath(file, path_to_tx)
        _set_source_file(path_to_tx, resource, options.language, path_to_file)
    elif options.resource or options.language:
        resource = options.resource
        lang = options.language

        if len(args) != 1:
            parser.error("Please specify a file")

        # Calculate relative path
        path_to_file = relpath(args[0], path_to_tx)

        try:
            _go_to_dir(path_to_tx)
        except UnInitializedError, e:
            utils.logger.error(e)
            return

        if not utils.valid_slug(resource):
            parser.error("Invalid resource slug. The format is <project_slug>"\
                ".<resource_slug> and the valid characters include [_-\w].")
        _set_translation(path_to_tx, resource, lang, path_to_file)