return strings from the module functions

__main__ is now handling file writes. This way
module is easier to use from other python scripts

Signed-off-by: Karol Gugala <kgugala@antmicro.com>
diff --git a/v2x/__main__.py b/v2x/__main__.py
index b54a175..91cd92d 100644
--- a/v2x/__main__.py
+++ b/v2x/__main__.py
@@ -7,9 +7,11 @@
 
 def main(args):
     if args.mode == "pb_type":
-        vlog_to_pbtype.vlog_to_pbtype(args.infiles, args.outfile, args.top)
+        with open(args.outfile, "w") as fp:
+            fp.write(vlog_to_pbtype.vlog_to_pbtype(args.infiles, args.outfile, args.top))
     else:
-        vlog_to_model.vlog_to_model(args.infiles, args.includes, args.top, args.outfile)
+        with open(args.outfile, "w") as fp:
+            fp.write(vlog_to_model.vlog_to_model(args.infiles, args.includes, args.top, args.outfile))
 
 if __name__ == '__main__':
     parser = argparse.ArgumentParser(
diff --git a/v2x/vlog_to_model.py b/v2x/vlog_to_model.py
index 993abe8..f95d924 100755
--- a/v2x/vlog_to_model.py
+++ b/v2x/vlog_to_model.py
@@ -200,8 +200,5 @@
     if len(models_xml) == 0:
         models_xml.insert(0, ET.Comment("this file is intentionally left blank"))
 
-    f = open(outfile, 'w')
-    f.write(ET.tostring(models_xml, pretty_print=True).decode('utf-8'))
-    f.close()
-    print("Generated {} from {}".format(outfile, iname))
+    return ET.tostring(models_xml, pretty_print=True).decode('utf-8')
 
diff --git a/v2x/vlog_to_pbtype.py b/v2x/vlog_to_pbtype.py
index 564d583..6333de6 100755
--- a/v2x/vlog_to_pbtype.py
+++ b/v2x/vlog_to_pbtype.py
@@ -800,8 +800,6 @@
 def vlog_to_pbtype(infiles, outfile, top=None):
     iname = os.path.basename(infiles[0])
 
-    outfile = open(outfile, 'w')
-
     run.add_define("PB_TYPE")
     vjson = run.vlog_to_json(infiles, flatten=False, aig=False)
     yj = YosysJSON(vjson)
@@ -824,16 +822,12 @@
 
     tmod = yj.module(top)
 
-    pb_type_xml = make_pb_type(infiles, outfile.name, yj, tmod)
+    pb_type_xml = make_pb_type(infiles, outfile, yj, tmod)
 
-    outfile.write(
-        ET.tostring(
+    return ET.tostring(
             pb_type_xml,
             pretty_print=True,
             encoding="utf-8",
             xml_declaration=True
         ).decode('utf-8')
-    )
-    print("Generated {} from {}".format(outfile.name, iname))
-    outfile.close()