@@ -112,7 +112,32 @@ def load_goals(data: str, funding: int = 0, project: Project | None = None) -> d
112
112
}
113
113
114
114
115
- def funding_goals (source : str | list [tuple [str , str , str ]], funding : int = 0 ) -> dict :
115
+ def _load_goals_from_disk (path : str , funding : int = 0 ) -> dict [int , Goal ]:
116
+ try :
117
+ data = Path (path ).read_text ()
118
+ except OSError as error :
119
+ raise RuntimeError (f"Could not load data from disk: { path } " ) from error
120
+ return load_goals (data , funding )
121
+
122
+
123
+ def _load_goals_from_url (source_data : tuple [str , str , str ], funding : int = 0 ) -> dict [int , Goal ]:
124
+ project_name , project_url , data_fragment = source_data
125
+ data_url = urljoin (project_url , data_fragment )
126
+ try :
127
+ with urlopen (data_url ) as response : # noqa: S310
128
+ data = response .read ()
129
+ except HTTPError as error :
130
+ raise RuntimeError (f"Could not load data from network: { data_url } " ) from error
131
+ return load_goals (data , funding , project = Project (name = project_name , url = project_url ))
132
+
133
+
134
+ def _load_goals (source : str | tuple [str , str , str ], funding : int = 0 ) -> dict [int , Goal ]:
135
+ if isinstance (source , str ):
136
+ return _load_goals_from_disk (source , funding )
137
+ return _load_goals_from_url (source , funding )
138
+
139
+
140
+ def funding_goals (source : str | list [str | tuple [str , str , str ]], funding : int = 0 ) -> dict [int , Goal ]:
116
141
"""Load funding goals from a given data source.
117
142
118
143
Parameters:
@@ -123,20 +148,10 @@ def funding_goals(source: str | list[tuple[str, str, str]], funding: int = 0) ->
123
148
A dictionaries of goals, keys being their target monthly amount.
124
149
"""
125
150
if isinstance (source , str ):
126
- try :
127
- data = Path (source ).read_text ()
128
- except OSError as error :
129
- raise RuntimeError (f"Could not load data from disk: { source } " ) from error
130
- return load_goals (data , funding )
151
+ return _load_goals_from_disk (source , funding )
131
152
goals = {}
132
- for project_name , project_url , data_fragment in source :
133
- data_url = urljoin (project_url , data_fragment )
134
- try :
135
- with urlopen (data_url ) as response : # noqa: S310
136
- data = response .read ()
137
- except HTTPError as error :
138
- raise RuntimeError (f"Could not load data from network: { data_url } " ) from error
139
- source_goals = load_goals (data , funding , project = Project (name = project_name , url = project_url ))
153
+ for src in source :
154
+ source_goals = _load_goals (src )
140
155
for amount , goal in source_goals .items ():
141
156
if amount not in goals :
142
157
goals [amount ] = goal
0 commit comments