Skip to content

trading_agent.extensions.source_fetcher

Turns a folder, a zip file or a git URL into a local folder, for imports and installs.

Both trading-agent skills import and trading-agent packages install accept the same three kinds of source, so this one class fetches them.

Typical usage example:

with tempfile.TemporaryDirectory() as scratch: folder = SourceFetcher().local_folder('https://github.com/someone/skills.git', Path(scratch))

SourceError

Bases: RuntimeError

The source does not exist, or cannot be cloned or unpacked.

SourceFetcher

Fetches import sources.

local_folder

local_folder(source, scratch)

Turns a source into a local folder.

Parameters:

Name Type Description Default
source str

A folder, a .zip file, or a git URL.

required
scratch Path

A temporary folder for unpacking or cloning, which the caller deletes.

required

Returns:

Type Description
Path

The folder, which for a local folder is the folder itself.

Raises:

Type Description
SourceError

The source cannot be used.

Source code in src/trading_agent/extensions/source_fetcher.py
def local_folder(self, source: str, scratch: Path) -> Path:
    """Turns a source into a local folder.

    Args:
        source (str): A folder, a .zip file, or a git URL.
        scratch (Path): A temporary folder for unpacking or cloning, which the caller deletes.

    Returns:
        Path: The folder, which for a local folder is the folder itself.

    Raises:
        SourceError: The source cannot be used.
    """
    if self.is_git_url(source):
        return self._clone(source, scratch / 'clone')
    path = Path(source).expanduser()
    if path.is_file() and path.suffix == '.zip':
        return self._unzip(path, scratch / 'unzipped')
    if path.is_dir():
        return path
    raise SourceError(f'{source} is not a folder, a .zip file or a git URL')

is_git_url

is_git_url(source)

Says whether a source is a git URL.

Parameters:

Name Type Description Default
source str

The source.

required

Returns:

Type Description
bool

True for http, https, ssh and git@ addresses.

Source code in src/trading_agent/extensions/source_fetcher.py
def is_git_url(self, source: str) -> bool:
    """Says whether a source is a git URL.

    Args:
        source (str): The source.

    Returns:
        bool: True for http, https, ssh and git@ addresses.
    """
    return source.startswith(_GIT_PREFIXES)