Skip to content

Commit

Permalink
Added ability to display humidity, Version bumped to 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepSpace2 committed Nov 1, 2020
1 parent 4d62938 commit 545f211
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.code-workspace

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ The following optional `args` are available:
| Argument | Type | Description | Default
| --- | --- | --- | --- |
| `condition_as_icon` | boolean | If `true`, condition will be displayed as an icon (if one of known conditions).<br>If `false` condition will be displayed as a string | `true` |
| `humidity_format` | string | A Python format string that accepts `humidity` as an argument | `"{humidity:.0f}%"` |
| `location_query` | string | Location in format CITY, 2-LETTERS-COUNTRY-CODE | Retrived using IP geolocation |
| `show `| string | Comma-separated string specifies what data to show.<br>Can include `"condition"`, `"temp"`.<br>See [Highlight Groups](#highlight-groups) | `"temp"` |
| `show `| string | Comma-separated string specifies what data to show.<br>Can include `"condition"`, `"humidity"`, `"temp"`.<br>See [Highlight Groups](#highlight-groups) | `"temp"` |
| `temp_format` | string | A Python format string that accepts `temp` as an argument | `"{temp:.0f}"` |
| `ttl_in_minutes` | integer | Time in minutes for which location and weather are cached.<br>**Warning: The lower the value the slower your terminal will be** | 60 |
| `units` | string | Temperature units.<br>Should be one of `"C"`, `"F"`, `"K"` | `"C"` |
Expand All @@ -87,9 +88,11 @@ Every data in `"show"` is displayed in its own segment with its own highlight gr

If a specific highlight group is not defined then the style of `"owmweather"` group will be used.


## Changelog

### 0.3 - Nov. 1 2020
* Added ability to display humidity

### 0.2 - Nov. 1 2020
* Added ability to display temperature, condition
* Added ability to display condition as either icons or strings
Expand All @@ -109,5 +112,5 @@ Initial release
- [x] Temperature
- [x] Condition
- [ ] Wind speed/direction
- [ ] Humidity
- [x] Humidity
- [ ] Pressure
34 changes: 24 additions & 10 deletions powerline_owmweather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,47 @@ def _fetch_weather(pl, location_query, units, openweathermap_api_key):
weather_json = json.loads(raw_response)
return {
'condition': weather_json['weather'][0]['main'].lower(),
'temp': float(weather_json['main']['temp'])
'humidity': float(weather_json['main']['humidity']),
'temp': float(weather_json['main']['temp'])
}
except (json.decoder.JSONDecodeError, KeyError, TypeError):
pl.error('openweathermap returned malformed or unexpected response: {0}', raw_response)
return None


@lru_cache()
def _weather(pl, *, openweathermap_api_key, location_query=None, units='C', temp_format='{temp:.0f}', show='temp', condition_as_icon=True,
ttl=None):
def _weather(pl, *, openweathermap_api_key,
condition_as_icon=True,
humidity_format='{humidity:.0f}%',
location_query=None,
show='temp',
temp_format='{temp:.0f}',
ttl=None,
units='C'):
pl.debug('_weather called with arguments {0}', locals())
location_query = location_query or _fetch_location(pl)
pl.debug('Fetching weather for {0}', location_query)
weather_dict = _fetch_weather(pl, location_query, units, openweathermap_api_key)
pl.debug('Parsed weather dict {0}', weather_dict)
if weather_dict:
condition = weather_dict['condition']
data_to_content_format = {
'temp': lambda: ' ' + temp_format.format(temp=weather_dict[data_to_show]) + temp_units_representation[units],
'condition': lambda: ' ' + (_get_icon_for_condition(condition) if condition_as_icon else condition)
'condition': lambda: ' ' + (_get_icon_for_condition(condition) if condition_as_icon else condition),
'humidity': lambda: ' ' + humidity_format.format(humidity=weather_dict[data_to_show]),
'temp': lambda: ' ' + temp_format.format(temp=weather_dict[data_to_show]) + temp_units_representation[units]
}
segments = []
for data_to_show in map(str.strip, show.split(',')):
segments.append({
'contents': data_to_content_format[data_to_show](),
'highlight_groups': ['owmweather_{}'.format(data_to_show), 'owmweather'],
'divider_highlight_group': 'background:divider'
})
try:
segment = {
'contents': data_to_content_format[data_to_show](),
'highlight_groups': ['owmweather_{}'.format(data_to_show), 'owmweather'],
'divider_highlight_group': 'background:divider'
}
pl.debug('Adding segment {0} for {1}', segment, data_to_show)
segments.append(segment)
except KeyError:
pl.error('Got invalid data_to_show {0}, ignoring it.', data_to_show)
return segments


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
description = 'A Powerline segment for fetching and showing the weather in the current location',
long_description = long_description,
long_description_content_type = 'text/markdown',
version = '0.2',
version = '0.3',
keywords = 'powerline weather segment terminal cli',
license = 'MIT',
author = 'DeepSpace2',
Expand Down

0 comments on commit 545f211

Please sign in to comment.