-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
755e292
commit d50b050
Showing
4 changed files
with
122 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
(ns python-generator.base | ||
(:require | ||
[python-generator.extractor :as extractors] | ||
[python-generator.helpers :as helpers] | ||
[cheshire.core] | ||
[clojure.string :as str]) | ||
(:gen-class)) | ||
|
||
(defn save-to [directory, filename] | ||
(fn [text] (helpers/write-to-file directory, filename, text))) | ||
|
||
(defn push-to-end [string1, string2] (str string2, string1)) | ||
|
||
(defn combine-base-file [definition, data] | ||
(-> (str "\nclass " (helpers/get-resource-name (:type definition)) "(BaseModel)" ":\n") | ||
(str (clojure.string/join "\n" (:elements data)) "\n"))) | ||
|
||
(defn combine-file [definition, data] | ||
(-> #_(str (helpers/add-element-import (filter boolean (:imports-element data)))) | ||
#_(str (helpers/add-backbone-element-import (filter boolean (:imports-backbone-element data)))) | ||
#_(push-to-end (str "if TYPE_CHECKING:\n\t\"import typings\"\n")) | ||
#_(str "else:\n\t\"import typings\"\n") | ||
#_((typechecking-ignore-runtime (ignore (filter boolean (:imports data))))) | ||
#_((if (= (helpers/get-resource-name (:base definition)) "DomainResource") (fn [string] (str string "from resource.index import DomainResource\n\n")) str)) | ||
#_((if (= (helpers/get-resource-name (:base definition)) "Resource") (fn [string] (str string "from base.resource import Resource\n\n")) str)) | ||
#_((if (= (helpers/get-resource-name (:base definition)) "BackboneElement") (fn [string] (str string "from element.index import BackboneElement\nfrom base.index import Element\n\n")) str)) | ||
#_((if (= (helpers/get-resource-name (:base definition)) "Element") (fn [string] (str string "from base.index import Element\n\n")) str)) | ||
(str "\nclass " (helpers/get-resource-name (:type definition)) (helpers/get-parent (:base definition)) ":\n") | ||
(str (clojure.string/join "\n" (:elements data)) "\n"))) | ||
|
||
(defn compile-single-class [] | ||
(fn [definition] | ||
(->> (helpers/elements-to-vector definition) | ||
(helpers/get-typings-and-imports (or (:required definition) [])) | ||
(combine-file definition)))) | ||
|
||
(defn compile-single-base-class [] | ||
(fn [definition] | ||
(->> (helpers/elements-to-vector definition) | ||
(helpers/get-typings-and-imports (or (:required definition) [])) | ||
(combine-base-file definition)))) | ||
|
||
(defn compile-bases [] | ||
(->> (helpers/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/fhir-schema/hl7.fhir.r4.core#4.0.1/package.ndjson.gz") | ||
(extractors/filter-base) | ||
(map (compile-single-base-class)) | ||
(str/join))) | ||
|
||
(defn compile-elements [] | ||
(->> (helpers/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/fhir-schema/hl7.fhir.r4.core#4.0.1/package.ndjson.gz") | ||
(extractors/filter-element) | ||
(map (compile-single-class)) | ||
(str/join))) | ||
|
||
(defn compile-backbone-elements [] | ||
(->> (helpers/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/fhir-schema/hl7.fhir.r4.core#4.0.1/package.ndjson.gz") | ||
(extractors/filter-backbone-element) | ||
(map (compile-single-class)) | ||
(str/join))) | ||
|
||
(defn compile-resources [] | ||
(->> (helpers/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/fhir-schema/hl7.fhir.r4.core#4.0.1/package.ndjson.gz") | ||
(extractors/filter-resource) | ||
(map (compile-single-class)) | ||
(str/join))) | ||
|
||
|
||
(defn root-compiler [] | ||
(->> (str (compile-resources)) | ||
(str (compile-backbone-elements)) | ||
(str (compile-elements)) | ||
(str (compile-bases)) | ||
(str "from pydantic import BaseModel\n\n") | ||
(str "from typing import Optional\n") | ||
(str "from __future__ import annotations\n") | ||
((save-to "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/test_dir/base" "__init__")))) | ||
|
||
(root-compiler) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,49 @@ | ||
(ns python-generator.index | ||
(:require | ||
[python-generator.extractor :as extractors] | ||
[python-generator.helpers :as helpers] | ||
[cheshire.core] | ||
[clojure.string :as str]) | ||
[clojure.string :as str] | ||
[python-generator.extractor :as extractors] | ||
[python-generator.helpers :as helpers]) | ||
(:gen-class)) | ||
|
||
(defn save-to [directory, filename] | ||
(fn [text] (helpers/write-to-file directory, filename, text))) | ||
|
||
(defn push-to-end [string1, string2] (str string2, string1)) | ||
(defn compile-single-backbone [definition, data] | ||
(->> #_(str "class " (helpers/get-resource-name (:type definition)) (helpers/get-parent (:base definition)) ":\n") | ||
(str (clojure.string/join "\n" (:elements data)) "\n\n"))) | ||
|
||
(defn ignore [data] | ||
(clojure.string/join (map (fn [item] (str "\t" item " = Any\n")), data))) | ||
(defn compile-backbone [definition] | ||
(->> (helpers/elements-to-vector definition) | ||
(helpers/get-typings-and-imports (or (:required definition) [])) | ||
(compile-single-backbone definition))) | ||
|
||
(defn typechecking-ignore-runtime [string] | ||
(fn [inner_string] (str inner_string string "\n"))) | ||
(defn test [data] | ||
(->> (filter (fn [item] (> (count item) 0)) (:backbone-elements data)) | ||
(map (fn [[k, v]] | ||
(->> (str (compile-backbone v)) | ||
(str "class " (helpers/uppercase-first-letter (name k)) "(BackboneElement):\n")))) | ||
(str/join))) | ||
|
||
(defn combine-file [definition, data] | ||
(-> (str (helpers/add-element-import (filter boolean (:imports-element data)))) | ||
(str (helpers/add-backbone-element-import (filter boolean (:imports-backbone-element data)))) | ||
#_(push-to-end (str "if TYPE_CHECKING:\n\t\"import typings\"\n")) | ||
(push-to-end (str "from typing import TYPE_CHECKING, Optional, Any\n\n")) | ||
#_(push-to-end (str "from __future__ import annotations\n")) | ||
#_(str "else:\n\t\"import typings\"\n") | ||
#_((typechecking-ignore-runtime (ignore (filter boolean (:imports data))))) | ||
((if (= (helpers/get-resource-name (:base definition)) "DomainResource") (fn [string] (str string "from resource.index import DomainResource\n\n")) str)) | ||
((if (= (helpers/get-resource-name (:base definition)) "Resource") (fn [string] (str string "from base.resource import Resource\n\n")) str)) | ||
((if (= (helpers/get-resource-name (:base definition)) "BackboneElement") (fn [string] (str string "from element.index import BackboneElement\nfrom base.index import Element\n\n")) str)) | ||
((if (= (helpers/get-resource-name (:base definition)) "Element") (fn [string] (str string "from base.index import Element\n\n")) str)) | ||
(str "\nclass " (helpers/get-resource-name (:type definition)) "Origin" (helpers/get-parent (:base definition)) ":\n") | ||
(str (clojure.string/join "\n" (:elements data)) "\n\n"))) | ||
(->> (str (clojure.string/join "\n" (:elements data)) "\n\n") | ||
(str "class " (helpers/get-resource-name (:type definition)) (helpers/get-parent (:base definition)) ":\n") | ||
(str (test data)) | ||
(str "from base import *\n\n") | ||
(str "from typing import Optional\n"))) | ||
|
||
(defn compile-single-class [directory] | ||
(fn [definition] | ||
(->> (helpers/elements-to-vector definition) | ||
(helpers/get-typings-and-imports (or (:required definition) [])) | ||
(combine-file definition) | ||
(push-to-end (str "class " (helpers/get-resource-name (:type definition)) "(" (helpers/get-resource-name (:type definition)) "Origin)" ": pass\n")) | ||
(str/join) | ||
((save-to directory (str/lower-case (helpers/get-resource-name (:type definition)))))))) | ||
|
||
(defn compile-bases [] | ||
(->> (helpers/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/fhir-schema/hl7.fhir.r4.core#4.0.1/package.ndjson.gz") | ||
(extractors/filter-base) | ||
(map (compile-single-class "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/test_dir/base")))) | ||
|
||
(defn compile-elements [] | ||
(->> (helpers/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/fhir-schema/hl7.fhir.r4.core#4.0.1/package.ndjson.gz") | ||
(extractors/filter-element) | ||
(map (compile-single-class "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/test_dir/element")))) | ||
|
||
(defn compile-backbone-elements [] | ||
(->> (helpers/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/fhir-schema/hl7.fhir.r4.core#4.0.1/package.ndjson.gz") | ||
(extractors/filter-backbone-element) | ||
(map (compile-single-class "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/test_dir/backbone")))) | ||
|
||
(defn compile-resources [] | ||
(->> (helpers/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/fhir-schema/hl7.fhir.r4.core#4.0.1/package.ndjson.gz") | ||
(extractors/filter-resource) | ||
(map (compile-single-class "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/test_dir/resource")))) | ||
|
||
(defn compile-domains [] | ||
(->> (helpers/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/fhir-schema/hl7.fhir.r4.core#4.0.1/package.ndjson.gz") | ||
(extractors/filter-domain-resource) | ||
(helpers/side-effect-map (compile-single-class "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/test_dir/domain")))) | ||
(helpers/side-effect-map (compile-single-class "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/test_dir/resources")))) | ||
|
||
(compile-bases) | ||
(compile-elements) | ||
(compile-backbone-elements) | ||
(compile-resources) | ||
(compile-domains) | ||
|