Hi Stefano. I have a bit on this bug, please see the attached patch.
Instead of filtering out files that should be hidden, I have added an 'hidden' (bool) key to the dictionary that it returns, so that the API can return all the files, even if they're hidden. It should not be the model nor the controller that decides what is shown on the screen, but the view. This also allow us to add a toggle button (show / hide hidden files) in directory listings eventually. I could do that in a separate commit in this bug if you'd like. Cheers ! -- Jason Pleau
>From 6b2b89b6e63baa089f4f671286a69cc2b22a915f Mon Sep 17 00:00:00 2001 From: Jason Pleau <[email protected]> Date: Mon, 9 Mar 2015 12:34:06 -0400 Subject: [PATCH] add a new hidden_files setting in config.ini Right now the .pc/ exclusion in directory listings is hardcoded. This new hidden_files configuration allows us to be more flexible in what is shown or hidden in those listings. */.pc/ is added as a default value for this configuration setting. Closes #762944 --- debsources/app/templates/source_folder.html | 48 ++++++++++++++++------------- debsources/app/views.py | 5 ++- debsources/models.py | 18 +++++++---- debsources/tests/test_webapp.py | 21 +++++++++++++ doc/examples/sample-config.local.ini | 4 +++ etc/config.ini | 4 +++ 6 files changed, 69 insertions(+), 31 deletions(-) diff --git a/debsources/app/templates/source_folder.html b/debsources/app/templates/source_folder.html index 444194a..99ca1c1 100644 --- a/debsources/app/templates/source_folder.html +++ b/debsources/app/templates/source_folder.html @@ -32,31 +32,35 @@ </tr> {% for dir in subdirs %} - <tr> - <td class="item-img"><img src="{{ config['ICONS_FOLDER'] }}22x22/places/folder.png" alt="d " /></td> - {% if config["DIR_LS_LONG"] %}<td class="stat-type"><span>{{ dir.stat.type }}</span></td>{% endif %} - <td class="stat-perms"><span>{{ dir.stat.perms }}</span></td> - {% if config["DIR_LS_LONG"] %}<td class="stat-size"><span>{{ "{:,d}".format(dir.stat.size) }}</span></td>{% endif %} - <td class="item-name"><a href="{{ url_for('source_html', path_to=path+'/'+dir.name) }}">{{ dir.name }}</a> - {% if config["DIR_LS_LONG"] %} - {% if dir.stat.symlink_dest is not none %}{{ " â " + dir.stat.symlink_dest }}{% endif %} - {% endif %} - </td> - </tr> + {% if not dir['hidden'] %} + <tr> + <td class="item-img"><img src="{{ config['ICONS_FOLDER'] }}22x22/places/folder.png" alt="d " /></td> + {% if config["DIR_LS_LONG"] %}<td class="stat-type"><span>{{ dir.stat.type }}</span></td>{% endif %} + <td class="stat-perms"><span>{{ dir.stat.perms }}</span></td> + {% if config["DIR_LS_LONG"] %}<td class="stat-size"><span>{{ "{:,d}".format(dir.stat.size) }}</span></td>{% endif %} + <td class="item-name"><a href="{{ url_for('source_html', path_to=path+'/'+dir.name) }}">{{ dir.name }}</a> + {% if config["DIR_LS_LONG"] %} + {% if dir.stat.symlink_dest is not none %}{{ " â " + dir.stat.symlink_dest }}{% endif %} + {% endif %} + </td> + </tr> + {% endif %} {% endfor %} {% for file_ in subfiles %} - <tr> - <td class="item-img"><img src="{{ config['ICONS_FOLDER'] }}22x22/mimetypes/ascii.png" alt="- " /></td> - {% if config["DIR_LS_LONG"] %}<td class="stat-type"><span>{{ file_.stat.type }}</span></td>{% endif %} - <td class="stat-perms"><span>{{ file_.stat.perms }}</span></td> - {% if config["DIR_LS_LONG"] %}<td class="stat-size"><span>{{ "{:,d}".format(file_.stat.size) }}</span></td>{% endif %} - <td class="item-name"><a href="{{ url_for('source_html', path_to=path+'/'+file_.name) }}">{{ file_.name }}</a> - {% if config["DIR_LS_LONG"] %} - {% if file_.stat.symlink_dest is not none %}{{ " â " + file_.stat.symlink_dest }}{% endif %} - {% endif %} - </td> - </tr> + {% if not file_['hidden'] %} + <tr> + <td class="item-img"><img src="{{ config['ICONS_FOLDER'] }}22x22/mimetypes/ascii.png" alt="- " /></td> + {% if config["DIR_LS_LONG"] %}<td class="stat-type"><span>{{ file_.stat.type }}</span></td>{% endif %} + <td class="stat-perms"><span>{{ file_.stat.perms }}</span></td> + {% if config["DIR_LS_LONG"] %}<td class="stat-size"><span>{{ "{:,d}".format(file_.stat.size) }}</span></td>{% endif %} + <td class="item-name"><a href="{{ url_for('source_html', path_to=path+'/'+file_.name) }}">{{ file_.name }}</a> + {% if config["DIR_LS_LONG"] %} + {% if file_.stat.symlink_dest is not none %}{{ " â " + file_.stat.symlink_dest }}{% endif %} + {% endif %} + </td> + </tr> + {% endif %} {% endfor %} </table> diff --git a/debsources/app/views.py b/debsources/app/views.py index 2519c5f..6a99377 100644 --- a/debsources/app/views.py +++ b/debsources/app/views.py @@ -554,10 +554,9 @@ class SourceView(GeneralView): """ renders a directory, lists subdirs and subfiles """ - directory = Directory(location, toplevel=(location.get_path() == "")) - # (if path == "", then the dir is toplevel, and we don't want - # the .pc directory) + hidden_files = app.config['HIDDEN_FILES'].split(" ") + directory = Directory(location, hidden_files) pkg_infos = Infobox(session, location.get_package(), diff --git a/debsources/models.py b/debsources/models.py index e4d05fc..2078e6e 100644 --- a/debsources/models.py +++ b/debsources/models.py @@ -19,6 +19,7 @@ import os import magic import stat +import fnmatch from collections import namedtuple from sqlalchemy import Column, ForeignKey @@ -652,11 +653,10 @@ class Location(object): class Directory(object): """ a folder in a package """ - def __init__(self, location, toplevel=False): - # if the directory is a toplevel one, we remove the .pc folder + def __init__(self, location, hidden_files=None): self.sources_path = location.sources_path - self.toplevel = toplevel self.location = location + self.hidden_files = hidden_files def get_listing(self): """ @@ -670,11 +670,17 @@ class Directory(object): else: return "file" get_stat, join_path = self.location.get_stat, os.path.join - listing = sorted(dict(name=f, type=get_type(f), + listing = sorted(dict(name=f, type=get_type(f),hidden=False, stat=get_stat(join_path(self.sources_path, f))) for f in os.listdir(self.sources_path)) - if self.toplevel: - listing = filter(lambda x: x['name'] != ".pc", listing) + + for hidden_file in self.hidden_files: + for f in listing: + full_path = os.path.join(self.location.sources_path, f['name']) + if f['type'] == "directory": + full_path += "/" + f['hidden'] = (f['hidden'] + or fnmatch.fnmatch(full_path, hidden_file)) return listing diff --git a/debsources/tests/test_webapp.py b/debsources/tests/test_webapp.py index 57e6d1f..6bae6cc 100644 --- a/debsources/tests/test_webapp.py +++ b/debsources/tests/test_webapp.py @@ -272,12 +272,33 @@ class DebsourcesTestCase(unittest.TestCase, DbTestFixture): self.assertEqual(rv['directory'], "2.01-6") self.assertIn({"type": "file", "name": "ledit.ml", + "hidden": False, "stat": {"perms": "rw-r--r--", "size": 45858, "type": "-", "symlink_dest": None} }, rv['content']) + def test_api_hidden_files_folder(self): + rv = json.loads(self.app.get('/api/src/nvidia-xconfig/319.72-1/').data) + self.assertIn({"type": "directory", + "name": ".pc", + "hidden": True, + "stat": {"perms": "rwxr-xr-x", + "size": 4096, + "type": "d", + "symlink_dest": None} + }, rv['content']) + + self.assertIn({"type": "file", + "name": "lscf.c", + "hidden": False, + "stat": {"perms": "rw-r--r--", + "size": 11940, + "type": "-", + "symlink_dest": None} + }, rv['content']) + def test_api_symlink_dest(self): rv = json.loads(self.app.get('/api/src/beignet/1.0.0-1/').data) self.assertIn({"type": "file", diff --git a/doc/examples/sample-config.local.ini b/doc/examples/sample-config.local.ini index 96973d6..1487066 100644 --- a/doc/examples/sample-config.local.ini +++ b/doc/examples/sample-config.local.ini @@ -78,3 +78,7 @@ serve_static_files: true # where to log webapp specific information log_file: %(log_dir)s/webapp.log + +# space-separated list of files or directories patterns to hide in +# directory listings +hidden_files: */.pc/ diff --git a/etc/config.ini b/etc/config.ini index 1f27d17..675c64c 100644 --- a/etc/config.ini +++ b/etc/config.ini @@ -85,3 +85,7 @@ log_file: %(log_dir)s/webapp.log # whether to enable the blueprint blueprint_copyright: true + +# space-separated list of files or directories patterns to hide in +# directory listings +hidden_files: */.pc/ -- 2.1.4

