Help me plz....Plex embedded Python on diff. OS

I have a new project in the pipeline, that will make sure, that a sidecar srt file is utf-8 encoded

 

Works perfectly on Linux, tested on both QNAP and OpenSuse, but fails badly on windows....SNIFF

 

 

Project is found here:

 

https://github.com/ukdtom/SRT2UTF-8.bundle

 

In short, during conversion of an srt file, I use the codecs module with an open command

 

Code is:

with codecs.open(myFile + '.tmpPlex', "w", "utf-8") as targetFile:

However, on Windows, I get hit by the fact, that the codecs module only takes two parameters!?

 

And takers, and do either give me a hint here, or even better, fork and fix, and help me in this walk up the hill, making Plex the next best thing since sliced bread

 

/Tommy

I looks like it actually bugs out on opening the file to read. Seems like it might be a bug in Plex itself.

codecs.py is calling  __builtin__.open with 3 arguments and not 2.

File "C:\Users\Administrator\AppData\Local\Plex Media Server\Plug-ins\Framework.bundle\Contents\Resources\Versions\2\Python\Framework\api\agentkit.py", line 970, in _update
 agent.update(obj, media, lang, force)
File "C:\Users\Administrator\AppData\Local\Plex Media Server\Plug-ins\SRT2UTF-8.bundle\Contents\Code\__init__.py", line 37, in update
 FindSRT(part)
File "C:\Users\Administrator\AppData\Local\Plex Media Server\Plug-ins\SRT2UTF-8.bundle\Contents\Code\__init__.py", line 78, in FindSRT
 GetEnc(sSource)
File "C:\Users\Administrator\AppData\Local\Plex Media Server\Plug-ins\SRT2UTF-8.bundle\Contents\Code\__init__.py", line 93, in GetEnc
 ConvertFile(myFile, soup.originalEncoding)
File "C:\Users\Administrator\AppData\Local\Plex Media Server\Plug-ins\SRT2UTF-8.bundle\Contents\Code\__init__.py", line 113, in ConvertFile
 with codecs.open(myFile, 'r') as sourceFile:
File "C:\Program Files (x86)\Plex\Plex Media Server\python27.zip\codecs.py", line 881, in open
file = __builtin__.open(filename, mode, buffering)
TypeError: builtins_open() takes at most 2 arguments (3 given)

I'm still digging.

Chris

Huge thanks for digging into this.

Sent from my GT-I9505

Even with changing line 113 to: 

with codecs.open(myFile) as sourceFile:

It still bugs out.

codecs.py is the same on Windows and Ubuntu. So, I do believe it is a bug with the Windows version of Plex.

Chris

I just tried it on my Linux test machine and it works flawlessly. I'm not sure what to tell you.

Chris

Holy double post.

And thoughts from the Ninja's here?

/T

I believe I have a solution for you.

First a bug in GetEnc you didn't close the file. So, insert the following at line 89:

		f.close() 

I rewrote ConvertFile to use io.open instead of codecs.open. I think codecs.open may be better, but obviously won't work in Windows. This at least does work on Windows and Linux.

def ConvertFile(myFile, enc):
	Log.Debug('Converting file %s with an encoding of %s' %(myFile, enc))
	sourceFile = io.open(myFile, 'r', encoding=enc)
	targetFile = io.open(myFile + '.tmpPlex', 'w', encoding="utf-8")
	while True:
		contents = sourceFile.read()
		if not contents:
			break
		targetFile.write(contents)
sourceFile.close()
targetFile.close()

# Remove the original file
os.remove(myFile)
# Name tmp file as the original file name
os.rename(myFile + '.tmpPlex', myFile)
Log.Debug('Successfully converted %s to utf-8' %(myFile))

Hehehe........

Not only a cool workaround you provided me with here, but also a lot better.....

Just found this, saying that codecs.open should only be used, if backwards compatibility is needed for python 2.5 and earlier:

http://bugs.python.org/issue8796

And thanks for spotting the bug as well.

Sometimes, you just miss obvious stuff, after been looking at the darn thing for hours ;-)

I'll test your solution, and report back

/Tommy

Nice catch on that. I feel better about using io.open versus codecs.open. 

Gotta love programming. 

- 60% Direct code

- 35% Workarounds

-  5% I don't know why that works, but it does

lol

Chris

So true, merged the pull, and it's working perfectly.

Now it's time to create an official thread for it, and check with mike, if an agent fits the bill for his app-store

/T

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