Honestly it's been a while, not sure what I was doing

and don't care enough to find out
This commit is contained in:
Adam Goldsmith 2018-09-04 13:34:39 -04:00
parent 4d27a34fa2
commit 7ded307b13
2 changed files with 22 additions and 7 deletions

18
sub.py
View File

@ -16,7 +16,7 @@ def getSub(item):
if "/:" not in item: if "/:" not in item:
return None return None
try: try:
sub=re.findall("[^ ]+/:[.A-Za-z0-9_]+", item)[0] sub = re.findall("[^ ]+/:[.A-Za-z0-9_]+", item)[0]
except IndexError: except IndexError:
return None return None
return sub.split("/:") return sub.split("/:")
@ -24,7 +24,8 @@ def getSub(item):
def addSub(num, sub, edit=True): def addSub(num, sub, edit=True):
with open(os.getenv("TODO_FILE"), "r") as todoFile: with open(os.getenv("TODO_FILE"), "r") as todoFile:
lines = todoFile.readlines() lines = todoFile.readlines()
if getSub(lines[num-1]) is not None and getSub(lines[num-1])[0] != sub: print(getSub(lines[num-1]))
if getSub(lines[num-1]) is not None and getSub(lines[num-1])[0] == sub:
sys.exit("The item already has a sub \"" + sub + "\":" + "\n" + lines[num-1][:-1]) sys.exit("The item already has a sub \"" + sub + "\":" + "\n" + lines[num-1][:-1])
if not os.path.exists(os.path.join(os.getenv("TODO_DIR"), sub)): if not os.path.exists(os.path.join(os.getenv("TODO_DIR"), sub)):
os.mkdir(os.path.join(os.getenv("TODO_DIR"), sub)) os.mkdir(os.path.join(os.getenv("TODO_DIR"), sub))
@ -50,7 +51,7 @@ def showSub(item, indent=0, color=True):
sub = getSub(item) sub = getSub(item)
if sub is None: if sub is None:
sys.exit("This item does not have a sub") sys.exit("This item does not have a sub")
command = ["todo.sh", "listfile", os.path.join(sub[0], sub[1])] command = ["todo.sh", "listfile", os.path.join(sub[0], sub[1])]
if not color: if not color:
command.insert(1, "-p") command.insert(1, "-p")
p = subprocess.Popen(command, stdout=subprocess.PIPE) p = subprocess.Popen(command, stdout=subprocess.PIPE)
@ -70,11 +71,18 @@ def showAll(out=sys.stdout, color=True):
out.write(showSub(line, indent=2, color=color)) out.write(showSub(line, indent=2, color=color))
def main(argv): def main(argv):
if len(argv) < 3: if(argv[1] == "usage"):
print(" sub")
print(" list subs")
print(" sub add [itemNum] [sub]")
print(" add a sub [sub] to [itemNum]")
print(" sub edit [itemNum]")
print(" edit the sub in [itemNum]")
elif len(argv) < 3:
showAll() showAll()
elif argv[2] == "add": elif argv[2] == "add":
if len(argv) != 5: if len(argv) != 5:
sys.exit("Usage: " + argv[1] + " add [itemNum] [subName]") sys.exit("Usage: " + argv[1] + " add [itemNum] [sub]")
itemNum = int(argv[3]) itemNum = int(argv[3])
addSub(itemNum, argv[4]) addSub(itemNum, argv[4])
elif argv[2] == "edit": elif argv[2] == "edit":

11
test.py
View File

@ -58,19 +58,26 @@ class TestShowAll(unittest.TestCase):
class TestAdd(unittest.TestCase): class TestAdd(unittest.TestCase):
def setUp(self): def setUp(self):
import shutil import shutil
shutil.rmtree(os.path.join(TEST_DATA_DIR, "test_add")) if os.path.exists(os.path.join(TEST_DATA_DIR, "test_add")):
shutil.rmtree(os.path.join(TEST_DATA_DIR, "test_add"))
os.mkdir(os.path.join(TEST_DATA_DIR, "test_add")) os.mkdir(os.path.join(TEST_DATA_DIR, "test_add"))
setup_environment("test_add")
def test_add_sub(self): def test_add_sub(self):
with open(os.path.join(TEST_DATA_DIR, "test_add/todo.txt"), "w") as f: with open(os.path.join(TEST_DATA_DIR, "test_add/todo.txt"), "w") as f:
f.write("(Z) gibberish") f.write("(Z) gibberish")
setup_environment("test_add")
sub.addSub(1, "sub", False) sub.addSub(1, "sub", False)
out = sub.getSub(sub.getItem(1)) out = sub.getSub(sub.getItem(1))
self.assertTrue(out is not None) self.assertTrue(out is not None)
fileExists = os.path.isfile(os.path.join(os.getenv("TODO_DIR"), out[0], out[1])) fileExists = os.path.isfile(os.path.join(os.getenv("TODO_DIR"), out[0], out[1]))
self.assertTrue(fileExists) self.assertTrue(fileExists)
def test_add_existing_sub(self):
with open(os.path.join(TEST_DATA_DIR, "test_add/todo.txt"), "w") as f:
f.write("(Z) gibberish sub:/test1.txt\n")
with self.assertRaises(SystemExit):
sub.addSub(1, "sub", False)
class TestEdit(unittest.TestCase): class TestEdit(unittest.TestCase):
def test_edit(self): def test_edit(self):
from unittest.mock import MagicMock from unittest.mock import MagicMock