~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to utilities/format-imports

  • Committer: William Grant
  • Date: 2011-12-30 06:46:44 UTC
  • mto: This revision was merged to the branch mainline in revision 14610.
  • Revision ID: william.grant@canonical.com-20111230064644-t01z90zc99zrn9i2
Some fixes to format-imports: treat _pythonpath specially, and recognise a few more packages as local.

Show diffs side-by-side

added added

removed removed

Lines of Context:
285
285
 
286
286
    return imports
287
287
 
 
288
LOCAL_PACKAGES = (
 
289
    'canonical', 'lp', 'launchpad_loggerhead', 'devscripts',
 
290
    # database/* have some implicit relative imports.
 
291
    'fti', 'replication', 'upgrade',
 
292
    )
288
293
 
289
294
def format_imports(imports):
290
295
    """Group and order imports, return the new import statements."""
 
296
    early_section = {}
291
297
    standard_section = {}
292
298
    first_section = {}
293
299
    thirdparty_section = {}
296
302
    for module, statement in imports.iteritems():
297
303
        module_base = module.split('.')[0]
298
304
        comment = statement.comment
299
 
        if comment is not None and comment.startswith("# FIRST"):
 
305
        if module_base == '_pythonpath':
 
306
            early_section[module] = statement
 
307
        elif comment is not None and comment.startswith("# FIRST"):
300
308
            first_section[module] = statement
301
 
        elif module_base in ('canonical', 'lp'):
 
309
        elif module_base in LOCAL_PACKAGES:
302
310
            local_section[module] = statement
303
311
        elif module_base in python_standard_libs:
304
312
            standard_section[module] = statement
308
316
    all_import_lines = []
309
317
    # Sort within each section and generate statement strings.
310
318
    sections = (
 
319
        early_section,
311
320
        standard_section,
312
321
        first_section,
313
322
        thirdparty_section,