Skip to content

Commit

Permalink
fix things from review
Browse files Browse the repository at this point in the history
  • Loading branch information
bleudev committed Jun 29, 2024
1 parent 190de66 commit ed6fa86
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
16 changes: 8 additions & 8 deletions examples/github/download.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Github / Download
# GitHub / Download

## Introduction

In `ufpy` there are 3 functions and 1 class for downloading things from public GitHub repositories.
You can without GitHub token download all repository, folder or several folders, and file or several
files. You can use `UGithubDownloader` class for all this actions or other 3 functions. Class is more
optimized for multi-requesting. If you want to download anything several times: use it. He is using 1]
unpacked zip archive for all operations and by the end deletes it. If you don't want to download anything
You can download an entire repository, a folder or multiple folders, and a file or multiple files without
a GitHub token. You can use the `UGithubDownloader` class for all these actions or the other 3 functions.
The class is more optimized for multiple requests. If you need to download multiple times, use it. It uses
an unpacked zip archive for all operations and deletes it at the end. If you don't want to download anything
several times -> use functions.

Import functions and class from `ufpy`:
Expand All @@ -24,8 +24,8 @@ For opening this class you should use `with` operator as you do with files and o
```python
with UGithubDownloader("honey-team/ufpy", "C:/Ufpy-Test", "0.1") as gd:
# First argument - "repository owner"/"repository name"
# Second - Base download path (in all methods is using download paths from base download path. How in cmd
# for example: base path: C:\; cd ufpy -> final path: C:\ufpy.) (default is cwd (current working directory)
# Second - Base download path (all methods use download paths relative to the base download path, similar to how it works in the command line.
# For example: base path: C:\; cd ufpy -> final path: C:\ufpy.) (default is the current working directory)
# Third argument: Branch or tag name (default is "main" (not master!))
gd.download_repo() # In C:/Ufpy-Test will appear all files from 0.1 tag in this repository.
gd.download_repo("ufpy-0.1") # In C:/Ufpy-Test/ufpy-0.1 will appear all files from 0.1 tag in this repository
Expand All @@ -38,7 +38,7 @@ You can use `download_file()` function, `ufpy.github.download.file()` function

> [!NOTE]
> You can use any iterable of strings in `download_file()` function for downloading several files.
> In `UGithubDownloader` there are `download_files()` method.
> In `UGithubDownloader` there is a `download_files()` method.
One file:
```python
Expand Down
13 changes: 3 additions & 10 deletions ufpy/github/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from zipfile import ZipFile

from requests import get
from requests.exceptions import RequestException

from ufpy.path import UOpen

Expand Down Expand Up @@ -68,8 +67,8 @@ def __enter__(self):
r = get(url)

if not r.ok:
raise RequestException(
"Error with getting file from GitHub. Check that repo is public and that file path is correct.")
r.raise_for_status()

self.__zip = ZipFile(io.BytesIO(r.content))

temp_dir = format_paths(gettempdir())
Expand All @@ -83,11 +82,6 @@ def __exit__(self, exc_type, exc_val, exc_tb):
if os.path.exists(self.__repo_path):
rmtree(self.__repo_path)

def __del__(self):
self.__zip.close()
if os.path.exists(self.__repo_path):
rmtree(self.__repo_path)

def download_file(self, file_path: str, download_path: str = ''):
file_path, download_path = format_paths(file_path, download_path)
download_path = f'{self.__base_download_path}/{download_path}'
Expand All @@ -96,8 +90,7 @@ def download_file(self, file_path: str, download_path: str = ''):
r = get(url)

if not r.ok:
raise RequestException(
"Error with getting file from GitHub. Check that repo is public and that file path is correct.")
r.raise_for_status()

path = f'{download_path}/{file_path}'

Expand Down
4 changes: 0 additions & 4 deletions ufpy/path/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ def __enter__(self):
def __exit__(self, exc_type, exc_val, exc_tb):
self.__f.close()

def __del__(self):
if self.__f and not self.__f.closed:
self.__f.close()

def write(self, data: AnyStr):
self.__f.write(data)

Expand Down

0 comments on commit ed6fa86

Please sign in to comment.