ScientificReport
generate.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import jinja2
3 import os
4 from sys import argv
5 
6 FILENAME = argv[0]
7 SCRIPTS_DIR = os.path.dirname(os.path.abspath(FILENAME))
8 BASE_DIR = os.path.dirname(SCRIPTS_DIR)
9 REPOSITORY_DIR = os.path.join(BASE_DIR, 'ScientificReport.DAL/Repositories')
10 REPOSITORY_TEMPLATE_FILE = "repository.cs.j2"
11 ENTITIES_DIR = os.path.join(BASE_DIR, 'ScientificReport.DAL/Entities')
12 
13 template_loader = jinja2.FileSystemLoader(searchpath=SCRIPTS_DIR)
14 template_env = jinja2.Environment(loader=template_loader)
15 repository_template = template_env.get_template(REPOSITORY_TEMPLATE_FILE)
16 
17 
18 def repository(name):
19  filename = os.path.join(REPOSITORY_DIR, name + 'Repository.cs')
20  if os.path.isfile(filename):
21  print("Delete or rename the file {} before generating a new one".format(filename))
22  exit(1)
23  name_singular = name[:len(name) - 1] if name[len(name) - 1] == 's' else name
24  output_text = repository_template.render(entity=name, entity_singular=name_singular)
25  return filename, output_text
26 
27 
28 if __name__ == '__main__':
29  if len(argv) != 3:
30  print("Usage: {} <class_type> <class_name>".format(FILENAME))
31  exit(1)
32  class_type = argv[1]
33  class_name = argv[2]
34 
35  entity_file = os.path.join(ENTITIES_DIR, class_name + '.cs')
36  if not os.path.isfile(entity_file):
37  print("Class {} does not exist, or is not at path {}".format(class_name, entity_file))
38  exit(1)
39 
40  if class_type == 'repository':
41  filename, output_text = repository(class_name)
42  with open(filename, 'w+') as f:
43  f.write(output_text)
def repository(name)
Definition: generate.py:18