Skip to content

Instantly share code, notes, and snippets.

@warner
Created October 15, 2011 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warner/1288827 to your computer and use it in GitHub Desktop.
Save warner/1288827 to your computer and use it in GitHub Desktop.
potential replacement for initializer()
from templates import EMPTY_FOLDER
def create(output_root, template_path, content, out=sys.stdout):
# content may be EMPTY_FOLDER, which means just make the directories
path_components = template_path.split("/")
parents = path_components[:-1]
if parents:
output_dir = os.path.join(output_root, *parents)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print >>out, '* %s directory created' % ("/".join(parents))
if content is not EMPTY_FOLDER:
output_filename = os.path.join(output_root, *path_components)
f = open(output_filename, "w")
f.write(content)
f.close()
print >>out, '* %s written' % (template_path)
def initializer(env_root, args, template_name, out=sys.stdout, err=sys.stderr):
path = os.getcwd()
addon = os.path.basename(path)
# if more than one argument
if len(args) > 1:
print >>err, 'Too many arguments.'
return 1
# avoid clobbering existing files, but we tolerate things like .git
existing = [fn for fn in os.listdir(path) if not fn.startswith(".")]
if existing:
print >>err, 'This command must be run in an empty directory.'
return 1
template = addon_templates[template_name]
target_cfg = find_target_cfg(path, require_id=True, err=err)
package_json_obj = template["get_package_json_obj"](addon)
package_json = json.dumps(package_json_obj, indent=4)+"\n"
create(path, "package.json", package_json, out)
for file_path, content in template["content"].items():
assert file_path != "package.json"
if isinstance(content, callable):
content = content(target_cfg, env_root)
elif isinstance(content, str):
content = content % {'name': addon}
else:
assert content is EMPTY_FOLDER
create(path, file_path, content, out)
print >>out, '\nYour sample add-on is now ready.'
print >>out, 'Do "cfx test" to test it and "cfx run" to try it. Have fun!'
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment