44from time import sleep
55
66import yaml
7+ import py2snes
78
8- from py2snes import usb2snes
9- from py2snes import usb2snesException
9+ import asyncio
1010
1111def show_exception_and_exit (exc_type , exc_value , tb ):
1212 import traceback
@@ -24,6 +24,7 @@ def show_exception_and_exit(exc_type, exc_value, tb):
2424 with open (scriptpath + "\\ romloader.yaml" ) as configfile :
2525 try :
2626 config = yaml .load (configfile )
27+ print ("loading config file at " + os .path .abspath (scriptpath + "\\ romloader.yaml" ))
2728 except yaml .YAMLError as e :
2829 print (e )
2930 sys .exit (1 )
@@ -32,22 +33,20 @@ def show_exception_and_exit(exc_type, exc_value, tb):
3233 with open ("romloader.yaml" ) as configfile :
3334 try :
3435 config = yaml .load (configfile )
36+ print ("loading config file at " + os .path .abspath ("romloader.yaml" ))
3537 except yaml .YAMLError as e :
3638 print (e )
3739 sys .exit (1 )
3840 except FileNotFoundError :
3941 config = {"default_destination" : '/romloader' }
4042
4143
42- def main ():
44+ async def main ():
4345 try :
4446 rompath = sys .argv [1 ]
4547 except IndexError :
46- try :
47- rompath = config ['debug_copypath' ]
48- except :
49- raise IndexError ('We need a path to the ROM file to load.' )
50- sys .exit (1 )
48+ raise Exception ('We need a path to the ROM file to load.' )
49+ sys .exit (1 )
5150 filename = os .path .basename (rompath )
5251
5352 rule = matchrule (filename )
@@ -73,59 +72,48 @@ def main():
7372 path = '/romloader'
7473 romname = filename
7574
76- for a in range (10 ):
77- try :
78- # initiate connection to the websocket server
79- conn = usb2snes ()
80-
81- devicelist = conn .DeviceList ()
82-
83- # Attach to usb2snes, use the device configured if it is set, otherwise
84- # have it find the first device.
85- if "device" in config :
86- print ('Attaching to specified device {device}' .format (
87- device = config ['device' ]
88- ))
89- com = conn .Attach (config ['device' ])
90- elif len (devicelist ) > 1 :
91- com = conn .Attach (get_comm_device (devicelist ))
92- else :
93- print ('Attaching to first device found.' )
94- com = conn .Attach ()
95- print ('Attached to device \" {com}\" ' .format (
96- com = com
97- ))
98-
99- conn .Name ('RomLoader' )
100- if not rule :
101- conn .MakeDir ('/romloader' )
102- conn .List (path )
103- print ("copying rom to {fullpath}" .format (
104- fullpath = path + '/' + romname
105- ))
106- conn .PutFile (rompath , path + '/' + romname )
107- print ("verifying rom copy is complete" )
108- conn .List (path )
109- print ("booting rom" )
110- conn .Boot (path + '/' + romname )
111- conn .close ()
112-
113- sleep (5 )
114- break
115- except KeyboardInterrupt :
116- break
117- except :
118- print ("Unexpected error:" , sys .exc_info ()[0 ])
119- conn .close ()
120- sleep (6 )
121- continue
75+ # initiate connection to the websocket server
76+ snes = py2snes .snes ()
77+ await snes .connect ()
78+
79+ devicelist = await snes .DeviceList ()
80+
81+ # Attach to usb2snes, use the device configured if it is set, otherwise
82+ # have it find the first device.
83+ if "device" in config :
84+ print ('Attaching to specified device {device}' .format (
85+ device = config ['device' ]
86+ ))
87+ await snes .Attach (config ['device' ])
88+ elif len (devicelist ) > 1 :
89+ await snes .Attach (get_comm_device (devicelist ))
90+ else :
91+ print ('Attaching to first device found.' )
92+ await snes .Attach (devicelist [0 ])
93+ print ('Attached to device \" {com}\" ' .format (
94+ com = snes .device
95+ ))
96+
97+ await snes .Name ('RomLoader' )
98+ if not rule :
99+ await snes .MakeDir ('/romloader' )
100+ await snes .List (path )
101+ print ("copying rom to {fullpath}" .format (
102+ fullpath = path + '/' + romname
103+ ))
104+ await snes .PutFile (rompath , path + '/' + romname )
105+ print ("booting rom" )
106+ await snes .Boot (path + '/' + romname )
107+
108+ await asyncio .sleep (5 )
122109
123110
124111def matchrule (name ):
125112 if "rules" in config :
126113 for rule in config ['rules' ]:
127- if fnmatch .fnmatch (name , config ['rules' ][rule ]['name_pattern' ]):
128- return rule
114+ for pattern in config ['rules' ][rule ]['name_pattern' ]:
115+ if fnmatch .fnmatch (name , pattern ):
116+ return rule
129117 else :
130118 return None
131119
@@ -152,4 +140,6 @@ def get_comm_device(devicelist):
152140 dst_idx = int (input ('What device? ' ))
153141 return devicelist [dst_idx ]
154142
155- main ()
143+ if __name__ == '__main__' :
144+ loop = asyncio .get_event_loop ()
145+ loop .run_until_complete (main ())
0 commit comments