Community: Blogs

你在这里: You Are Here: Olet tässä: Vous êtes ici: Sie befinden sich hier: Tu sei qui: 現在のページ: Você está aqui: Вы здесь: Usted está aquí:

Tasty Multimedia Journal's Forum Nokia Blog

Unique, sortable filenames with Python

kevin_s2f | 31 July, 2007 01:32

I finally got some time to work on this code. I've accomplished some relatively minor tasks:

I thought through the best way to name and organize files. Time-stamping is a good way to pull unique names on the fly, and that's how LFD does it in the camera-and-viewfinder example over in the Wiki. 

 
     import time
     ctime = time.localtime()
     picName = str(ctime[0])+str(ctime[1])+str(ctime[2])+str(ctime[3])+str(ctime[4])+str(ctime[5])

Working with that assignment a bit, I didn't like the way it treated months and days -- January 11 comes out the same as November 1.

So I used formatting like this:

     import time
     ctime = time.localtime()
     fileNameRoot = '%4i%02i%02i%02i%02i%02i' % ((ctime[0]), (ctime[1]), (ctime[2]), (ctime[3]), (ctime[4]), (ctime[5]))

This allows me to assign filenames by the time when the first version was created, update the files as necessary (by adding more audio to a recording, for example,) and still reliably sort by first creation.

This lead me relatively quickly to split the image and audio files into separate directories. Using the same root file names for both the image files and audio files lets me put them back together easily (before I dig into the database engine). Separating them into two directories allows me to fill a listbox with elements drawn from one directory and not have to manually eliminate duplicates.

So that leaves me with the code below. Next step is to factor out the construction of the primary listbox so I can re-build it after adding a record. 


import e32, appuifw, time
import cameraandview

debug = 1
picPath = u'e:tmjimages' # remember first '' escapes the character.
audPath = u'e:tmjaudio'


# ToDo: add code to catch and process SymbianError -12 KErrPathNotFound if path does not yet exist.

def takepic(aPath, aFilename):
    import camera
    # save UI elements
    oldBody = appuifw.app.body
    oldExit = appuifw.app.exit_key_handler
    oldFocus = appuifw.app.focus
    oldMenu = appuifw.app.menu
    oldScreen = appuifw.app.screen
    oldTitle = appuifw.app.title
    if debug: appuifw.app.title = u'Let us take a picture here.'
    im = camera.take_photo()  # use all default values -- eventually this fn will change *.body into canvas control for viewfinder
    im.save(aPath + aFilename)
    #restore UI elements
    appuifw.app.body = oldBody
    appuifw.app.exit_key_handler = oldExit
    appuifw.app.focus = oldFocus
    appuifw.app.menu = oldMenu
    appuifw.app.screen = oldScreen
    appuifw.app.title = oldTitle
   

def recordaudio(aPath, aFilename):
   import e32, audio
 
   if debug: appuifw.note(u'hello from inside recordaudio',)
   s = audio.Sound.open(aPath + aFilename) # if file exists, append sound to end
   s.record()  # start recording
   e32.ao_sleep(5)  # do if for 5 seconds
   s.stop() # stop recording
 
 
   # the file is now created, ready to be played
   # s.play()       

def handle_selection():
    appuifw.note(u'hello from inside handle_selection',)
    index = lb.current()
    code = choices[index][1]
    lb.set_list([u'Please wait...'])
    if code == 'new':
        # construct unique name root that's easily sortable by time.
        ctime = time.localtime()
        fileNameRoot = '%4i%02i%02i%02i%02i%02i' % ((ctime[0]), (ctime[1]), (ctime[2]), (ctime[3]), (ctime[4]), (ctime[5]))
        if debug: appuifw.note(u'here is where we process a new experience', 'info')
        picName = fileNameRoot + u'.jpg'
        takepic (picPath, picName)
        if debug: appuifw.note(u'back to handle_selection after takepic',)
        audName = fileNameRoot + u'.amr'
        recordaudio (audPath, audName)
       
    elif code == 'comment':
        if debug: appuifw.note(u'here is where we add a comment', 'info')
        fileNameRoot = choices[index][0]
        fileNameRoot = fileNameRoot[8:] # strip off 'comment ' from UI
        audName = fileNameRoot + u'.amr'
        recordaudio (audPath, audName)
    else:
        appuifw.note(u'no valide code detected', 'info')
    lb.set_list(choices_labels)

def handle_add():
    pass 

def handle_delete():
    pass

def exit_key_handler():
    app_lock.signal()

# todo -- need to factor this out so handle_selection / new can re-build the lb
choices =[(u'New experience', 'new')]
picFiles = []
for item in os.listdir(picPath):
    if not item[0] == '_': picFiles.append(unicode(item[:-4])) # strip off file extension

picFiles.sort() # just in case it does not come from directory already sorted
picFiles.reverse() # and show last addition first

for item in picFiles:
    choices.append( (u'Comment ' + item, 'comment') )  # todo strip off extra chars b4 displaying
 
choices_labels = [x[0] for x in choices]
lb = appuifw.Listbox(choices_labels, handle_selection)

appuifw.app.title = u'Tasty Multimedia Journal'
appuifw.app.body = lb
appuifw.app.menu = [(u'Add new item', handle_add),
                    (u'Delete item', handle_delete)]
appuifw.app.exit_key_handler = exit_key_handler
if debug: appuifw.note(u'prepare to lock',)
app_lock = e32.Ao_lock()
if debug: appuifw.note(u'and now we wait',)
app_lock.wait()

 
 

Rate This

 
 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditFurlTechnocratiMagnoliaTwitter  Share this page Share this page Invite a friend Invite a friend
E-mail Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us Regional websites: Chinese Japanese © 2008 Nokia 
RDF Facets: qfnZtypeQUqfnTypeZBlogContentQ qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZWebpageQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX