Files
sky-cam/sunrise.py
T
Claude 45af48e46d Fix Python config parser swallowing inline comments as part of values
read_config / _read_conf now strips trailing inline shell comments
(whitespace + # + anything) before processing the value.  Without this,
a line like:

  SUNRISE_CAM=east                   # which camera faces east

produced SUNRISE_CAM = 'east                   # which camera faces east',
causing sunrise2mm.py to build a path with the comment embedded in it.

The regex \s+#.*$ requires at least one whitespace before # so passwords
or URLs containing # (e.g. %23 URL-encoded) are unaffected.

https://claude.ai/code/session_01C4jbd3waXG3eKZYbGUjLUQ
2026-04-22 15:01:53 +00:00

51 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
from datetime import datetime
import pathlib
import re
import pytz
from suntime import Sun
_here = pathlib.Path(__file__).resolve().parent
def _read_conf(path):
conf = {}
try:
with open(path) as f:
for line in f:
line = line.strip()
if not line or line.startswith('#') or '=' not in line:
continue
k, v = line.split('=', 1)
k = k.strip()
v = v.strip()
v = re.sub(r'\s+#.*$', '', v) # strip inline comments
v = v.strip('"').strip("'")
# Extract default from bash ${VAR:-default} pattern
m = re.match(r'^\$\{[^}]+:-([^}]*)\}$', v)
if m:
v = m.group(1).strip('"').strip("'")
conf[k] = v
except FileNotFoundError:
pass
return conf
conf = _read_conf(_here / 'sky-cam.conf')
# .env overrides sky-cam.conf — mirrors the sourcing order at the bottom of sky-cam.conf
conf.update(_read_conf(_here / '.env'))
for _key in ('LATITUDE', 'LONGITUDE', 'TIMEZONE'):
if not conf.get(_key):
raise SystemExit(f"sunrise.py: {_key} not set in sky-cam.conf or .env")
latitude = float(conf['LATITUDE'])
longitude = float(conf['LONGITUDE'])
tz = pytz.timezone(conf['TIMEZONE'])
sun = Sun(latitude, longitude)
now = datetime.now(tz)
sunrise_utc = sun.get_sunrise_time(now)
sunrise_time = sunrise_utc.astimezone(tz)
print(sunrise_time.strftime('%Y-%m-%d %H:%M:%S'))