Skip to content

Commit

Permalink
v2023.10.02
Browse files Browse the repository at this point in the history
TWEAKS:

- change font
- make debug mode indicator more visible
- show clear completed btn only if there are completed todos

MISC:

- (bugfix) try/catch clauses for repos without certain files
- (documentation) update docs/docstrings
- (feature) clear completed todos
- (refactor) Make main db in home_dir for todos, allowing db fnality in web ui
- (ui) redo TodoItem and addtodoform components
  • Loading branch information
misterrager8 committed Oct 2, 2023
1 parent d5b7702 commit 359e887
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 223 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ CodeGarden.egg-info/
*.env
*.pyc
.DS_Store
todos.txt
.coverage
htmlcov/
*.db
Expand Down
153 changes: 40 additions & 113 deletions code_garden/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import requests

from code_garden.readme import Readme
from code_garden.todos import Todo

from . import config

Expand All @@ -21,7 +22,7 @@ class Repository(object):
branches: All local branches of the Repository.
current_branch: The currently checked-out branch.
log: List of (5 default) commits, sorted by most recent.
todos: List of tasks found in the todos.txt file.
todos: List of tasks found in the database file.
diffs: List of all changed file in the current Repository.
readme: Dict object of text in the README.md file 'txt' is the plaintext content, 'md' is the Markdown-formatted text.
ignored: List of items in the .gitignore file.
Expand Down Expand Up @@ -60,41 +61,36 @@ def current_branch(self):
@property
def log(self):
_ = []
for i in self.run_command(
["git", "log", "--oneline", "-5", "--pretty=format:%s\t%at\t%h"]
).split("\n"):
if len(i.strip().split("\t")) == 2:
_.append(
LogItem(
self.name,
"[No Commit Message]",
datetime.datetime.min,
i.strip().split("\t")[0],
try:
for i in self.run_command(
["git", "log", "--oneline", "-5", "--pretty=format:%s\t%at\t%h"]
).split("\n"):
if len(i.strip().split("\t")) == 2:
_.append(
LogItem(
self.name,
"[No Commit Message]",
datetime.datetime.min,
i.strip().split("\t")[0],
)
)
)
else:
_.append(
LogItem(
self.name,
i.strip().split("\t")[0],
datetime.datetime.fromtimestamp(int(i.split("\t")[1])),
i.strip().split("\t")[2],
else:
_.append(
LogItem(
self.name,
i.strip().split("\t")[0],
datetime.datetime.fromtimestamp(int(i.split("\t")[1])),
i.strip().split("\t")[2],
)
)
)

return _
return _
except:
return []

@property
def todos(self):
return (
[
Todo(self.name, i.strip())
for i in open(self.path / "todos.txt").readlines()
if i.strip()
]
if (self.path / "todos.txt").exists()
else []
)
return Todo.see_list(self.name)

@property
def diffs(self):
Expand All @@ -106,16 +102,22 @@ def diffs(self):

@property
def readme(self):
raw = open(self.path / "README.md").read()
return dict(txt=raw, md=markdown.markdown(raw))
try:
raw = open(self.path / "README.md").read()
return dict(txt=raw, md=markdown.markdown(raw))
except:
return {}

@property
def ignored(self):
return [
IgnoreItem(self.name, i.strip())
for i in open(self.path / ".gitignore").readlines()
if i.strip()
]
try:
return [
IgnoreItem(self.name, i.strip())
for i in open(self.path / ".gitignore").readlines()
if i.strip()
]
except:
return []

@property
def remote_url(self):
Expand Down Expand Up @@ -150,13 +152,11 @@ def init(self, brief_descrip: str):
Args:
brief_descrip (str): Short description of what the Repository contains.
"""
files = ["LICENSE.md", ".gitignore", "todos.txt"]
files = ["LICENSE.md", ".gitignore"]
self.path.mkdir()
Readme(self.name, brief_descrip).write(self.path)
for i in files:
(self.path / i).touch()
if i == ".gitignore":
open(self.path / i, "w").write("todos.txt\n")

self.run_command(["git", "init"])
self.commit("Initial commit")
Expand Down Expand Up @@ -288,79 +288,6 @@ def to_dict(self):
)


class Todo(object):
"""Todo item found in todos.txt.
Attributes:
repository (str): name of the containing Repository
name (str): description of this Todo item.
"""

def __init__(self, repository, name):
self.repository = repository
self.name = name.replace("[x] ", "")
self.done = name.startswith("[x] ")

def create(self):
"""Create a new Todo."""
todos_ = Repository(self.repository).todos
todos_.append(self)

with open((Repository(self.repository).path / "todos.txt"), "w") as f:
for i in todos_:
f.write(f"{'[x] ' if i.done else ''} {i.name}\n")

@classmethod
def edit(cls, repository, id, new_name):
"""Edit a Todo item.
Args:
repository (str): name of the Repository that contains this Todo.
id (int): index, or location, of the Todo in the list.
new_name (str): new description of the Todo item.
"""
todos_ = Repository(repository).todos
todos_[id].name = new_name

with open((Repository(repository).path / "todos.txt"), "w") as f:
for i in todos_:
f.write(f"{'[x] ' if i.done else ''} {i.name}\n")

@classmethod
def delete(cls, repository, id):
"""Delete a Todo item.
Args:
repository (str): name of the Repository that contains this Todo.
id (int): index, or location, of the Todo in the list.
"""
todos_ = Repository(repository).todos
del todos_[id]

with open((Repository(repository).path / "todos.txt"), "w") as f:
for i in todos_:
f.write(f"{'[x] ' if i.done else ''} {i.name}\n")

@classmethod
def toggle(cls, repository, id):
"""Delete a Todo item.
Args:
repository (str): name of the Repository that contains this Todo.
id (int): index, or location, of the Todo in the list.
"""
todos_ = Repository(repository).todos
todos_[id].done = not todos_[id].done

with open((Repository(repository).path / "todos.txt"), "w") as f:
for i in todos_:
f.write(f"{'[x] ' if i.done else ''} {i.name}\n")

def to_dict(self):
"""Get a dict representation of this object (for API use)."""
return dict(repository=self.repository, name=self.name, done=self.done)


class DiffItem(object):
"""Changed item in the Repository.
Expand Down
19 changes: 12 additions & 7 deletions code_garden/repos.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import datetime
import subprocess
from pathlib import Path
from pprint import pformat
import subprocess

import click

from code_garden.models import Repository
from code_garden.todos import Task
from code_garden.todos import Todo


@click.group()
Expand All @@ -17,6 +17,7 @@ def repo_cli():
@repo_cli.command()
@click.option("--name")
def add_repo(name):
"""Create a new repo."""
repo_ = Repository(name or Repository.generate_name())
repo_.init(f"Created {datetime.date.today().strftime('%d/%m/%Y')}")

Expand All @@ -30,25 +31,25 @@ def add_repo(name):
@click.option("--fixup", "-f", is_flag=True, help="Capitalize input (convenience).")
def commit(title: str, desc, tag, fixup):
"""Commit changes to git using task info as the commit message."""
task_ = Task(
todo_ = Todo(
title.capitalize() if fixup else title,
desc,
tag,
datetime.datetime.now(),
"open",
)

if click.confirm(f"Commit {task_.title}?", default=True):
task_.status = "completed"
task_.add()
if click.confirm(f"Commit {todo_.title}?", default=True):
todo_.status = "completed"
todo_.add()

click.secho(
subprocess.run(
[
"git",
"commit",
"-am",
f"({task_.tag or datetime.date.today().strftime('%d/%m/%Y')}) {task_.title}",
f"({todo_.tag or datetime.date.today().strftime('%d/%m/%Y')}) {todo_.title}",
],
cwd=Path.cwd(),
text=True,
Expand All @@ -62,25 +63,29 @@ def commit(title: str, desc, tag, fixup):

@repo_cli.command()
def generate_name():
"""Generate a random placeholder name for a new repo."""
click.secho(Repository.generate_name(), fg="green")


@repo_cli.command()
@click.argument("name")
def view_repo(name):
"""View all attributes of a repo for exporting."""
repo_ = Repository(name)

click.secho(pformat(repo_.to_dict()), fg="green")


@repo_cli.command()
def view_repos():
"""See a list of all repos found in the home directory."""
click.secho("\n".join([str(i) for i in Repository.all()]), fg="green")


@repo_cli.command()
@click.argument("name")
def delete_repo(name):
"""Delete a repo."""
repo_ = Repository(name)
if click.confirm(f"Delete {repo_.name}?", default=True):
repo_.delete()
Expand Down
Loading

0 comments on commit 359e887

Please sign in to comment.