Skip to content

Commit

Permalink
fix: add guard python keyword props
Browse files Browse the repository at this point in the history
  • Loading branch information
spicyfalafel committed Sep 23, 2024
1 parent 06146e2 commit 4676887
Show file tree
Hide file tree
Showing 145 changed files with 1,154 additions and 1,138 deletions.
26 changes: 21 additions & 5 deletions src/aidbox_sdk/generator/python.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
(defn class-name
"Generate class name from schema url."
[url]
(uppercase-first-letter (url->resource-name url)))
(-> url
url->resource-name
uppercase-first-letter
(str/replace #"-" "_")))

This comment has been minimized.

Copy link
@krvital

krvital Sep 24, 2024

Contributor

Classes in python should be named in PascalCase (no underscores)

@spicyfalafel

This comment has been minimized.

Copy link
@spicyfalafel

spicyfalafel Sep 24, 2024

Author Contributor

That's true


(defn generate-deps [deps]
(->> deps
Expand Down Expand Up @@ -88,10 +91,21 @@
(defn ->backbone-type [element]
(str (:base element) "_" (uppercase-first-letter (:name element))))

(def restricted-python-words
#{"False" "None" "True" "and" "as" "assert" "async" "await" "break" "class"
"continue" "def" "del" "elif" "else" "except" "finally" "for" "from" "global"
"if" "import" "in" "is" "lambda" "nonlocal" "not" "or" "pass" "raise"
"return" "try" "while" "with" "yield"})

(defn guard-python-property-name [prop]
(if (restricted-python-words prop)
(str prop "_")
prop))

(defn generate-property
"Generates class property from schema element."
[element]
(let [name (->snake-case (:name element))
(let [name (guard-python-property-name (->snake-case (:name element)))
lang-type (if (= "BackboneElement" (:type element))
(->backbone-type element)
(->lang-type (:type element)))
Expand Down Expand Up @@ -158,7 +172,9 @@
"class " class-name' "(" base-class-name "):"
"\n"
properties
(when-not (seq properties) " pass"))))
(when-not (seq properties)
(str (apply str (repeat 4 " "))
"pass")))))

(defn generate-module
[& {:keys [deps classes]
Expand Down Expand Up @@ -193,7 +209,7 @@
:content (generate-module
:deps (concat [{:module "typing" :members ["Optional" "List"]}
{:module "pydantic" :members ["*"]}]
(map (fn [d] {:module (str "base." d) :members [d]}) (:deps ir-schema)))
(map (fn [d] {:module (str "..base." d) :members [d]}) (:deps ir-schema)))
:classes [(generate-class ir-schema
(map generate-class (:backbone-elements ir-schema)))])})

Expand All @@ -215,7 +231,7 @@
:content (generate-module
:deps (concat [{:module "typing" :members ["Optional" "List"]}
{:module "pydantic" :members ["*"]}]
(map (fn [d] {:module (str "base." d) :members [d]}) (:deps schema)))
(map (fn [d] {:module (str "..base." d) :members [d]}) (:deps schema)))
:classes (generate-class (assoc schema :url constraint-name)
(map generate-class (:backbone-elements schema))))})
constraint-ir-schemas))
Expand Down
12 changes: 6 additions & 6 deletions test/aidbox_sdk/snapshots/python/hl7-fhir-r4-core/Account.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions test/aidbox_sdk/snapshots/python/hl7-fhir-r4-core/Appointment.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions test/aidbox_sdk/snapshots/python/hl7-fhir-r4-core/AuditEvent.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/aidbox_sdk/snapshots/python/hl7-fhir-r4-core/Basic.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions test/aidbox_sdk/snapshots/python/hl7-fhir-r4-core/CarePlan.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions test/aidbox_sdk/snapshots/python/hl7-fhir-r4-core/CareTeam.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions test/aidbox_sdk/snapshots/python/hl7-fhir-r4-core/ChargeItem.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4676887

Please sign in to comment.