88
99import os .path
1010from configparser import NoOptionError , RawConfigParser
11- from typing import TYPE_CHECKING
11+ from typing import cast
1212
13- from xdg .BaseDirectory import load_config_paths # type: ignore[import-untyped]
13+ from xdg .BaseDirectory import load_first_config # type: ignore[import-untyped]
1414
1515import wlc
1616
17- if TYPE_CHECKING :
18- from collections .abc import Generator
19-
2017__all__ = ["NoOptionError" , "WeblateConfig" ]
2118
2219
2320class WeblateConfig (RawConfigParser ):
2421 """Configuration parser wrapper with defaults."""
2522
26- def __init__ (self , section = "weblate" ) -> None :
23+ def __init__ (self , section : str = "weblate" ) -> None :
2724 """Construct WeblateConfig object."""
2825 super ().__init__ (delimiters = ("=" ,))
29- self .section = section
26+ self .section : str = section
27+ self .cli_key : str | None = None
28+ self .cli_url : str | None = None
3029 self .set_defaults ()
3130
3231 def set_defaults (self ) -> None :
3332 """Set default values."""
3433 self .add_section ("keys" )
3534 self .add_section (self .section )
36- self .set (self .section , "key" , "" )
3735 self .set (self .section , "url" , wlc .API_URL )
3836 self .set (self .section , "retries" , "0" )
3937 self .set (self .section , "timeout" , "300" )
@@ -44,23 +42,27 @@ def set_defaults(self) -> None:
4442 self .set (self .section , "backoff_factor" , "0" )
4543
4644 @staticmethod
47- def find_configs () -> Generator [ str ] :
45+ def find_config () -> str | None :
4846 # Handle Windows specifically
4947 for envname in ("APPDATA" , "LOCALAPPDATA" ):
5048 if path := os .environ .get (envname ):
5149 win_path = os .path .join (path , "weblate.ini" )
5250 if os .path .exists (win_path ):
53- yield win_path
51+ return win_path
5452
5553 # Generic XDG paths
56- yield from load_config_paths ("weblate" )
57- yield from load_config_paths ("weblate.ini" )
54+ for filename in ("weblate" , "weblate.ini" ):
55+ if config := load_first_config (filename ):
56+ return config
57+
58+ return None
5859
59- def load (self , path = None ) -> None :
60+ def load (self , path : str | None = None ) -> None :
6061 """Load configuration from XDG paths."""
6162 if path is None :
62- path = list (self .find_configs ())
63- self .read (path )
63+ path = self .find_config ()
64+ if path :
65+ self .read (path )
6466
6567 # Try reading from current dir
6668 cwd = os .path .abspath ("." )
@@ -74,15 +76,10 @@ def load(self, path=None) -> None:
7476 prev = cwd
7577 cwd = os .path .dirname (cwd )
7678
77- def get_url_key (self ):
79+ def get_url_key (self ) -> tuple [ str , str ] :
7880 """Get API URL and key."""
79- url = self .get (self .section , "url" )
80- key = self .get (self .section , "key" )
81- if not key :
82- try :
83- key = self .get ("keys" , url )
84- except NoOptionError :
85- key = ""
81+ url = self .cli_url or cast ("str" , self .get (self .section , "url" ))
82+ key = self .cli_key or cast ("str" , self .get ("keys" , url , fallback = "" ))
8683 return url , key
8784
8885 def get_request_options (self ):
0 commit comments