[mythtv-users] Rename a file problems

David Crawford davidcrawford83 at gmail.com
Fri Feb 10 15:49:18 UTC 2012


On 10 February 2012 15:44, David Crawford <davidcrawford83 at gmail.com> wrote:

>
>
> On 10 February 2012 15:36, Raymond Wagner <raymond at wagnerrp.com> wrote:
>
>> On 2/10/2012 09:12, David Crawford wrote:
>> > path = '%cn %cc %cN.srt'
>>
>> Doing this in Python means you've stored that string to the variable
>> 'path'.  Now in Python, everything is object, and objects have methods.
>> In this particular case, you want to use the 'replace' method, which
>> replaces one string with another, and outputs the result.  That is this...
>>
>> >     path = path.replace('%'+tag, tmp)
>>
>> In Python, strings are 'mutable', meaning they cannot be changed.  The
>> only way to modify the string stored to a variable is to store a new
>> string to that variable, as described above.  Except you don't want that
>> path string up there.  You want to use this one defined lower...
>>
>> > '%m-%d_%H-%i-%s_%cN_%T'
>>
>> So define that to a variable, and replace whatever the bindings can't
>> handle.  That's what this does down here.
>>
>> > for (tag, data) in (('cn', 'channum'),('cc','callsign'),('cN','name')):
>> >     tmp = unicode(chan[data]).replace('/','-')
>> >     path = path.replace('%'+tag, tmp)
>>
>> It loops through those three sets of values, setting 'tag' to 'cn', and
>> 'data' to 'channum' on the first pass.  The Channel class is configured
>> to imitate a dictionary, or map, or associative array, depending on your
>> programming language of preference.  When it pulls 'chan[data]', it is
>> pulling the value mapped to the string currently stored in the 'data'
>> variable, which is 'channum', out of that associative array.
>>
>> Now you're only trying to use %cN, so there is no need for this
>> construct.  You can just do the following directly...
>>
>> > path = '%m-%d_%H-%i-%s_%cN_%T'
>> > chan = Channel(chanid)
>> > path = path.replace('%cN', chan.name)
>>
>> The Channel class makes all of those associative array values also
>> available as class attributes.  Attributes are just variables stored
>> against an object, and accessed in the same manner as methods (with a
>> '.').  Remember, you still need to feed this new, modified path into the
>> 'formatPath' method, to get out your desired result.
>>
>> _______________________________________________
>> mythtv-users mailing list
>> mythtv-users at mythtv.org
>> http://www.mythtv.org/mailman/listinfo/mythtv-users
>>
>
> Hi Raymond,

Thanks for the insights, i've now changed the script around a bit. The
thing is I still don't know how to feed the replaced object in this case
%cN into the format.path

it just gives me the same output and prints the path:
 path = '%m-%d_%H-%i-%sBBC ONE_%T'

How to I feed this into
newbase = prog.formatPath('%m-%d_%H-%i-%s_%cN_%T').rsplit('.',1)[0] ?

This is the script up to date:

#!/usr/bin/python

import sys
import os
from MythTV import Channel
from MythTV import MythBE, findfile
from MythTV import System


if len(sys.argv) != 3:
    raise Exception('Invalid argument count')
chanid, starttime = sys.argv[1:3]

be = MythBE()
prog = be.getRecording(chanid, starttime)
path = '%m-%d_%H-%i-%s_%cN_%T'
chan = Channel(chanid)
path = path.replace('%cN', chan.name)
oldbase = prog.filename.rsplit('.',1)[0]
newbase = prog.formatPath('%m-%d_%H-%i-%s_%cN_%T').rsplit('.',1)[0]
sg = findfile(prog.filename, prog.storagegroup)
if sg is None:
    raise Exception('file not found')


ccextractor = System(path="/home/dave/uksub2srt/uksub2srt.py")
ccextractor('-i', os.path.join(sg.dirname, prog.filename),
             '-o', '/home/dave/Downloads/subtitles/{0}.srt'.format(newbase))
print path
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.mythtv.org/pipermail/mythtv-users/attachments/20120210/be2fc7d4/attachment.html 


More information about the mythtv-users mailing list