~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_import.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-11 22:09:06 UTC
  • mto: This revision was merged to the branch mainline in revision 2004.
  • Revision ID: john@arbash-meinel.com-20060911220906-41ee1e306aad0464
Handle 'from foo import bar' syntax

This includes:
  from foo import bar
  from foo import bar, baz
  from foo import bar as bing, baz as bar

Show diffs side-by-side

added added

removed removed

Lines of Context:
150
150
 
151
151
 
152
152
def _convert_import_str_to_map(import_str, imports):
153
 
    """This converts a import string into a list of imports.
 
153
    """This converts a import string into an import map.
154
154
 
155
155
    This only understands 'import foo, foo.bar, foo.bar.baz as bing'
156
156
 
193
193
                    next = (cur_path[:], None, {})
194
194
                    cur[child] = next
195
195
                    cur = next[2]
 
196
 
 
197
 
 
198
def _convert_from_str_to_map(from_str, imports):
 
199
    """This converts a 'from foo import bar' string into an import map.
 
200
 
 
201
    :param from_str: The import string to process
 
202
    :param imports: The current map of all imports
 
203
    """
 
204
    assert from_str.startswith('from ')
 
205
    from_str = from_str[len('from '):]
 
206
 
 
207
    from_module, import_list = from_str.split(' import ')
 
208
 
 
209
    from_module_path = from_module.split('.')
 
210
 
 
211
    for path in import_list.split(','):
 
212
        path = path.strip()
 
213
        as_hunks = path.split(' as ')
 
214
        if len(as_hunks) == 2:
 
215
            # We have 'as' so this is a different style of import
 
216
            # 'import foo.bar.baz as bing' creates a local variable
 
217
            # named 'bing' which points to 'foo.bar.baz'
 
218
            name = as_hunks[1].strip()
 
219
            module = as_hunks[0].strip()
 
220
        else:
 
221
            name = module = path
 
222
        assert name not in imports
 
223
        imports[name] = (from_module_path, module, {})