Converting plugin V1 to V2 framework issues

I've been trying for a while to convert a plugin gamelauncher from V1 to V2 and managed to get most of it working again with the help of Mike. He pointed me to the "elevated permissions" option. After enabling most is working again except for some annoying quirks.

 

I can't seem to access files anymore ( they are outside the bundle ). Even with elevated permissions it looks like i run into the sandbox problem with that giving me "global name 'open' is not defined", same as for the "file" command:

 

prev = 0
for eachLine in open(fileLocation,"rb"):
	prev = zlib.crc32(eachLine, prev)
	fileCRC = "%X"%(prev & 0xFFFFFFFF)
if (fileCRC == "0"):
	fileCRC = None

 

Does someone have a sollution for that? i use it to calculate the CRC for files ...

 

Another problem i run into is howto test if an url works or not.

Even in a try statement it still dies hard.

 

try:
	result = HTTP.Request('http://localhost:32401' )
	Log(result)
except  Ex.HTTPError, e:
	Log(e.code)	

 

It results in:

 

  File "/Users/aequitas/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 1192, in do_open
    raise URLError(err)
URLError: 

2013-07-25 22:29:14,560 (-4faed000) : DEBUG (runtime:911) - Response: [404] NoneType, 0 bytes

 

 

Any help would be greatly appreciated :)

For the open() issue, you could try:

file_contents = Core.storage.load(path_to_file)
 

It's an undocumented internal framework function that *should* load the contents of the file into the assigned variable. I've also been advised that it defaults to binary mode which it looks is what you want.

Your issue with HTTP.Request() brings 2 things to mind. First, it looks like you're getting a '404 - Connection Refused' error which leads me to ask, do you mean to be attempting to open something on port 32401, rather than connect to PMS on port 32400? The second thing that I think of, which likely won't solve your problem but might help further down the road is that you probably want to access the content of the HTTP. Request rather than the request object itself, at least for logging purposes.

try:
    result = HTTP.Request('http://localhost:32400').content
    Log(result)
except  Ex.HTTPError, e:
    Log(e.code)

You could alternatively, check the headers with HTTP.Request(url).headers.

Thanks allot! the Core.storage.load functions works like a charm. Downside is, it's pulling the file into memory so i've limited to a 100Mb and it's doing it's job again :)

I've tried your suggestion by catching the headers, but it fails already at making the connection. You are correct, i've created a small webserver to coexist with the plugin to create a tiny interface to manipulate the games library. The webserver will be persistant so i need todo a check if it's not already started.

2013-07-26 12:29:41,598 (-4fa6b000) :  DEBUG (runtime:811) - Found route matching /video/gamelauncher/:/function/WebserverMenu
2013-07-26 12:29:41,598 (-4fa6b000) :  DEBUG (runtime:143) - Calling function 'WebserverMenu'
2013-07-26 12:29:41,599 (-4fa6b000) :  DEBUG (networking:233) - Fetching HTTP headers for 'http://localhost:32401'
2013-07-26 12:29:41,602 (-4fa6b000) :  CRITICAL (sandbox:303) - Exception when calling function 'WebserverMenu' (most recent call last):
  File "/Users/aequitas/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/code/sandbox.py", line 294, in call_named_function
    result = f(*args, **kwargs)
  File "/Users/aequitas/Library/Application Support/Plex Media Server/Plug-ins/Game Launcher.bundle/Contents/Code/__init__.py", line 89, in WebserverMenu
    result = HTTP.Request('http://localhost:' + str(Prefs['WEBSERVER_PORT']) ).headers
  File "/Users/aequitas/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 237, in headers
    f = self._opener.open(req)
  File "/Users/aequitas/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 438, in open
    response = self._open(req, data)
  File "/Users/aequitas/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 456, in _open
    '_open', req)
  File "/Users/aequitas/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 416, in _call_chain
    result = func(*args)
  File "/Users/aequitas/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 1217, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/Users/aequitas/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 1192, in do_open
    raise URLError(err)
URLError:  

Hi!

URLError != HTTPError, but you can catch both:

try:
    result = HTTP.Request('http://localhost:32401').headers
    Log(result)
except Ex.HTTPError, e:
    Log(e.code)
except Ex.URLError, e:
    Log(e.reason)
except:
    Log('Something else went wrong')

You can find some more info on these exceptions in the Python docs here: http://docs.python.org/2/library/urllib2.html#urllib2.URLError

Thanks Sander, that did the trick :) pfff feels like giving birth .. 2 small problems left:

Loading a resource image from a subdir in /resources doens't work. When putting the same image straight in the resource folder itself it does work ...

R('MAME.png') <-- this works
R('systems/MAME.png') <-- this doesn't work

Any idea why?

The last problem is a bit tricker that i can work around if needed but its nicer if it can be started from within plex, the webserver part. I fire off a bash helper file that starts a python webserver in it's turn ( so it can be pushed in the background and the helper finished without killing the webserver ). This used to work just fine. However with the sandbox thingy, the webserver won't start. I know the helper gets executed, but after that it just returns without any output.

Code/webserver.py -- "${serverport}" "${sqlitepath}"

I can't figure out why it's not starting / persistant running in the background ... none of the log files from "System log" to PMS log nor the plugin log reveal anything. Running the helper form terminal works just fine.

As far as I know, the Resource loader doesn't handle sub-folders. Not sure if that's by design or a bug. I'll try to find out.

I *think* that you should be able to launch your webserver via the channel code with a little help from the Framework's threading support and avoid the need for the bash helper which, btw would need to be ported for each platform you hope to support (OSX/Windows/Linux). I'm not particularly familiar with it but, I think that you should be able to launch your webserver function in a thread which gets backgrounded so that it doesn't interfere with the rest of the plugin execcution.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.