Merge lp:~gesha/linaro-license-protection/human-readable-size into lp:~linaro-automation/linaro-license-protection/trunk

Proposed by Georgy Redkozubov
Status: Merged
Approved by: Stevan Radaković
Approved revision: 119
Merged at revision: 121
Proposed branch: lp:~gesha/linaro-license-protection/human-readable-size
Merge into: lp:~linaro-automation/linaro-license-protection/trunk
Diff against target: 58 lines (+23/-1)
2 files modified
license_protected_downloads/tests/test_views.py (+9/-0)
license_protected_downloads/views.py (+14/-1)
To merge this branch: bzr merge lp:~gesha/linaro-license-protection/human-readable-size
Reviewer Review Type Date Requested Status
Stevan Radaković Approve
Review via email: mp+121330@code.launchpad.net

Description of the change

As part of updating template to "apache generated" style this branch adds displaying filesize in human readable format.
It's more appearance improvement then necessity :)

To post a comment you must log in.
Revision history for this message
Stevan Radaković (stevanr) wrote :

Looks good.
Approve +1.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'license_protected_downloads/tests/test_views.py'
2--- license_protected_downloads/tests/test_views.py 2012-08-24 14:17:02 +0000
3+++ license_protected_downloads/tests/test_views.py 2012-08-26 11:42:19 +0000
4@@ -10,6 +10,7 @@
5 from license_protected_downloads import bzr_version
6 from license_protected_downloads.buildinfo import BuildInfo
7 from license_protected_downloads.views import _insert_license_into_db
8+from license_protected_downloads.views import _sizeof_fmt
9 from license_protected_downloads.config import INTERNAL_HOSTS
10
11 THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
12@@ -479,5 +480,13 @@
13 # this test should not cause an exception. Anything else is a pass.
14 self.assertEqual(response.status_code, 200)
15
16+ def test_sizeof_fmt(self):
17+ self.assertEqual(_sizeof_fmt(1), '1')
18+ self.assertEqual(_sizeof_fmt(1234), '1.2K')
19+ self.assertEqual(_sizeof_fmt(1234567), '1.2M')
20+ self.assertEqual(_sizeof_fmt(1234567899), '1.1G')
21+ self.assertEqual(_sizeof_fmt(1234567899999), '1.1T')
22+
23+
24 if __name__ == '__main__':
25 unittest.main()
26
27=== modified file 'license_protected_downloads/views.py'
28--- license_protected_downloads/views.py 2012-08-24 14:13:10 +0000
29+++ license_protected_downloads/views.py 2012-08-26 11:42:19 +0000
30@@ -41,6 +41,19 @@
31 return False
32
33
34+def _sizeof_fmt(num):
35+ ''' Returns in human readable format for num.
36+ '''
37+ if num < 1024 and num > -1024:
38+ return str(num)
39+ num /= 1024.0
40+ for x in ['K', 'M', 'G']:
41+ if num < 1024.0 and num > -1024.0:
42+ return "%3.1f%s" % (num, x)
43+ num /= 1024.0
44+ return "%3.1f%s" % (num, 'T')
45+
46+
47 def dir_list(url, path):
48 files = os.listdir(path)
49 files.sort()
50@@ -87,7 +100,7 @@
51 license_digest_list = is_protected(pathname)
52 license_list = License.objects.all_with_hashes(license_digest_list)
53 listing.append({'name': name,
54- 'size': size,
55+ 'size': _sizeof_fmt(size),
56 'type': type,
57 'mtime': mtime,
58 'license_digest_list': license_digest_list,

Subscribers

People subscribed via source and target branches