[optik_ext] Bytes should be returned as ints, not floats (closes #2086835)

float is the default type used in apply_units.

authorRémi Cardona <remi.cardona@logilab.fr>
changesete63eaf55cecc
branchdefault
phasepublic
hiddenno
parent revision#6374e3d83000 [testlib] Stop using internal TestCase._addSkip method
child revision#ca5d6124428b [pkg] 1.1.0
files modified by this revision
logilab/common/optik_ext.py
test/unittest_configuration.py
# HG changeset patch
# User Rémi Cardona <remi.cardona@logilab.fr>
# Date 1444662450 -7200
# Mon Oct 12 17:07:30 2015 +0200
# Node ID e63eaf55cecc34d69ba0c58d9a682b47c4c5b0ca
# Parent 6374e3d8300043d0a380c011b9c3a1dd556be187
[optik_ext] Bytes should be returned as ints, not floats (closes #2086835)

float is the default type used in apply_units.

diff --git a/logilab/common/optik_ext.py b/logilab/common/optik_ext.py
@@ -176,11 +176,11 @@
1      return apply_units(value, TIME_UNITS)
2 
3  def check_bytes(option, opt, value):
4      if hasattr(value, '__int__'):
5          return value
6 -    return apply_units(value, BYTE_UNITS)
7 +    return apply_units(value, BYTE_UNITS, final=int)
8 
9 
10  class Option(BaseOption):
11      """override optik.Option to add some new option types
12      """
diff --git a/test/unittest_configuration.py b/test/unittest_configuration.py
@@ -20,10 +20,12 @@
13  from os.path import join, dirname, abspath
14  import re
15 
16  from sys import version_info
17 
18 +from six import integer_types
19 +
20  from logilab.common import attrdict
21  from logilab.common.compat import StringIO
22  from logilab.common.testlib import TestCase, unittest_main
23  from logilab.common.optik_ext import OptionValueError
24  from logilab.common.configuration import Configuration, OptionError, \
@@ -36,10 +38,11 @@
25             ('value', {'type': 'string', 'metavar': '<string>', 'short': 'v'}),
26             ('multiple', {'type': 'csv', 'default': ['yop', 'yep'],
27                           'metavar': '<comma separated values>',
28                           'help': 'you can also document the option'}),
29             ('number', {'type': 'int', 'default':2, 'metavar':'<int>', 'help': 'boom'}),
30 +           ('bytes', {'type': 'bytes', 'default':'1KB', 'metavar':'<bytes>'}),
31             ('choice', {'type': 'choice', 'default':'yo', 'choices': ('yo', 'ye'),
32                         'metavar':'<yo|ye>'}),
33             ('multiple-choice', {'type': 'multiple_choice', 'default':['yo', 'ye'],
34                                  'choices': ('yo', 'ye', 'yu', 'yi', 'ya'),
35                                  'metavar':'<yo|ye>'}),
@@ -69,10 +72,12 @@
36          cfg = self.cfg
37          self.assertEqual(cfg['dothis'], True)
38          self.assertEqual(cfg['value'], None)
39          self.assertEqual(cfg['multiple'], ['yop', 'yep'])
40          self.assertEqual(cfg['number'], 2)
41 +        self.assertEqual(cfg['bytes'], 1024)
42 +        self.assertIsInstance(cfg['bytes'], integer_types)
43          self.assertEqual(cfg['choice'], 'yo')
44          self.assertEqual(cfg['multiple-choice'], ['yo', 'ye'])
45          self.assertEqual(cfg['named'], {'key': 'val'})
46 
47      def test_base(self):
@@ -89,23 +94,26 @@
48 
49      def test_load_command_line_configuration(self):
50          cfg = self.cfg
51          args = cfg.load_command_line_configuration(['--choice', 'ye', '--number', '4',
52                                                      '--multiple=1,2,3', '--dothis=n',
53 +                                                    '--bytes=10KB',
54                                                      'other', 'arguments'])
55          self.assertEqual(args, ['other', 'arguments'])
56          self.assertEqual(cfg['dothis'], False)
57          self.assertEqual(cfg['multiple'], ['1', '2', '3'])
58          self.assertEqual(cfg['number'], 4)
59 +        self.assertEqual(cfg['bytes'], 10240)
60          self.assertEqual(cfg['choice'], 'ye')
61          self.assertEqual(cfg['value'], None)
62          args = cfg.load_command_line_configuration(['-v', 'duh'])
63          self.assertEqual(args, [])
64          self.assertEqual(cfg['value'], 'duh')
65          self.assertEqual(cfg['dothis'], False)
66          self.assertEqual(cfg['multiple'], ['1', '2', '3'])
67          self.assertEqual(cfg['number'], 4)
68 +        self.assertEqual(cfg['bytes'], 10240)
69          self.assertEqual(cfg['choice'], 'ye')
70 
71      def test_load_configuration(self):
72          cfg = self.cfg
73          args = cfg.load_configuration(choice='ye', number='4',
@@ -132,10 +140,12 @@
74  multiple=yop,yepii
75 
76  # boom
77  number=3
78 
79 +bytes=1KB
80 +
81  choice=yo
82 
83  multiple-choice=yo,ye
84 
85  named=key:val
@@ -199,10 +209,12 @@
86  multiple=yop,yep
87 
88  # boom
89  number=2
90 
91 +bytes=1KB
92 +
93  choice=yo
94 
95  multiple-choice=yo,ye
96 
97  named=key:val
@@ -235,10 +247,12 @@
98  multiple=yop,yep
99 
100  # boom
101  number=2
102 
103 +bytes=1KB
104 +
105  choice=yo
106 
107  multiple-choice=yo,ye
108 
109  named=key:val
@@ -264,17 +278,19 @@
110          stream = open(f, 'w')
111          try:
112              self.cfg['dothis'] = False
113              self.cfg['multiple'] = ["toto", "tata"]
114              self.cfg['number'] = 3
115 +            self.cfg['bytes'] = 2048
116              cfg.generate_config(stream)
117              stream.close()
118              new_cfg = MyConfiguration(name='test', options=OPTIONS)
119              new_cfg.load_file_configuration(f)
120              self.assertEqual(cfg['dothis'], new_cfg['dothis'])
121              self.assertEqual(cfg['multiple'], new_cfg['multiple'])
122              self.assertEqual(cfg['number'], new_cfg['number'])
123 +            self.assertEqual(cfg['bytes'], new_cfg['bytes'])
124              self.assertEqual(cfg['choice'], new_cfg['choice'])
125              self.assertEqual(cfg['value'], new_cfg['value'])
126              self.assertEqual(cfg['multiple-choice'], new_cfg['multiple-choice'])
127          finally:
128              os.remove(f)
@@ -301,10 +317,11 @@
129    --dothis=<y or n>
130    -v<string>, --value=<string>
131    --multiple=<comma separated values>
132                          you can also document the option [current: yop,yep]
133    --number=<int>        boom [current: 2]
134 +  --bytes=<bytes>
135    --choice=<yo|ye>
136    --multiple-choice=<yo|ye>
137    --named=<key=val>
138    -r <string>, --reset-value=<string>
139 
@@ -363,10 +380,12 @@
140  multiple=yop
141 
142  # boom
143  number=2
144 
145 +bytes=1KB
146 +
147  choice=yo
148 
149  multiple-choice=yo,ye
150 
151  named=key:val