Skip to content

Instantly share code, notes, and snippets.

@Slater-Victoroff
Created January 18, 2019 03:44
Show Gist options
  • Save Slater-Victoroff/8293acd1d4241de557b0895d45d808f8 to your computer and use it in GitHub Desktop.
Save Slater-Victoroff/8293acd1d4241de557b0895d45d808f8 to your computer and use it in GitHub Desktop.
import os
import re
def fix_moodle_output(assn_dir, outdir):
for (_, _, filenames) in os.walk(assn_dir):
for filename in filenames:
# Remove spaces so we can use this as a package name
name = filename.split("_")[0].replace(" ", "").replace("-", "")
if not os.path.exists("%s/%s" % (outdir, name)):
os.makedirs("%s/%s" % (outdir, name))
file_type = filename.split(".")[-1]
if file_type == "java":
new_name = filename.split("_")[1]
source = "%s/%s" % (assn_dir, filename)
print(filename)
java_class = open(source, encoding="utf-8", errors="ignore").read()
modified_class = re.sub(
"package (.*?);",
"package %s;" % name,
java_class
)
dest = "%s/%s/%s.java" % (outdir, name, new_name)
with open(dest, "w") as sink:
sink.write(modified_class)
if __name__ == "__main__":
fix_moodle_output(<assignment_directory>, <package_destination>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment