Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UGithubDownloader + some functions and methods for downloading files and folders from repo or downloading all repo. #37

Merged
merged 31 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
190c6a6
download_file()
bleudev Jun 27, 2024
4f49f9d
download_repo()
bleudev Jun 27, 2024
3c3c65b
download_folder()
bleudev Jun 28, 2024
38877d6
several folders to download
bleudev Jun 28, 2024
ab583e3
Fix things from review
bleudev Jun 28, 2024
5f903d8
rewrite with "with": file
bleudev Jun 28, 2024
80f6f49
download_files
bleudev Jun 28, 2024
24776f2
download_repo()
bleudev Jun 28, 2024
12f6fa1
Fixes + download_folder(s)()
bleudev Jun 28, 2024
1022a76
fix things from review
bleudev Jun 28, 2024
651a68a
fix things from review №2
bleudev Jun 28, 2024
1874753
fix things from review №3
bleudev Jun 28, 2024
fe5599e
fix things from review №4
bleudev Jun 28, 2024
f1edaed
Examples №1
bleudev Jun 28, 2024
7b7d0c4
Examples №2 final
bleudev Jun 29, 2024
3950575
Add images to example
bleudev Jun 29, 2024
5474911
Add link to pull request
bleudev Jun 29, 2024
eefb6ea
tests №1
bleudev Jun 29, 2024
991dd33
fix tests
bleudev Jun 29, 2024
1ecb12c
fix tests №2
bleudev Jun 29, 2024
89261f1
fix tests №3
bleudev Jun 29, 2024
e3e0209
fix tests №4
bleudev Jun 29, 2024
fb4be18
fix tests №5
bleudev Jun 29, 2024
190de66
delete tests
bleudev Jun 29, 2024
ed6fa86
fix things from review
bleudev Jun 29, 2024
14cf541
fix things from review №2
bleudev Jun 29, 2024
44441dd
fix things from review №3
bleudev Jun 29, 2024
0132289
fix things from review №4
bleudev Jun 29, 2024
edd5dfb
fix things from review №5
bleudev Jun 29, 2024
5839eaa
fix things from review №6
bleudev Jun 29, 2024
e651a4e
fix things from review №7
bleudev Jun 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests~=2.31.0
11 changes: 9 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
long_description = mdf.read()

install_requires = [

'requests~=2.31.0',
bleudev marked this conversation as resolved.
Show resolved Hide resolved
]

organization_name = 'honey-team'
author, author_email = 'bleudev', '[email protected]'
project_name = 'ufpy'
github_url = f'https://github.com/{organization_name}/{project_name}'

def package(name: str) -> str:
return f'{project_name}.{name}'
bleudev marked this conversation as resolved.
Show resolved Hide resolved

setup(
name=project_name,
version=__version__,
Expand All @@ -24,7 +27,11 @@
long_description=long_description,
long_description_content_type='text/markdown',
url=github_url,
packages=[project_name, f'{project_name}.typ'],
packages=[
project_name,
package('typ'),
package('github'),
],
classifiers=[
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.12',
Expand Down
16 changes: 12 additions & 4 deletions ufpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
__version__ = '0.1.2'

# Typing package
from ufpy import typ
from ufpy.cmp import *
bleudev marked this conversation as resolved.
Show resolved Hide resolved
bleudev marked this conversation as resolved.
Show resolved Hide resolved
from ufpy.math_op import *
from ufpy.path_tools import *
from ufpy.typ.protocols import *
from ufpy.typ.type_alias import *
from ufpy.udict import *
from ufpy.ustack import *
from ufpy.utils import *

# Typing package
__typ_version__ = '0.1'
bleudev marked this conversation as resolved.
Show resolved Hide resolved
bleudev marked this conversation as resolved.
Show resolved Hide resolved
bleudev marked this conversation as resolved.
Show resolved Hide resolved
bleudev marked this conversation as resolved.
Show resolved Hide resolved
bleudev marked this conversation as resolved.
Show resolved Hide resolved
bleudev marked this conversation as resolved.
Show resolved Hide resolved
from ufpy import typ
from ufpy.typ.protocols import *
from ufpy.typ.type_alias import *

# Github package
__github_version__ = '0.1'
from ufpy import github
from ufpy.github.download import UGithubDownloader
from ufpy.github import download_file, download_folder, download_repo, download
3 changes: 3 additions & 0 deletions ufpy/github/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .download import file as download_file
from .download import folder as download_folder
from .download import repo as download_repo
bleudev marked this conversation as resolved.
Show resolved Hide resolved
91 changes: 91 additions & 0 deletions ufpy/github/download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import os
from shutil import copy, copytree, rmtree
from zipfile import ZipFile

from requests import get

__all__ = (
'file',
'folder',
'repo',
'UGithubDownloader',
)

def file(repo: str, file_path: str, download_path: str, branch_name: str = 'main'):
bleudev marked this conversation as resolved.
Show resolved Hide resolved
file_path = file_path.replace('\\', '/')

url = f'https://raw.githubusercontent.com/{repo}/{branch_name}/{file_path}'
r = get(url)

download_path = download_path.replace('\\', '/')
path = f'{download_path}/{file_path}'

directory = '/'.join(path.split('/')[:-1])

if not os.path.exists(directory):
os.makedirs(directory)

with open(path, 'w+') as f:
f.write(r.text)

def folder(repo: str, folder_path: str, download_path: str, branch_name: str = 'main'):
...

def repo(repo: str, download_path: str, branch_name: str = 'main'):
filename = f'{download_path}/repo_temp.zip'
url = f'https://github.com/{repo}/archive/{branch_name}.zip'

r = get(url)

with open(filename, 'wb') as f:
f.write(r.content)

repo_name = repo.split('/')[-1]
main_directory_name = f'{repo_name}-{branch_name}'

with ZipFile(filename) as archive:
for file in archive.namelist():
if file.startswith(main_directory_name):
archive.extract(file, download_path)

if os.path.exists(filename):
os.remove(filename)

repo_dir = f'{download_path}/{main_directory_name}'
for file in os.listdir(repo_dir):
file_path = f'{repo_dir}/{file}'
new_file_path = f'{download_path}/{file}'
if os.path.isdir(file_path):
bleudev marked this conversation as resolved.
Show resolved Hide resolved
if os.path.exists(new_file_path):
print(
f"Warning ({new_file_path}): Currently we don't support editing recursive folders if it's exists"
"when repo directory was downloaded. Sorry, you can just delete all folders with same name as in"
"repo before you use this function instead."
)
continue
copytree(file_path, new_file_path)
else:
if os.path.exists(new_file_path):
with open(new_file_path, 'w') as nf:
with open(file_path, 'r') as f:
nf.write(f.read())
else:
copy(file_path, new_file_path)

if os.path.exists(f'{download_path}/{main_directory_name}'):
rmtree(f'{download_path}/{main_directory_name}')


class UGithubDownloader:
def __init__(self, repo: str, branch_name: str = 'main'):
self.__repo = repo
self.__branch = branch_name

def download_file(self, file_path: str, download_path: str):
file(self.__repo, file_path, download_path, self.__branch)
bleudev marked this conversation as resolved.
Show resolved Hide resolved

def download_folder(self, folder_path: str, download_path: str):
folder(self.__repo, folder_path, download_path, self.__branch)

def download_repo(self, download_path: str):
repo(self.__repo, download_path, self.__branch)
bleudev marked this conversation as resolved.
Show resolved Hide resolved
Loading