Add some comments. This makes the code look less daunting.
[python-gnt.git] / example / gntparted / common.py
blob8ad5ca268c7e331e44229c0ff067b8ee3220b0d6
1 #!/usr/bin/env python
3 """Some common utility functions.
4 """
6 import gnt
7 import re
8 import parted
10 def convert_size(size):
11 """Convert a string like '500MB' into size that can be used to create partitions etc.
12 """
13 ret = 0
14 mb = re.compile("^([0-9]+)\s*(MB?)?$")
15 match = mb.match(size)
16 if match:
17 ret = long(match.group(1)) * (1 << 20) / parted.SECTOR_SIZE
18 return ret
20 gkb = re.compile("^([0-9]+)\s*(G|K)B?$")
21 match = gkb.match(size)
22 if match:
23 unit = 1 << 10
24 if match.group(2) == 'G':
25 unit = 1 << 30
26 ret = long(match.group(1)) * unit / parted.SECTOR_SIZE
27 return ret
28 return ret
30 def get_display_size(size):
31 """Return a good display string for size (in bytes).
32 """
33 unit = "b"
34 if size > (1 << 30):
35 size = float(size / (1 << 30))
36 unit = "G"
37 elif size > (1 << 20):
38 size = float(size / (1 << 20))
39 unit = "M"
40 elif size > (1 << 10):
41 size = float(size / (1 << 10))
42 uni = "K"
43 size = ("%.1lf " + unit) % (float(size))
44 return size
46 def create_box(label, widget, homo = False, vert = False):
47 hbox = gnt.Box(homo = homo, vert = vert)
48 hbox.add_widget(label)
49 hbox.add_widget(widget)
50 hbox.set_fill(False)
51 hbox.set_alignment(gnt.ALIGN_MID)
52 return hbox
54 filesystems = ['ext2', 'fat32', 'fat16', 'HFS', 'linux-swap', 'NTFS', 'reiserfs', 'ufs']