def get_listing(self): """ returns the list of folders/files in a directory, along with their type (directory/file) in a tuple (name, type) """ def get_type(f): if Path.is_dir(self.sources_path / f): return "directory" else: return "file" listing = [{ "name": f.name, "percent_encoded_name": url_encode(f.name), "type": get_type(f), "hidden": False, "stat": qry.location_get_stat(self.sources_path / f), } for f in sorted(Path.iterdir(self.sources_path))] for hidden_file in self.hidden_files: for f in listing: full_path = bytes(self.location.sources_path / f["name"]) if f["type"] == "directory": full_path += b"/" f["hidden"] = f["hidden"] or fnmatch.fnmatch( full_path, hidden_file) return listing
def _render_file(self, location): """ renders a file """ file_ = SourceFile(location) checksum = file_.get_sha256sum(session) number_of_duplicates = (qry.count_files_checksum(session, checksum) .first()[0] ) pkg_infos = Infobox(session, location.get_package(), location.get_version()).get_infos() text_file = file_.istextfile() raw_url = file_.get_raw_url() path = location.get_path_to() if self.d.get('api'): self.render_func = jsonify return dict(type="file", file=location.get_deepest_element(), package=location.get_package(), version=location.get_version(), mime=file_.get_mime(), raw_url=raw_url, path=path, text_file=text_file, stat=qry.location_get_stat(location.sources_path), checksum=checksum, number_of_duplicates=number_of_duplicates, pkg_infos=pkg_infos ) # prepare the non-api render func self.render_func = None # more work to do with files # as long as 'lang' is in keys, then it's a text_file lang = None if 'lang' in request.args: lang = request.args['lang'] # if the file is not a text file, we redirect to it elif not text_file: self.render_func = bind_redirect(raw_url) # set render func (non-api form) if not self.render_func: sources_path = raw_url.replace( current_app.config['SOURCES_STATIC'], current_app.config['SOURCES_DIR'], 1) # ugly, but better than global variable, # and better than re-requesting the db # TODO: find proper solution for retrieving souces_path # (without putting it in kwargs, we don't want it in # json rendering eg) # we get the variables for highlighting and message (if they exist) try: highlight = request.args.get('hl') except (KeyError, ValueError, TypeError): highlight = None try: msg = request.args.getlist('msg') if msg == "": msg = None # we don't want empty messages except (KeyError, ValueError, TypeError): msg = None # we preprocess the file with SourceCodeIterator sourcefile = SourceCodeIterator( sources_path, hl=highlight, msg=msg, lang=lang) self.render_func = bind_render( self.d['templatename'], nlines=sourcefile.get_number_of_lines(), pathl=qry.location_get_path_links(".source", path), file_language=sourcefile.get_file_language(), msgs=sourcefile.get_msgdict(), code=sourcefile) return dict(type="file", file=location.get_deepest_element(), package=location.get_package(), version=location.get_version(), mime=file_.get_mime(), raw_url=raw_url, path=path, text_file=text_file, stat=qry.location_get_stat(location.sources_path), checksum=checksum, number_of_duplicates=number_of_duplicates, pkg_infos=pkg_infos )