Commits

Ronny Pfannschmidt  committed 9d5495d

module maker helpers

  • Participants
  • Parent commits 522ad77

Comments (0)

Files changed (5)

 *.class
 
 .tox
+.coverage
 *.egg-info

File genscript/__init__.py

Empty file added.

File genscript/module_maker.py

+def metadata_lines(metadata):
+    return ['%s = %r\n'%item for item in metadata.items()]
+
+
+def update_script(source, metadata):
+    lines = source.splitlines(True)
+    for index, line in enumerate(lines):
+        line = line.strip()
+        if line and line[0] == '#' and 'Genscript Metadata' in line:
+            if not line[-1] == '\n':
+                lines[index] = line + '\n'
+            break
+    else:
+        raise ValueError('no metadata insert point found')
+    lines[index+1:index] = metadata_lines(metadata)
+    return ''.join(lines)
+

File testing/__init__.py

Empty file added.

File testing/test_module_maker.py

+import py
+from genscript.module_maker import update_script
+
+def test_update_script():
+    source = py.code.Source("""
+    def fun():
+        pass
+
+    # Genscript Metadata
+    """)
+    result = update_script(str(source), dict(version='1.3'))
+    print result
+    assert "\nversion = '1.3'\n" in result
+
+def test_update_fails_for_missing():
+    py.test.raises(ValueError, update_script, 'test_line\n', {})
+
+
+
+