Ejemplo n.º 1
0
def post_url(url, post_data, message=None, follow_redirects=False,
            progress_method=None, additional_headers=None):
    """Sends POST data to a URL and then returns the result.
    Accepts the URL to send the POST to, URL encoded data and
    optionally can follow redirects
    """
    temp_file = os.path.join(tempfile.mkdtemp(), 'tempdata')
    options = {'url': url,
               'file': temp_file,
               'follow_redirects': follow_redirects,
               'post_data': post_data,
               'additional_headers': header_dict_from_list(additional_headers),
               'logging_function': NSLog}
    NSLog('gurl options: %@', options)

    connection = Gurl.alloc().initWithOptions_(options)
    stored_percent_complete = -1
    stored_bytes_received = 0
    connection.start()
    try:
        while True:
            # if we did `while not connection.isDone()` we'd miss printing
            # messages and displaying percentages if we exit the loop first
            connection_done = connection.isDone()
            if message and connection.status and connection.status != 304:
                # log always, display if verbose is 1 or more
                # also display in progress field
                NSLog(message)
                if progress_method:
                    progress_method(None, None, message)
                # now clear message so we don't display it again
                message = None
            if (str(connection.status).startswith('2')
                    and connection.percentComplete != -1):
                if connection.percentComplete != stored_percent_complete:
                    # display percent done if it has changed
                    stored_percent_complete = connection.percentComplete
                    NSLog('Percent done: %@', stored_percent_complete)
                    if progress_method:
                        progress_method(None, stored_percent_complete, None)
            elif connection.bytesReceived != stored_bytes_received:
                # if we don't have percent done info, log bytes received
                stored_bytes_received = connection.bytesReceived
                NSLog('Bytes received: %@', stored_bytes_received)
                if progress_method:
                    progress_method(None, None,
                                    'Bytes received: %s'
                                    % stored_bytes_received)
            if connection_done:
                break

    except (KeyboardInterrupt, SystemExit):
        # safely kill the connection then re-raise
        connection.cancel()
        raise
    except Exception, err: # too general, I know
        # Let us out! ... Safely! Unexpectedly quit dialogs are annoying...
        connection.cancel()
        # Re-raise the error as a GurlError
        raise GurlError(-1, str(err))
Ejemplo n.º 2
0
def post_url(url, post_data, message=None, follow_redirects=False,
            progress_method=None, additional_headers=None):
    """Sends POST data to a URL and then returns the result.
    Accepts the URL to send the POST to, URL encoded data and
    optionally can follow redirects
    """
    temp_file = os.path.join(tempfile.mkdtemp(), 'tempdata')
    options = {'url': url,
               'file': temp_file,
               'follow_redirects': follow_redirects,
               'post_data': post_data,
               'additional_headers': header_dict_from_list(additional_headers),
               'logging_function': NSLog}
    NSLog('gurl options: %@', options)

    connection = Gurl.alloc().initWithOptions_(options)
    stored_percent_complete = -1
    stored_bytes_received = 0
    connection.start()
    try:
        while True:
            # if we did `while not connection.isDone()` we'd miss printing
            # messages and displaying percentages if we exit the loop first
            connection_done = connection.isDone()
            if message and connection.status and connection.status != 304:
                # log always, display if verbose is 1 or more
                # also display in progress field
                NSLog(message)
                if progress_method:
                    progress_method(None, None, message)
                # now clear message so we don't display it again
                message = None
            if (str(connection.status).startswith('2')
                    and connection.percentComplete != -1):
                if connection.percentComplete != stored_percent_complete:
                    # display percent done if it has changed
                    stored_percent_complete = connection.percentComplete
                    NSLog('Percent done: %@', stored_percent_complete)
                    if progress_method:
                        progress_method(None, stored_percent_complete, None)
            elif connection.bytesReceived != stored_bytes_received:
                # if we don't have percent done info, log bytes received
                stored_bytes_received = connection.bytesReceived
                NSLog('Bytes received: %@', stored_bytes_received)
                if progress_method:
                    progress_method(None, None,
                                    'Bytes received: %s'
                                    % stored_bytes_received)
            if connection_done:
                break

    except (KeyboardInterrupt, SystemExit):
        # safely kill the connection then re-raise
        connection.cancel()
        raise
    except Exception, err: # too general, I know
        # Let us out! ... Safely! Unexpectedly quit dialogs are annoying...
        connection.cancel()
        # Re-raise the error as a GurlError
        raise GurlError(-1, str(err))
Ejemplo n.º 3
0
Archivo: Utils.py Proyecto: groob/imagr
def get_url(url, destinationpath, message=None, follow_redirects=False):
    """Gets an HTTP or HTTPS URL and stores it in
    destination path. Returns a dictionary of headers, which includes
    http_result_code and http_result_description.
    Will raise GurlError if Gurl returns an error.
    Will raise HTTPError if HTTP Result code is not 2xx or 304.
    If destinationpath already exists, you can set 'onlyifnewer' to true to
    indicate you only want to download the file only if it's newer on the
    server.
    If you set resume to True, Gurl will attempt to resume an
    interrupted download."""

    tempdownloadpath = destinationpath + '.download'
    if os.path.exists(tempdownloadpath):
        os.remove(tempdownloadpath)

    options = {'url': url,
               'file': tempdownloadpath,
               'follow_redirects': follow_redirects,
               'logging_function': NSLog}
    NSLog('gurl options: %@',  options)

    connection = Gurl.alloc().initWithOptions_(options)
    stored_percent_complete = -1
    stored_bytes_received = 0
    connection.start()
    try:
        while True:
            # if we did `while not connection.isDone()` we'd miss printing
            # messages and displaying percentages if we exit the loop first
            connection_done = connection.isDone()
            if message and connection.status and connection.status != 304:
                # log always, display if verbose is 1 or more
                # also display in MunkiStatus detail field
                NSLog(message)
                # now clear message so we don't display it again
                message = None
            if (str(connection.status).startswith('2')
                and connection.percentComplete != -1):
                if connection.percentComplete != stored_percent_complete:
                    # display percent done if it has changed
                    stored_percent_complete = connection.percentComplete
                    NSLog('Percent done: %@', stored_percent_complete)
            elif connection.bytesReceived != stored_bytes_received:
                # if we don't have percent done info, log bytes received
                stored_bytes_received = connection.bytesReceived
                NSLog('Bytes received: %@', stored_bytes_received)
            if connection_done:
                break

    except (KeyboardInterrupt, SystemExit):
        # safely kill the connection then re-raise
        connection.cancel()
        raise
    except Exception, err: # too general, I know
        # Let us out! ... Safely! Unexpectedly quit dialogs are annoying...
        connection.cancel()
        # Re-raise the error as a GurlError
        raise GurlError(-1, str(err))
Ejemplo n.º 4
0
    def test_run_test_buttons():
        pygame.init()
        datingsim.init()
        gurl_imgs = {}
        gurl_imgs['askance'] = datingsim.assets.get_img_safe('GURL_kanaya_askance')
        gurl_imgs['happy'] = datingsim.assets.get_img_safe('GURL_kanaya_smile')
        gurl = Gurl("Kanaya", gurl_imgs, None)
        gurl.exp = 3998
        d = MeetScene(gurl, use_default_buttons=False)
        d.add_button("test button", lambda: print("test 1"))
        def finish():
            d.done = True
        d.add_button("end", finish)
        d.add_button("update conv", lambda: d.update_conversation("changed"))
        def increase_exp():
            d.gurl.exp += 1
        d.add_button("increase exp", increase_exp)
        d.add_button("happy", lambda: d.change_mood("happy"))
        d.add_button("askance", lambda: d.change_mood("askance"))

        d.main_loop()
        d.ath()
        datingsim.quit()
        pygame.quit()
Ejemplo n.º 5
0
def get_url(url, destinationpath, message=None, follow_redirects=False,
            progress_method=None, additional_headers=None):
    """Gets an HTTP or HTTPS URL and stores it in
    destination path. Returns a dictionary of headers, which includes
    http_result_code and http_result_description.
    Will raise GurlError if Gurl returns an error.
    Will raise HTTPError if HTTP Result code is not 2xx or 304.
    If destinationpath already exists, you can set 'onlyifnewer' to true to
    indicate you only want to download the file only if it's newer on the
    server.
    If you set resume to True, Gurl will attempt to resume an
    interrupted download."""

    tempdownloadpath = destinationpath + '.download'
    if os.path.exists(tempdownloadpath):
        os.remove(tempdownloadpath)

    options = {'url': url,
               'file': tempdownloadpath,
               'follow_redirects': follow_redirects,
               'additional_headers': header_dict_from_list(additional_headers),
               'logging_function': NSLog}
    NSLog('gurl options: %@', options)

    connection = Gurl.alloc().initWithOptions_(options)
    stored_percent_complete = -1
    stored_bytes_received = 0
    connection.start()
    try:
        while True:
            # if we did `while not connection.isDone()` we'd miss printing
            # messages and displaying percentages if we exit the loop first
            connection_done = connection.isDone()
            if message and connection.status and connection.status != 304:
                # log always, display if verbose is 1 or more
                # also display in progress field
                NSLog(message)
                if progress_method:
                    progress_method(None, None, message)
                # now clear message so we don't display it again
                message = None
            if (str(connection.status).startswith('2')
                    and connection.percentComplete != -1):
                if connection.percentComplete != stored_percent_complete:
                    # display percent done if it has changed
                    stored_percent_complete = connection.percentComplete
                    NSLog('Percent done: %@', stored_percent_complete)
                    if progress_method:
                        progress_method(None, stored_percent_complete, None)
            elif connection.bytesReceived != stored_bytes_received:
                # if we don't have percent done info, log bytes received
                stored_bytes_received = connection.bytesReceived
                NSLog('Bytes received: %@', stored_bytes_received)
                if progress_method:
                    progress_method(None, None,
                                    'Bytes received: %s'
                                    % stored_bytes_received)
            if connection_done:
                break

    except (KeyboardInterrupt, SystemExit):
        # safely kill the connection then re-raise
        connection.cancel()
        raise
    except Exception, err: # too general, I know
        # Let us out! ... Safely! Unexpectedly quit dialogs are annoying...
        connection.cancel()
        # Re-raise the error as a GurlError
        raise GurlError(-1, str(err))
Ejemplo n.º 6
0
def get_url(url,
            destinationpath,
            custom_headers=None,
            message=None,
            onlyifnewer=False,
            resume=False,
            follow_redirects=False):
    """Gets an HTTP or HTTPS URL and stores it in
    destination path. Returns a dictionary of headers, which includes
    http_result_code and http_result_description.
    Will raise CurlError if Gurl returns an error.
    Will raise HTTPError if HTTP Result code is not 2xx or 304.
    If destinationpath already exists, you can set 'onlyifnewer' to true to
    indicate you only want to download the file only if it's newer on the
    server.
    If you set resume to True, Gurl will attempt to resume an
    interrupted download."""

    tempdownloadpath = destinationpath + '.download'
    if os.path.exists(tempdownloadpath) and not resume:
        if resume and not os.path.exists(destinationpath):
            os.remove(tempdownloadpath)

    cache_data = None
    if onlyifnewer and os.path.exists(destinationpath):
        # create a temporary Gurl object so we can extract the
        # stored caching data so we can download only if the
        # file has changed on the server
        gurl_obj = Gurl.alloc().initWithOptions_({'file': destinationpath})
        cache_data = gurl_obj.get_stored_headers()
        del gurl_obj

    options = {
        'url': url,
        'file': tempdownloadpath,
        'follow_redirects': follow_redirects,
        'can_resume': resume,
        'additional_headers': header_dict_from_list(custom_headers),
        'download_only_if_changed': onlyifnewer,
        'cache_data': cache_data,
        'logging_function': munkicommon.display_debug2
    }
    munkicommon.display_debug2('Options: %s' % options)

    connection = Gurl.alloc().initWithOptions_(options)
    stored_percent_complete = -1
    stored_bytes_received = 0
    connection.start()
    try:
        while True:
            # if we did `while not connection.isDone()` we'd miss printing
            # messages and displaying percentages if we exit the loop first
            connection_done = connection.isDone()
            if message and connection.status and connection.status != 304:
                # log always, display if verbose is 1 or more
                # also display in MunkiStatus detail field
                munkicommon.display_status_minor(message)
                # now clear message so we don't display it again
                message = None
            if (str(connection.status).startswith('2')
                    and connection.percentComplete != -1):
                if connection.percentComplete != stored_percent_complete:
                    # display percent done if it has changed
                    stored_percent_complete = connection.percentComplete
                    munkicommon.display_percent_done(stored_percent_complete,
                                                     100)
            elif connection.bytesReceived != stored_bytes_received:
                # if we don't have percent done info, log bytes received
                stored_bytes_received = connection.bytesReceived
                munkicommon.display_detail('Bytes received: %s',
                                           stored_bytes_received)
            if connection_done:
                break

    except (KeyboardInterrupt, SystemExit):
        # safely kill the connection then re-raise
        connection.cancel()
        raise
    except Exception, err:  # too general, I know
        # Let us out! ... Safely! Unexpectedly quit dialogs are annoying...
        connection.cancel()
        # Re-raise the error as a GurlError
        raise GurlError(-1, str(err))
Ejemplo n.º 7
0
def getToken(message=None, follow_redirects=False,
            progress_method=None):
    """generates user and token"""
    global token_temp
    if not token_temp:
        temp_file = os.path.join(tempfile.mkdtemp(), 'tempdata')
        token_temp = temp_file

        url = getPlistData('token_url')
        username = getPlistData('tokenuser')
        password = getPlistData('tokenpass')

        post_data = {
            'username': username,
            'password': password
        }
        post_data = urllib.urlencode(post_data)

        options = {'url': url,
                   'file': temp_file,
                   'follow_redirects': follow_redirects,
                   'post_data': post_data,
                   'logging_function': NSLog}
        NSLog('gurl options: %@', options)

        connection = Gurl.alloc().initWithOptions_(options)
        stored_percent_complete = -1
        stored_bytes_received = 0
        connection.start()
        try:
            while True:
                # if we did `while not connection.isDone()` we'd miss printing
                # messages and displaying percentages if we exit the loop first
                connection_done = connection.isDone()

                if message and connection.status and connection.status != 304:
                    # log always, display if verbose is 1 or more
                    # also display in progress field
                    NSLog(message)
                    if progress_method:
                        progress_method(None, None, message)
                    # now clear message so we don't display it again
                    message = None
                if (str(connection.status).startswith('2')
                        and connection.percentComplete != -1):
                    if connection.percentComplete != stored_percent_complete:
                        # display percent done if it has changed
                        stored_percent_complete = connection.percentComplete
                        NSLog('Percent done: %@', stored_percent_complete)
                        if progress_method:
                            progress_method(None, stored_percent_complete, None)
                elif connection.bytesReceived != stored_bytes_received:
                    # if we don't have percent done info, log bytes received
                    stored_bytes_received = connection.bytesReceived
                    NSLog('Bytes received: %@', stored_bytes_received)
                    if progress_method:
                        progress_method(None, None,
                                        'Bytes received: %s'
                                        % stored_bytes_received)
                if connection_done:
                    break

        except (KeyboardInterrupt, SystemExit):
            # safely kill the connection then re-raise
            connection.cancel()
            raise
        except Exception, err: # too general, I know
            # Let us out! ... Safely! Unexpectedly quit dialogs are annoying...
            connection.cancel()
            # Re-raise the error as a GurlError
            raise GurlError(-1, str(err))

        if connection.error != None:
            # Gurl returned an error
            NSLog('Download error %@: %@', connection.error.code(),
                  connection.error.localizedDescription())
            if connection.SSLerror:
                NSLog('SSL error detail: %@', str(connection.SSLerror))
            NSLog('Headers: %@', str(connection.headers))
            raise GurlError(connection.error.code(),
                            connection.error.localizedDescription())

        if connection.response != None:
            NSLog('Status: %@', connection.status)
            NSLog('Headers: %@', connection.headers)
        if connection.redirection != []:
            NSLog('Redirection: %@', connection.redirection)

        connection.headers['http_result_code'] = str(connection.status)
        description = NSHTTPURLResponse.localizedStringForStatusCode_(
            connection.status)
        connection.headers['http_result_description'] = description
Ejemplo n.º 8
0
def get_url(url, destinationpath,
            custom_headers=None, message=None, onlyifnewer=False,
            resume=False, follow_redirects=False):
    """Gets an HTTP or HTTPS URL and stores it in
    destination path. Returns a dictionary of headers, which includes
    http_result_code and http_result_description.
    Will raise CurlError if Gurl returns an error.
    Will raise HTTPError if HTTP Result code is not 2xx or 304.
    If destinationpath already exists, you can set 'onlyifnewer' to true to
    indicate you only want to download the file only if it's newer on the
    server.
    If you set resume to True, Gurl will attempt to resume an
    interrupted download."""

    tempdownloadpath = destinationpath + '.download'
    if os.path.exists(tempdownloadpath) and not resume:
        if resume and not os.path.exists(destinationpath):
            os.remove(tempdownloadpath)

    cache_data = None
    if onlyifnewer and os.path.exists(destinationpath):
        # create a temporary Gurl object so we can extract the
        # stored caching data so we can download only if the
        # file has changed on the server
        gurl_obj = Gurl.alloc().initWithOptions_({'file': destinationpath})
        cache_data = gurl_obj.get_stored_headers()
        del gurl_obj

    options = {'url': url,
               'file': tempdownloadpath,
               'follow_redirects': follow_redirects,
               'can_resume': resume,
               'additional_headers': header_dict_from_list(custom_headers),
               'download_only_if_changed': onlyifnewer,
               'cache_data': cache_data,
               'logging_function': munkicommon.display_debug2}
    munkicommon.display_debug2('Options: %s' % options)

    connection = Gurl.alloc().initWithOptions_(options)
    stored_percent_complete = -1
    stored_bytes_received = 0
    connection.start()
    try:
        while True:
            # if we did `while not connection.isDone()` we'd miss printing
            # messages and displaying percentages if we exit the loop first
            connection_done = connection.isDone()
            if message and connection.status and connection.status != 304:
                # log always, display if verbose is 1 or more
                # also display in MunkiStatus detail field
                munkicommon.display_status_minor(message)
                # now clear message so we don't display it again
                message = None
            if (str(connection.status).startswith('2')
                and connection.percentComplete != -1):
                if connection.percentComplete != stored_percent_complete:
                    # display percent done if it has changed
                    stored_percent_complete = connection.percentComplete
                    munkicommon.display_percent_done(
                                                stored_percent_complete, 100)
            elif connection.bytesReceived != stored_bytes_received:
                # if we don't have percent done info, log bytes received
                stored_bytes_received = connection.bytesReceived
                munkicommon.display_detail(
                    'Bytes received: %s', stored_bytes_received)
            if connection_done:
                break

    except (KeyboardInterrupt, SystemExit):
        # safely kill the connection then re-raise
        connection.cancel()
        raise
    except Exception, err: # too general, I know
        # Let us out! ... Safely! Unexpectedly quit dialogs are annoying...
        connection.cancel()
        # Re-raise the error as a GurlError
        raise GurlError(-1, str(err))
Ejemplo n.º 9
0
 def test_instantiate():
     pygame.init()
     datingsim.init()
     gurl = Gurl("Rudy", None, None)
     gurl.exp = 2000
     d = MeetScene(gurl)