Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #678 (closed defect: fixed)

Opened 11 years ago

Last modified 10 years ago

[PATCH] Toolbox admi18n is not handling files grouping properly

Reported by: roger.demetrescu Owned by: anonymous
Priority: normal Milestone: 0.9a2
Component: Toolbox Version: 0.9a6
Severity: normal Keywords:
Cc:

Description

The Collect Strings option from Toolbox admi18n is grouping files under the wrong parent folder.

Please see this  ML thread for further information.

Attachments

admi18n.patch Download (4.0 KB) - added by roger.demetrescu 11 years ago.

Change History

Changed 11 years ago by roger.demetrescu

comment:1 Changed 11 years ago by roger.demetrescu

  • Summary changed from Toolbox admi18n is not handling files grouping properly to [PATCH] Toolbox admi18n is not handling files grouping properly

This patch handles file grouping correctly and also fix the "jumping over forbidden folders" behavior (see #339 and #512).

Roger

PS: The flatten() implementation was stolen from  http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/121294 ... Thanks H. Krekel :)

comment:2 Changed 11 years ago by simon

flatten() should go to util.

if type(arg) in (type(()),type([])):

would be more flexible (and faster) if written as:

if hasattr(arg, "__iter__"):

comment:3 Changed 11 years ago by roger.demetrescu

Hi Simon,

>>> a,b,c = [],{},()
>>> for x in a,b,c:
	print hasattr(x, "__iter__"),

True True True

If we use the hasattr() approach, it will break this code, because it will flat even the dictionaries... :(

Just for the sake of sanity, I tried your suggestion and I got this error:

Page handler: <bound method Internationalization.string_collection of <turbogears.toolbox.admi18n.Internationalization object at 0x012299D0>>
Traceback (most recent call last):
  File "c:\python24\lib\site-packages\cherrypy-2.2.0rc1-py2.4.egg\cherrypy\_cphttptools.py", line 99, in _run
    self.main()
  File "c:\python24\lib\site-packages\cherrypy-2.2.0rc1-py2.4.egg\cherrypy\_cphttptools.py", line 248, in main
    body = page_handler(*virtual_path, **self.params)
  File "<string>", line 3, in string_collection
  File "D:\Turbogears\turbogears\controllers.py", line 206, in expose
    output = database.run_with_transaction(expose._expose,func, accept, allow_json, allow_json_from_config,*args, **kw)
  File "D:\Turbogears\turbogears\database.py", line 219, in run_with_transaction
    retval = func(*args, **kw)
  File "<string>", line 5, in _expose
  File "D:\Turbogears\turbogears\controllers.py", line 227, in <lambda>
    expose._expose.when(rule)(lambda _func, accept, allow_json, allow_json_from_config,*args,**kw: _execute_func(
  File "D:\Turbogears\turbogears\controllers.py", line 245, in _execute_func
    output = errorhandling.try_call(func, *args, **kw)
  File "D:\Turbogears\turbogears\errorhandling.py", line 59, in try_call
    output = func(self, *args, **kw)
  File "D:\Turbogears\turbogears\toolbox\admi18n\__init__.py", line 299, in string_collection
    return dict(project_files=self.project_files(),
  File "D:\Turbogears\turbogears\toolbox\admi18n\__init__.py", line 252, in project_files
    return [x for x in flatten(fl) if not x["isdir"] or visibility[x["path"]]]
TypeError: string indices must be integers

[]s Roger

comment:4 Changed 11 years ago by kevin

  • Status changed from new to closed
  • Resolution set to fixed
  • Milestone changed from 0.9 to 0.9a2

comment:5 Changed 11 years ago by kevin

Committed in [1012].

comment:6 Changed 11 years ago by simon

If we want to flatten only sequences, there is always opertor.isSequenceType().

comment:7 Changed 11 years ago by roger.demetrescu

Hi Simon,

I didn't know opertor.isSequenceType()... It works nice with this code... :)

But if used in util.flatten() as a general utility, my only concern would be with the direct use of strings... The "old" version (which I stole from the Cookbook) and the "new" version (using isSequenceType) would lead to different results:

>>> for x in old_flatten([55,'turbogears', True, {'a':111, 'b':222}]): print x,
55 turbogears True {'a': 111, 'b': 222}

>>> for x in new_flatten([55,'turbogears', True, {'a':111, 'b':222}]): print x,
55 turbogears True {'a': 111, 'b': 222}

>>> for x in old_flatten('turbogears'): print x,
turbogears

>>> for x in new_flatten('turbogears'): print x,
t u r b o g e a r s

Of course, documenting this behavior could solve the problem.

Anyway, I like the idea of moving the flatten() to util.py... :)

So what do we do now ? Should we open an improvement ticket (this one is already closed) or would you mind doing the task yourself (since you have write access to svn) ?

Thanks Simon... :)

[]s Roger

comment:8 Changed 11 years ago by simon

Here is how I would do it:

def flatten_sequence(seq):
    for item in seq:
        if isSequenceType(item) and not isinstance(item, basestring):
            for item in flatten_sequence(item):
                yield item
        else:
            yield item

I really don't like the "throw anything in and it will work" approach. If such behaviour is desired one can always combines flatten() with util.ensure_sequence().

comment:9 Changed 11 years ago by anonymous

Simon, let's move to #680... :)

[]s Roger

Note: See TracTickets for help on using tickets.