python-2.7githubpygamedropboxdropbox-sdk

Dropbox script/Do something when download done


Maybe I missed something when I looked over the Dropbox uploader https://github.com/andreafabrizi/Dropbox-Uploader . I'm creating a python script that uploads files from a USB plugged into a RPi, but need to have it so when the upload is complete, a boolean changes to false. Is there a way to detect this? Code is commented where I want this to occur.

import pygame
from pygame.locals import *
from subprocess import call

uploadSym = pygame.image.load('uploadsymbol.png')
loadingSym = pygame.image.load('loading.png')
uploadSym = pygame.transform.scale(uploadSym, (250,300))
loadingSym = pygame.transform.scale(loadingSym, (300,300))
rect = uploadSym.get_rect()
rect = rect.move((200,60))
rect = loadingSym.get_rect()
rect = rect.move((200,60))
firstSlide = False

def toggle_fullscreen():
    screen = pygame.display.get_surface()
    tmp = screen.convert()
    caption = pygame.display.get_caption()
    cursor = pygame.mouse.get_cursor()  
    w,h = screen.get_width(),screen.get_height()
    flags = screen.get_flags()
    bits = screen.get_bitsize()

    pygame.display.quit()
    pygame.display.init()

    screen = pygame.display.set_mode((w,h),flags^FULLSCREEN,bits)
    screen.blit(tmp,(0,0))
    pygame.display.set_caption(*caption)
    pygame.key.set_mods(0)
    pygame.mouse.set_cursor( *cursor )

    return screen

if __name__ == '__main__':
    SW,SH = 640,480
    screen = pygame.display.set_mode((SW,SH),pygame.NOFRAME)
    pygame.display.set_caption('Uploader')

    _quit = False
    while not _quit:
        for e in pygame.event.get():
            if (e.type is KEYDOWN and e.key == K_RETURN and (e.mod&(KMOD_LALT|KMOD_RALT)) != 0):
                toggle_fullscreen()
            elif e.type is QUIT:
                _quit = True
            elif e.type is KEYDOWN and e.key == K_ESCAPE:
                _quit = True

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()  
    screen = pygame.display.get_surface()
    screen.fill([255, 255, 255])    

    #Click within the given area and upload begins  
    if 450 > mouse[0] > 250 and 360 > mouse[1] > 60 and click[0] == 1:
        firstSlide = True
    #T/F keeps png up for new slide
    elif firstSlide == True:
        loadRot = pygame.transform.rotate(loadingSym,0)
        screen.blit(loadRot, rect)

        #Upload test file to dropbox
        Upload = "/home/pi/Uploader/Dropbox-Uploader/dropbox_uploader.sh upload loading.png /"
        call ([Upload], shell=True)
        #Here I need an if statement that says, when upload done firstSlide = False
        #{
        #
        #
        #}

    else:
        screen.blit(uploadSym, rect)
    pygame.display.update()

Solution

  • The subprocess.call() function returns the exit-code of the command. So the call() will (should) return 0 on success, and non-zero on error.

    So:

    uploaded_ok = ( subprocess.call( Upload, shell=True) == 0 )
    

    Will give you a True/False success.

    NOTE: Link to 2.7 doco, since OP tagged question Python2.7