mirror of
https://github.com/leoncvlt/loconotion.git
synced 2024-08-30 18:12:12 +00:00
Non-zero exit code on error
This changes all critical errors to raise exceptions. This allows to have a single critical error handler that sets exit code to non-zero. This way loconotion can be better used in automation jobs, where loconotion failure should stop subsequent tasks.
This commit is contained in:
parent
46c77076ab
commit
b4bc861e16
@ -3,6 +3,12 @@ import sys
|
|||||||
|
|
||||||
import modules.main as main
|
import modules.main as main
|
||||||
|
|
||||||
|
def _exit():
|
||||||
|
try:
|
||||||
|
sys.exit(1)
|
||||||
|
except SystemExit:
|
||||||
|
os._exit(1)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
args = main.get_args()
|
args = main.get_args()
|
||||||
@ -11,7 +17,10 @@ if __name__ == "__main__":
|
|||||||
parser.run()
|
parser.run()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
log.critical("Interrupted by user")
|
log.critical("Interrupted by user")
|
||||||
try:
|
_exit()
|
||||||
sys.exit(0)
|
except Exception as ex:
|
||||||
except SystemExit:
|
if args.verbose:
|
||||||
os._exit(0)
|
log.exception(ex)
|
||||||
|
else:
|
||||||
|
log.critical(f"{ex.__class__.__name__}: {str(ex)}")
|
||||||
|
_exit()
|
||||||
|
@ -16,7 +16,7 @@ try:
|
|||||||
|
|
||||||
except ModuleNotFoundError as error:
|
except ModuleNotFoundError as error:
|
||||||
log.critical(f"ModuleNotFoundError: {error}. Have you installed the requirements?")
|
log.critical(f"ModuleNotFoundError: {error}. Have you installed the requirements?")
|
||||||
sys.exit()
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def get_args():
|
def get_args():
|
||||||
@ -121,32 +121,30 @@ def setup_logging(args):
|
|||||||
|
|
||||||
def init_parser(args, log):
|
def init_parser(args, log):
|
||||||
# initialise the website parser
|
# initialise the website parser
|
||||||
try:
|
if urllib.parse.urlparse(args.target).scheme:
|
||||||
if urllib.parse.urlparse(args.target).scheme:
|
try:
|
||||||
try:
|
requests.get(args.target)
|
||||||
requests.get(args.target)
|
except requests.ConnectionError as exception:
|
||||||
except requests.ConnectionError as exception:
|
log.critical("Connection error")
|
||||||
log.critical("Connection error")
|
raise exception
|
||||||
|
|
||||||
if "notion.so" in args.target or "notion.site" in args.target:
|
|
||||||
log.info("Initialising parser with simple page url")
|
|
||||||
config = {"page": args.target}
|
|
||||||
parser = Parser(config=config, args=vars(args))
|
|
||||||
else:
|
|
||||||
log.critical(f"{args.target} is not a notion.so page")
|
|
||||||
|
|
||||||
elif Path(args.target).is_file():
|
|
||||||
with open(args.target, encoding="utf-8") as f:
|
|
||||||
parsed_config = toml.loads(f.read())
|
|
||||||
log.info("Initialising parser with configuration file")
|
|
||||||
log.debug(parsed_config)
|
|
||||||
parser = Parser(config=parsed_config, args=vars(args))
|
|
||||||
|
|
||||||
|
if "notion.so" in args.target or "notion.site" in args.target:
|
||||||
|
log.info("Initialising parser with simple page url")
|
||||||
|
config = {"page": args.target}
|
||||||
|
parser = Parser(config=config, args=vars(args))
|
||||||
else:
|
else:
|
||||||
log.critical(f"Config file {args.target} does not exist")
|
log.critical(f"{args.target} is not a notion.so page")
|
||||||
|
raise Exception()
|
||||||
|
|
||||||
except FileNotFoundError as e:
|
elif Path(args.target).is_file():
|
||||||
log.critical(f"FileNotFoundError: {e}")
|
with open(args.target, encoding="utf-8") as f:
|
||||||
sys.exit(0)
|
parsed_config = toml.loads(f.read())
|
||||||
|
log.info("Initialising parser with configuration file")
|
||||||
|
log.debug(parsed_config)
|
||||||
|
parser = Parser(config=parsed_config, args=vars(args))
|
||||||
|
|
||||||
|
else:
|
||||||
|
log.critical(f"Config file {args.target} does not exist")
|
||||||
|
raise FileNotFoundError(args.target)
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
@ -26,7 +26,7 @@ try:
|
|||||||
cssutils.log.setLevel(logging.CRITICAL) # removes warning logs from cssutils
|
cssutils.log.setLevel(logging.CRITICAL) # removes warning logs from cssutils
|
||||||
except ModuleNotFoundError as error:
|
except ModuleNotFoundError as error:
|
||||||
log.critical(f"ModuleNotFoundError: {error}. have your installed the requirements?")
|
log.critical(f"ModuleNotFoundError: {error}. have your installed the requirements?")
|
||||||
sys.exit()
|
sys.exit(1)
|
||||||
|
|
||||||
from .conditions import notion_page_loaded, toggle_block_has_opened
|
from .conditions import notion_page_loaded, toggle_block_has_opened
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ class Parser:
|
|||||||
" make sure it contains a 'page' key with the url of the notion.site"
|
" make sure it contains a 'page' key with the url of the notion.site"
|
||||||
" page to parse"
|
" page to parse"
|
||||||
)
|
)
|
||||||
return
|
raise Exception()
|
||||||
|
|
||||||
# get the site name from the config, or make it up by cleaning the target page's slug
|
# get the site name from the config, or make it up by cleaning the target page's slug
|
||||||
site_name = self.config.get("name", self.get_page_slug(index_url, extension=False))
|
site_name = self.config.get("name", self.get_page_slug(index_url, extension=False))
|
||||||
@ -222,7 +222,7 @@ class Parser:
|
|||||||
" https://chromedriver.chromium.org/downloads and use the"
|
" https://chromedriver.chromium.org/downloads and use the"
|
||||||
" --chromedriver argument to point to the chromedriver executable"
|
" --chromedriver argument to point to the chromedriver executable"
|
||||||
)
|
)
|
||||||
sys.exit()
|
raise exception
|
||||||
|
|
||||||
log.info(f"Initialising chromedriver at {chromedriver_path}")
|
log.info(f"Initialising chromedriver at {chromedriver_path}")
|
||||||
logs_path = Path.cwd() / ".logs" / "webdrive.log"
|
logs_path = Path.cwd() / ".logs" / "webdrive.log"
|
||||||
@ -259,12 +259,12 @@ class Parser:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
self.load_correct_theme(url)
|
self.load_correct_theme(url)
|
||||||
except TimeoutException:
|
except TimeoutException as ex:
|
||||||
log.critical(
|
log.critical(
|
||||||
"Timeout waiting for page content to load, or no content found."
|
"Timeout waiting for page content to load, or no content found."
|
||||||
" Are you sure the page is set to public?"
|
" Are you sure the page is set to public?"
|
||||||
)
|
)
|
||||||
return
|
raise ex
|
||||||
|
|
||||||
self.scroll_to_the_bottom()
|
self.scroll_to_the_bottom()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user