Esempio n. 1
0
def extract_target_config_from_path(path, config=None):
    # type: (str, TargetConfig)-> TargetConfig
    config = config or TargetConfig()
    if path is None:
        return config
    if path.endswith("[noflag]"):
        config = config.with_flag(None)
        path = path[:-8]

    if trailing_slash(path):
        # we move to folder mode regardles config
        config = config.as_folder()
        path = os.path.dirname(path)

    path = normpath(path)
    if config.format or config.compression:
        # we don't set "forced" settings
        return config
    path = os.path.basename(path)

    # Infer format and compression from the filename/URL extension
    parts = path.split(".")
    if len(parts) > 2:
        parts = parts[-2:]
    for p in parts:
        # priority to last elements
        # for example  parquet.csv is csv
        format = KNOWN_FORMATS.extension_to_name(p)
        if format:
            config = config.with_format(format)
        else:
            compression = KNOWN_COMPRESSIONS.extension_to_name(p)
            if compression:
                config = config.with_compression(compression)
    return config
Esempio n. 2
0
def target(*path, **kwargs):
    """
    autoresolving function
    :param path:
    :param kwargs:
    :return: FileTarget
    """

    path = [str(p) for p in path]
    path = os.path.join(*path)
    if not path:
        raise DatabandError("Can not convert empty string '%s' to Target" % path)
    if "," in path:
        return MultiTarget(targets=[target(p, **kwargs) for p in path.split(",")])

    fs = kwargs.pop("fs", None)
    config = kwargs.pop("config", None)
    config = extract_target_config_from_path(path, config=config)
    if path.endswith("[noflag]"):
        config = config.with_flag(None)
        path = path[:-8]

    if config.folder and not trailing_slash(path):
        path = "%s%s" % (path, os.path.sep if isinstance(fs, LocalFileSystem) else "/")

    if config.target_factory:
        return config.target_factory(path, fs=fs, config=config)

    if config.folder:
        from targets.dir_target import DirTarget

        return DirTarget(path, fs=fs, config=config)

    return FileTarget(path=path, fs=fs, config=config, **kwargs)