Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuwynu23 committed Feb 10, 2024
1 parent 7d08fd4 commit e1ac7b1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 76 deletions.
Binary file modified lib/__pycache__/sdir.cpython-310.pyc
Binary file not shown.
111 changes: 55 additions & 56 deletions lib/sdir.py
Original file line number Diff line number Diff line change
@@ -1,97 +1,96 @@

import sqlite3
from os import getcwd,chdir,path
from os import getcwd, path
from shutil import which
import datetime
import pyperclip

from tabulate import tabulate
import random


now = datetime.datetime.now()
now = now.strftime('%Y-%m-%d %H:%M:%S')
executable_path = path.dirname(which("sdir.exe"))

def DB():
conn = sqlite3.connect(f"{executable_path}\\sdir.db.sqlite3")
# conn = sqlite3.connect("sdir.db.sqlite3")
return conn

def __create_table(conn):
c = conn.cursor()
c.execute('''
CREATE TABLE if not exists Directory (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date DATETIME,
directory TEXT
)
''')
conn.commit()




def __insert_directory(conn):
c = conn.cursor()
c.execute("INSERT INTO Directory(date,directory) VALUES(?,?)",(now,getcwd(),))
conn.commit()

def DB():
conn = sqlite3.connect(f"{executable_path}\\sdir.db.sqlite3")
return conn


def __create_table(conn):
c = conn.cursor()
c.execute('''
CREATE TABLE if not exists Directory (
id INTEGER PRIMARY KEY,
date DATETIME,
label TEXT,
directory TEXT
)
''')
conn.commit()


def __del_directory(conn,id):
c = conn.cursor()
c.execute("DELETE FROM Directory WHERE id=?",(id,))
conn.commit()
def __insert_directory(conn, label):
c = conn.cursor()
random_id = random.randint(10000, 99999)
c.execute("INSERT INTO Directory(id, date, label, directory) VALUES(?,?,?,?)",
(random_id, now, label, getcwd()))
conn.commit()


def __del_directory(conn, id):
c = conn.cursor()
c.execute("DELETE FROM Directory WHERE id=?", (id,))
conn.commit()


def __del_directory_all(conn):
c = conn.cursor()
c.execute("DELETE FROM Directory")
conn.commit()


c = conn.cursor()
c.execute("DELETE FROM Directory")
conn.commit()


def __select_dir_all(conn):
c = conn.cursor()
c.execute("SELECT * FROM Directory")
result = c.fetchall()
print(f'{str("ID").ljust(5)} {str("Date").ljust(20)} Directory Path')
c = conn.cursor()
c.execute("SELECT * FROM Directory")
result = c.fetchall()

if result == []:
print("empty.")
return
if not result:
print("empty.")
return

for row in result:
print(f'{str(row[0]).ljust(5)}| {str(row[1]).ljust(5)}| {row[2]}')

conn.commit()
headers = ["ID", "Date", "Label", "Directory Path"]
data = [(str(row[0]), str(row[1]), row[2], row[3]) for row in result]
print(tabulate(data, headers=headers, tablefmt="plain"))
conn.commit()


def create_table_once(db):
__create_table(db)
__create_table(db)


def save_dir(db, label):
__insert_directory(db, label)

def save_dir(db):
__insert_directory(db)

def remove_dir(db,id):
__del_directory(db,id)
def remove_dir(db, id):
__del_directory(db, id)


def remove_dir_all(db):
__del_directory_all(db)
__del_directory_all(db)


def list_dir(db):
__select_dir_all(db)
__select_dir_all(db)


def get_directory_by_id(db, id):
c = db.cursor()
c.execute("SELECT directory FROM Directory WHERE id=?", (id,))
result = c.fetchone()
return result
c = db.cursor()
c.execute("SELECT directory FROM Directory WHERE id=?", (id,))
result = c.fetchone()
return result


def copy_path_to_clip(db, id):
Expand Down
66 changes: 46 additions & 20 deletions sdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,60 @@
from tabulate import tabulate


def main(cmd=None, id=None):
db = DB()
create_table_once(db)

if cmd == "save":
save_dir(db)
elif cmd == "remove" and id is not None:
remove_dir(db, id)
elif cmd == "remove-all":
remove_dir_all(db)
elif cmd == "list":
list_dir(db)
elif cmd == "clip" and id is not None:
copy_path_to_clip(db, id)
else:
class SDir:
def __init__(self):
self.__db = DB()
create_table_once(self.__db)

def save(self, label):
"""Save the current directory."""
save_dir(self.__db, label)

def remove(self, id):
"""Remove a saved directory by its ID.
Args:
id (int): The ID of the directory to remove.
"""
remove_dir(self.__db, id)

def remove_all(self):
"""Remove all saved directories."""
remove_dir_all(self.__db)

def list(self):
"""List all saved directories."""
list_dir(self.__db)

def clip(self, id):
"""Copy the path of a saved directory to the clipboard.
Args:
id (int): The ID of the directory to copy the path from.
"""
copy_path_to_clip(self.__db, id)

def help(self):
"""Display the help information for all commands."""
help_table = [
["Command", "Description"],
["save", "Save the current directory."],
["save [label]", "Save the current directory."],
["remove [id]", "Remove a saved directory by its ID."],
["remove-all", "Remove all saved directories."],
["list", "List all saved directories."],
["clip [id]", "Copy the path of a saved directory to the clipboard."]
]
print("SDIR - Saved Directory")
print(tabulate(help_table, headers="firstrow", tablefmt="plain"))
print("\nAuthor: Mark Wayne Menorca")
print("SDIR v1.0.0")
print("Author: Mark Wayne Menorca")
print("GitHub: https://github.com/marcuwynu23/sdir")
print("Description: SDIR is a tool to save and manage directories\n")
print(tabulate(help_table, headers="firstrow", tablefmt="plain"))


def main():
sdir = SDir()
fire.Fire(sdir, name="sdir")


if __name__ == '__main__':
fire.Fire(main)
main()

0 comments on commit e1ac7b1

Please sign in to comment.