Commit 58f32e5f authored by Ruben Seggers's avatar Ruben Seggers
Browse files

Merge branch 'schema-export-indexers' into 'dev'

Schema export indexers

See merge request memri/schema!19
parents 8e5d3398 2168301b
Showing with 114 additions and 0 deletions
+114 -0
......@@ -6,6 +6,7 @@
*schema_target.ts
*autogenerated_database_schema.json
*autogenerated_database_schema_target.json
*schema.py
# IDE / editor
*.swp
......
const fs = require('fs');
const helpers = require('./helpers');
const path = require('path');
const entityHierarchyPath = path.resolve('../TypeHierarchy/Item');
const predicateHierarchyPath = path.resolve('../EdgeAndPropertyHierarchy');
const outputFile = './schema.py';
function getItemClasses() {
let attributesItem = entityHierarchy['Item']['properties'].concat(Object.keys(entityHierarchy['Item']['relations']));
let itemArguments = "";
let itemClasses = [];
for (const item of Object.keys(entityHierarchy)) {
if (['SyncableItem', 'Edge', 'Datasource', 'UserState', 'ViewArguments', 'CVUStateDefinition'].includes(item)) continue;
let classDescription = `\n# ${entityHierarchy[item]['description']}\n`;
classDescription = helpers.wrapText(`# ${entityHierarchy[item]['description']}`, 100, '\n# ');
let ancestry = helpers.getAncestry(entityHierarchy[item]['path'].split('/'));
let properties = [], edges = [];
for (const _item in ancestry) {
properties = properties.concat(entityHierarchy[_item]['properties']);
edges = edges.concat(Object.keys(entityHierarchy[_item]['relations']));
}
let arguments = "", fromJsonEdgeLoop = [];
let attributes = [], fromJsonEdges = [], fromJsonProperties = [];
for (const attribute of properties.concat(edges)) {
if (['genericType', 'functions', 'updatedFields', 'allEdges'].includes(attribute)) continue;
arguments += `${arguments === '' ? '' : ', '}${attribute}=None`;
if (item === 'Item') itemArguments += `${itemArguments === '' ? '' : ', '}${attribute}=${attribute}`;
if (item === 'Item' && attribute === 'uid') continue
if (properties.includes(attribute)) {
fromJsonProperties.push(`${attribute} = json.get("${attribute}", None)`);
} else {
fromJsonEdges.push(`${attribute} = []`)
let singular;
for (const _item of Object.keys(entityHierarchy)) {
if (entityHierarchy[_item]['relations'][attribute]) singular = entityHierarchy[_item]['relations'][attribute]['singular'];
}
let isOrAppend = singular ? '= edge' : '.append(edge)'
let ifOrElif = fromJsonEdgeLoop.length === 0 ? 'if' : 'elif';
fromJsonEdgeLoop.push(`${ifOrElif} edge._type == "${attribute}" or edge._type == "~${attribute}":
${attribute}${isOrAppend}`)
}
if (attributesItem.includes(attribute) && item !== 'Item') continue;
attributes.push(`self.${attribute} = ${attribute}`);
}
let dataItemClass;
if (item === 'Item') {
dataItemClass = `
${classDescription}
class Item(ItemBase):
${helpers.wrapText(`def __init__(self, ${arguments})`, 100, '\n' + ' '.repeat(17))}:
super().__init__(uid)
${helpers.insertList(attributes, 8)}`;
} else {
dataItemClass = `
${classDescription}
class ${item}(Item):
${helpers.wrapText(`def __init__(self, ${arguments})`, 100, '\n' + ' '.repeat(17))}:
${helpers.wrapText(`super().__init__(${itemArguments})`, 100, '\n' + ' '.repeat(25))}
${helpers.insertList(attributes, 8)}
@classmethod
def from_json(cls, json):
all_edges = json.get("allEdges", None)
${helpers.insertList(fromJsonProperties, 8)}
${helpers.insertList(fromJsonEdges,8)}
if all_edges is not None:
for edge_json in all_edges:
edge = Edge.from_json(edge_json)
${helpers.insertList(fromJsonEdgeLoop,16)}
${helpers.wrapText(`res = cls(${arguments}`, 100, '\n' + ' '.repeat(18))})
return res`;
}
itemClasses.push(dataItemClass);
}
return itemClasses;
}
let entityHierarchy = {};
let predicateHierarchy = {};
(async () => {
await helpers.getHierarchy(entityHierarchyPath, entityHierarchy, entityHierarchyPath, 'Item');
await helpers.getHierarchy(predicateHierarchyPath, predicateHierarchy, predicateHierarchyPath, 'EdgeOrProperty');
const itemClasses = getItemClasses();
const output = `#
# WARNING: THIS FILE IS AUTOGENERATED; DO NOT CHANGE.
# Visit https://gitlab.memri.io/memri/schema to learn more.
#
# schema.py
#
# Copyright © 2020 memri. All rights reserved.
#
from .itembase import ItemBase
${helpers.insertList(itemClasses, 0)}`;
fs.writeFile(outputFile, output, (err) => {
if (err) throw err;
console.log('File saved as ' + outputFile);
});
})();
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment