def torrent(conn, options, files): if options.user == None: user = '******' else: user = options.user params = [] useTarget = options.target != '' for file in files: # try: catch: finally: is buggy in python 2.4 # using separate try blocks as workaround try: try: stream = open(file, 'rb') content = db.PgBytea(stream.read()) now = int(time.time()) name = parseFilename(file) if useTarget: tupel = (user, name, name, now, content, options.target) else: tupel = (user, name, name, now, content) params.append(tupel) except IOError: sys.stderr.write('could not read file %s\n' % file) finally: stream.close() if useTarget: sql = "INSERT INTO download_queue (username, url, status, filename, pid, created_time, torrent, task_flags, seeding_interval, destination) VALUES (%s, %s, 1, %s, -1, %s, %s, 4, -1, %s)" else: sql = "INSERT INTO download_queue (username, url, status, filename, pid, created_time, torrent, task_flags, seeding_interval) VALUES (%s, %s, 1, %s, -1, %s, %s, 4, -1)" cursor = conn.cursor() cursor.executemany(sql, params) cursor.close() print 'Adding Torrent Files:' for e in params: print e[2] conn.commit()
print "encoding:", encoding log.write(u"----------------------------------------------" + '\n') log.write(u"testing encoding: [%s]" % encoding + '\n') log.write(u"Python string encoding [%s]" % sys.getdefaultencoding() + '\n') # reading file log.write(u"os.path.getsize(%s): [%s]" % (fname, os.path.getsize(fname)) + '\n') f = file(fname, "rb") img_data = f.read() f.close() log.write(u"type(img_data): [%s]" % type(img_data) + '\n') log.write(u"len(img_data) : [%s]" % len(img_data) + '\n') img_obj = dbapi.PgBytea(img_data) del (img_data) log.write(u"len(img_obj) : [%s]" % len(str(img_obj)) + '\n') log.write(u"type(img_obj): [%s]" % type(img_obj) + '\n') # setting connection level client encoding try: if encoding == 'sql_ascii': conn = dbapi.connect(dsn=dsn, unicode_results=0) else: conn = dbapi.connect(dsn=dsn, client_encoding=(encoding, 'strict'), unicode_results=0) curs = conn.cursor() cmd = "set client_encoding to '%s'" % encoding curs.execute(cmd)