Hi everyone,

This is not my code, but the code of washtub the liquidsoap controller 
made with django so for I've mailed both ML, django and liquidsoap

I have a directory full of any kind of files, some of this filenames are 
in latin1 and when I run my django app I get UnicodeDecodeError like this

Request Method:         GET
Request URL:    http://myhost/washtub/mediapool/scan
Exception Type:         DjangoUnicodeDecodeError
Exception Value:        

('utf8', '/MUSIC/bettie/music2/Buena Vista Social Club - Veinte 
A\xf1os.mp3', 73, 77, 'invalid data')

Exception Location: 
/usr/lib/python2.6/dist-packages/django/utils/encoding.py in 
force_unicode, line 92
Python Executable:      /usr/bin/python
Python Version:         2.6.2


from django.conf import settings
from django.core.paginator import Paginator
from django.http import Http404, HttpResponseRedirect, HttpResponse
from django.utils.encoding import smart_str, smart_unicode
from django.contrib.auth.decorators import login_required
from wtsite.mediapool.models import *
from os import access, stat, path, walk, F_OK, R_OK
from os.path import join, getsize
from stat import ST_MTIME
import tagpy, datetime, logging

def build_file_list(dir):
     logging.info('Start of build_file_list(%s)' % dir)
     if not (access(dir, (F_OK or R_OK))):
         return
     list = walk(dir,topdown=True)
     for root, dirs, files in list:
         for f in files:
             ext = path.splitext(f)[1]
             if ext in ('.mp3', '.flac'):
                 full_path = path.join(root,f)
                 mod_time = stat(full_path)[ST_MTIME]
                 mod_time = datetime.datetime.fromtimestamp(mod_time)
                 try:
                     #check update time and compare against database.
                     s = Song.objects.get(filename=full_path)
                     if(mod_time > s.date_modified):
                         s.date_modified=mod_time.isoformat(' ')
                         s.save()
                 except Song.DoesNotExist:
                     #add it into the database
                     now = datetime.datetime.now().isoformat(' ')
                     s = Song(filename=full_path, 
date_modified=mod_time, date_entered=now)
                     s.save()
     logging.info('End of build_file_list(%s)' % dir)
     return

def build_file_list2(dir):
     logging.info('Start of build_file_list2(%s)' % dir)
     if not (access(dir, (F_OK or R_OK))):
         return
     #empty all songs from current database.  This should be fast!!!
     d = Song.objects.all()
     d.delete()
     list = walk(dir,topdown=True)
     for root, dirs, files in list:
         for f in files:
             ext = path.splitext(f)[1]
             if ext in ('.mp3', '.flac'):
                 full_path = path.join(root,f)
                 mod_time = stat(full_path)[ST_MTIME]
                 mod_time = datetime.datetime.fromtimestamp(mod_time)
                 #try:
                 #    #check update time and compare against database.
                 #    s = Song.objects.get(filename=full_path)
                 #    if(mod_time > s.date_modified):
                 #        s.date_modified=mod_time.isoformat(' ')
                 #        s.save()
                 #except Song.DoesNotExist:
                     #add it into the database
                 now = datetime.datetime.now().isoformat(' ')
                 s = Song(filename=full_path, date_modified=mod_time, 
date_entered=now)
                 s.save()
     logging.info('End of build_file_list2(%s)' % dir)
     return

This are the 2 snippets that should generate the file list of a given 
directory

I tryed to change it like this:
     for root, dirs, files in list:
         for f in files:
            f = f.decode=('latin1').encode('utf-8')
             ext = path.splitext(f)[1]

the chars are computed correctly but then it does not match anymore the 
file on the filesystem.
I'm now tryng to write a tuple to match the decoded filename with the 
file on the filesystem but I thought there might be an easier way to do this

Thanks for the attention/suggestion/help.

fruity

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to