1414from .cursor import LGTVCursor
1515
1616
17- search_config = [
17+ config_paths = [
1818 "/etc/lgtv/config.json" ,
19- "~/.lgtv/config.json" ,
19+ os .path .join (os .environ .get ("XDG_CONFIG_HOME" , os .path .expanduser ("~/.config" )), "lgtv/config.json" ),
20+ os .path .expanduser ("~/.lgtv/config.json" ),
2021 "/opt/venvs/lgtv/config/config.json"
2122]
2223
@@ -60,37 +61,34 @@ def parseargs(command, argv):
6061 return output
6162
6263
63- def find_config ():
64- w = None
65- for f in search_config :
66- f = os .path .expanduser (f )
67- f = os .path .abspath (f )
64+ def find_config () -> str :
65+ for f in config_paths :
66+ if os .path .isfile (f ):
67+ return f
68+ # no config file exists yet
69+ for f in config_paths :
6870 d = os .path .dirname (f )
69- if os .path .exists (d ):
70- if os .access (d , os .W_OK ):
71- w = f
72- if os .path .exists (f ):
73- if os .access (f , os .W_OK ):
74- return f
75- elif os .access (os .path .dirname (d ), os .W_OK ):
76- os .makedirs (d )
77- w = f
78- if w is None :
79- print ("Cannot find suitable config path to write, create one in %s" % ' or ' .join (search_config ))
80- raise Exception ("No config file" )
81- return w
82-
83-
84- def write_config (filename , config ):
71+ if os .path .exists (d ) and os .access (d , os .W_OK ):
72+ return f
73+ # no config dir exists yet
74+ for f in config_paths :
75+ d = os .path .dirname (f )
76+ dd = os .path .dirname (d )
77+ if os .path .exists (dd ) and os .access (dd , os .W_OK ):
78+ return f
79+ print ("Cannot find suitable config path to write, create one in {}" .format (" or " .join (config_paths )))
80+ raise Exception ("No config file" )
81+
82+ def write_config (filename : str , config ):
83+ os .makedirs (os .path .dirname (filename ), exist_ok = True )
8584 with open (filename , 'w' ) as f :
86- f .write (json .dumps (config ))
87-
85+ json .dump (config , f )
8886
8987def main ():
9088 parser = argparse .ArgumentParser (
9189 'lgtv' ,
9290 description = '''LGTV Controller\n Author: Karl Lattimer <karl@qdh.org.uk>''' ,
93- epilog = get_commands (),
91+ epilog = get_commands (),
9492 formatter_class = argparse .RawDescriptionHelpFormatter
9593 )
9694 parser .add_argument ('--name' , '-n' , default = None )
@@ -105,24 +103,21 @@ def main():
105103 config = {}
106104
107105 filename = find_config ()
108- if filename is not None :
109- try :
110- with open (filename ) as f :
111- config = json .loads (f .read ())
112- except :
113- pass
106+ if os .path .isfile (filename ):
107+ with open (filename , "r" ) as f :
108+ config = json .load (f )
114109
115110 if args .command == "scan" :
116111 results = LGTVScan ()
117112 if len (results ) > 0 :
118- print (json .dumps ({
113+ print (json .dumps ({
119114 "result" : "ok" ,
120115 "count" : len (results ),
121116 "list" : results
122117 }))
123118 sys .exit (0 )
124119 else :
125- print (json .dumps ({
120+ print (json .dumps ({
126121 "result" : "failed" ,
127122 "count" : len (results )
128123 }))
@@ -138,9 +133,8 @@ def main():
138133 ws .run_forever ()
139134 sleep (1 )
140135 config [name ] = ws .serialise ()
141- if filename is not None :
142- write_config (filename , config )
143- print ("Wrote config file: " + filename )
136+ write_config (filename , config )
137+ print (f"Wrote config file { filename } " )
144138 sys .exit (0 )
145139
146140 elif args .command == "setDefault" :
@@ -153,7 +147,7 @@ def main():
153147 sys .exit (1 )
154148 config ["_default" ] = name
155149 write_config (filename , config )
156- print ( "Wrote default to config file: " + filename )
150+ print ( f "Wrote default to config file { filename } " )
157151
158152 # These commands require a TV name and config
159153 else :
@@ -196,6 +190,5 @@ def main():
196190 except KeyboardInterrupt :
197191 ws .close ()
198192
199-
200193if __name__ == '__main__' :
201194 main ()
0 commit comments