is file access was forbidden in python plugins?

Hi,

I was trying to write a plugin which grab movie information from Internet and save them into a customized XML file with the movie file together for future reading.

 

but seems I can't use either "open()" and "file()" function in the scripts. please check the log below

 

file1 = file(path, "w")

NameError: global name "file" is not defined

 

and when I change the calling into "open()" I got the same error message

 

Any one have idea with it?

 

Thanks a lot

 

 

 

 

 

I'm only now just starting to learn Python... but don't you have to import some module for that? 

"Plugin" is a bit evasive. it's an agent which follows a specifically defined framework [dev.plexapp.com/docs/api/datakit.html] or a scanner which can't use theses functions.  If you manage a metadata agent, it's in the wrong category (scanner)

Agents can't write to the video files folder but can read it. Agents can however write to the agent data folder but not create new folders, need to be done manually. Hama agent write to the agent data folder for xml and picture cache. Check Hama source code for Data.load like:


error_log['log_file']=["log1", "log 2", log 3]
 
 ### HAMA - Load logs, add non-present entried then Write log files to Plug-in /Support/Data/com.plexapp.agents.hama/DataItems ###
    for log in error_log:
      if error_log[log] != []:
        if not Data.Exists(log+".htm"):
          string=""
          if log == 'TVDB posters missing': string = WEB_LINK % ("http://thetvdb.com/wiki/index.php/Posters",             "Restrictions") + "
"
          if log == 'Plex themes missing':  string = WEB_LINK % ("https://plexapp.zendesk.com/hc/en-us/articles/201572843","Restrictions")+ "
"
        else:                               string = Data.Load(log+".htm")
    for entry in error_log[log]:
    if entry not in string:  Data.Save(log+".htm", string + entry + "
")

Scanners generally don't write files. My "Absolute series scanner" is the only one i saw to do such thing. It can write to the hdd using:

### Log function ########################################################################################
def Log(entry, filename='Plex Media Scanner Custom.log'): #need relative path
  #Logging = [ ['Synology',          "/volume1/Plex/Library/Application Support/Plex Media Server/Logs/"],
  #            ['Synology2',         "../../Plex/Library/Application Support/Plex Media Server/Logs/"],
  #            ['Ubuntu-10.04',      "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Logs/"]
  #            ['windows Vista/7/8', "C:\\Users\\Default\\AppData\\Local\\Plex Media Server\\Logs\\"],
  #            ['Qnap',              ""]
  #          ]
  try: 
    with open(filename, 'a') as file: #with open("/volume1/Plex/Library/Application Support/Plex Media Server/Logs/" + filename, 'a') as file:
      file.write( entry + "
" ) #
 make it readable in notepad under windows directly
      print line + "
"
  except:  pass
You call it like: Log("Scan: (root: '%s', path='%s', subdirs: %s, language: '%s')" % (root, path, str(subdirs), language)) from scanner side

open() is not available when running inside the Plugin sandbox environment. You can use os.open() instead

Here's the code to read a file and return the content as a string

def readFile(filename):
    '''Read a file and return its content
    This is used when running under Agent as open() is not
    available for use.
    @param filename  file to read
    @return          file content or None
    '''
    size = os.path.getsize(filename)
    fd = os.open(filename, os.O_RDONLY)
    data = os.read(fd, size)
    return data

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