Show relative paths for package paths

Since most of the paths are the same, only display the path relative to the
main Python library path.
This commit is contained in:
Matt Good
2015-04-14 19:18:45 -07:00
parent ccd8ba66b2
commit 612f93c129
2 changed files with 29 additions and 20 deletions

View File

@@ -1,3 +1,4 @@
import os
from distutils.sysconfig import get_python_lib
from flask import __version__ as flask_version
@@ -5,6 +6,16 @@ from flask_debugtoolbar.panels import DebugPanel
_ = lambda x: x
def relpath(location, python_lib):
location = os.path.normpath(location)
relative = os.path.relpath(location, python_lib)
if relative == os.path.curdir:
return ''
elif relative.startswith(os.path.pardir):
return location
return relative
class VersionDebugPanel(DebugPanel):
"""
Panel that displays the Flask version.
@@ -28,20 +39,13 @@ class VersionDebugPanel(DebugPanel):
try:
import pkg_resources
except ImportError:
context = self.context.copy()
context.update({
'packages': []
})
packages = []
else:
active_packages = pkg_resources.WorkingSet()
_pkgs = dict([(p.project_name, p) for p in active_packages])
packages = [_pkgs[key] for key in sorted(_pkgs.iterkeys())]
for package in packages:
package.develop_mode = not (package.location.lower().startswith(get_python_lib().lower()))
packages = sorted(pkg_resources.working_set,
key=lambda p: p.project_name.lower())
context = self.context.copy()
context.update({
'packages': packages
})
return self.render('panels/versions.html', context)
return self.render('panels/versions.html', {
'packages': packages,
'python_lib': os.path.normpath(get_python_lib()),
'relpath': relpath,
})

View File

@@ -1,26 +1,31 @@
<h4>Installed Packages</h4>
<p>
Installation paths relative to:
</p>
<pre>
{{ python_lib }}
</pre>
<table>
<thead>
<tr>
<th>Package</th>
<th>Version</th>
<th>Develop?</th>
<th>Installed Path</th>
</tr>
</thead>
<tbody>
{% for package in packages %}
<tr class="{{ loop.cycle('flDebugOdd' 'flDebugEven') }}">
<tr class="{{ loop.cycle('flDebugOdd', 'flDebugEven') }}">
<td>{{ package.project_name }}</td>
<td>{{ package.version }}</td>
<td>{{ package.develop_mode }}</td>
<td>{{ package.location }}</td>
<td>{{ relpath(package.location, python_lib) }}</td>
</tr>
{% else %}
<tr>
<td>setuptools</td>
<td>NOT INSTALLED</td>
<td>&nbsp;</td>
<td>Install setuptools to display installed packages and version information</td>
</tr>
{% endfor %}