urllib.request.request - python examples

Here are the examples of the python api urllib.request.request taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

2 Examples 7

0 View Complete Implementation : runTests.py
Copyright GNU Lesser General Public License v3.0
Author : kata198
def download_goodTests(GOODTESTS_URL=None):
    '''
        download_goodTests - Attempts to download GoodTests, using the default global url (or one provided).
    
        @return <int> - 0 on success (program should continue), otherwise non-zero (program should abort with this exit status)
    '''
    if GOODTESTS_URL is None:
        GOODTESTS_URL = globals()['GOODTESTS_URL']

    validAnswer = False
    while validAnswer == False:
        sys.stdout.write('GoodTests not found. Would you like to install it to local folder? (y/n): ')
        sys.stdout.flush()
        answer = sys.stdin.readline().strip().lower()
        if answer not in ('y', 'n', 'yes', 'no'):
            continue
        validAnswer = True
        answer = answer[0]

    if answer == 'n':
        sys.stderr.write('Cannot run tests without installing GoodTests. http://pypi.python.org/pypi/GoodTests or https://github.com/kata198/Goodtests\n')
        return 1
    try:
        import urllib2 as urllib
    except ImportError:
        try:
            import urllib.request as urllib
        except:
            sys.stderr.write('Failed to import urllib. Trying pip.\n')
            res = try_pip_install()
            if res != 0:
                sys.stderr.write('Failed to install GoodTests with pip or direct download. aborting.\n')
                return 1
    try:
        response = urllib.urlopen(GOODTESTS_URL)
        contents = response.read()
        if str != bytes:
            contents = contents.decode('ascii')
    except Exception as e:
        sys.stderr.write('Failed to download GoodTests.py from "%s"\n%s\n' %(GOODTESTS_URL, str(e)))
        sys.stderr.write('\nTrying pip.\n')
        res = try_pip_install()
        if res != 0:
            sys.stderr.write('Failed to install GoodTests with pip or direct download. aborting.\n')
            return 1
    try:
        with open('GoodTests.py', 'w') as f:
            f.write(contents)
    except Exception as e:
        sys.stderr.write('Failed to write to GoodTests.py\n%s\n' %(str(e,)))
        return 1
    try:
        os.chmod('GoodTests.py', 0o775)
    except:
        sys.stderr.write('WARNING: Failed to chmod +x GoodTests.py, may not be able to be executed.\n')

    try:
        import GoodTests
    except ImportError:
        sys.stderr.write('Seemed to download GoodTests okay, but still cannot import. Aborting.\n')
        return 1

    return 0

0 View Complete Implementation : Blender_Screenwriter_original.py
Copyright MIT License
Author : tin2tin
def screenplay_export(context, screenplay_filepath, opt_exp, open_browser):

    import os
    dir = os.path.dirname(bpy.data.filepath)
    if not dir in sys.path:
        sys.path.append(dir)

    fountain_script = bpy.context.area.spaces.active.text.as_string()
    if fountain_script.strip() == "": return {"CANCELLED"}

    # screenplain
    try:
        import screenplain
    except ImportError:
        #Installing screenplain module (this is only required once)...
        import urllib.request as urllib
        import zipfile
        import shutil

        url = 'https://github.com/vilcans/screenplain/archive/0.8.0.zip'
        home_url = bpy.utils.script_path_user() + "\\addons\\"
        urllib.urlretrieve(url, home_url + 'screenplain-0.8.0.zip')
        with zipfile.ZipFile(home_url + 'screenplain-0.8.0.zip', 'r') as z:
            z.extractall(home_url)
        target_dir = home_url
        shutil.move(home_url + 'screenplain-0.8.0/screenplain', target_dir)
        os.remove(home_url + 'screenplain-0.8.0.zip')
        shutil.rmtree(home_url + 'screenplain-0.8.0')
        import screenplain

    import screenplain.parsers.fountain as fountain
    from io import StringIO
    import webbrowser
    s = StringIO(fountain_script)
    screenplay = fountain.parse(s)
    output = StringIO()
    if opt_exp == "HTML":
        from screenplain.export.html import convert
        convert(screenplay, output, bare=False)
    if opt_exp == "FDX":
        from screenplain.export.fdx import to_fdx
        to_fdx(screenplay, output)
    if opt_exp == "PDF":
        from screenplain.export.pdf import to_pdf
        to_pdf(screenplay, output)
    sp_out = output.getvalue()
    filename, extension = os.path.splitext(screenplay_filepath)
    fileout_name = filename + "." + opt_exp.lower()
    file = open(fileout_name, "w")
    file.write(sp_out)
    file.close()
    if open_browser:
        if opt_exp == "HTML" or opt_exp == "PDF":
            webbrowser.open(fileout_name)

    return {'FINISHED'}