[mythtv-users] Rename a file problems

David Crawford davidcrawford83 at gmail.com
Fri Feb 10 18:27:25 UTC 2012


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

>
>
> On 10 February 2012 15:49, David Crawford <davidcrawford83 at gmail.com>wrote:
>
>>
>>
>> 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
>>
>> Remember, you still need to feed this new, modified path into the
> 'formatPath' method, to get out your desired result.
>
> Still can't figure this part out. Can anyone tell me how I can add this
> value:
> path = path.replace('%cN', chan.name)
>
> so it works with the 'formatPath'
>
> Thought it might work just adding it all to one line but it gives me a
> syntax error. Is there a certain command i need to add?
>
> cheers
>

Ok changing the formatPath to:

newbase = prog.formatPath(path).rsplit('.',1)[0]

half works as half of the filename is missing and outputs the file up until
where it says BBC, failing to output BBC ONE and the title.srt

Does anyone know what i need to add to make it give the full path here?

Dave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.mythtv.org/pipermail/mythtv-users/attachments/20120210/432ca1ad/attachment.html 


More information about the mythtv-users mailing list