Skip to content

Commit f9827da

Browse files
committed
don't use a global variable, pass the args instead
1 parent 1f2ec01 commit f9827da

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

github_backup/cli.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import logging
55
import os
66
import sys
7-
from github_backup import max_retries
87

98
from github_backup.github_backup import (
109
backup_account,
@@ -40,7 +39,6 @@
4039
def main():
4140
"""Main entry point for github-backup CLI."""
4241
args = parse_args()
43-
max_retries.MAX_RETRIES = args.max_retries
4442

4543
if args.private and not get_auth(args):
4644
logger.warning(

github_backup/github_backup.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from urllib.error import HTTPError, URLError
2626
from urllib.parse import urlencode, urlparse
2727
from urllib.request import HTTPRedirectHandler, Request, build_opener, urlopen
28-
from github_backup import max_retries
2928

3029
try:
3130
from . import __version__
@@ -636,7 +635,7 @@ def fetch_all() -> Generator[dict, None, None]:
636635
while True:
637636
# FIRST: Fetch response
638637

639-
for attempt in range(max_retries.MAX_RETRIES):
638+
for attempt in range(args.max_retries):
640639
request = _construct_request(
641640
per_page=per_page if paginated else None,
642641
query_args=query_args,
@@ -645,7 +644,7 @@ def fetch_all() -> Generator[dict, None, None]:
645644
as_app=args.as_app,
646645
fine=args.token_fine is not None,
647646
)
648-
http_response = make_request_with_retry(request, auth)
647+
http_response = make_request_with_retry(request, auth, args.max_retries)
649648

650649
match http_response.getcode():
651650
case 200:
@@ -659,10 +658,10 @@ def fetch_all() -> Generator[dict, None, None]:
659658
TimeoutError,
660659
) as e:
661660
logger.warning(f"{type(e).__name__} reading response")
662-
if attempt < max_retries.MAX_RETRIES - 1:
661+
if attempt < args.max_retries - 1:
663662
delay = calculate_retry_delay(attempt, {})
664663
logger.warning(
665-
f"Retrying in {delay:.1f}s (attempt {attempt + 1}/{max_retries.MAX_RETRIES})"
664+
f"Retrying read in {delay:.1f}s (attempt {attempt + 1}/{args.max_retries})"
666665
)
667666
time.sleep(delay)
668667
continue # Next retry attempt
@@ -688,10 +687,10 @@ def fetch_all() -> Generator[dict, None, None]:
688687
)
689688
else:
690689
logger.error(
691-
f"Failed to read response after {max_retries.MAX_RETRIES} attempts for {next_url or template}"
690+
f"Failed to read response after {args.max_retries} attempts for {next_url or template}"
692691
)
693692
raise Exception(
694-
f"Failed to read response after {max_retries.MAX_RETRIES} attempts for {next_url or template}"
693+
f"Failed to read response after {args.max_retries} attempts for {next_url or template}"
695694
)
696695

697696
# SECOND: Process and paginate
@@ -723,7 +722,7 @@ def fetch_all() -> Generator[dict, None, None]:
723722
return list(fetch_all())
724723

725724

726-
def make_request_with_retry(request, auth):
725+
def make_request_with_retry(request, auth, max_retries=5):
727726
"""Make HTTP request with automatic retry for transient errors."""
728727

729728
def is_retryable_status(status_code, headers):
@@ -735,7 +734,7 @@ def is_retryable_status(status_code, headers):
735734
return int(headers.get("x-ratelimit-remaining", 1)) < 1
736735
return False
737736

738-
for attempt in range(max_retries.MAX_RETRIES):
737+
for attempt in range(max_retries):
739738
try:
740739
return urlopen(request, context=https_ctx)
741740

@@ -745,32 +744,31 @@ def is_retryable_status(status_code, headers):
745744
logger.error(f"API Error: {exc.code} {exc.reason} for {request.full_url}")
746745
raise # Non-retryable error
747746

748-
if attempt >= max_retries.MAX_RETRIES - 1:
749-
logger.error(f"HTTP {exc.code} failed after {max_retries.MAX_RETRIES} attempts for {request.full_url}")
747+
if attempt >= max_retries - 1:
748+
logger.error(f"HTTP {exc.code} failed after {max_retries} attempts for {request.full_url}")
750749
raise
751750

752751
delay = calculate_retry_delay(attempt, exc.headers)
753752
logger.warning(
754753
f"HTTP {exc.code} ({exc.reason}), retrying in {delay:.1f}s "
755-
f"(attempt {attempt + 1}/{max_retries.MAX_RETRIES}) for {request.full_url}"
756-
754+
f"(attempt {attempt + 1}/{max_retries}) for {request.full_url}"
757755
)
758756
if auth is None and exc.code in (403, 429):
759757
logger.info("Hint: Authenticate to raise your GitHub rate limit")
760758
time.sleep(delay)
761759

762760
except (URLError, socket.error) as e:
763-
if attempt >= max_retries.MAX_RETRIES - 1:
764-
logger.error(f"Connection error failed after {max_retries.MAX_RETRIES} attempts: {e} for {request.full_url}")
761+
if attempt >= max_retries - 1:
762+
logger.error(f"Connection error failed after {max_retries} attempts: {e} for {request.full_url}")
765763
raise
766764
delay = calculate_retry_delay(attempt, {})
767765
logger.warning(
768766
f"Connection error: {e}, retrying in {delay:.1f}s "
769-
f"(attempt {attempt + 1}/{max_retries.MAX_RETRIES}) for {request.full_url}"
767+
f"(attempt {attempt + 1}/{max_retries}) for {request.full_url}"
770768
)
771769
time.sleep(delay)
772770

773-
raise Exception(f"Request failed after {max_retries.MAX_RETRIES} attempts") # pragma: no cover
771+
raise Exception(f"Request failed after {max_retries} attempts") # pragma: no cover
774772

775773

776774
def _construct_request(per_page, query_args, template, auth, as_app=None, fine=False):

0 commit comments

Comments
 (0)