ModelDiscovery.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. from contextlib import closing
  2. from glob import glob
  3. from json import load, dumps
  4. from os import path
  5. def discover(database):
  6. """
  7. find models in the corresponding folder and add them to the database
  8. :param database:
  9. :return:
  10. """
  11. with closing(database.cursor()) as cursor:
  12. # list folders in models/
  13. for folder in glob('models/*'):
  14. # load distribution.json
  15. with open(path.join(folder, 'configuration.json'), 'r') as file:
  16. model = load(file)
  17. # extract data
  18. name = model['name']
  19. description = model['description'] if 'description' in model else None
  20. supports = dumps(model['supports'])
  21. # save to database
  22. cursor.execute('''
  23. INSERT INTO models (name, description, root_folder, supports)
  24. VALUES (?, ?, ?, ?)
  25. ON CONFLICT (root_folder)
  26. DO UPDATE SET name = ?, description = ?, supports = ?
  27. ''', (name, description, folder, supports, name, description, supports))