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

Commit

Permalink
style: format code with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Oct 30, 2024
1 parent 66e59ac commit 3891582
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 42 deletions.
32 changes: 19 additions & 13 deletions package/scrap/grab.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,33 @@
import sys
import shutil


def copy_to_clipboard(text):
# Linux
if "linux" in sys.platform:
if shutil.which("xclip") is None:
print("Error: xclip not found. Install it.", file=sys.stderr)
return
subprocess.run(
["xclip", "-selection", "clipboard"],
input=text.strip().encode(),
check=True)
["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)
["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)
subprocess.run(["/usr/bin/pbcopy"], input=text.strip().encode(), check=True)

else:
raise OSError("Unsupported operating system")


def grab_content(url_name):
url = f"https://cl1p.net/{url_name}"
try:
Expand All @@ -51,31 +50,38 @@ def grab_content(url_name):
pass
return content


def show(url_name):
try:
content = grab_content(url_name)
if content:
print("The content is: ", content)
else:
print("Nothing found. The clipboard might be empty or you have entered a wrong URL.")
print(
"Nothing found. The clipboard might be empty or you have entered a wrong URL."
)
except Exception as e:
print(f"Error occurred: {e}")


def clip(url_name):
try:
content = grab_content(url_name)
if content:
copy_to_clipboard(content)
print("Content copied to clipboard.")
else:
print("Nothing found. The clipboard might be empty or you have entered a wrong URL.")
print(
"Nothing found. The clipboard might be empty or you have entered a wrong URL."
)
except Exception as e:
print(f"Error occurred: {e}")


def write(url_name, file):
try:
content = grab_content(url_name)
with open(file, 'w') as file:
with open(file, "w") as file:
file.write(content)
print("Content written to file successfully.")
except IOError as e:
Expand Down
4 changes: 1 addition & 3 deletions package/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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:
Expand Down Expand Up @@ -50,7 +50,6 @@ def display(snippet_name=None, password=None, clipboard=None):
print(f"Syntax Error: {e}")



def copy_to_clipboard(text):
# Linux
if "linux" in sys.platform:
Expand Down Expand Up @@ -83,4 +82,3 @@ def ls(directory_path):
contents = os.listdir(directory_path)
for files in contents:
print(files)

1 change: 0 additions & 1 deletion package/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
write("fghd", "text.txt")
show("fghd")
clip("fghd")

55 changes: 30 additions & 25 deletions tests/test_scrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,68 @@
from unittest.mock import patch, mock_open
from package.scrap.grab import copy_to_clipboard, grab_content, show, clip, write


class TestGrabFunctions(unittest.TestCase):
def setUp(self):
"""Set up test data that will be used across test methods"""
self.sample_html = '''
self.sample_html = """
<html>
<body>
<textarea id="cl1pTextArea">Test content</textarea>
</body>
</html>
'''
"""
self.test_content = "Test content"
@patch('subprocess.run')

@patch("subprocess.run")
def test_copy_to_clipboard_linux(self, mock_run):
# Mock platform to test Linux clipboard
with patch('sys.platform', 'linux'), \
patch('shutil.which', return_value='/usr/bin/xclip'):
with patch("sys.platform", "linux"), patch(
"shutil.which", return_value="/usr/bin/xclip"
):
copy_to_clipboard("test content")

mock_run.assert_called_once_with(
['xclip', '-selection', 'clipboard'],
input=b'test content',
check=True
["xclip", "-selection", "clipboard"], input=b"test content", check=True
)

@patch('subprocess.run')
@patch("subprocess.run")
def test_copy_to_clipboard_windows(self, mock_run):
with patch('sys.platform', 'win32'):
with patch("sys.platform", "win32"):
copy_to_clipboard("test text")
mock_run.assert_called_once_with(["C:\\Windows\\System32\\clip.exe"], input=b'test text', check=True)
mock_run.assert_called_once_with(
["C:\\Windows\\System32\\clip.exe"], input=b"test text", check=True
)

@patch('subprocess.run')
@patch("subprocess.run")
def test_copy_to_clipboard_macos(self, mock_run):
with patch('sys.platform', 'darwin'):
with patch("sys.platform", "darwin"):
copy_to_clipboard("test text")
mock_run.assert_called_once_with(["/usr/bin/pbcopy"], input=b'test text', check=True)

@patch('builtins.print')
@patch('package.scrap.grab.grab_content', return_value="test content")
mock_run.assert_called_once_with(
["/usr/bin/pbcopy"], input=b"test text", check=True
)

@patch("builtins.print")
@patch("package.scrap.grab.grab_content", return_value="test content")
def test_show(self, mock_grab_content, mock_print):
show("test-url")
mock_print.assert_called_once_with("The content is: ", "test content")

@patch('builtins.print')
@patch('package.scrap.grab.grab_content', return_value="test content")
@patch('package.scrap.grab.copy_to_clipboard')
@patch("builtins.print")
@patch("package.scrap.grab.grab_content", return_value="test content")
@patch("package.scrap.grab.copy_to_clipboard")
def test_clip(self, mock_copy_to_clipboard, mock_grab_content, mock_print):
clip("test-url")
mock_copy_to_clipboard.assert_called_once_with("test content")
mock_print.assert_called_once_with("Content copied to clipboard.")

@patch('builtins.open', new_callable=mock_open)
@patch('package.scrap.grab.grab_content', return_value="test content")
@patch("builtins.open", new_callable=mock_open)
@patch("package.scrap.grab.grab_content", return_value="test content")
def test_write(self, mock_grab_content, mock_open):
write("test-url", "test.txt")
mock_open.assert_called_once_with("test.txt", 'w')
mock_open.assert_called_once_with("test.txt", "w")
mock_open().write.assert_called_once_with("test content")

if __name__ == '__main__':

if __name__ == "__main__":
unittest.main()

0 comments on commit 3891582

Please sign in to comment.