Help with Libraries/Shared for Sharing Code

I have Plex Plugin, that works, (https://github.com/stuckless/sagetv-phoenix-plex-channel), and it contains a main Channel (__init__.py), a Search Service and a URL Service.   

 

I need to share code between all 3 parts, and I've googled, and I've search, and I found that you can use a "Libraries/Shared" folder to put in shared code (although I saw in one plugin that it used a "Shared Code" folder... so I'm not 100% sure which is correct).

 

My problem is that the Libraries/Shared seems fairly useless for actually sharing code between the different parts of my channel.  I created a very simple use case ... 

 

In my Shared/Libraries/ I have a file sagedebug.py

 

def debugit(val='x'):
    Log("xxxxxxxxxxxxxxxxxxxx:" + val)
and if I call it from my init.py...
 
import sagedebug as xdebug
...
def Start():
    ...
    xdebug.debugit('init.py')
I get an error....
2014-01-28 18:51:27,735 (-48dcf700) :  CRITICAL (sandbox:306) - Exception when calling function 'Start' (most recent call last):
  File "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/code/sandbox.py", line 297, in call_named_function
    result = f(*args, **kwargs)
  File "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-ins/SageTVPhoenix.bundle/Contents/Code/__init__.py", line 82, in Start
    xdebug.debugit('init.py')
  File "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-ins/SageTVPhoenix.bundle/Contents/Libraries/Shared/sagedebug.py", line 4, in debugit
    Log("xxxxxxxxxxxxxxxxxxxx:" + val)
NameError: global name 'Log' is not defined
 

 

 

 

So, it would appear that I can't use the Plex Logging facilities from the shared file??

 

In another case, I replaced the Log statement with a simple print statement to log out the value of a Prefs[] field... and it didn't work :(

print("DEBUG: " + Prefs['server'] )

Again, I get an error that Prefs is not valid.

 

So, it would appear that I share code code, like a string utility, but I can't use the Plex system in the shared code, and I can't access any Plex Plugin preferences for my channel??  I'm pretty sure I must be doing something wrong, since this is a pretty trival use case.

 

Currently, to get around this, I have common file, sagex.py, that I actually have to cut and paste into each of my ServiceCode.pys files :(  (This is not a very maintainable way to organize "shared" code)

 

I'd appreciate if anyone could tell me how to properly have a shared code section.

 

Thanks,

 

 

 

 

If you want your Services to have access to the shared code, then it needs to be located in a directory called "Shared Code" within the plugin's Services directory. It will also need to be named xxx.pys rather than xxx.py (still a python file).

Take a look at the NHL Channel for an example.

This method has the added benefit of allowing you to use Plex framework functions such as Log().

Handling the import is different in the channel's __init__.py than it is in the ServiceCode.pys.

In the NHL channel's __init__.py, you can see that I defined a function as a reference to the shared code via the SharedCodeService..

In the URL Service, it can be done with a more straightforward import statement.

The other method (/Libraries/Shared/file.py) is more intended for external libraries. It's not used much so I'm having trouble thinking of examples. The plugin framework treats it entirely as a separate code library and as such it has no access to the plugin framework API. It is also not available for use by URL Services. In general, I would recommend against this method if you can avoid it.

Thank you very much... I KNEW I had to be missing something.  I can't wait to try this in a few hours :)

Thanks!

I finally managed to import a "webserver" I made - but it doesn't work really - responses aren't getting through.

I guess there's some limitation on what to run in Plex?

File descriptors are hijacked or something?

This isn't getting through nor cause an exception

    self.send_response(200) 
    self.send_header('Content-Type', 'text/xml')
    self.end_headers()
    with open('data.file') as f:
        data = f.read()
        f.close()
        self.wfile.write(data)

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