Skip to content

Commit

Permalink
chore: ruff formatting and pre-commit tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
codito committed Jan 12, 2024
1 parent c0e7ea5 commit 5697bec
Show file tree
Hide file tree
Showing 23 changed files with 352 additions and 263 deletions.
45 changes: 22 additions & 23 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,33 @@ name: linux

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]
workflow_dispatch:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
uses: actions/setup-python@v4
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r development.txt
pip install codecov
pip freeze
- name: Lint with flake8
run: |
flake8
- name: Test with pytest
run: |
pytest
- name: Upload coverage results
run: |
codecov
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.12
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r development.txt
pip install codecov
pip freeze
- name: Lint with ruff
run: |
ruff
- name: Test with pytest
run: |
pytest
- name: Upload coverage results
run: |
codecov
39 changes: 19 additions & 20 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,32 @@ name: windows

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]
workflow_dispatch:

jobs:
build:

runs-on: windows-latest
strategy:
matrix:
python-version: [3.9]
python-version: [3.10, 3.11, 3.12]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r development.txt
pip freeze
- name: Lint with flake8
run: |
flake8
- name: Test with pytest
run: |
pytest
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r development.txt
pip freeze
- name: Lint with ruff
run: |
ruff
- name: Test with pytest
run: |
pytest
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.12
hooks:
- id: ruff
types_or: [python, pyi, jupyter]
args: [--fix]
- id: ruff-format
types_or: [python, pyi, jupyter]
5 changes: 3 additions & 2 deletions development.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
-r requirements.txt
coverage
flake8
flake8-docstrings
mkdocs
mkdocs-material
pre-commit
pytest
pytest-cov
pytest-mock
ruff
twine
types-peewee
wheel
21 changes: 11 additions & 10 deletions habito/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
@click.option("--units", "-u", default="units", help="Units of data.")
def add(name, quantum, units):
"""Add a habit."""
habit_name = ' '.join(name)
models.Habit.add(name=habit_name,
created_date=datetime.now(),
quantum=quantum,
units=units,
magica="")
habit_name = " ".join(name)
models.Habit.add(
name=habit_name,
created_date=datetime.now(),
quantum=quantum,
units=units,
magica="",
)

msg_unit = click.style("{0} {1}".format(quantum, units), fg='green')
msg_name = click.style("{0}".format(habit_name), fg='green')
click.echo("You have commited to {0} of {1} every day!"
.format(msg_unit, msg_name))
msg_unit = click.style("{0} {1}".format(quantum, units), fg="green")
msg_name = click.style("{0}".format(habit_name), fg="green")
click.echo("You have commited to {0} of {1} every day!".format(msg_unit, msg_name))
53 changes: 30 additions & 23 deletions habito/commands/checkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,49 @@

@click.command()
@click.argument("name", nargs=-1)
@click.option("--review", "-r", is_flag=True,
help="Update activity for all tracked habits.")
@click.option("--date", "-d",
help="Date of activity in mm/dd format (defaults to today).",
default=datetime.now().strftime("%m/%d"))
@click.option(
"--review", "-r", is_flag=True, help="Update activity for all tracked habits."
)
@click.option(
"--date",
"-d",
help="Date of activity in mm/dd format (defaults to today).",
default=datetime.now().strftime("%m/%d"),
)
@click.option("--quantum", "-q", type=float, help="Progress for the day.")
def checkin(name, review, date, quantum):
"""Commit progress for a habit."""
# Set a date for this checkin. Use past year if month/day is in future
query = ' '.join(name)
query = " ".join(name)
date = date.strip()
d = datetime.strptime(date, "%m/%d").replace(year=datetime.now().year)
update_date = d if d < datetime.now() else d.replace(year=d.year-1)
update_date = d if d < datetime.now() else d.replace(year=d.year - 1)
update_date_str = update_date.strftime("%a %b %d %Y")

def print_header(date_str):
header = "Please update progress of habits for {0}:"
click.echo(header.format(click.style(date_str, fg='green')))
click.echo(header.format(click.style(date_str, fg="green")))

def get_quantum(habit, required=True):
msg = " - {0} (Goal: {1} {2})"

# Keep prompting until we have a value if required is True
value = None if required else float_info.max
q = click.prompt(msg.format(habit.name, habit.quantum, habit.units),
type=float,
show_default=False,
default=value)
q = click.prompt(
msg.format(habit.name, habit.quantum, habit.units),
type=float,
show_default=False,
default=value,
)
if not required and q == value:
return None
return q

def update_activity(habit, quantum, date):
# Create an activity for this checkin
activity = models.Activity.create(for_habit=habit,
quantum=quantum,
update_date=update_date)
activity = models.Activity.create(
for_habit=habit, quantum=quantum, update_date=update_date
)

# Update streak for the habit
models.Summary.update_streak(habit)
Expand All @@ -70,12 +76,14 @@ def update_activity(habit, quantum, date):
habits = models.Habit.all_active().where(models.Habit.name.regexp(query))
if habits.count() == 0:
error = "No habit matched the name '{0}'.".format(query)
click.secho(error, fg='red')
click.secho(error, fg="red")
return
elif habits.count() > 1:
error = ("More than one habits matched the name '{0}'. "
"Don't know which to update.").format(query)
click.secho(error, fg='red')
error = (
"More than one habits matched the name '{0}'. "
"Don't know which to update."
).format(query)
click.secho(error, fg="red")
for h in habits:
click.echo("- {0}".format(h.name))
click.echo("Try a different filter, or `habito checkin --review`.")
Expand All @@ -88,8 +96,7 @@ def update_activity(habit, quantum, date):
quantum = get_quantum(habit, required=True)
activity = update_activity(habit, quantum, update_date)

act_msg = click.style("{0} {1}".format(activity.quantum, habit.units),
fg='green')
act_date = click.style(update_date_str, fg='green')
habit_msg = click.style("{0}".format(habit.name), fg='green')
act_msg = click.style("{0} {1}".format(activity.quantum, habit.units), fg="green")
act_date = click.style(update_date_str, fg="green")
habit_msg = click.style("{0}".format(habit.name), fg="green")
click.echo("Added {0} to habit {1} for {2}.".format(act_msg, habit_msg, act_date))
25 changes: 16 additions & 9 deletions habito/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,32 @@

@click.command()
@click.argument("id", type=click.INT)
@click.option("--keeplogs", is_flag=True, default=False,
help="Preserve activity logs for the habit.")
@click.option(
"--keeplogs",
is_flag=True,
default=False,
help="Preserve activity logs for the habit.",
)
def delete(id, keeplogs):
"""Delete a habit."""
try:
habit = models.Habit.get(models.Habit.id == id)
except models.Habit.DoesNotExist:
click.echo("The habit you want to remove does not seem to exist!")
raise SystemExit(1)
confirm = click.confirm("Are you sure you want to delete habit"
" {}: {} (this cannot be undone!)"
.format(habit.id, habit.name))
confirm = click.confirm(
"Are you sure you want to delete habit"
" {}: {} (this cannot be undone!)".format(habit.id, habit.name)
)
if confirm:
click.echo("Habit {}: {} has been deleted!".format(habit.id, habit.name))
if not keeplogs:
models.Activity.delete().where(models.Activity.for_habit ==
habit.id).execute()
models.Summary.delete().where(models.Summary.for_habit ==
habit.id).execute()
models.Activity.delete().where(
models.Activity.for_habit == habit.id
).execute()
models.Summary.delete().where(
models.Summary.for_habit == habit.id
).execute()
habit.delete_instance()
else:
habit.active = False
Expand Down
25 changes: 15 additions & 10 deletions habito/commands/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

@click.command()
@click.argument("id", type=click.INT)
@click.option('--name', '-n',
help="The new name (leave empty to leave unchanged).")
@click.option('--quantum', '-q',
type=click.FLOAT,
help="The new quantum (leave empty to leave unchanged).")
@click.option("--name", "-n", help="The new name (leave empty to leave unchanged).")
@click.option(
"--quantum",
"-q",
type=click.FLOAT,
help="The new quantum (leave empty to leave unchanged).",
)
def edit(id, name, quantum):
"""Edit a habit."""
try:
Expand All @@ -23,8 +25,11 @@ def edit(id, name, quantum):
habit.quantum = quantum or habit.quantum
habit.save()

msg_id = click.style(str(habit.id), fg='green')
msg_name = click.style(habit.name, fg='green')
msg_quantum = click.style(str(habit.quantum), fg='green')
click.echo("Habit with id {} has been saved with name: {} and quantum: {}."
.format(msg_id, msg_name, msg_quantum))
msg_id = click.style(str(habit.id), fg="green")
msg_name = click.style(habit.name, fg="green")
msg_quantum = click.style(str(habit.quantum), fg="green")
click.echo(
"Habit with id {} has been saved with name: {} and quantum: {}.".format(
msg_id, msg_name, msg_quantum
)
)
29 changes: 21 additions & 8 deletions habito/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@

from habito import models as models

TICK = u"\u25A0" # tick - 2713, black square - 25A0, 25AA, 25AF
CROSS = u"\u25A1" # cross - 2717, white square - 25A1, 25AB, 25AE
PARTIAL = u"\u25A0" # tick - 2713, black square - 25A0, 25AA, 25AF
TICK = "\u25A0" # tick - 2713, black square - 25A0, 25AA, 25AF
CROSS = "\u25A1" # cross - 2717, white square - 25A1, 25AB, 25AE
PARTIAL = "\u25A0" # tick - 2713, black square - 25A0, 25AA, 25AF

logger = logging.getLogger("habito")


@click.command()
@click.option("-l", "long_list", is_flag=True, help="Long listing with date and quantum.")
def list(long_list):
@click.option(
"-l", "long_list", is_flag=True, help="Long listing with date and quantum."
)
@click.option(
"-f",
"--format",
type=click.Choice(["csv", "table"], case_sensitive=False),
default="table",
help="Output format. Default is table.",
)
def list(long_list, format="table"):
"""List all tracked habits."""
from terminaltables import SingleTable
from textwrap import wrap
Expand All @@ -26,9 +35,13 @@ def list(long_list):

nr_of_dates = terminal_width // 10 - 4
if nr_of_dates < 1:
logger.debug("list: Actual terminal width = {0}.".format(shutil.get_terminal_size()[0]))
logger.debug(
"list: Actual terminal width = {0}.".format(shutil.get_terminal_size()[0])
)
logger.debug("list: Observed terminal width = {0}.".format(terminal_width))
click.echo("Your terminal window is too small. Please make it wider and try again")
click.echo(
"Your terminal window is too small. Please make it wider and try again"
)
raise SystemExit(1)

table_title = ["Habit", "Goal", "Streak"]
Expand Down Expand Up @@ -70,6 +83,6 @@ def list(long_list):
max_col_width = max_col_width if max_col_width > 0 else 20

for r in table_rows:
r[0] = '\n'.join(wrap(r[0], max_col_width))
r[0] = "\n".join(wrap(r[0], max_col_width))

click.echo(table.table)
1 change: 1 addition & 0 deletions habito/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

if __name__ == "__main__":
from habito.commands import cli

cli()
Loading

0 comments on commit 5697bec

Please sign in to comment.