1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
import os
import shutil
import sys
import jsonpickle
def capitalize_first_letter(identifier):
return identifier[0].capitalize() + identifier[1:]
def create_directory(directory, force):
if os.path.exists(directory):
if force:
shutil.rmtree(directory, ignore_errors=True)
else:
exit_with_error("output directory {} already exists", directory)
os.makedirs(directory)
def to_underscore_separated_name(name):
result = ""
first_char = True
for ch in name:
if ch.islower():
next_char = ch
else:
next_char = ch.lower()
if not first_char:
next_char = "_" + next_char
first_char = False
result += next_char
return result
def class_name_to_private_module_name(class_name):
# The leading underscore indicates that users are not intended to import
# the class module individually.
return "_" + to_underscore_separated_name(class_name)
def unpickle_file(path):
with open(path, mode="rt") as file_obj:
contents = file_obj.read()
return jsonpickle.decode(contents)
def exit_with_error(message, *args):
sys.stderr.write("error : " + message.format(*args) + "\n")
sys.exit(1)
|