Skip to content
This repository was archived by the owner on Feb 22, 2020. It is now read-only.

Commit 39f8abc

Browse files
authored
Merge pull request #5 from biskette/configload-1
Adds config load + save for lat/lon from geoip, closes #1
2 parents d8718e4 + bc7e8bd commit 39f8abc

File tree

6 files changed

+238
-47
lines changed

6 files changed

+238
-47
lines changed

gutsy-gamblers/Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ verify_ssl = true
88
[packages]
99
kivy = "*"
1010
flake8 = "*"
11+
geopy="*"
1112
suntime = "*"
1213
kivy-deps-sdl2 = {markers = "platform_system == 'Windows'",version = "*"}
1314
kivy-deps-glew = {markers = "platform_system == 'Windows'",version = "*"}

gutsy-gamblers/Pipfile.lock

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gutsy-gamblers/datahelpers.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import requests
2+
3+
LOCATION_FRIENDLY = 'location_friendly'
4+
LOCATION_LATLON = 'location_latlon'
5+
LOCATION_VALID = 'location_valid'
6+
7+
8+
FAKE_DATA = False
9+
10+
11+
def commaval_or_empty(src: dict, key: str) -> str:
12+
"""
13+
Returns f", {value}" or '' if there's no value in the dict
14+
:param src: dict to fetch from
15+
:param key: key to get value from
16+
:return: str
17+
"""
18+
if key in src:
19+
return f", {src[key]}"
20+
return ''
21+
22+
23+
def guess_location_by_ip() -> dict:
24+
"""
25+
Guess location by IP
26+
27+
Needs more work in case of failure conditions
28+
:return:
29+
"""
30+
if FAKE_DATA:
31+
return {
32+
'location_latlon': '40.730610,-73.935242',
33+
'location_friendly': 'New York City, NY, United States'}
34+
35+
try:
36+
resp = requests.get('http://ip-api.com/json/').json()
37+
except ConnectionError:
38+
return None
39+
40+
location = {}
41+
42+
location[LOCATION_LATLON] = f"{resp['lat']},{resp['lon']}"
43+
44+
friendly_name = resp.get('city')
45+
friendly_name += commaval_or_empty(resp, 'region')
46+
friendly_name += commaval_or_empty(resp, 'country')
47+
# including ZIP might be too long
48+
# friendly_name += commaval_or_empty(resp, 'zip')
49+
50+
location[LOCATION_FRIENDLY] = friendly_name
51+
52+
return location

gutsy-gamblers/dials.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import pickle
21
from datetime import timedelta, datetime
32

3+
from geopy import Point
44
from kivy.animation import Animation
5+
from kivy.clock import Clock
56
from kivy.core.window import Window
6-
from kivy.properties import NumericProperty, Clock
7+
from kivy.properties import (
8+
NumericProperty,
9+
ObjectProperty,
10+
ConfigParserProperty,
11+
ReferenceListProperty
12+
)
713
from kivy.uix.effectwidget import EffectWidget, EffectBase
814
from kivy.uix.floatlayout import FloatLayout
915
from suntime import SunTimeException, Sun
1016

17+
import datahelpers
18+
1119
hv_blur = """
1220
vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords)
1321
{{
@@ -63,6 +71,20 @@ class DialWidget(FloatLayout):
6371
"""
6472
angle = NumericProperty(0)
6573

74+
config_latlon = latlon = ConfigParserProperty(
75+
'', 'global', datahelpers.LOCATION_LATLON, 'app', val_type=str)
76+
77+
latlon_point = ObjectProperty()
78+
79+
sunrise = NumericProperty()
80+
sunset = NumericProperty()
81+
sun_angles = ReferenceListProperty(sunrise, sunset)
82+
83+
def on_config_latlon(self, instance, value):
84+
"""Handler for property change event"""
85+
self.latlon_point = Point(value)
86+
self.redraw()
87+
6688
def __init__(self, **kwargs):
6789
super(DialWidget, self).__init__(**kwargs)
6890

@@ -78,10 +100,8 @@ def __init__(self, **kwargs):
78100
day=self.midnight.day,
79101
hour=0, minute=0, second=0) - self.date).seconds
80102

81-
# Split suntime tuple into named variables
103+
# Set sunrise and sunset through reference list
82104
self.sun_angles = self.sun_time()
83-
self.sunrise = self.sun_angles[0]
84-
self.sunset = self.sun_angles[1]
85105

86106
# Shading widget
87107
self.dial_shading = DialEffectWidget((self.sunrise, self.sunset))
@@ -106,9 +126,7 @@ def animate_dial(self):
106126

107127
def redraw(self, a=None):
108128
# Split suntime tuple into named variables
109-
sun_angles = self.sun_time()
110-
self.sunrise = sun_angles[0]
111-
self.sunset = sun_angles[1]
129+
self.sun_angles = self.sun_time()
112130

113131
# Remove widgets
114132
self.remove_widget(self.dial_shading)
@@ -132,10 +150,9 @@ def redraw(self, a=None):
132150
self.clock = Clock.schedule_interval(self.redraw, self.midnight_delta)
133151

134152
def sun_time(self):
135-
with open('latlong.tmp', 'rb') as f:
136-
lat_long = pickle.load(f)
137153

138-
sun_time = Sun(lat_long[0], lat_long[1])
154+
sun_time = Sun(self.latlon_point.latitude, self.latlon_point.longitude)
155+
139156
self.date = self.date + timedelta(days=self.date_increase)
140157

141158
try:

gutsy-gamblers/main.kv

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
font_size: 12
1515
pos_hint: {'center_x': 0.5, 'center_y': 0.95}
1616
size_hint: 0.2, 0.05
17+
on_press: root.settings_button()
1718

1819

1920
<TimeWizard>:
@@ -79,4 +80,43 @@
7980

8081
Button:
8182
text: "Revert date"
82-
on_press: root.revert_date()
83+
on_press: root.revert_date()
84+
85+
<SettingsScreen>:
86+
id: body
87+
# kivy automatically fills the corresponding properties on the
88+
# class in main.py with the objects of the id for propertyname : id
89+
location_field : loc
90+
feedback : feedback
91+
guess_location : guess
92+
close_button: close_settings
93+
title: "Settings"
94+
95+
BoxLayout:
96+
orientation: "vertical"
97+
98+
Label:
99+
text: "Enter a location or guess by ip"
100+
101+
TextInput:
102+
id: loc
103+
104+
Label:
105+
id: feedback
106+
text: ""
107+
108+
BoxLayout:
109+
orientation: "horizontal"
110+
111+
# Button:
112+
# text: "Update"
113+
114+
GuessLocationButton:
115+
id: guess
116+
settingsscreen: body
117+
text: "Guess by IP"
118+
119+
Button:
120+
id : close_settings
121+
text : "Close Settings"
122+
on_release: root.dismiss()

0 commit comments

Comments
 (0)