Architecture¶
src/ is installed as the mousam package, in four layers. The folder is the layer.
app.py → ui → state → services → core → config
Each layer imports only the ones to its right. Nothing enforces this, so keep to it by hand. Imports are relative (from ..core.levels import classify_aqi).
| Layer | Holds | Never |
|---|---|---|
core/ |
models, units, levels, forecast logic, overview rules, cache, logging | gi, requests, settings |
services/ |
Open-Meteo and geocoding HTTP, the internet check, the GSettings wrapper | widgets, GLib.idle_add |
state/ |
WeatherStore, OverviewEngine: what is loaded now, loading and error status |
widgets |
ui/ |
windows, sections, cards, drawings, dialogs, .ui templates |
HTTP calls |
core/ and config.py import no GTK and read no settings (a value they need, such as the unit or 24h clock, is an argument), so tests import them without a display. core/log.py imports gi only inside the methods that find the cache folder and open the log.
Files¶
src/
app.py WeatherApplication (Adw.Application): actions, CSS, auto-refresh,
notifications, normal ↔ compact switching
config.py constants: app ID, version, URLs, timeouts, thresholds
mousam.in launcher: gettext, GResource, then app.main()
mousam.gresource.xml files bundled into the GResource
css/style.css all custom styling
core/
models.py WeatherField, CurrentWeather, HourlyWeather, DailyWeather,
AirPollutionData, Location
levels.py classify_*, AQI helpers, compass_direction
units.py conversions the API doesn't do (visibility)
forecast.py nearest hour, rain drops, snow codes, today's high/low, sun times
locations.py LocationData, JsonProcessor, parse_coords_key, find_selected_city
timezones.py time_difference: a city's time by zone name
cache.py @cached: TTL + LRU cache for API calls
log.py LogManager and get_logger
forecast_client.py ForecastClient: the Protocol tests fake
overview/ the overview rules (see Weather overview)
services/
open_meteo.py fetch_current / fetch_hourly / fetch_daily / fetch_many / clear_cache
air_quality.py fetch_air_quality, clear_cache
geocoding.py find_city
network.py check_internet_connection
settings.py the Settings singleton: one property per GSettings key
selected_city.py coordinates, name and time zone of the selected city
state/
weather_store.py WeatherStore (weather_store)
overview_engine.py OverviewEngine (overview_engine)
ui/
window.py WeatherMainWindow: header bar, view states, the card grid
compact_window.py CompactWeather, CompactWeatherWindow
icons.py weather icon paths, background CSS classes, condition names
gtk_utils.py AutoRefreshTimer, weak_connect, safe_set_draw_func, toasts, is_on_screen
notifications.py OverviewNotifications
sections/ current.py, hourly.py, forecast.py
cards/ metric.py, day_night.py, air_quality.py
drawing/ small Cairo pieces (level_bar, bar, line_graph, temp_range, …)
dialogs/ preferences, locations, about, air_quality, shortcuts
templates/ Gtk.Template .ui files (shortcuts.ui)
tests/ unit tests and fakes
data/ gschema, .desktop, appdata, icons, Flatpak dependency
po/ translations
snap/ snapcraft.yaml
docs/ overview-rules.md (source of truth for the overview), screenshots/
Rules between the layers¶
- UI never calls HTTP. It reads
weather_storeand asks for data withweather_store.refresh(). The one exception is the city search inui/dialogs/locations.py, which callsfind_city. - API modules are pure. They build the URL, call
requests, and return JSON (with the unit conversions the API doesn't do) orNone. No GTK, no models, nosettings: every input is an argument. - The store builds models. It turns JSON into
core/models.pyobjects and adds derived values (levels fromcore/levels.py, this hour's fields fromcore/forecast.py). - Saved values only through
settings, never a newGio.Settings(...). - Constants go in
config.py: URLs, timeouts, thresholds, option lists. - Pass data in. A card takes the model it shows (
CardAirPollution(air)), not the store. Readingsettingsfor display preferences at build time is fine. - Import cycles are dodged with imports inside functions (the store, in
ui/window.py). Keep them there rather than reordering imports.
The shared instances are module-level singletons: settings, weather_store, overview_engine, log_manager. Never construct Settings() or WeatherStore(); tests do, with fakes.
Choosing the right tool¶
| What it is | Use |
|---|---|
| A calculation or decision (units, levels, drop count) | a plain function |
| Data with no behaviour (a forecast, a location) | @dataclass (frozen=True for new models) |
| State plus behaviour (cache, planner) | a class |
| State the UI must react to | a GObject.Object with properties and signals |
| Something on screen | a GTK widget subclass, or a class that owns one |
| A seam tests need to swap (an HTTP client) | a typing.Protocol, not an ABC |
A class with one method and no state is a function. There are no "Manager", "Helper" or "Utils" classes, no dependency-injection frameworks and no event buses. Prefer composition over inheritance: FirstHourRule is an HourTest plus a Phrasing, not one subclass per rule.
Adding a file¶
A new module goes in the folder of its layer, with a lowercase snake_case name that isn't a stdlib module's. Then add it to:
- the
meson.buildof its folder, or it won't be installed. A new folder needs an__init__.py, its ownmeson.buildand asubdir()in the parent's po/POTFILES, if it has translatable stringssrc/mousam.gresource.xml, if it is a.uifile or another bundled resource
Weather codes¶
Weather codes are WMO codes as strings ("61"), with an n suffix at night ("61n"). A code must be present in all four of icons_default, icons_material, bg_css and condition in ui/icons.py.