Skip to content

Commit 208640c

Browse files
authored
Merge pull request #82 from aaryanrr/master
Type Hint Fixes and Updated Guidelines
2 parents 3a577d7 + f0a1c56 commit 208640c

File tree

12 files changed

+54
-80
lines changed

12 files changed

+54
-80
lines changed

CONTRIBUTING.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Thank you so much for your interest in contributing!. All types of contributions
44

55
Please make sure to read the documentation before making your contribution! It will make it a lot easier for us maintainers to make the most of it and smooth out the experience for all involved. 💚
66

7-
### If you have a question about this project, how to use it, or just need clarification about something:
7+
### If you have a question about this project, how to use it, or just need clarification about something
88

99
* Open an Issue [here](https://github.com/Ankit404butfound/PyWhatKit/issues)
1010
* Provide as much context as you can about what you're running into.
@@ -20,9 +20,20 @@ If the project doesn't do something you need or want it to do:
2020

2121
### Code Formatting
2222

23-
Please make sure that your code follows the PEP8 standards and that you provide proper type hinting for the functions and parameters.
23+
Please make sure that your code follows the PEP8 standards and that you provide proper type hinting for the functions and parameters with proper spacing.
24+
For PyCharm users, you can use the shortcut `Ctrl + Alt + L` to reformat your code.
25+
For VSCode users, create a `.vscode` folder in the project directory and a `settings.json` file inside it.
26+
Inside the file add the following lines:
27+
28+
```json
29+
{
30+
"python.formatting.provider": "autopep8",
31+
"editor.formatOnSave": true
32+
}
33+
```
34+
35+
For others, you can directly use `autopep8 -i filename` in the terminal to format your code.
2436
Before committing your changes run `pre-commit` in your terminal, this would fix most of the common errors.
25-
You can use tools like `mypy` to look for more errors. We will be adding some configurations for that soon.
2637

2738
### Contributing Code
2839

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Ankit Raj Mahapatra
3+
Copyright (c) 2019 - present Ankit Raj Mahapatra
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

pywhatkit/ascii_art.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from PIL import Image
22

33

4-
def image_to_ascii_art(imgpath: str, output_file: str = "pywhatkit_asciiart.txt") -> str:
4+
def image_to_ascii_art(imgpath: str, output_file: str = "pywhatkit_asciiart") -> str:
55
"""Converts the given image to ascii art and save it to output_file"""
66
# pass the image as command line argument
77
image_path = imgpath
@@ -33,6 +33,6 @@ def image_to_ascii_art(imgpath: str, output_file: str = "pywhatkit_asciiart.txt"
3333
ascii_image = "\n".join(ascii_image)
3434

3535
# write to a text file.
36-
with open(output_file, "w") as f:
36+
with open(f"{output_file}.txt", "w") as f:
3737
f.write(ascii_image)
3838
return ascii_image

pywhatkit/chr_to_handwriting.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import requests
2-
from typing import NoReturn
32

43

5-
def text_to_handwriting(string: str, save_to: str = "pywhatkit.png", rgb: tuple = (0, 0, 138)) -> NoReturn:
4+
def text_to_handwriting(string: str, save_to: str = "pywhatkit.png", rgb: tuple = (0, 0, 138)) -> None:
65
"""Convert the given str to handwriting"""
76
data = requests.get(
87
"https://pywhatkit.herokuapp.com/handwriting?text=%s&rgb=%s,%s,%s" % (string, rgb[0], rgb[1], rgb[2])).content

pywhatkit/exceptions.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@ class InternetException(Exception):
1010
pass
1111

1212

13-
class InvalidBrowserName(Exception):
14-
pass
15-
16-
1713
class UnsupportedEmailProvider(Exception):
1814
pass

pywhatkit/help.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
import webbrowser as web
2-
from typing import NoReturn
32

43
baseurl = "https://github.com/Ankit404butfound/PyWhatKit/wiki/"
54

65

7-
def sendwhatmsg() -> NoReturn:
6+
def sendwhatmsg() -> None:
87
"""Open the Help Page for Sending WhatsApp Messages"""
98
web.open(f"{baseurl}Sending-WhatsApp-Messages")
109

1110

12-
def misc_functions() -> NoReturn:
11+
def misc_functions() -> None:
1312
"""Playonyt, search, info, take_screenshot, show_history, open_web help page"""
1413
web.open(f"{baseurl}Miscellaneous-Functions")
1514

1615

17-
def image_to_ascii_art() -> NoReturn:
16+
def image_to_ascii_art() -> None:
1817
"""Open the Help Page for image_to_ascii_art function"""
1918
web.open(f"{baseurl}ASCII-Art")
2019

2120

22-
def text_to_handwriting() -> NoReturn:
21+
def text_to_handwriting() -> None:
2322
"""Open the Help Page for text_to_handwriting function"""
2423
web.open(f"{baseurl}Text-to-Handwriting")
2524

2625

27-
def exceptions() -> NoReturn:
26+
def exceptions() -> None:
2827
"""Open the help page for the Exceptions within this library"""
2928
web.open(f"{baseurl}Exception")
3029

3130

32-
def mail() -> NoReturn:
31+
def mail() -> None:
3332
"""Open the help page for send_mail and send_hmail function"""
3433
web.open(f"{baseurl}Sending-a-Mail")
3534

3635

37-
def shutdown() -> NoReturn:
36+
def shutdown() -> None:
3837
"""Open the help page for shutdown and cancel_shutdown function"""
3938
web.open(f"{baseurl}Shutdown-and-CancelShutdown")

pywhatkit/history.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from typing import NoReturn
2-
3-
4-
def show_history() -> NoReturn:
1+
def show_history() -> None:
52
"""Prints the information of all the sent messages using this program"""
63
with open("pywhatkit_dbs.txt", "r") as f:
74
content = f.read()

pywhatkit/mail.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from email.mime.text import MIMEText
44
import re
55
from .exceptions import UnsupportedEmailProvider
6-
from typing import Union, NoReturn
6+
from typing import Union
77

88

99
def send_mail(email_sender: str, password: str, subject: str,
10-
message: Union[str, MIMEText], email_receiver: str) -> NoReturn:
10+
message: Union[str, MIMEText], email_receiver: str) -> None:
1111
"""Send an Email"""
1212

1313
domain = re.search("(?<=@)[^.]+(?=\\.)", email_sender)
@@ -39,7 +39,7 @@ def send_mail(email_sender: str, password: str, subject: str,
3939

4040

4141
def send_hmail(email_sender: str, password: str, subject: str,
42-
html_code: str, email_receiver: str) -> NoReturn:
42+
html_code: str, email_receiver: str) -> None:
4343
"""Send an Email with HTML code"""
4444

4545
message = MIMEText(html_code, "html")

pywhatkit/main.py

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
import os
77
from urllib.parse import quote
88
from .exceptions import *
9-
from typing import NoReturn, Optional
9+
from typing import Union, Optional
1010
from platform import system
1111

1212
if system().lower() in ("windows", "darwin"):
1313
from PIL import ImageGrab
1414

15-
def take_screenshot(file_name: str = 'pywhatkit_screenshot') -> NoReturn:
15+
def take_screenshot(file_name: str = 'pywhatkit_screenshot') -> None:
1616
"""Take Screenshot, you can change the filename as per your Wish"""
1717
screen = ImageGrab.grab()
1818
screen.show()
@@ -27,25 +27,21 @@ def take_screenshot(file_name: str = 'pywhatkit_screenshot') -> NoReturn:
2727
current_path = os.getcwd()
2828

2929

30-
def print_sleep_time() -> str:
30+
def print_sleep_time() -> Union[str, int]:
3131
return sleep_time
3232

3333

34-
def check_window() -> NoReturn:
34+
def check_window() -> None:
3535
"""Check if the browser Window is maximized or not"""
3636
web.open("https://www.google.com")
3737
pg.alert("If the browser's window is not maximized,\nMaximise and then close it if you want,\nor sendwhatmsg() "
3838
"function will not work", "Pywhatkit")
3939

4040

4141
def sendwhatmsg_instantly(phone_no: str, message: str, wait_time: int = 20,
42-
browser: str = None, tab_close: bool = False) -> NoReturn:
42+
tab_close: bool = False) -> None:
4343
"""Send WhatsApp Message Instantly"""
4444

45-
if browser and browser.lower() not in ["chrome", "firefox", "brave", "opera"]:
46-
raise InvalidBrowserName(
47-
"Browser name can be firefox, chrome, brave, opera")
48-
4945
if "+" not in phone_no:
5046
raise CountryCodeException("Country code missing from phone_no")
5147

@@ -54,10 +50,6 @@ def sendwhatmsg_instantly(phone_no: str, message: str, wait_time: int = 20,
5450
phone_no + '&text=' + parsed_message)
5551
time.sleep(2)
5652
width, height = pg.size()
57-
if browser:
58-
whats = pg.getWindowsWithTitle(browser)[0]
59-
whats.maximize()
60-
whats.activate()
6153
pg.click(width / 2, height / 2)
6254
time.sleep(wait_time - 2)
6355
pg.press('enter')
@@ -66,18 +58,11 @@ def sendwhatmsg_instantly(phone_no: str, message: str, wait_time: int = 20,
6658

6759

6860
def sendwhatmsg(phone_no: str, message: str, time_hour: int, time_min: int, wait_time: int = 20,
69-
print_wait_time: bool = True, browser: str = None, tab_close: bool = False) -> NoReturn:
61+
print_wait_time: bool = True, tab_close: bool = False) -> None:
7062
"""Sends a WhatsApp Message"""
7163
# Phone number should be given as a string
7264
# If the browser Window is not maximized this function won't work
7365
# Use check_window to check this
74-
75-
if browser is None:
76-
pass
77-
elif browser.lower() not in ["chrome", "firefox", "brave", "opera"]:
78-
raise InvalidBrowserName(
79-
"Browser name can be firefox, chrome, brave, opera")
80-
8166
global sleep_time
8267
if "+" not in phone_no:
8368
raise CountryCodeException("Country code missing from phone_no")
@@ -125,10 +110,6 @@ def sendwhatmsg(phone_no: str, message: str, time_hour: int, time_min: int, wait
125110
phone_no + '&text=' + parsed_message)
126111
time.sleep(2)
127112
width, height = pg.size()
128-
if browser:
129-
whats = pg.getWindowsWithTitle(browser)[0]
130-
whats.maximize()
131-
whats.activate()
132113
pg.click(width / 2, height / 2)
133114
time.sleep(wait_time - 2)
134115
pg.press('enter')
@@ -137,7 +118,7 @@ def sendwhatmsg(phone_no: str, message: str, time_hour: int, time_min: int, wait
137118

138119

139120
def sendwhatmsg_to_group(group_id: str, message: str, time_hour: int, time_min: int, wait_time: int = 20,
140-
print_wait_time: bool = True, tab_close: bool = False) -> NoReturn:
121+
print_wait_time: bool = True, tab_close: bool = False) -> None:
141122
"""Send WhatsApp Message to a Group"""
142123
# Group ID is in the group's invite link
143124
# https://chat.whatsapp.com/AB123CDEFGHijklmn, here AB123CDEFGHijklmn is group ID
@@ -183,9 +164,6 @@ def sendwhatmsg_to_group(group_id: str, message: str, time_hour: int, time_min:
183164
web.open('https://web.whatsapp.com/accept?code=' + group_id)
184165
time.sleep(2)
185166
width, height = pg.size()
186-
whats = pg.getWindowsWithTitle("WhatsApp")[0]
187-
whats.maximize()
188-
whats.activate()
189167
time.sleep(wait_time - 2)
190168
pg.click(width / 2, height - height / 10)
191169
pg.typewrite(message + "\n")
@@ -201,14 +179,14 @@ def info(topic: str, lines: int = 3, return_value: bool = False) -> Optional[str
201179
return spe
202180

203181

204-
def close_tab(wait_time: int = 2) -> NoReturn:
182+
def close_tab(wait_time: int = 2) -> None:
205183
"""Closes the Currently Opened Browser Tab"""
206184
time.sleep(wait_time)
207185
pg.hotkey("ctrl", "w")
208186
pg.press("enter")
209187

210188

211-
def playonyt(topic: str, use_api: bool = False) -> str:
189+
def playonyt(topic: str, use_api: bool = False) -> Union[str, None]:
212190
"""Play a YouTube Video"""
213191
# use_api uses the pywhatkit playonyt API to get the url for the video
214192
# use the api only if the function is not working properly on its own
@@ -246,7 +224,7 @@ def open_web() -> bool:
246224
return True
247225

248226

249-
def search(topic: str) -> NoReturn:
227+
def search(topic: str) -> None:
250228
"""Searches about the topic on Google"""
251229
link = 'https://www.google.com/search?q={}'.format(topic)
252230
web.open(link)

pywhatkit/sc.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
import platform
22
import os
3-
from typing import NoReturn
43
try:
54
import winerror
65
except ImportError or ModuleNotFoundError:
76
pass
8-
osname = platform.system()
97

10-
def shutdown(time: int = 20) -> NoReturn:
8+
9+
def shutdown(time: int = 20) -> None:
1110
"""
1211
Gives a shutdown request to the system with the specified time
13-
14-
Args:
15-
`time`: Governs the time for shutdown. Taken as `int` in unit `seconds`. `Specified default value is 20`
1612
"""
1713
# For Windows, time should be given in seconds
1814
# For MacOS and Linux based distributions, time should be given in minutes
19-
global osname
2015
osname = platform.system()
2116
if "window" in osname.lower():
2217
cont = f"shutdown -s -t {time}"
2318
ErrorCode = os.system(cont)
24-
if ErrorCode == winerror.ERROR_SHUTDOWN_IN_PROGRESS or ErrorCode == 1115:# Here 1115 is the error code of scheduled shutdown.
25-
print("A shutdown process has already been scheduled...\nIgnoring this process")
19+
# Here 1115 is the error code of scheduled shutdown.
20+
if ErrorCode == winerror.ERROR_SHUTDOWN_IN_PROGRESS or ErrorCode == 1115:
21+
print(
22+
"A shutdown process has already been scheduled...\nIgnoring this process")
2623
else:
2724
print(f"Your System will shutdown in {time} seconds")
2825

@@ -39,17 +36,17 @@ def shutdown(time: int = 20) -> NoReturn:
3936
"This function is for Windows, Mac and Linux users only, can't execute on {}".format(osname))
4037

4138

42-
def cancel_shutdown() -> NoReturn:
39+
def cancel_shutdown() -> None:
4340
"""
4441
Will cancel the scheduled shutdown
4542
"""
46-
global osname
4743
osname = platform.system()
4844
if "window" in osname.lower():
4945
cont = "shutdown /a"
5046
ErrorCode = os.system(cont)
5147
if ErrorCode == winerror.ERROR_NO_SHUTDOWN_IN_PROGRESS:
52-
print("ShutDown cancellation process has been aborted! [NO shutdown scheduled]")
48+
print(
49+
"ShutDown cancellation process has been aborted! [NO shutdown scheduled]")
5350
else:
5451
print("ShutDown has been cancelled")
5552

0 commit comments

Comments
 (0)