Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Offline updated file structure #203

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions package/clip.py

This file was deleted.

10 changes: 0 additions & 10 deletions package/display.py

This file was deleted.

Empty file added package/offline/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions package/offline/clip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os
from package.offline.support import copy_to_clipboard
from package.offline.support import display



def clip(snippet_name, password):
display(snippet_name, password, action="copy")
12 changes: 12 additions & 0 deletions package/offline/show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
from package.offline.support import list_snippets, display
from package.password import valid_password

def show(snippet_name=None, password=None):
if snippet_name is None and password is None:
base_dir = os.path.dirname(__file__)
snippets_dir = os.path.join(base_dir, "stash")
list_snippets(snippets_dir)
return

display(snippet_name, password, action="display")
Empty file.
22 changes: 22 additions & 0 deletions package/offline/support/copy_to_clipboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import subprocess
import sys
import shutil


def copy_to_clipboard(text):
try:
if "linux" in sys.platform:
if shutil.which("xclip") is None:
raise RuntimeError("xclip not found. Install it.")
subprocess.run(["xclip", "-selection", "clipboard"], input=text.strip().encode(), check=True)

elif "win32" in sys.platform:
subprocess.run(["C:\\Windows\\System32\\clip.exe"], input=text.strip().encode(), check=True)

elif "darwin" in sys.platform:
subprocess.run(["/usr/bin/pbcopy"], input=text.strip().encode(), check=True)

else:
raise OSError("Unsupported operating system")
except subprocess.CalledProcessError:
print("Error: Failed to copy to clipboard.")
25 changes: 25 additions & 0 deletions package/offline/support/display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
from package.offline.support import copy_to_clipboard
from package.password import valid_password

def display(snippet_name, password, action):
if not isinstance(password, int) or not valid_password(password):
raise ValueError("Incorrect password")

base_dir = os.path.dirname(__file__)
snippets_dir = os.path.join(base_dir, "stash")

try:
snippet_path = os.path.join(snippets_dir, snippet_name)
with open(snippet_path, "r") as file:
content = file.read()

if action == "display":
print(content)
elif action == "copy":
copy_to_clipboard(content)
print("Snippet copied to clipboard.")
except FileNotFoundError:
print("Error: No file found with the specified name.")
except Exception as e:
print(f"Error: {e}")
11 changes: 11 additions & 0 deletions package/offline/support/list_snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os

def list_snippets(snippets_dir):
try:
contents = os.listdir(snippets_dir)
for file in contents:
print(file)
except FileNotFoundError:
print("Error: Stash directory not found.")
except Exception as e:
print(f"Error: {e}")
22 changes: 22 additions & 0 deletions package/offline/write.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import shutil
import os
from package.password import valid_password


def write(snippet_name, password):
if not isinstance(password, int) or not valid_password(password):
raise ValueError("Incorrect password")

base_dir = os.path.dirname(__file__)
snippets_dir = os.path.join(base_dir, "stash")

try:
snippet_path = os.path.join(snippets_dir, snippet_name)
output_path = os.path.join(base_dir, snippet_name)

shutil.copyfile(snippet_path, output_path)
print(f"File '{snippet_path}' copied successfully to '{output_path}'.")
except FileNotFoundError:
print("Error: No file found with the specified name.")
except Exception as e:
print(f"Error: {e}")
84 changes: 0 additions & 84 deletions package/show.py

This file was deleted.

5 changes: 0 additions & 5 deletions package/test.py

This file was deleted.

36 changes: 0 additions & 36 deletions package/write.py

This file was deleted.

Loading