11 Commits

Author SHA1 Message Date
Azalea Gui 46ea72641f [F] Fix import 2023-11-06 03:52:52 -05:00
Azalea Gui 62929dd48a [F] Fix typo 2023-11-06 03:32:31 -05:00
Azalea Gui 25e319d898 [U] Update readme 2023-11-06 03:00:33 -05:00
Azalea Gui 6291d178d4 [+] Git utils 2023-11-06 03:00:05 -05:00
Azalea 87a46fcf28 [F] ipconfig.me is blocking proxy blacklist 2023-10-05 21:31:47 -04:00
azalea df16f90a8f [U] Release 1.0.19 2023-07-28 20:52:27 -07:00
azalea 25aecabd34 [+] SafeNamespace 2023-07-28 20:49:51 -07:00
Azalea Gui 332a63479e [U] Release 1.0.18 2023-03-09 02:42:36 -05:00
Azalea Gui b748a217a0 [+] Logging utils 2023-03-09 02:42:03 -05:00
Azalea Gui afaef06f40 [+] Add setup_proxy 2023-01-29 23:54:53 -05:00
Azalea Gui 1948ff4a9c [O] Allow disabling progress bar during download 2023-01-13 05:48:43 -05:00
8 changed files with 119 additions and 8 deletions
+1
View File
@@ -8,3 +8,4 @@
| `tqdm_utils` | tqdm |
| `downloader` | tqdm, requests |
| `scientific_utils` | numpy, numba, matplotlib |
| `git_utils` | dateutil |
+1 -1
View File
@@ -1,6 +1,6 @@
from __future__ import annotations
__version__ = "1.0.17"
__version__ = "1.0.22"
import time
from typing import Callable
+10 -5
View File
@@ -7,7 +7,7 @@ import requests
import tqdm
def download_file(url: str, file: str | Path):
def download_file(url: str, file: str | Path, progress: bool = True):
"""
Helper method handling downloading large files from `url` to `filename`.
Returns a pointer to `filename`.
@@ -32,11 +32,16 @@ def download_file(url: str, file: str | Path):
tqdm_args['total'] = int(r.headers['content-length']) / 1024 / 1024
with open(file, 'wb') as f:
pbar = tqdm.tqdm(unit=" MB", ncols=term_len,
bar_format='{desc} {rate_noinv_fmt} {remaining} [{bar}] {percentage:.0f}%', ascii=' #',
desc=file.name[:bar_len].ljust(bar_len), **tqdm_args)
pbar = None
if progress:
pbar = tqdm.tqdm(unit=" MB", ncols=term_len,
bar_format='{desc} {rate_noinv_fmt} {remaining} [{bar}] {percentage:.0f}%', ascii=' #',
desc=file.name[:bar_len].ljust(bar_len), **tqdm_args)
for chunk in r.iter_content(chunk_size=chunk_size):
if chunk:
pbar.update(len(chunk) / 1024 / 1024)
if pbar:
pbar.update(len(chunk) / 1024 / 1024)
f.write(chunk)
return file
+45
View File
@@ -0,0 +1,45 @@
import datetime
import shlex
from pathlib import Path
from subprocess import check_output
from typing import NamedTuple
import dateutil.parser
class ExtractedCommit(NamedTuple):
sha: str
author: str
email: str
time: str
message: str
file_names: list[str]
def get_time(self) -> datetime:
return dateutil.parser.isoparse(self.time)
def git_log(path: Path, fail_silently: bool = False) -> list[ExtractedCommit]:
"""
Call and parse git log. This function requires that git>=2.37.1 is installed on your system.
:param path: Path of git repository
:param fail_silently: If true, ignore errors. If false, raise exception when errors occur.
:return: List of commits
"""
# check_call(shlex.split('git config diff.renames 0'))
cmd = f"git -c 'diff.renamelimit=0' -c 'diff.renames=0' -C '{path.absolute()}' log --name-status --diff-filter=AMD --pretty=format:'START_COMMIT_QwQ %H%n%aN%n%aE%n%aI%n%s%n'"
log = check_output(shlex.split(cmd)).decode('utf-8', 'ignore')
def extract_commit(block: str) -> ExtractedCommit:
try:
lines = block.split('\n')
sha, author, email, date, message = lines + [""] if len(lines) == 4 else lines[:5]
files = [f.replace('\t', '/') for f in lines[6:]]
return ExtractedCommit(sha, author, email, date, message, files)
except Exception as e:
print(f'========== Commit Extract Error {e} ==========\n{block}\n==========')
if not fail_silently:
raise e
return [extract_commit(c.strip()) for c in log.split('START_COMMIT_QwQ') if c]
+24
View File
@@ -0,0 +1,24 @@
import logging
import os
def setup_logger(debug: bool = os.environ.get("DEBUG", False)):
# Try to use rich for pretty printing
try:
from rich.logging import RichHandler
handler = RichHandler(rich_tracebacks=True)
from rich.traceback import install
install(show_locals=True)
except ImportError:
handler = logging.StreamHandler()
# Initialize debug logger
logging.basicConfig(
level="NOTSET" if debug else "INFO",
format="%(message)s",
datefmt="[%X]",
handlers=[handler]
)
return logging.getLogger("a2")
+27
View File
@@ -0,0 +1,27 @@
import requests
def setup_proxy(session: requests.Session, addr: str = 'socks5://localhost:9050', verbose: bool = True):
url = 'https://ip.me'
# Setup proxy
ip = session.get(url).text.strip()
session.proxies = {
'http': addr,
'https': addr
}
proxy_ip = session.get(url).text.strip()
# Print ip
if verbose:
print(f'Raw ip: {ip}')
print(f'Proxy ip: {proxy_ip}')
# ips shouldn't match
assert ip != proxy_ip, 'Proxy did not start correctly.'
# Disable default requests behavior
def warn(*args, **kwargs):
raise ReferenceError('Use session.get instead of requests.get')
requests.get = warn
requests.post = warn
+10 -2
View File
@@ -119,8 +119,16 @@ def json_stringify(obj: object, forced: bool = True, **kwargs) -> str:
return json.dumps(obj, **args)
def jsn(s: str) -> SimpleNamespace:
return json.loads(s, object_hook=lambda d: SimpleNamespace(**d))
class SafeNamespace(SimpleNamespace):
def __getattr__(self, attr):
try:
return super().__getattr__(attr)
except AttributeError:
return None
def jsn(s: str) -> SafeNamespace:
return json.loads(s, object_hook=lambda d: SafeNamespace(**d))
def ensure_dir(path: Path | str) -> Path:
+1
View File
@@ -26,6 +26,7 @@ setup(
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
],
packages=find_packages(exclude=("tests",)),
include_package_data=True,