~julie-w/unifield-web/US-2457

« back to all changes in this revision

Viewing changes to addons/openerp/utils/tools.py

  • Committer: jf
  • Date: 2017-02-08 15:03:00 UTC
  • mfrom: (4827.6.4 unifield-web)
  • Revision ID: jfb@tempo-consulting.fr-20170208150300-xozav4824nwp3exk
US-2110 [FIX] New widget human_size to display file size

lp:~fabien-morin/unifield-web/fm-us-2110

Show diffs side-by-side

added added

removed removed

Lines of Context:
82
82
 
83
83
def node_attributes(node):
84
84
    attrs = node.attributes
85
 
    
 
85
 
86
86
    if not attrs:
87
87
        return {}
88
88
    # localName can be a unicode string, we're using attribute names as
110
110
        nodes = [n for n in ref.childNodes if n.localName == name]
111
111
        try:
112
112
            return nodes[index-1]
113
 
        except Exception, e:
 
113
        except Exception:
114
114
            return []
115
115
 
116
116
    parts = expr.split('/')
117
117
    for part in parts:
118
118
        if part in ('', '.'):
119
 
#            for node in ref.childNodes:
120
 
#               if node.nodeType == node.ELEMENT_NODE:
121
 
#                   ref = node
 
119
            #            for node in ref.childNodes:
 
120
            #               if node.nodeType == node.ELEMENT_NODE:
 
121
            #                   ref = node
122
122
            continue
123
123
        ref = xml_locate(part, ref)
124
124
 
125
125
    return [ref]
126
126
 
127
127
def get_xpath(expr, pn):
128
 
    
 
128
 
129
129
    """Find xpath.
130
130
 
131
131
    >>> get_xpath("/form/group[3]/notebook/page[@string:'Extra Info']/field[@name='progress'], doc)
135
135
 
136
136
    @return: list of nodes
137
137
    """
138
 
    
 
138
 
139
139
    if '/' not in expr:
140
140
        name = expr
141
141
        param = None
142
142
        index = None
143
 
        
 
143
 
144
144
        if '[' in expr:
145
145
            name, param = expr.split('[')
146
146
            try:
172
172
            get_child_nodes = all_child_nodes(pn, [])
173
173
            if len(get_child_nodes):
174
174
                return get_child_nodes[-1]
175
 
            
 
175
 
176
176
        for child in pn.childNodes:
177
177
            if child.localName and child.localName == name:
178
178
                if param and key in child.attributes.keys():
181
181
                else:
182
182
                    return child
183
183
        return False
184
 
    
 
184
 
185
185
    parts = expr.split('/')
186
186
    for part in parts:
187
187
        if part in ('', '.'):
196
196
 
197
197
    pn = node.parentNode
198
198
    xp = '/' + node.localName
199
 
    root = xp + '[1]'
200
199
 
201
200
    if pn and pn.localName and pn.localName != 'view':
202
201
        xp = get_node_xpath(pn) + xp
206
205
 
207
206
    return xp
208
207
 
209
 
def get_size(sz):
 
208
def get_size(data):
210
209
    """
211
210
    Return the size in a human readable format
212
211
    """
213
 
    if not sz:
214
 
        return False
215
 
 
216
 
    units = ('bytes', 'Kb', 'Mb', 'Gb')
217
 
    if isinstance(sz,basestring):
218
 
        sz=len(sz)
219
 
    s, i = float(sz), 0
220
 
    while s >= 1024 and i < len(units)-1:
221
 
        s = s / 1024
222
 
        i = i + 1
223
 
    return "%0.2f %s" % (s, units[i])
 
212
    units = ('Bytes', 'KB', 'MB', 'GB', 'TB')
 
213
    if isinstance(data, basestring):
 
214
        size = float(len(data))
 
215
    elif isinstance(data, (int, long)):
 
216
        size = float(data)
 
217
    elif isinstance(data, float):
 
218
        size = data
 
219
    else:
 
220
        return '0 Bytes'
 
221
    unit_index = 0
 
222
    while size >= 1024 and unit_index < len(units) - 1:
 
223
        size = size / 1024
 
224
        unit_index += 1
 
225
    return "%0.2f %s" % (size, units[unit_index])
224
226
 
225
227
def context_with_concurrency_info(context, concurrency_info):
226
228
    ctx = (context or {})
237
239
        fd, fn = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir, text=text)
238
240
        os.close(fd)
239
241
        return str.__new__(cls, fn)
240
 
    
 
242
 
241
243
    def __init__(self, *args, **kwargs):
242
244
        self.__os_path_exists = os.path.exists
243
245
        self.__os_unlink = os.unlink