def _compile_file_list_from_staticfiles_dirs(self): """ Compile list of static files for the given extension Used when serving out of STATICFILES_DIRS (i.e. DEBUG = True) """ finder = FileSystemFinder() for path, storage in finder.list(ignore_patterns=None): if path.endswith(".%s" % self._ext) and path.count(os.sep) < 2: self._items.update({path.split(os.sep)[-1]: (path, storage.location)})
def test_maps_markers_exist(self): # Given icons = [ getattr(maps, attr) for attr in dir(maps) if attr.endswith('_ICON') ] finder = FileSystemFinder() relative_paths = [icon.lstrip(settings.STATIC_URL) for icon in icons] # When absolute_paths = [finder.find(path) for path in relative_paths] # Then for path in absolute_paths: self.assertTrue(path)
def addMaginifyingGlass(image): magGlass = Image.open(FileSystemFinder().find(MAG_GLASS_PNG)) cordinates = (image.size[0] - magGlass.size[0], image.size[1] - magGlass.size[1]) image.paste(magGlass, cordinates, magGlass) return image
def walk_finders(path): """Find all qunit related files given the path component that comes after '/qunit/' Works similarly to 'os.walk' but returns just files and directories by surfing over all possible files. """ # Get a file system path from url path path_comps = [c for c in path.split('/') if c != u''] tmp = os.sep.join(path_comps) file_path = os.path.join(settings.QUNIT_TEST_PATH, tmp) finder_files = [] # Get list of files from app directories from app file finder adf = AppDirectoriesFinder() for fpath, filestorageobj in adf.list(''): if settings.QUNIT_TEST_PATH in fpath: finder_files.append((fpath, filestorageobj)) # Get list of files from app directories from file system file finder # By adding this second, files in the project base override app specific files fsf = FileSystemFinder() for fpath, filestorageobj in fsf.list(''): if settings.QUNIT_TEST_PATH in fpath: finder_files.append((fpath, filestorageobj)) # Form arrays of files in this directory and sub directories matchfiles = [] subdirectories = [] for ffile_path, fso in finder_files: split = ffile_path.split(file_path) if len(split) > 1: # Check to see if sub-directories exist path_split = split[1].split(os.sep) if len(path_split) > 1 and path_split[0] != u'': # this file indicates a sub directory subdirectories.append(path_split[0]) else: # this is a file in this directory matchfiles.append((ffile_path, fso)) # Get rid of duplicates subdirectories = list(set(subdirectories)) # array of strings matchfiles = list(set(matchfiles)) # array of tuples return (subdirectories, matchfiles)
def _check_template_exists(self, path): template = FileSystemFinder().find(path) if not template: template = AppDirectoriesFinder().find(path) if not template: raise InvalidTemplateError("%s is not a known template" % path) return template
def overview_map(request): incidents = get_incidents() map_center = center_geolocation(incidents) template = loader.get_template('overview_map.html') finder = FileSystemFinder() perimeters = None perimeters_path = finder.find('fire_perimeters.json') if perimeters_path: perimeters_file = open(perimeters_path, 'r') perimeters = perimeters_file.read() perimeters_file.close() context = RequestContext(request, { 'incidents': incidents, 'map_center_lat': map_center[0], 'map_center_lon': map_center[1], 'fire_perimeters': perimeters }) return HttpResponse(template.render(context))
def handle(self, *args, **options): a = AppDirectoriesFinder() f = FileSystemFinder() print(f'Found installed apps : {a.apps}') print('Static localisations :') for key, dirs in a.storages.items(): print(f'{key} ==> {dirs._location}') for key, dirs in f.storages.items(): print(f'{key} ==> {dirs._location}')
def coffeescript(path): try: STATIC_ROOT = settings.STATIC_ROOT except AttributeError: STATIC_ROOT = settings.MEDIA_ROOT filename = os.path.split(path)[-1] try: from django.contrib.staticfiles.finders import \ FileSystemFinder, AppDirectoriesFinder full_path = AppDirectoriesFinder().find(path) if not full_path: full_path = FileSystemFinder().find(path) except ImportError: # normal, non-statcfiles-enabled way: full_path = os.path.join(STATIC_ROOT, path) output_directory = os.path.join(STATIC_ROOT, COFFEESCRIPT_OUTPUT_DIR, os.path.dirname(path)) hashed_mtime = get_hashed_mtime(full_path) if filename.endswith(".coffee"): base_filename = filename[:-7] else: base_filename = filename output_path = os.path.join(output_directory, "%s-%s.js" % (base_filename, hashed_mtime)) if not os.path.exists(output_path): source_file = open(full_path) source = source_file.read() source_file.close() args = shlex.split("%s -c -s -p" % COFFEESCRIPT_EXECUTABLE, posix=POSIX_COMPATIBLE) p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, errors = p.communicate(source) if out: if not os.path.exists(output_directory): os.makedirs(output_directory) compiled_file = open(output_path, "w+") compiled_file.write(out) compiled_file.close() # Remove old files compiled_filename = os.path.split(output_path)[-1] for filename in os.listdir(output_directory): if filename.startswith(base_filename) and filename != compiled_filename: os.remove(os.path.join(output_directory, filename)) elif errors: logger.error(errors) return path return output_path[len(STATIC_ROOT):].replace(os.sep, '/').lstrip("/")
class CompressedStorage(FileSystemStorage): transformers = [CSSURLTransformer] def __init__(self, *args, **kwargs): super(CompressedStorage, self).__init__(*args, **kwargs) self.finder = FileSystemFinder() self.compressor = get_compressor_class()() self.transformers = copy(CompressedStorage.transformer) def path(self, name): found = self.finder.find(name) return found if found else name def delete(self, name): pass def modified_time(self, name): stamps = [os.stat(self.path(filename)).st_mtime for filename in settings.COMPRESS_SETS[name]] return datetime.fromtimestamp(max(stamps)) def munge_file(self, filename, contents): for transformer in self.transformers: if transformer.can_handle(filename): contents = transformer()(filename, contents) return contents def _load_file(self, filename, buffer): contents = "" with open(self.path(filename), 'rb') as fp: contents = fp.read() buffer.write(self.munge_file(filename, contents)) def _open(self, name, mode='rb'): buffer = StringIO() for filename in settings.COMPRESS_SETS[name]: self._load_file(filename, buffer) return ContentFile(self.compressor(buffer, filename))
def get_implementation(action_name): implementation_path = FileSystemFinder().find(f"bundles/{action_name}.js") with open(implementation_path) as f: return f.read()
def __init__(self, *args, **kwargs): super(CompressedStorage, self).__init__(*args, **kwargs) self.finder = FileSystemFinder() self.compressor = get_compressor_class()() self.transformers = copy(CompressedStorage.transformer)