Skip to content

Commit 49eb76d

Browse files
feat(pfiles): add default meta profile
1 parent 354384a commit 49eb76d

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/python-generator/second-try/main.clj

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
(safe-conj (hash-map :base (get schema :base) :url (get schema :url))))) schemas))
6161

6262
(defn combine-elements [schemas]
63-
(map (fn [[name, schema]]
63+
(map (fn [[_, schema]]
6464
(->> schema
6565
(mix-parents-elements-circular schemas)
6666
(mix-parents-backbones-circular schemas))) schemas))
@@ -93,8 +93,6 @@
9393
(case (help/get-resource-name (some #(when (= (name key) (:name %)) (:value %)) elements))
9494
"CodeableConcept" (pattern-codeable-concept (str (help/uppercase-first-letter (help/get-resource-name constraint-name)) (help/uppercase-first-letter (subs (str key) 1))) schema) ""))
9595

96-
97-
9896
(defn apply-patterns [constraint-name patterns schema]
9997
(->> (map (fn [item]
10098
(if-let [pattern (some #(when (= (name (first %)) (:name item)) (last %)) patterns)]
@@ -103,16 +101,19 @@
103101
"CodeableConcept" (conj item (hash-map :value (str (str/join (map help/uppercase-first-letter (str/split (help/get-resource-name constraint-name) #"-"))) (str/join (map help/uppercase-first-letter (str/split (:name item) #"-")))) :codeable-concept-pattern true))
104102
"Quantity" item item) item)) (:elements schema))
105103
(hash-map :elements)
106-
(conj schema)
107-
(conj (hash-map :patterns (map (fn [item] (create-single-pattern constraint-name item (:elements schema))) patterns)))))
104+
(conj schema (hash-map :patterns (concat (get schema :patterns []) (map (fn [item] (create-single-pattern constraint-name item (:elements schema))) patterns))))))
108105

106+
(defn add-meta [constraint-name elements]
107+
(->> (filter #(not (= (:name %) "meta")) elements)
108+
(concat [{:name "meta" :required true :value (str "Meta = Meta(profile=[\"" constraint-name "\"])")}])))
109109

110110
(defn apply-single-constraint [constraint parent-schema]
111111
(println (:url constraint) (reset! constraint-count (+ 1 (deref constraint-count))))
112112
(->> (:elements parent-schema)
113113
(apply-required (:required constraint))
114114
(apply-excluded (:excluded constraint))
115115
(apply-choises (filter #(contains? (last %) :choices) (:elements constraint)))
116+
(add-meta (:url constraint))
116117
(hash-map :elements)
117118
(conj parent-schema)
118119
(apply-patterns (:url constraint) (filter #(contains? (last %) :pattern) (:elements constraint)))))
@@ -131,7 +132,7 @@
131132
(defn get-class-name [profile-name]
132133
(str/join "" (map help/uppercase-first-letter (clojure.string/split (help/get-resource-name profile-name) #"-"))))
133134

134-
(defn combine-single-class [name elements]
135+
(defn combine-single-class [name elements t]
135136
(->> (map (fn [item]
136137
(when (not (contains? item :choices))
137138
(->> (:value item)
@@ -144,36 +145,36 @@
144145
(str "\t" (:name item) ": ")
145146
(str "\n")))) elements)
146147
(str/join "")
147-
(str "\n\nclass " (get-class-name name) "(BaseModel):")))
148+
(str "\n\nclass " (get-class-name name) "(" (case t "backbone" "BackboneElement" "BaseModel") "):")))
148149

149150
(defn save-to-file [[name, definition]]
150-
(->> (str (combine-single-class name (:elements definition)))
151-
(str (str/join (map (fn [definition] (combine-single-class (:name definition) (:elements definition))) (:backbone-elements definition))))
151+
(->> (str (combine-single-class name (:elements definition) "default"))
152+
(str (str/join (map (fn [definition] (combine-single-class (:name definition) (:elements definition) "backbone")) (:backbone-elements definition))))
152153
(str (str/join (:patterns definition)))
153154
(str "from base import *\n")
154155
(str "from typing import Optional, List, Literal\n")
155156
(str "from pydantic import BaseModel\n")
156-
(help/write-to-file "/Users/letzabelin/projects/health-samurai/aidbox-sdk-js/test_dir" (str/join "_" (str/split (help/get-resource-name name) #"-")))))
157+
(help/write-to-file "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/test_dir/constraint" (str/join "_" (str/split (help/get-resource-name name) #"-")))))
157158

158159
(defn doallmap [elements] (doall (map save-to-file elements)))
159160

160161
(defn flat-backbones [backbone-elements accumulator]
161-
(reduce (fn [acc, item] (concat (flat-backbones (:backbone-elements item) acc)
162-
[(dissoc item :backbone-elements)]) )
163-
accumulator
162+
(reduce (fn [acc, item] (concat (flat-backbones (:backbone-elements item) acc)
163+
[(dissoc item :backbone-elements)]))
164+
accumulator
164165
backbone-elements))
165166

166167
(defn main []
167-
(let [schemas (help/parse-ndjsonk -gz "/Users/letzabelin/projects/health-samurai/aidbox-sdk-js/test_dir/1.0.0_hl7.fhir.r4.core#4.0.1_package.ndjson (1).gz")
168+
(let [schemas (help/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-python-tooklit/fhir-schema-2/1.0.0_hl7.fhir.r4.core#4.0.1_package.ndjson.gz")
168169
base-schemas (->> schemas (filter #(or (= (:url %) "http://hl7.org/fhir/StructureDefinition/BackboneElement") (= (:url %) "http://hl7.org/fhir/StructureDefinition/Resource") (= (:derivation %) "specialization"))))
169170
constraint-schemas (->> schemas
170171
(filter #(= (:derivation %) "constraint"))
171172
(filter #(and (not (= (:type %) "Extension")) (not (= (:url %) "http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris")))))
172-
us-core (->> (help/parse-ndjson-gz "/Users/letzabelin/projects/health-samurai/aidbox-sdk-js/test_dir/1.0.0_hl7.fhir.us.core#4.0.0_package.ndjson.gz")
173+
us-core (->> (help/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-python-tooklit/fhir-schema-2/1.0.0_hl7.fhir.us.core#4.0.0_package.ndjson.gz")
173174
(filter #(and (not (= (:type %) "Extension")) (= (:derivation %) "constraint"))))
174-
mcode (->> (help/parse-ndjson-gz "/Users/letzabelin/projects/health-samurai/aidbox-sdk-js/test_dir/1.0.0_hl7.fhir.us.mcode#2.1.0_package.ndjson.gz")
175+
mcode (->> (help/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-python-tooklit/fhir-schema-2/1.0.0_hl7.fhir.us.mcode#2.1.0_package.ndjson.gz")
175176
(filter #(and (not (= (:type %) "Extension")) (= (:derivation %) "constraint"))))
176-
codex (->> (help/parse-ndjson-gz "/Users/letzabelin/projects/health-samurai/aidbox-sdk-js/test_dir/1.0.0_hl7.fhir.us.codex-radiation-therapy#1.0.0_package.ndjson.gz")
177+
codex (->> (help/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-python-tooklit/fhir-schema-2/1.0.0_hl7.fhir.us.codex-radiation-therapy#1.0.0_package.ndjson.gz")
177178
(filter #(and (not (= (:type %) "Extension")) (= (:derivation %) "constraint")))
178179
#_(filter #(= (:url %) "http://hl7.org/fhir/us/codex-radiation-therapy/StructureDefinition/codexrt-radiotherapy-adverse-event")))]
179180

0 commit comments

Comments
 (0)