~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:48:57 UTC
  • mto: This revision was merged to the branch mainline in revision 2004.
  • Revision ID: john@arbash-meinel.com-20060911224857-d7008be21aeee33e
Switch from individual functions to a class

Show diffs side-by-side

added added

removed removed

Lines of Context:
149
149
        return module
150
150
 
151
151
 
152
 
def _convert_import_str_to_map(import_str, imports):
153
 
    """This converts a import string into an import map.
154
 
 
155
 
    This only understands 'import foo, foo.bar, foo.bar.baz as bing'
156
 
 
157
 
    :param import_str: The import string to process
158
 
    :param imports: The current map of all imports
159
 
    """
160
 
    assert import_str.startswith('import ')
161
 
    import_str = import_str[len('import '):]
162
 
 
163
 
    for path in import_str.split(','):
164
 
        path = path.strip()
165
 
        as_hunks = path.split(' as ')
166
 
        if len(as_hunks) == 2:
167
 
            # We have 'as' so this is a different style of import
168
 
            # 'import foo.bar.baz as bing' creates a local variable
169
 
            # named 'bing' which points to 'foo.bar.baz'
170
 
            name = as_hunks[1].strip()
171
 
            module_path = as_hunks[0].strip().split('.')
172
 
            assert name not in imports
173
 
            # No children available in 'import foo as bar'
174
 
            imports[name] = (module_path, None, {})
175
 
        else:
176
 
            # Now we need to handle
177
 
            module_path = path.split('.')
178
 
            name = module_path[0]
179
 
            if name not in imports:
180
 
                # This is a new import that we haven't seen before
181
 
                module_def = ([name], None, {})
182
 
                imports[name] = module_def
183
 
            else:
184
 
                module_def = imports[name]
185
 
 
186
 
            cur_path = [name]
187
 
            cur = module_def[2]
188
 
            for child in module_path[1:]:
189
 
                cur_path.append(child)
190
 
                if child in cur:
191
 
                    cur = cur[child]
192
 
                else:
193
 
                    next = (cur_path[:], None, {})
194
 
                    cur[child] = next
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, {})
224
 
 
225
 
 
226
 
def _canonicalize_import_strings(text):
227
 
    """Take a list of imports, and split it into regularized form.
228
 
 
229
 
    This is meant to take regular import text, and convert it to
230
 
    the forms that the rest of the converters prefer.
231
 
    """
232
 
    out = []
233
 
    cur = None
234
 
    continuing = False
235
 
 
236
 
    for line in text.split('\n'):
237
 
        line = line.strip()
238
 
        loc = line.find('#')
239
 
        if loc != -1:
240
 
            line = line[:loc].strip()
241
 
 
242
 
        if not line:
243
 
            continue
244
 
        if cur is not None:
245
 
            if line.endswith(')'):
246
 
                out.append(cur + ' ' + line[:-1])
247
 
                cur = None
248
 
            else:
249
 
                cur += ' ' + line
250
 
        else:
251
 
            if '(' in line and ')' not in line:
252
 
                cur = line.replace('(', '')
253
 
            else:
254
 
                out.append(line.replace('(', '').replace(')', ''))
255
 
    assert cur is None
256
 
    return out
 
152
class ImportProcessor(object):
 
153
    """Convert text that users input into import requests"""
 
154
 
 
155
    __slots__ = ['imports']
 
156
 
 
157
    def __init__(self):
 
158
        self.imports = {}
 
159
 
 
160
    def _convert_import_str(self, import_str):
 
161
        """This converts a import string into an import map.
 
162
 
 
163
        This only understands 'import foo, foo.bar, foo.bar.baz as bing'
 
164
 
 
165
        :param import_str: The import string to process
 
166
        """
 
167
        assert import_str.startswith('import ')
 
168
        import_str = import_str[len('import '):]
 
169
 
 
170
        for path in import_str.split(','):
 
171
            path = path.strip()
 
172
            as_hunks = path.split(' as ')
 
173
            if len(as_hunks) == 2:
 
174
                # We have 'as' so this is a different style of import
 
175
                # 'import foo.bar.baz as bing' creates a local variable
 
176
                # named 'bing' which points to 'foo.bar.baz'
 
177
                name = as_hunks[1].strip()
 
178
                module_path = as_hunks[0].strip().split('.')
 
179
                assert name not in self.imports
 
180
                # No children available in 'import foo as bar'
 
181
                self.imports[name] = (module_path, None, {})
 
182
            else:
 
183
                # Now we need to handle
 
184
                module_path = path.split('.')
 
185
                name = module_path[0]
 
186
                if name not in self.imports:
 
187
                    # This is a new import that we haven't seen before
 
188
                    module_def = ([name], None, {})
 
189
                    self.imports[name] = module_def
 
190
                else:
 
191
                    module_def = self.imports[name]
 
192
 
 
193
                cur_path = [name]
 
194
                cur = module_def[2]
 
195
                for child in module_path[1:]:
 
196
                    cur_path.append(child)
 
197
                    if child in cur:
 
198
                        cur = cur[child]
 
199
                    else:
 
200
                        next = (cur_path[:], None, {})
 
201
                        cur[child] = next
 
202
                        cur = next[2]
 
203
 
 
204
    def _convert_from_str(self, from_str):
 
205
        """This converts a 'from foo import bar' string into an import map.
 
206
 
 
207
        :param from_str: The import string to process
 
208
        """
 
209
        assert from_str.startswith('from ')
 
210
        from_str = from_str[len('from '):]
 
211
 
 
212
        from_module, import_list = from_str.split(' import ')
 
213
 
 
214
        from_module_path = from_module.split('.')
 
215
 
 
216
        for path in import_list.split(','):
 
217
            path = path.strip()
 
218
            as_hunks = path.split(' as ')
 
219
            if len(as_hunks) == 2:
 
220
                # We have 'as' so this is a different style of import
 
221
                # 'import foo.bar.baz as bing' creates a local variable
 
222
                # named 'bing' which points to 'foo.bar.baz'
 
223
                name = as_hunks[1].strip()
 
224
                module = as_hunks[0].strip()
 
225
            else:
 
226
                name = module = path
 
227
            assert name not in self.imports
 
228
            self.imports[name] = (from_module_path, module, {})
 
229
 
 
230
 
 
231
    def _canonicalize_import_strings(self, text):
 
232
        """Take a list of imports, and split it into regularized form.
 
233
 
 
234
        This is meant to take regular import text, and convert it to
 
235
        the forms that the rest of the converters prefer.
 
236
        """
 
237
        out = []
 
238
        cur = None
 
239
        continuing = False
 
240
 
 
241
        for line in text.split('\n'):
 
242
            line = line.strip()
 
243
            loc = line.find('#')
 
244
            if loc != -1:
 
245
                line = line[:loc].strip()
 
246
 
 
247
            if not line:
 
248
                continue
 
249
            if cur is not None:
 
250
                if line.endswith(')'):
 
251
                    out.append(cur + ' ' + line[:-1])
 
252
                    cur = None
 
253
                else:
 
254
                    cur += ' ' + line
 
255
            else:
 
256
                if '(' in line and ')' not in line:
 
257
                    cur = line.replace('(', '')
 
258
                else:
 
259
                    out.append(line.replace('(', '').replace(')', ''))
 
260
        assert cur is None
 
261
        return out