Example #1
0
        public void Imports(PythonFileBuilder builder)
        {
            Dictionary <string, HashSet <string> > namespaces = new Dictionary <string, HashSet <string> >();

            foreach (Type type in parentClasses)
            {
                string ns   = type.Namespace;
                string name = Utils.CleanupNamePython(type);

                HashSet <string> inNs;
                if (!namespaces.TryGetValue(ns, out inNs))
                {
                    inNs = new HashSet <string>();
                    namespaces.Add(ns, inNs);
                }
                inNs.Add(name);
            }

            foreach (var pair in namespaces)
            {
                builder.Append("from ");
                builder.Append(pair.Key);
                builder.Append(" import ");
                builder.Append(String.Join(", ", pair.Value));
                builder.LineBreak();
            }
        }
Example #2
0
        public void Constructor(PythonFileBuilder builder)
        {
            ctr.StringRepr(builder);
            builder.AddIndent();
            builder.LineBreak();

            if (fields.Count == 0 && events.Count == 0)
            {
                builder.Append("pass");
                builder.RemoveIndent();
                builder.LineBreak();
                return;
            }

            foreach (PythonField field in fields)
            {
                field.StringRepr(builder);
                builder.LineBreak();
            }
            foreach (PythonEvent ev in events)
            {
                ev.StringRepr(builder);
                builder.LineBreak();
            }
            builder.RemoveIndent();
            builder.LineBreak();
        }
Example #3
0
 public void Methods(PythonFileBuilder builder)
 {
     foreach (PythonMethod method in methods)
     {
         method.StringRepr(builder);
         builder.LineBreak();
     }
 }
Example #4
0
 public void StaticMethods(PythonFileBuilder builder)
 {
     foreach (PythonStaticMethod method in staticMethods)
     {
         method.StringRepr(builder);
         builder.LineBreak();
     }
     if (staticMethods.Count != 0)
     {
         builder.LineBreak();
     }
 }
Example #5
0
        public override void StringRepr(PythonFileBuilder builder)
        {
            MemberDocumentation doc = parent.xmlDoc?.GetDocs(method);

            builder.Append($"def {method.Name}{GenericArguments()}({Args()}):");
            builder.AddIndent();
            builder.LineBreak();
            //generate doc string for type hints

            if (doc != null || method.GetParameters().Length != 0)
            {
                builder.Append("\"\"\"");
                builder.LineBreak();
            }

            if (doc != null)
            {
                MemberDocExporter exporter = new PythonDocCommentExporter(doc);

                string       docstring = exporter.Export();
                string       line;
                StringReader reader = new StringReader(docstring);
                while ((line = reader.ReadLine()) != null)
                {
                    line = line.Trim();
                    builder.Append(line);
                    builder.LineBreak();
                }
            }
            if (method.GetParameters().Length != 0)
            {
                foreach (ParameterInfo param in method.GetParameters())
                {
                    Type paramType = param.ParameterType;
                    builder.Append($":type {param.Name}: {paramType.Namespace}.{Utils.CleanupNamePython(paramType)}");
                    builder.LineBreak();
                }
                if (method.ReturnType != typeof(void))
                {
                    builder.Append($":rtype: {method.ReturnType.Namespace}.{Utils.CleanupNamePython(method.ReturnType)}");
                    builder.LineBreak();
                }
            }

            if (doc != null || method.GetParameters().Length != 0)
            {
                builder.Append("\"\"\"");
                builder.LineBreak();
            }

            builder.Append("pass");
            builder.RemoveIndent();
        }
Example #6
0
        public string StringRepr()
        {
            PythonFileBuilder builder = new PythonFileBuilder();

            //this creates an import line above each python class,
            //there might be duplicate import line after merging all the classes
            //in a single file but since it'll never never anyway
            Imports(builder);

            builder.Append($"class {Utils.CleanupNamePython(className)}({ParentClasses()}):");
            builder.AddIndent();
            builder.LineBreak();

            StaticFields(builder);
            StaticMethods(builder);
            Constructor(builder);
            Methods(builder);

            return(builder.ToString());
        }
Example #7
0
 public override void StringRepr(PythonFileBuilder builder)
 {
     builder.Append($"self.{ev.Name} = None");
 }
Example #8
0
 public override void StringRepr(PythonFileBuilder builder)
 {
     builder.Append(name + " = None");
 }
Example #9
0
 public override void StringRepr(PythonFileBuilder builder)
 {
     builder.Append("@staticmethod");
     builder.LineBreak();
     base.StringRepr(builder);
 }
Example #10
0
 public override void StringRepr(PythonFileBuilder builder)
 {
     builder.Append($"def __init__({Args()}):");
 }