Skip to content
Edit this page

Core logic

src/core/ holds the plain-Python logic: no GTK, no requests, no settings. A value it needs from the settings (unit, 24h clock) is an argument. That is why it can be tested without a display, and why a decision ("is this humidity high?") belongs here and not in a widget.

Models (core/models.py)

API JSON becomes objects that are read as attributes. Every value is a WeatherField:

@dataclass
class WeatherField:
    data: Any          # the value, or a list of values for hourly and daily series
    unit: str = ""     # "°C", "km/h", "hPa", … as the API reported it
    level_str: str = ""  # "Moderate", "High", … added by the store
Model Built from Notes
CurrentWeather current and current_units uv_index, dewpoint_2m and visibility are added by the store from the hourly forecast; relativehumidity_2m, windspeed_10m and pressure_msl get a level_str
HourlyWeather hourly and hourly_units lists that share time
DailyWeather daily and daily_units days are in GMT (see below)
AirPollutionData air-quality hourly current_us_aqi and current_eu_aqi are added for this hour
Location a geocoding result

The models set an attribute for each key the API returns, so a new field only needs the request and (for IDE hints) an annotation. Read cw.temperature_2m.data, never a raw dict.

Levels (core/levels.py)

classify_* functions turn a number into the word a card shows. Words go through _() or C_() so they are translated, and ambiguous ones have a context ("Low" of UV is not "Low" of humidity).

Function Bands
classify_uv_index ≤2 Low · ≤5 Moderate · ≤7 High · ≤10 Very High · above Extreme
classify_humidity_level <50 Low · ≤80 Moderate · above High
classify_pressure_level below PRESSURE_LOW (1000 hPa) Low · up to PRESSURE_HIGH (1026) Normal · above High; imperial limits are converted
classify_wind_speed_level ≤1 Calm · ≤25 Light · ≤40 Moderate · ≤60 Strong · above Extreme (km/h)
classify_aqi, aqi_level_class, aqi_advice US AQI: Good, Moderate, Poor, Unhealthy, Severe, Hazardous; colour class and EPA-based advice
classify_eu_aqi European AQI: Good, Fair, Moderate, Poor, Very Poor, Extremely Poor
classify_pollutant(key, value) (word, colour class) against the WHO limits in config.THRESHOLDS
classify_pollen(value) Low ≤15 · Moderate ≤90 · High ≤500 · Very High
main_pollutant(values) the pollutant furthest above its WHO limit
classify_rain_intensity, classify_snow_intensity 1 to 4, from RAIN_INTENSITY_FROM_MM and SNOW_INTENSITY_FROM_CM
compass_direction(degrees) the nearest of eight compass points

Colour classes are libadwaita's success, warning and error. Every level colour has words next to it.

The wind bands match the overview's STRONG_WIND_KMH and EXTREME_WIND_KMH, so change them together.

Forecast helpers (core/forecast.py)

Function Does
nearest_hour_index(times, now) the index of the hour that is now (within half an hour), or 0. "Which hour is now" lives here and nowhere else
rain_drop_count(chance) one to four drops from the chance of rain
is_snow_code(code) whether a WMO code is snow
hourly_precipitation(snow_cm, rain_mm) ("snow", cm), ("rain", mm) or None, comparing the snow's water
today_high_low(...), today_sun_times(...) today's high and low, sunrise and sunset from daily data

Daily data is in GMT

fetch_daily asks for timezone=GMT, so each day's timestamp is its midnight in UTC. Match days by their UTC date against the city's "today", as today_high_low does, and convert for display with the city's time zone.

Locations (core/locations.py)

Saved cities are JSON strings in the added-cities setting. JsonProcessor converts the list both ways; LocationData has the formatting helpers (format_display_name, format_place, format_place_and_time, get_coords_key, saved_coords); parse_coords_key("lat,lon") returns [lat, lon]; find_selected_city(cities, selected) finds the selected one. The key of a city is "lat,lon" from its stored coordinates, which is also what selected-city holds.

Other modules

Module Does
core/units.py transform_visibility_data: metres to km or miles
core/timezones.py time_difference(zone, now): a city's time by zone name
core/cache.py the @cached decorator (see Data flow)
core/log.py get_logger, log_manager (silent unless debug mode is on)
core/forecast_client.py the ForecastClient protocol tests fake
core/overview/ see Overview architecture