summaryrefslogtreecommitdiff
path: root/main.py
diff options
context:
space:
mode:
authorMichaël Ball <git@michaelball.name>2021-10-13 20:47:12 +0100
committerMichaël Ball <git@michaelball.name>2021-10-13 20:47:12 +0100
commit6d2c2ea3222c89c6fe13d6c589b7652475a984a0 (patch)
tree62f1ab8d2dae8adc6c516d255712da4774ddcecc /main.py
parentd1c5bf514be300931609b5c2213fc3aa630d18fd (diff)
Fix so it actually works
Diffstat (limited to 'main.py')
-rw-r--r--main.py46
1 files changed, 33 insertions, 13 deletions
diff --git a/main.py b/main.py
index 4a014b9..aac9e51 100644
--- a/main.py
+++ b/main.py
@@ -1,5 +1,5 @@
import argparse
-from configparser import ConfigParser
+from configparser import ConfigParser, NoOptionError
from datetime import datetime, timezone
import sys
@@ -8,36 +8,56 @@ from xdg import xdg_config_home
def get_location(args):
+ if args.latitude is not None and args.longitude is not None:
+ return args.latitude, args.longitude
+
config = ConfigParser()
config_location = xdg_config_home().joinpath("auto-dark-mode", "config")
+ with open(config_location) as stream:
+ config.read_string("[auto-dark-mode]\n" + stream.read())
config.read(config_location)
-
- if args.latitude and args.longitude:
- return latitude, longitude
try:
- latitude = config.getfloat("DEFAULT", "latitude")
- longitude = config.getfloat("DEFAULT", "longitude")
- except KeyError:
- sys.exit(f"Please supply latitude and longitude with the --latitude and --longtide flags, or set them in {config_location}")
+ latitude = config.getfloat("auto-dark-mode", "latitude")
+ longitude = config.getfloat("auto-dark-mode", "longitude")
+ except NoOptionError:
+ sys.exit(
+ f"Please supply latitude and longitude with the --latitude and --longitude flags, or set them in {config_location}"
+ )
return latitude, longitude
if __name__ == "__main__":
- parser = argparse.ArgumentParser(description="Check if we're in day (after sunrise but before sunset) or night (after sunset but before sunrise) time")
- parser.add_argument("--day", action="store_true", default=False, help="return successfully if it's day")
- parser.add_argument("--night", action="store_true", default=True, help="return successfully if it's night (default)")
+ parser = argparse.ArgumentParser(
+ description="Check if we're in day (after sunrise but before sunset) or night (after sunset but before sunrise) time"
+ )
+ parser.add_argument(
+ "--day",
+ action="store_true",
+ default=False,
+ help="return successfully if it's day",
+ )
+ parser.add_argument(
+ "--night",
+ action="store_true",
+ default=True,
+ help="return successfully if it's night (default)",
+ )
parser.add_argument("--latitude", type=float, help="specify your latitude")
parser.add_argument("--longitude", type=float, help="specify your longitude")
args = parser.parse_args()
latitude, longitude = get_location(args)
- sun = Sun(latitude, longitude)
+
+ try:
+ sun = Sun(latitude, longitude)
+ except SunTimeException:
+ sys.exit("Could not determine sunrise/sunset times")
utc_now = datetime.now(timezone.utc)
-
+
if utc_now > sun.get_sunrise_time() and utc_now < sun.get_sunset_time():
if not args.day:
sys.exit(1)