Exemplo n.º 1
0
def load(filename, *args, **kwargs):
    """ Load data content from a filename """
    
    extension = os.path.basename(filename).split('.')[-1]

    if extension == 'npy':
        return np.load(filename, *args, **kwargs)
    elif extension in ['ttf', 'otf']:
        if filename is not None:
            return filename
        if depth == 0:
            log.warning("Falling back to default font")
            return get("SourceSansPro-Regular.otf", 1)
        else:
            log.critical("Default font not available")
            raise RuntimeError
    elif extension == 'obj':
        return objload(filename, *args, **kwargs)
    elif extension in ['svg', 'json', 'csv', 'tsv']:
        return filename
    elif extension in ('png', 'jpg', 'jpeg', 'tif', 'tiff', 'tga'):
        if Image is not None:
            if filename is not None:
                return np.array(Image.open(filename))
            log.warning("File not found")
            return checkerboard(16,32)
        else:
            log.warning("PIL/Pillow not installed, cannot load image")
            return checkerboard(16,32)

    log.warning("Data not found (%s)" % name)
    raise RuntimeError
    return None
Exemplo n.º 2
0
def load(filename, *args, **kwargs):
    """ Load data content from a filename """

    extension = os.path.basename(filename).split('.')[-1]

    if extension == 'npy':
        return np.load(filename, *args, **kwargs)
    elif extension in ['ttf', 'otf']:
        if filename is not None:
            return filename
        if depth == 0:
            log.warning("Falling back to default font")
            return get("SourceSansPro-Regular.otf", 1)
        else:
            log.critical("Default font not available")
            raise RuntimeError
    elif extension == 'obj':
        return objload(filename, *args, **kwargs)
    elif extension in ['svg', 'json', 'csv', 'tsv']:
        return filename
    elif extension in ('png', 'jpg', 'jpeg', 'tif', 'tiff', 'tga'):
        if Image is not None:
            if filename is not None:
                return np.array(Image.open(filename))
            log.warning("File not found")
            return checkerboard(16, 32)
        else:
            log.warning("PIL/Pillow not installed, cannot load image")
            return checkerboard(16, 32)

    log.warning("Data not found (%s)" % name)
    raise RuntimeError
    return None
Exemplo n.º 3
0
def _fetch_file(filename):
    """
    Fetch a font file from a remote data server

    Available servers:

      * https://github.com/glumpy/glumpy-font/raw/master/Fonts
      * https://github.com/glumpy/glumpy-data/raw/master/Data
    """

    local_directory = os.path.dirname(__file__) or '.'
    local_file = os.path.join(local_directory, filename)

    if os.path.isfile(local_file):
        return local_file

    extension = os.path.basename(filename).split('.')[-1]

    # Font server
    if extension in ['ttf', 'otf']:
        server = "https://github.com/glumpy/glumpy-font/raw/master/Fonts"
    # Data server
    else:
        server = "https://github.com/glumpy/glumpy-data/raw/master/Data"

    filename = os.path.basename(filename)
    remote = os.path.join(server, filename)

    # Build url request
    log.info('Requesting "%s" from remote server' % filename)
    try:
        response = request.urlopen(remote)
    except:
        log.warning('Data not available on remote server')
        return None
    # Fetch symlink data (font location)
    symlink = response.read().decode()

    remote = os.path.join(server, symlink)
    response = request.urlopen(remote)

    # Fetch data
    size = response.headers['Content-Length'].strip()
    log.info('Fetching data (%s bytes) to "%s"' % (size, local_file))
    with open(local_file, 'wb') as fp:
        fp.write(response.read())
    return local_file
Exemplo n.º 4
0
def _fetch_file(filename):
    """
    Fetch a font file from a remote data server

    Available servers:

      * https://github.com/glumpy/glumpy-font/raw/master/Fonts
      * https://github.com/glumpy/glumpy-data/raw/master/Data
    """

    local_directory = os.path.dirname(__file__) or '.'
    local_file = os.path.join(local_directory, filename)

    if os.path.isfile(local_file):
        return local_file

    extension = os.path.basename(filename).split('.')[-1]

    # Font server
    if extension in ['ttf', 'otf']:
        server = "https://github.com/glumpy/glumpy-font/raw/master/Fonts"
    # Data server
    else:
        server = "https://github.com/glumpy/glumpy-data/raw/master/Data"

    filename = os.path.basename(filename)
    remote = os.path.join(server, filename)

    # Build url request
    log.info('Requesting "%s" from remote server' % filename)
    try:
        response = request.urlopen(remote)
    except:
        log.warning('Data not available on remote server')
        return None
    # Fetch symlink data (font location)
    symlink = response.read().decode()

    remote = os.path.join(server, symlink)
    response = request.urlopen(remote)

    # Fetch data
    size = response.headers['Content-Length'].strip()
    log.info('Fetching data (%s bytes) to "%s"' % (size, local_file))
    with open(local_file, 'wb') as fp:
        fp.write(response.read())
    return local_file
Exemplo n.º 5
0
def get_file(name):
    """ Retrieve a shader full path from sub-directories """

    path = os.path.dirname(__file__) or '.'

    filename = os.path.abspath(os.path.join(path,name))
    if os.path.exists(filename):
        return filename

    for d in os.listdir(path):
        fullpath = os.path.abspath(os.path.join(path,d))
        if os.path.isdir(fullpath):
            filename = os.path.abspath(os.path.join(fullpath,name))
            if os.path.exists(filename):
                return filename

    log.warning("Shader '%s' not found" % name)
    return None
Exemplo n.º 6
0
def get(name):
    """ Retrieve data content from a name """

    filename = _get_file(name)
    extension = os.path.basename(name).split('.')[-1]

    if name == "checkerboard":
        return checkerboard(16,32)
    elif extension in ['ttf', 'otf']:
        return filename
    elif extension == 'npy':
        return np.load(filename)
    elif extension == 'obj':
        return objload(filename)
    elif extension in ('png', 'jpg', 'jpeg', 'tif', 'tiff', 'tga'):
        if Image is not None:
            return np.array(Image.open(filename))
        else:
            log.warning("PIL/Pillow not installed, cannot load image")
            return checkerboard(16,32)

    log.warning("Data not found (%s)" % name)
    return None
Exemplo n.º 7
0
 def on_error(error, message):
     log.warning(message)
Exemplo n.º 8
0
def use(backend, api=None, major=None, minor=None, profile=None):
    """ Select a specific backend

    Parameters
    ----------

    backend : ['osxglut', 'freeglut', 'pyglet', 'glfw', 'sdl', 'sdl2', 'pyside']
        Graphical toolkit to use

    api : ['GL'|'ES']
        OpenGL API to use

    major : int
        OpenGL major version to use

    minor : int
        OpenGL minor version to use

    profile : ['compatibility'|'core']
        OpenGL profile to use

    Note
    ----
    A shortened version is available with the following syntax:

    use("backend (api major.minor profile)")

    For example, `use("glfw (GL 3.3 core)")`
    """

    global __backend__

    # Parse options (in backend name, see note above)
    exp = """(?P<backend>\w+)?
             (.*\(
             (.*(?P<api>GL|ES))?
             (.*(?P<major>[1234])\.(?P<minor>[012345]))?
             (.*(?P<profile>compatibility|core))?.*\))?"""
    r = re.search(exp, backend, re.IGNORECASE | re.VERBOSE)
    _backend = r.group('backend') or "glfw"
    _api = r.group('api') or "GL"
    _major = int(r.group('major') or "2")
    _minor = int(r.group('minor') or "1")
    _profile = r.group('profile') or ""

    # Arguments take precedence over shortened options
    backend = _backend
    api = api or _api
    major = major or _major
    minor = minor or _minor
    profile = profile or _profile

    config = configuration.get_default()
    config.api = api
    config.major_version = major
    config.minor_version = minor
    config.profile = profile

    if backend not in backends.__backends__:
        log.critical("Unknown backend (%s)" % backend)
        log.cirtical("Available backends are: %s", str(backends.__backends__))
        sys.exit(0)

    # BUG: For some reason, the import module changes the working directory
    #      We save it beforehand and restore it just after
    workdir = os.getcwd()
    name = "glumpy.app.window.backends.backend_" + backend
    importlib.import_module(name)
    backend = sys.modules[name]
    os.chdir(workdir)

    # Check availability
    if backend.available():
        __backend__ = backend
        return backend
    else:
        log.warning("Backend (%s) not available" % backend)
        return None
Exemplo n.º 9
0
 def on_error(self, error, message):
     log.warning(message)
Exemplo n.º 10
0
 def on_error(error, message):
     log.warning(message)
Exemplo n.º 11
0
def use(backend, api=None, major=None, minor=None, profile=None):
    """ Select a specific backend

    Parameters
    ----------

    backend : ['osxglut', 'freeglut', 'pyglet', 'glfw', 'sdl', 'sdl2', 'pyside']
        Graphical toolkit to use

    api : ['GL'|'ES']
        OpenGL API to use

    major : int
        OpenGL major version to use

    minor : int
        OpenGL minor version to use

    profile : ['compatibility'|'core']
        OpenGL profile to use

    Note
    ----
    A shortened version is available with the following syntax:

    use("backend (api major.minor profile)")

    For example, `use("glfw (GL 3.3 core)")`
    """

    global __backend__

    # Parse options (in backend name, see note above)
    exp = """(?P<backend>\w+)?
             (.*\(
             (.*(?P<api>GL|ES))?
             (.*(?P<major>[1234])\.(?P<minor>[012345]))?
             (.*(?P<profile>compatibility|core))?.*\))?"""
    r = re.search(exp, backend, re.IGNORECASE | re.VERBOSE)
    _backend = r.group('backend') or "glfw"
    _api     = r.group('api') or "GL"
    _major   = int(r.group('major') or "2")
    _minor   = int(r.group('minor') or "1")
    _profile = r.group('profile') or ""

    # Arguments take precedence over shortened options
    backend = _backend
    api     = api or _api
    major   = major or _major
    minor   = minor or _minor
    profile = profile or _profile

    config = configuration.get_default()
    config.api = api
    config.major_version = major
    config.minor_version = minor
    config.profile = profile

    if backend not in backends.__backends__:
        log.critical("Unknown backend (%s)" % backend)
        log.critical("Available backends are: %s", str(backends.__backends__))
        sys.exit(0)

    # BUG: For some reason, the import module changes the working directory
    #      We save it beforehand and restore it just after
    workdir = os.getcwd()
    name = "glumpy.app.window.backends.backend_" + backend
    importlib.import_module(name)
    backend = sys.modules[name]
    os.chdir(workdir)

    # Check availability
    if backend.available():
        __backend__ = backend
        return backend
    else:
        log.warning("Backend (%s) not available" % backend)
        return None