Skip to content

Commit 2bad91d

Browse files
Added user config yaml
1 parent bcbd974 commit 2bad91d

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
.vscode
44
/spacesay/__pycache__
55
/dist
6-
poetry.lock
6+
poetry.lock
7+
config.yaml

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ spacesay = "spacesay.main:app"
1111
[tool.poetry.dependencies]
1212
python = "^3.10"
1313
typer = {extras = ["all"], version = "^0.7.0"}
14-
python-dotenv = "^0.21.1"
1514
requests = "^2.28.2"
15+
pyyaml = "^6.0"
1616

1717

1818
[build-system]

spacesay/location.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from datetime import date
55

66

7-
def iss_location() -> list[str]:
7+
def iss_location(username: str) -> list[str]:
88
# Request ISS location from open-notify
99
try:
1010
r = requests.get("http://api.open-notify.org/iss-now.json").json()
@@ -30,7 +30,7 @@ def iss_location() -> list[str]:
3030
querystring_geonames = {
3131
"lat": {lat},
3232
"lng": {lng},
33-
"username": os.environ.get("GEONAMES_USERNAME"),
33+
"username": username,
3434
}
3535

3636
responce_geonames = requests.request(

spacesay/main.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
11
import typer
2+
import rich
3+
import yaml
24
from typing import Optional
3-
from dotenv import load_dotenv
5+
46

57
from spacesay.location import iss_location
68
from spacesay.speech import speech_bubble
79

810

9-
load_dotenv() # Export dotenv into enviroment
11+
# Create or open a YAML config file and set username
12+
def set_username() -> str:
13+
try:
14+
stream = open("config.yaml", "w")
15+
rich.print(
16+
"Username not set, please visit [bold magenta]https://www.geonames.org/login[/bold magenta] to sign up and get a username."
17+
)
18+
username = input("Please enter your username: ")
19+
yaml.dump({"username": username}, stream)
20+
return username
21+
except:
22+
print("Failed to create config")
23+
raise FileNotFoundError
24+
25+
26+
username = ""
27+
28+
try:
29+
stream = open("config.yaml", "r")
30+
config = yaml.safe_load(stream)
31+
try:
32+
if config["username"] is None:
33+
username = set_username()
34+
else:
35+
username = config["username"]
36+
except:
37+
username = set_username()
38+
39+
except:
40+
print("Failed to open config, creating config file")
41+
username: str = set_username()
1042

1143

1244
astronaut = """
@@ -36,7 +68,7 @@
3668
@app.command()
3769
def main(text: Optional[str] = typer.Argument(None)):
3870
if text is None:
39-
location_time = iss_location()
71+
location_time = iss_location(username)
4072
speech_bubble(location_time)
4173

4274
else:

0 commit comments

Comments
 (0)