Skip to content

Commit

Permalink
Merge pull request #622 from fnmeyer/v2
Browse files Browse the repository at this point in the history
fixes to pass ruff checks
  • Loading branch information
sbarrios93 authored Jan 31, 2024
2 parents 7618227 + 0b4db45 commit ee5b6d7
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 120 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: pip
directory: "/"
schedule:
interval: daily
time: "13:38"
groups:
python-packages:
patterns:
- "*"
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ repos:
language: fail
files: "\\.rej$"
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.6.2
rev: v3.0.3
hooks:
- id: prettier
stages: [commit]
additional_dependencies:
- prettier@2.6.2
- "@prettier/plugin-xml@2.1.0"
- prettier@2.8.8
- "@prettier/plugin-xml@2.2.0"
args:
- --plugin=@prettier/plugin-xml
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: "v0.0.263"
rev: "v0.1.9"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand All @@ -48,7 +48,7 @@ repos:
files: '\.(rst|md|markdown|py|tex)$'

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
rev: v4.5.0
hooks:
- id: check-added-large-files
args:
Expand Down
19 changes: 9 additions & 10 deletions amazon_ynab/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ def init_app(
else:
console.print("[red]Aborting...[/]")
raise typer.Exit()
# check if file containing the secrets exists
elif check_if_path_exists(path_to_secrets):
console.print("[green]✔[/] Secrets file exists")
else:
# check if file containing the secrets exists
if check_if_path_exists(path_to_secrets):
console.print("[green]✔[/] Secrets file exists")
else:
console.print("[red]✘[/] Secrets file does not exist, creating it...")
utils.create_secrets_file(path_to_secrets)
console.print("[red]✘[/] Secrets file does not exist, creating it...")
utils.create_secrets_file(path_to_secrets)


@app.command("run")
def run( # noqa
def run(
path_to_secrets: str = typer.Option(
PATHS["SECRETS_PATH"], "--secrets", "-s", help="Path to secrets file"
),
Expand Down Expand Up @@ -107,17 +106,17 @@ def run( # noqa
# add callback so we can access some options without using arguments
@app.callback()
def callback(
print_version: bool = typer.Option( # noqa
print_version: bool = typer.Option(
None,
"-v",
"--version",
callback=version_callback,
is_eager=True,
help="Prints the version of the amazon-ynab package.",
)
),
) -> None:
"""Print the version of the package."""
pass # noqa
pass


if __name__ == "__main__":
Expand Down
51 changes: 21 additions & 30 deletions amazon_ynab/amazon/amazon_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time
from datetime import datetime
from random import randint
from secrets import randbelow

from rich.console import Console
from rich.progress import MofNCompleteColumn, Progress, SpinnerColumn, TimeElapsedColumn
Expand All @@ -24,14 +24,14 @@


class AmazonClient:
def __init__(
def __init__( # noqa: PLR0913 Too many arguments to function call
self,
user_credentials: tuple[str, str],
run_headless: bool,
cutoff_date: datetime,
short_items: bool,
words_per_item: int,
): # noqa
):
self.user_email = user_credentials[0]
self.user_password = user_credentials[1]
self.run_headless = run_headless
Expand Down Expand Up @@ -105,15 +105,13 @@ def _get_raw_transactions(self) -> None:

while True:
transaction_divs = self.wait_driver.until(
EC.presence_of_all_elements_located(
EC.presence_of_all_elements_located((
By.XPATH,
(
By.XPATH,
(
'//div[@class="a-section a-spacing-base'
' apx-transactions-line-item-component-container"]'
),
)
)
'//div[@class="a-section a-spacing-base'
' apx-transactions-line-item-component-container"]'
),
))
)
transaction_texts = list(
map(
Expand All @@ -127,10 +125,8 @@ def _get_raw_transactions(self) -> None:
# we need stop the loop
dates_divs = self.driver.find_elements(
"xpath",
(
'//div[contains(@class,"a-section a-spacing-base a-padding-base'
' apx-transaction-date-container")]'
),
'//div[contains(@class,"a-section a-spacing-base a-padding-base'
' apx-transaction-date-container")]',
)

transaction_dates_texts = list(
Expand All @@ -150,12 +146,10 @@ def _get_raw_transactions(self) -> None:
break
else:
pagination_elem = self.wait_driver.until(
EC.element_to_be_clickable(
(
By.XPATH,
'//span[contains(text(), "Next Page")]//parent::span/input',
)
)
EC.element_to_be_clickable((
By.XPATH,
'//span[contains(text(), "Next Page")]//parent::span/input',
))
)

# cutoff date might be in the middle of the page, so we need to count
Expand All @@ -177,10 +171,8 @@ def _get_raw_transactions(self) -> None:
transaction_count = len(
date_container.find_elements(
By.XPATH,
(
"following-sibling::*[1]//div[contains(@class,"
" 'apx-transactions-line-item-component-container')]"
),
"following-sibling::*[1]//div[contains(@class,"
" 'apx-transactions-line-item-component-container')]",
)
)
# Add the date to the list once for each transaction
Expand All @@ -198,7 +190,7 @@ def _get_raw_transactions(self) -> None:
self.raw_transaction_data += transaction_texts[:transactions_to_count]

pagination_elem.click()
time.sleep(randint(200, 350) / 100.0)
time.sleep((randbelow(150) + 200) / 100.0)

@staticmethod
def _transaction_to_dict(
Expand Down Expand Up @@ -227,9 +219,8 @@ def _parse_raw_transactions(self) -> None:

for transaction in transactions:
order_number, order_info = self._transaction_to_dict(transaction)
if order_info["is_tip"]: # dont parse tip orders
pass
else:
# dont parse tip orders
if not order_info["is_tip"]:
# some transactions can be paid with more than one type of payment type,
# lets look if the order number
# already exists, meaning that there are multiple entries for the same
Expand All @@ -245,7 +236,7 @@ def _parse_raw_transactions(self) -> None:

def _get_invoice_page(self, order_number: str) -> str:
self.driver.get(self.urls["invoice"].format(order_number))
time.sleep(randint(50, 200) / 100.0)
time.sleep((randbelow(150) + 50) / 100.0)
return self.driver.page_source

def _process_invoices(self) -> None:
Expand Down
17 changes: 7 additions & 10 deletions amazon_ynab/amazon/invoice_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
https://github.com/davidz627/AmazonSyncForYNAB/
"""


import contextlib
import re
from datetime import date, datetime
from typing import Pattern
from re import Pattern

import bs4
from bs4 import BeautifulSoup as bs
Expand All @@ -17,7 +17,7 @@

# from amazon_ynab.amazon.product_summarizer import shorten_string
class TransactionInvoice:
def __init__(
def __init__( # noqa: PLR0913 Too many arguments to function call
self,
invoice_number: str,
transaction_page: str,
Expand Down Expand Up @@ -138,11 +138,10 @@ def _parse_payment_date(self) -> None:
)[1].findAll("td")

for ix, text_ in enumerate(search_in_block):
text_ = (
text_.text.strip().replace("$", "").replace(",", "")
) # this is to clean the potential amount paid
try:
if float(text_) == abs(
# Clean the potential amount paid
cleaned_text = text_.text.strip().replace("$", "").replace(",", "")
with contextlib.suppress(ValueError):
if float(cleaned_text) == abs(
not_none(self.total_amount_paid)
): # self.total_amount_paid is negative
date_string: str = (
Expand All @@ -152,8 +151,6 @@ def _parse_payment_date(self) -> None:
self.payment_date = datetime.strptime(
date_string, "%B %d, %Y"
).date()
except ValueError:
pass

def _parse_orchestrator(self) -> None:
self._parse_items()
Expand Down
7 changes: 4 additions & 3 deletions amazon_ynab/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class Engine:
def __init__( # noqa
def __init__( # noqa: PLR0913 Too many arguments to function call
self,
secrets: dict[str, dict[str, str]],
run_headless: bool,
Expand Down Expand Up @@ -69,10 +69,11 @@ def pre_start_ynab(self) -> None:
# if we only have one budget and no budget id, we can use that one
self.console.print(
"[yellow]WARNING:[/] No budget ID found on secrets file, using the"
f" only budget ID found: {list(self.ynab_client.all_budgets.keys())[0]}"
" only budget ID found: "
f"{next(iter(self.ynab_client.all_budgets.keys()))}"
)
self.ynab_client.selected_budget = self.ynab_client.all_budgets[
list(self.ynab_client.all_budgets.keys())[0]
next(iter(self.ynab_client.all_budgets.keys()))
]
else:
# if no budget id is found in the secrets file, and there is
Expand Down
26 changes: 10 additions & 16 deletions amazon_ynab/engine/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ def match_transactions(

for amazon_transaction_id, amazon_invoice in amazon_transactions.items():
for ynab_transaction_id, ynab_transaction_details in ynab_transactions.items():
if amazon_invoice.total_amount_paid is None:
pass
else:
if amazon_invoice.total_amount_paid is not None:
# check matching values
if float(
ynab_transaction_details["amount"] / ynab_amount_multiplier
Expand All @@ -36,26 +34,22 @@ def match_transactions(

# check matching dates
if (
ynab_transaction_details["date"] is None
or amazon_invoice.payment_date is None
):
pass
else:
if (
ynab_transaction_details["date"] is not None
and amazon_invoice.payment_date is not None
and (
timedelta(timedelta_lower_bound)
<= (
ynab_transaction_details["date"]
- amazon_invoice.payment_date
)
<= timedelta(timedelta_upper_bound)
):
transaction_candidates_for_date.append(
(amazon_transaction_id, ynab_transaction_id)
)
matches = [
)
):
transaction_candidates_for_date.append(
(amazon_transaction_id, ynab_transaction_id)
)
return [
(amazon, ynab)
for (amazon, ynab) in transaction_candidates_for_amount
if (amazon, ynab) in transaction_candidates_for_date
]

return matches
3 changes: 1 addition & 2 deletions amazon_ynab/engine/patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def patcher(

transactions_element = {
"id": ynab_transaction_id,
# memo has a max limit of 200 characters
"memo": items_string[:190] + " | AMAZON",
"memo": f"{items_string[:190]} | AMAZON",
"payee_id": payee_id,
"payee_name": payee_name,
}
Expand Down
3 changes: 1 addition & 2 deletions amazon_ynab/utils/custom_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import TypedDict

import datetime
from typing import TypedDict

from amazon_ynab.amazon.invoice_parser import TransactionInvoice

Expand Down
3 changes: 1 addition & 2 deletions amazon_ynab/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import TypeVar

import pathlib
from datetime import datetime, timedelta
from typing import TypeVar

import typer
import yaml
Expand Down
9 changes: 5 additions & 4 deletions amazon_ynab/ynab/ynab_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import re
from datetime import datetime
from http import HTTPStatus
from typing import Any

import requests
Expand Down Expand Up @@ -67,7 +68,7 @@ def prompt_user_for_budget_id(self) -> None:

console.print(Rule(title="[bold]Select a budget from the list below:[/]"))

for num, (budget_name, budget_id) in enumerate(self.all_budgets.items()):
for num, (budget_name, _budget_id) in enumerate(self.all_budgets.items()):
console.print(Markdown(f"{num + 1}. {budget_name}"))

selected = int(
Expand Down Expand Up @@ -151,7 +152,7 @@ def bulk_patch_transactions(self, transactions: list[dict[str, Any]]) -> None:
headers=self.request_headers,
timeout=self.request_timeout,
)
if resp.status_code != 200:
print(f"Something went wrong, got response: {str(resp.content)}")
else:
if resp.status_code == HTTPStatus.OK:
print(f"Successfully updated transactions {transactions}")
else:
print(f"Something went wrong, got response: {resp.content!s}")
Loading

0 comments on commit ee5b6d7

Please sign in to comment.