|
@@ -46,29 +46,14 @@ class OperationWriter(object):
|
|
|
self.buff = []
|
|
|
|
|
|
def serialize(self):
|
|
|
- imports = set()
|
|
|
- name, args, kwargs = self.operation.deconstruct()
|
|
|
- argspec = inspect.getargspec(self.operation.__init__)
|
|
|
- normalized_kwargs = inspect.getcallargs(self.operation.__init__, *args, **kwargs)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if getattr(migrations, name, None) == self.operation.__class__:
|
|
|
- self.feed('migrations.%s(' % name)
|
|
|
- else:
|
|
|
- imports.add('import %s' % (self.operation.__class__.__module__))
|
|
|
- self.feed('%s.%s(' % (self.operation.__class__.__module__, name))
|
|
|
|
|
|
- self.indent()
|
|
|
- for arg_name in argspec.args[1:]:
|
|
|
- arg_value = normalized_kwargs[arg_name]
|
|
|
- if (arg_name in self.operation.serialization_expand_args and
|
|
|
- isinstance(arg_value, (list, tuple, dict))):
|
|
|
- if isinstance(arg_value, dict):
|
|
|
- self.feed('%s={' % arg_name)
|
|
|
+ def _write(_arg_name, _arg_value):
|
|
|
+ if (_arg_name in self.operation.serialization_expand_args and
|
|
|
+ isinstance(_arg_value, (list, tuple, dict))):
|
|
|
+ if isinstance(_arg_value, dict):
|
|
|
+ self.feed('%s={' % _arg_name)
|
|
|
self.indent()
|
|
|
- for key, value in arg_value.items():
|
|
|
+ for key, value in _arg_value.items():
|
|
|
key_string, key_imports = MigrationWriter.serialize(key)
|
|
|
arg_string, arg_imports = MigrationWriter.serialize(value)
|
|
|
self.feed('%s: %s,' % (key_string, arg_string))
|
|
@@ -77,18 +62,47 @@ class OperationWriter(object):
|
|
|
self.unindent()
|
|
|
self.feed('},')
|
|
|
else:
|
|
|
- self.feed('%s=[' % arg_name)
|
|
|
+ self.feed('%s=[' % _arg_name)
|
|
|
self.indent()
|
|
|
- for item in arg_value:
|
|
|
+ for item in _arg_value:
|
|
|
arg_string, arg_imports = MigrationWriter.serialize(item)
|
|
|
self.feed('%s,' % arg_string)
|
|
|
imports.update(arg_imports)
|
|
|
self.unindent()
|
|
|
self.feed('],')
|
|
|
else:
|
|
|
- arg_string, arg_imports = MigrationWriter.serialize(arg_value)
|
|
|
- self.feed('%s=%s,' % (arg_name, arg_string))
|
|
|
+ arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
|
|
|
+ self.feed('%s=%s,' % (_arg_name, arg_string))
|
|
|
imports.update(arg_imports)
|
|
|
+
|
|
|
+ imports = set()
|
|
|
+ name, args, kwargs = self.operation.deconstruct()
|
|
|
+ argspec = inspect.getargspec(self.operation.__init__)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if getattr(migrations, name, None) == self.operation.__class__:
|
|
|
+ self.feed('migrations.%s(' % name)
|
|
|
+ else:
|
|
|
+ imports.add('import %s' % (self.operation.__class__.__module__))
|
|
|
+ self.feed('%s.%s(' % (self.operation.__class__.__module__, name))
|
|
|
+
|
|
|
+ self.indent()
|
|
|
+
|
|
|
+
|
|
|
+ for i, arg in enumerate(args, 1):
|
|
|
+ arg_value = arg
|
|
|
+ arg_name = argspec.args[i]
|
|
|
+ _write(arg_name, arg_value)
|
|
|
+
|
|
|
+ i = len(args)
|
|
|
+
|
|
|
+ for arg_name in argspec.args[i + 1:]:
|
|
|
+ if arg_name in kwargs:
|
|
|
+ arg_value = kwargs[arg_name]
|
|
|
+ _write(arg_name, arg_value)
|
|
|
+
|
|
|
self.unindent()
|
|
|
self.feed('),')
|
|
|
return self.render(), imports
|