diff --git a/package/clip.py b/package/clip.py deleted file mode 100644 index 11524a8..0000000 --- a/package/clip.py +++ /dev/null @@ -1,64 +0,0 @@ -import os -import subprocess -import sys -import shutil -from datetime import datetime -import glob - - -def display(snippet_name, password): - current_time = datetime.now().strftime("%H%M") - if str(password).zfill(4) != current_time: - raise ValueError("Invalid password") - - base_dir = os.path.dirname(__file__) - snippets_dir = os.path.join(base_dir, "stash") - pattern = os.path.join(snippets_dir, f"{snippet_name}.*") - - matching_files = glob.glob(pattern) - - if not matching_files: - raise FileNotFoundError("No file found with the name.") - elif len(matching_files) > 1: - raise ValueError("Multiple files found with the given name.") - - snippet_path = os.path.join(snippets_dir, matching_files[0]) - - try: - with open(snippet_path, "r") as file: - source_code = file.read() - - # Call the copy to clipboard function - copy_to_clipboard(source_code) - - except FileNotFoundError: - print("File not found") - raise - - -def copy_to_clipboard(text): - # Linux - if "linux" in sys.platform: - # Check if xclip is installed - if shutil.which("xclip") is None: - print("Error: xclip not found. Install it.", file=sys.stderr) - return - # If xclip is installed, proceed with copying text - subprocess.run( - ["xclip", "-selection", "clipboard"], - input=text.strip().encode(), - check=True, - ) - - # Windows - elif "win32" in sys.platform: - subprocess.run( - ["C:\\Windows\\System32\\clip.exe"], input=text.strip().encode(), check=True - ) - - # macOS - elif "darwin" in sys.platform: - subprocess.run(["/usr/bin/pbcopy"], input=text.strip().encode(), check=True) - - else: - raise OSError("Unsupported operating system") diff --git a/package/display.py b/package/display.py deleted file mode 100644 index acc3190..0000000 --- a/package/display.py +++ /dev/null @@ -1,10 +0,0 @@ -from package.show import display -from package.write import plot - - -def write(snippet_name, password): - return plot(snippet_name, password) - - -def show(snippet_name, password, clipboard=None): - return display(snippet_name, password, clipboard=None) diff --git a/package/offline/__init__.py b/package/offline/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/package/offline/clip.py b/package/offline/clip.py new file mode 100644 index 0000000..cbfc254 --- /dev/null +++ b/package/offline/clip.py @@ -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") \ No newline at end of file diff --git a/package/offline/show.py b/package/offline/show.py new file mode 100644 index 0000000..fda3780 --- /dev/null +++ b/package/offline/show.py @@ -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") \ No newline at end of file diff --git a/package/offline/support/__init__.py b/package/offline/support/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/package/offline/support/copy_to_clipboard.py b/package/offline/support/copy_to_clipboard.py new file mode 100644 index 0000000..795fae3 --- /dev/null +++ b/package/offline/support/copy_to_clipboard.py @@ -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.") \ No newline at end of file diff --git a/package/offline/support/display.py b/package/offline/support/display.py new file mode 100644 index 0000000..41bc6bc --- /dev/null +++ b/package/offline/support/display.py @@ -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}") \ No newline at end of file diff --git a/package/offline/support/list_snippets.py b/package/offline/support/list_snippets.py new file mode 100644 index 0000000..3b18432 --- /dev/null +++ b/package/offline/support/list_snippets.py @@ -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}") \ No newline at end of file diff --git a/package/offline/write.py b/package/offline/write.py new file mode 100644 index 0000000..a4a21ef --- /dev/null +++ b/package/offline/write.py @@ -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}") \ No newline at end of file diff --git a/package/show.py b/package/show.py deleted file mode 100644 index 3d8315f..0000000 --- a/package/show.py +++ /dev/null @@ -1,84 +0,0 @@ -import os -import shutil -import subprocess -import sys -from datetime import datetime -import glob - - -def display(snippet_name=None, password=None, clipboard=None): - - if snippet_name is None and password is None and clipboard is None: - ls("/package/stash") # Enter stash directory - return - - current_time = datetime.now().strftime("%H%M") - - if snippet_name is None or password is None: - print("Both snippet_name and password must be provided") - return - - if str(password).zfill(4) != current_time: - raise ValueError("syntax error: incorrect password") - try: - base_dir = os.path.dirname(__file__) - snippets_dir = os.path.join(base_dir, "stash") - pattern = os.path.join(snippets_dir, f"{snippet_name}.*") - - matching_files = glob.glob(pattern) - - if not matching_files: - raise FileNotFoundError("No file found with the name.") - elif len(matching_files) > 1: - raise ValueError("Multiple files found with the given name.") - - snippet_path = os.path.join(snippets_dir, matching_files[0]) - - # If clipboard argument is passed as 1, copy content to clipboard - if clipboard == 1: - with open(snippet_path, "r") as file: - content = file.read() - copy_to_clipboard(content) - print("Content copied to clipboard.") - else: - # regular - with open(snippet_path, "r") as file: - content = file.read() - print(content) - - except Exception as e: - print(f"Syntax Error: {e}") - - -def copy_to_clipboard(text): - # Linux - if "linux" in sys.platform: - # Check if xclip is installed - if shutil.which("xclip") is None: - print("Error: xclip not found. Install it.", file=sys.stderr) - return - # If xclip is installed, proceed with copying text - subprocess.run( - ["xclip", "-selection", "clipboard"], - input=text.strip().encode(), - check=True, - ) - - # Windows - elif "win32" in sys.platform: - subprocess.run( - ["C:\\Windows\\System32\\clip.exe"], input=text.strip().encode(), check=True - ) - - # macOS - elif "darwin" in sys.platform: - subprocess.run(["/usr/bin/pbcopy"], input=text.strip().encode(), check=True) - - else: - raise OSError("Unsupported operating system") - - -def ls(directory_path): - contents = os.listdir(directory_path) - for files in contents: - print(files) diff --git a/package/test.py b/package/test.py deleted file mode 100644 index edc06f5..0000000 --- a/package/test.py +++ /dev/null @@ -1,5 +0,0 @@ -from scrap.grab import write, show, clip - -write("fghd", "text.txt") -show("fghd") -clip("fghd") diff --git a/package/write.py b/package/write.py deleted file mode 100644 index d984697..0000000 --- a/package/write.py +++ /dev/null @@ -1,36 +0,0 @@ -import os -import shutil -from datetime import datetime -import glob - - -def plot(snippet_name, password): - current_time = datetime.now().strftime("%H%M") - if str(password).zfill(4) != current_time: - raise ValueError("syntax error: incorrect password") - - try: - base_dir = os.path.dirname(__file__) - snippets_dir = os.path.join(base_dir, "stash") - pattern = os.path.join(snippets_dir, f"{snippet_name}.*") - - matching_files = glob.glob(pattern) - - if not matching_files: - raise FileNotFoundError("No file found with the name.") - elif len(matching_files) > 1: - raise ValueError("Multiple files found with the given name.") - - snippet_path = matching_files[0] - snippet_extension = os.path.splitext(snippet_path)[1] - output_path = os.path.join(base_dir, f"{snippet_name}{snippet_extension}") - - shutil.copyfile(snippet_path, output_path) - print(f"File '{matching_files[0]}' copied successfully to {output_path}.") - - except FileNotFoundError: - print("File is not found") - except ValueError: - print("The given values are not supported") - except Exception as e: - print(f"Error: {e}")