Skip to content

Commit

Permalink
dev(narugo): add more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed Dec 25, 2023
1 parent 8a47d5b commit 9f68c65
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 8 deletions.
53 changes: 53 additions & 0 deletions docs/source/api_doc/entry/base.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
hfutils.entry.base
================================

.. currentmodule:: hfutils.entry.base

.. automodule:: hfutils.entry.base


CONTEXT_SETTINGS
--------------------------

.. autodata:: CONTEXT_SETTINGS



ClickWarningException
--------------------------------

.. autoclass:: ClickWarningException
:members: exit_code, show



ClickErrorException
------------------------------------

.. autoclass:: ClickErrorException
:members: exit_code, show



print_exception
----------------------------------

.. autofunction:: print_exception



KeyboardInterrupted
------------------------------------

.. autoclass:: KeyboardInterrupted
:members: __init__, exit_code, show



command_wrap
-------------------------------

.. autofunction:: command_wrap



14 changes: 14 additions & 0 deletions docs/source/api_doc/entry/cli.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
hfutils.entry.cli
================================

.. currentmodule:: hfutils.entry.cli

.. automodule:: hfutils.entry.cli


cli
----------------------

.. autodata:: cli


22 changes: 22 additions & 0 deletions docs/source/api_doc/entry/dispatch.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
hfutils.entry.dispatch
================================

.. currentmodule:: hfutils.entry.dispatch

.. automodule:: hfutils.entry.dispatch


print_version
----------------------------------------

.. autofunction:: print_version



hfutilcli
----------------------------------------

.. autofunction:: hfutilcli



15 changes: 15 additions & 0 deletions docs/source/api_doc/entry/download.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
hfutils.entry.download
================================

.. currentmodule:: hfutils.entry.download

.. automodule:: hfutils.entry.download


NoRemotePathAssignedWithDownload
--------------------------------------------

.. autoclass:: NoRemotePathAssignedWithDownload
:members: exit_code, show


17 changes: 17 additions & 0 deletions docs/source/api_doc/entry/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
hfutils.entry
================================

.. currentmodule:: hfutils.entry

.. automodule:: hfutils.entry


.. toctree::
:maxdepth: 3

base
cli
dispatch
download
upload

15 changes: 15 additions & 0 deletions docs/source/api_doc/entry/upload.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
hfutils.entry.upload
================================

.. currentmodule:: hfutils.entry.upload

.. automodule:: hfutils.entry.upload


NoRemotePathAssignedWithUpload
--------------------------------------

.. autoclass:: NoRemotePathAssignedWithUpload
:members: exit_code, show


1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ configuration file's structure and their versions.

api_doc/archive/index
api_doc/config/index
api_doc/entry/index
api_doc/operate/index
api_doc/utils/index

Expand Down
57 changes: 55 additions & 2 deletions hfutils/entry/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,50 @@


class ClickWarningException(ClickException):
"""
Custom exception class for displaying warnings in yellow color.
:param message: The error message.
:type message: str
"""

def show(self, file: Optional[IO] = None) -> None:
"""
Display the warning message in yellow.
:param file: File to write the output to.
:type file: Optional[IO]
"""
click.secho(self.format_message(), fg='yellow', file=sys.stderr)


class ClickErrorException(ClickException):
"""
Custom exception class for displaying errors in red color.
:param message: The error message.
:type message: str
"""

def show(self, file: Optional[IO] = None) -> None:
"""
Display the error message in red.
:param file: File to write the output to.
:type file: Optional[IO]
"""
click.secho(self.format_message(), fg='red', file=sys.stderr)


# noinspection PyShadowingBuiltins
def print_exception(err: BaseException, print: Optional[Callable] = None):
# noinspection PyShadowingBuiltins
"""
Print exception information, including traceback.
:param err: The exception object.
:type err: BaseException
:param print: Custom print function. If not provided, uses built-in print.
:type print: Optional[Callable]
"""
print = print or builtins.print

lines = list(itertools.chain(*map(
Expand All @@ -47,13 +79,34 @@ def print_exception(err: BaseException, print: Optional[Callable] = None):


class KeyboardInterrupted(ClickWarningException):
"""
Exception class for handling keyboard interruptions.
:param msg: Custom message to display.
:type msg: Optional[str]
"""
exit_code = 0x7

def __init__(self, msg=None):
"""
Initialize the exception.
:param msg: Custom message to display.
:type msg: Optional[str]
"""
ClickWarningException.__init__(self, msg or 'Interrupted.')


def command_wrap():
"""
Decorator for wrapping Click commands.
This decorator catches exceptions and provides consistent error handling.
:return: Decorator function.
:rtype: Callable
"""

def _decorator(func):
@wraps(func)
def _new_func(*args, **kwargs):
Expand Down
36 changes: 33 additions & 3 deletions hfutils/entry/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@
from ..operate.base import REPO_TYPES, RepoTypeTyping


class NoRemotePathAssigned(ClickErrorException):
class NoRemotePathAssignedWithDownload(ClickErrorException):
"""
Custom exception class for indicating that no remote path in the repository is assigned.
"""
exit_code = 0x11


def _add_download_subcommand(cli: click.Group) -> click.Group:
"""
Add the 'download' subcommand to the CLI.
:param cli: The Click CLI application.
:type cli: click.Group
:return: The modified Click CLI application.
:rtype: click.Group
"""

@cli.command('download', help='Download data from HuggingFace.\n\n'
'Set environment $HF_TOKEN to use your own access token.',
context_settings=CONTEXT_SETTINGS)
Expand All @@ -34,9 +46,27 @@ def _add_download_subcommand(cli: click.Group) -> click.Group:
def download(repo_id: str, repo_type: RepoTypeTyping,
file_in_repo: Optional[str], archive_in_repo: Optional[str], dir_in_repo: Optional[str],
output_path: str, revision: str):
"""
Download data from HuggingFace repositories.
:param repo_id: Repository to download from.
:type repo_id: str
:param repo_type: Type of the HuggingFace repository.
:type repo_type: RepoTypeTyping
:param file_in_repo: File in repository to download.
:type file_in_repo: Optional[str]
:param archive_in_repo: Archive file in repository to download and extract from.
:type archive_in_repo: Optional[str]
:param dir_in_repo: Directory in repository to download the full directory tree.
:type dir_in_repo: Optional[str]
:param output_path: Output path for download.
:type output_path: str
:param revision: Revision of repository.
:type revision: str
"""
if not file_in_repo and not archive_in_repo and not dir_in_repo:
raise NoRemotePathAssigned('No remote path in repository assigned.\n'
'One of the -f, -a or -d option is required.')
raise NoRemotePathAssignedWithDownload('No remote path in repository assigned.\n'
'One of the -f, -a, or -d option is required.')

if file_in_repo:
if archive_in_repo:
Expand Down
41 changes: 38 additions & 3 deletions hfutils/entry/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@
from ..operate.base import REPO_TYPES, RepoTypeTyping, get_hf_client


class NoRemotePathAssigned(ClickErrorException):
class NoRemotePathAssignedWithUpload(ClickErrorException):
"""
Custom exception class for indicating that no remote path in the repository is assigned.
"""
exit_code = 0x21


def _add_upload_subcommand(cli: click.Group) -> click.Group:
"""
Add the 'upload' subcommand to the CLI.
:param cli: The Click CLI application.
:type cli: click.Group
:return: The modified Click CLI application.
:rtype: click.Group
"""

@cli.command('upload', help='Upload data from HuggingFace.\n\n'
'Set environment $HF_TOKEN to use your own access token.',
context_settings=CONTEXT_SETTINGS)
Expand All @@ -39,9 +51,32 @@ def _add_upload_subcommand(cli: click.Group) -> click.Group:
def upload(repo_id: str, repo_type: RepoTypeTyping,
file_in_repo: Optional[str], archive_in_repo: Optional[str], dir_in_repo: Optional[str],
input_path: str, revision: str, clear: bool, private: bool):
"""
Upload data to HuggingFace repositories.
:param repo_id: Repository to upload to.
:type repo_id: str
:param repo_type: Type of the HuggingFace repository.
:type repo_type: RepoTypeTyping
:param file_in_repo: File in repository to upload.
:type file_in_repo: Optional[str]
:param archive_in_repo: Archive file in repository to upload and extract from.
:type archive_in_repo: Optional[str]
:param dir_in_repo: Directory in repository to upload the full directory tree.
:type dir_in_repo: Optional[str]
:param input_path: Input path for upload.
:type input_path: str
:param revision: Revision of repository.
:type revision: str
:param clear: Clear the remote directory before uploading.
Only applied when -d is used.
:type clear: bool
:param private: Use private repository when created.
:type private: bool
"""
if not file_in_repo and not archive_in_repo and not dir_in_repo:
raise NoRemotePathAssigned('No remote path in repository assigned.\n'
'One of the -f, -a or -d option is required.')
raise NoRemotePathAssignedWithUpload('No remote path in repository assigned.\n'
'One of the -f, -a, or -d option is required.')

hf_client = get_hf_client()
if not hf_client.repo_exists(repo_id, repo_type=repo_type):
Expand Down

0 comments on commit 9f68c65

Please sign in to comment.