-
Notifications
You must be signed in to change notification settings - Fork 3
/
resources.py
55 lines (37 loc) · 1.84 KB
/
resources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# System imports
import os
# Vroom imports
import vroom.core.environment as env
from debug import *
class ResourceNotFound(Exception):
def __init__(self, filename):
self.filename = filename
def get_resource(filename):
""" Search for resource in application and global directories.
Application resources may be stored in the following locations:
1. In a data/ directory located in the application's root directory
2. In the global resource directory (usually /usr/local/share/vroom/data)
Files in these locations can be retrieved by their path relative to the data
directory. If a files exist in both locations with the same name the local
(application) resource will be returned.
Data required by the application should be placed in the application data
directory. Resources that will be used by multiple applications (i.e. fonts)
should be stored in the global directory.
filename -- pathname of the resource relative to data directory
return -- the full pathname of the resource
raises -- A ResourceNotFound exception is raised if the file cannot be found
in any of the resource directories.
"""
debug(level=VERBOSE).add('filename', filename).flush()
debug(level=VERBOSE).add('_Resource_Paths', env._Resource_Paths).flush()
# Check for the file in the application's data directory
path = os.path.join(env._Resource_Paths[0], 'data', filename)
if os.path.exists(path):
debug(msg='found application resource {}'.format(path), level=STATUS, indent=1).flush()
return path
# Check for the file in the global vroom resource dir
path = os.path.join(env._Resource_Paths[1], 'data', filename)
if os.path.exists(path):
debug(msg='found global resource {}'.format(path), level=STATUS, indent=1).flush()
return path
raise ResourceNotFound(filename)