Skip to content

Commit 1f2ec01

Browse files
committed
readme, simplify the logic a bit
1 parent 8b1b632 commit 1f2ec01

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

github_backup/github_backup.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ def __init__(self, message, dmca_url=None):
7575
" 3. Debian/Ubuntu: apt-get install ca-certificates\n\n"
7676
)
7777

78-
# Retry configuration
79-
MAX_RETRIES = max_retries.MAX_RETRIES
80-
8178

8279
def logging_subprocess(
8380
popenargs, stdout_log_level=logging.DEBUG, stderr_log_level=logging.ERROR, **kwargs
@@ -639,7 +636,7 @@ def fetch_all() -> Generator[dict, None, None]:
639636
while True:
640637
# FIRST: Fetch response
641638

642-
for attempt in range(MAX_RETRIES):
639+
for attempt in range(max_retries.MAX_RETRIES):
643640
request = _construct_request(
644641
per_page=per_page if paginated else None,
645642
query_args=query_args,
@@ -662,10 +659,10 @@ def fetch_all() -> Generator[dict, None, None]:
662659
TimeoutError,
663660
) as e:
664661
logger.warning(f"{type(e).__name__} reading response")
665-
if attempt < MAX_RETRIES - 1:
662+
if attempt < max_retries.MAX_RETRIES - 1:
666663
delay = calculate_retry_delay(attempt, {})
667664
logger.warning(
668-
f"Retrying in {delay:.1f}s (attempt {attempt + 1}/{MAX_RETRIES})"
665+
f"Retrying in {delay:.1f}s (attempt {attempt + 1}/{max_retries.MAX_RETRIES})"
669666
)
670667
time.sleep(delay)
671668
continue # Next retry attempt
@@ -691,10 +688,10 @@ def fetch_all() -> Generator[dict, None, None]:
691688
)
692689
else:
693690
logger.error(
694-
f"Failed to read response after {MAX_RETRIES} attempts for {next_url or template}"
691+
f"Failed to read response after {max_retries.MAX_RETRIES} attempts for {next_url or template}"
695692
)
696693
raise Exception(
697-
f"Failed to read response after {MAX_RETRIES} attempts for {next_url or template}"
694+
f"Failed to read response after {max_retries.MAX_RETRIES} attempts for {next_url or template}"
698695
)
699696

700697
# SECOND: Process and paginate
@@ -738,7 +735,7 @@ def is_retryable_status(status_code, headers):
738735
return int(headers.get("x-ratelimit-remaining", 1)) < 1
739736
return False
740737

741-
for attempt in range(MAX_RETRIES):
738+
for attempt in range(max_retries.MAX_RETRIES):
742739
try:
743740
return urlopen(request, context=https_ctx)
744741

@@ -748,33 +745,32 @@ def is_retryable_status(status_code, headers):
748745
logger.error(f"API Error: {exc.code} {exc.reason} for {request.full_url}")
749746
raise # Non-retryable error
750747

751-
if attempt >= MAX_RETRIES - 1:
752-
logger.error(f"HTTP {exc.code} failed after {MAX_RETRIES} attempts")
753-
logger.error(f"HTTP {exc.code} failed after {MAX_RETRIES} attempts for {request.full_url}")
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}")
754750
raise
755751

756752
delay = calculate_retry_delay(attempt, exc.headers)
757753
logger.warning(
758754
f"HTTP {exc.code} ({exc.reason}), retrying in {delay:.1f}s "
759-
f"(attempt {attempt + 1}/{MAX_RETRIES}) for {request.full_url}"
755+
f"(attempt {attempt + 1}/{max_retries.MAX_RETRIES}) for {request.full_url}"
760756

761757
)
762758
if auth is None and exc.code in (403, 429):
763759
logger.info("Hint: Authenticate to raise your GitHub rate limit")
764760
time.sleep(delay)
765761

766762
except (URLError, socket.error) as e:
767-
if attempt >= MAX_RETRIES - 1:
768-
logger.error(f"Connection error failed after {MAX_RETRIES} attempts: {e} for {request.full_url}")
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}")
769765
raise
770766
delay = calculate_retry_delay(attempt, {})
771767
logger.warning(
772768
f"Connection error: {e}, retrying in {delay:.1f}s "
773-
f"(attempt {attempt + 1}/{MAX_RETRIES}) for {request.full_url}"
769+
f"(attempt {attempt + 1}/{max_retries.MAX_RETRIES}) for {request.full_url}"
774770
)
775771
time.sleep(delay)
776772

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

779775

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

0 commit comments

Comments
 (0)