Skip to content

Commit

Permalink
updated formatting and fixed deepsource issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Descent098 committed Aug 31, 2021
1 parent 72209ab commit a76d6cb
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion sws/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Allows for direct invocation of cli using `python -m sws`"""
from cli import main

main()
main()
6 changes: 3 additions & 3 deletions sws/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def main():
autocomplete_file.write(autocomplete_file_text)
print("Bash autocompletion file written to /etc/bash_completion.d/sws.sh \nPlease restart shell for autocomplete to update")
except PermissionError:
print(f"Unable to write bash autocompletion file for sws are you sudo?")
exit()
print("Unable to write bash autocompletion file for sws are you sudo?")
sys.exit()

if args["dns"]:
dns_dict = get_dns_records(args['<domain>'], as_dict=True)
Expand Down Expand Up @@ -116,4 +116,4 @@ def main():
autocomplete_file.write(autocomplete_file_text)
print("Bash autocompletion file written to /etc/bash_completion.d/sws.sh \nPlease restart shell for autocomplete to update")
except PermissionError:
print(f"Unable to write bash autocompletion file for sws are you sudo?")
print("Unable to write bash autocompletion file for sws are you sudo?")
4 changes: 2 additions & 2 deletions sws/dns_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def get_dns_records(domain:str, as_dict:bool=False) -> Union[List[str], Dict[str
else: # If new record without existing value
logging.info(f"Writing data; {record_type}: {record_data.to_text()}")
result[record_type.name] = record_data.to_text()
except Exception as e:
except Exception:
... # Record doesn't exist
else:
result = []
Expand All @@ -180,7 +180,7 @@ def get_dns_records(domain:str, as_dict:bool=False) -> Union[List[str], Dict[str
for record_data in response:
logging.info(f"Parsing record response {record_type}: {record_data.to_text()}")
result.append(f"{record_type.name}: {record_data.to_text()}")
except Exception as e:
except Exception:
... # Record doesn't exist
if not result:
raise ValueError(f"Domain {domain} did not have any configured records, please check ")
Expand Down
2 changes: 1 addition & 1 deletion sws/domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def _install_whois():
subprocess.Popen("whois") # Check if binary is installed
except FileNotFoundError:
if os.name == "nt": # Install windows version of whois
logging.info(f"System is windows manually installing: {DOWNLOAD_FOLDER=}, {INSTALL_FOLDER=}")
logging.info(f"System is windows manually installing: DOWNLOAD_FOLDER = {DOWNLOAD_FOLDER}, INSTALL_FOLDER = {INSTALL_FOLDER}")
logging.info(f"Downloading whois from https://download.sysinternals.com/files/WhoIs.zip and installing to {INSTALL_FOLDER}")
build(ZIPResource("whois", "https://download.sysinternals.com/files/WhoIs.zip", overwrite_agreement=True))
move(f"{DOWNLOAD_FOLDER}{os.sep}whois", INSTALL_FOLDER)
Expand Down
15 changes: 11 additions & 4 deletions sws/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import requests # Used to download Files, and get file metadata
from tqdm import tqdm # Used to create a progress bar for active downloads


class Download:
"""Class used to help download files
Expand Down Expand Up @@ -123,7 +124,8 @@ class Download:
print(repr(d)) # Prints: Download for https://raw.githubusercontent.com/Descent098/sws/master/docs/img/sws-banner.png to download image.png to C:\\Users\\Kieran\\Desktop and has not been downloaded yet
```
"""
"""

def __init__(self, url: str, download_path: Union[bool, str] = False, filename: Union[bool, str] = False):
self.url = url
self.downloaded = False
Expand All @@ -143,7 +145,8 @@ def __init__(self, url: str, download_path: Union[bool, str] = False, filename:
self.download_path = download_path
else:
raise ValueError(f"Provided download path {download_path} does not exist")



def _get_filename(self):
"""Check http headers for filename"""
logging.info("No filename provided, checking headers")
Expand All @@ -153,7 +156,8 @@ def _get_filename(self):
else:
self.filename = self.url.split("/")[-1]
logging.info(f"Could not find filename, falling back to {self.filename}")



def _get_size(self) -> int:
"""Get the size of the file download
Expand All @@ -176,6 +180,7 @@ def _get_size(self) -> int:
except Exception as e: # Have to do catchall and re-raise as ValueError because hundreds of exception types can be raised
raise ValueError(f"{self.url} is not a valid url, connection failed to establish with error {e}")


def download(self):
"""Download a file from self.url"""
file_path = os.path.realpath(f"{self.download_path}{os.sep}{self.filename}")
Expand All @@ -199,18 +204,20 @@ def download(self):
progress_bar.close()
self.downloaded = True


def __str__(self):
return f"""Details for {self.filename}
size: {self.size}kb
downloaded: {self.downloaded}
url: {self.url}
download path: {self.download_path}"""


def __repr__(self):
return f"Download for {self.url} to download {self.filename} to {self.download_path} {'and has been downloaded' if self.downloaded else 'and has not been downloaded yet'}"


if __name__ == "__main__":
d = Download("https://raw.githubusercontent.com/Descent098/sws/master/docs/img/sws-banner.png")
print(d)
print(str(d))
print(str(d))
3 changes: 1 addition & 2 deletions sws/redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def trace(url: str, ignored_domains: Union[list, bool], print_result: bool = Tru
url = "http://" + url
logging.info(f"Changed url to {url}")


# Try going to the provided URL
logging.info("Starting HTTP request")
try:
Expand Down Expand Up @@ -123,7 +122,7 @@ def trace(url: str, ignored_domains: Union[list, bool], print_result: bool = Tru
else: # If the request was not redirected
if print_result:
print("Request was not redirected")
logging.info(f"Exiting trace() and returning ['Request was not redirected']")
logging.info("Exiting trace() and returning ['Request was not redirected']")
return ["Request was not redirected"]


Expand Down
2 changes: 1 addition & 1 deletion sws/ssl_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def get_ssl_issuer(hostname: str) -> Union[list, bool]:
logging.info(f"exiting get_ssl_issuer() and returning {issuer}")
return issuer
else:
logging.info(f"exiting get_ssl_issuer() and returning False")
logging.info("exiting get_ssl_issuer() and returning False")
return False


Expand Down
2 changes: 1 addition & 1 deletion sws/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import pytube.exceptions



def _request_path():
"""Prompts user for the link to a youtube video, and path of where to download the video to"""
logging.info("Entering _request_path()")
Expand Down Expand Up @@ -91,6 +90,7 @@ def get_video_metadata(video_url:str) -> dict:
logging.info(f"Exiting get_video_metadata() and returning {result}")
return result


def download(video_url: str, path: Union[str, bool]) -> str:
"""Downloads specified video_url to path
Expand Down

0 comments on commit a76d6cb

Please sign in to comment.