@@ -55,38 +55,92 @@ class NoClasses(Error):
5555 pass
5656
5757
58+ abbreviations = {
59+ "expr" : "expression" ,
60+ "arg" : "argument" ,
61+ "stmt" : "statement" ,
62+ "decl" : "declaration" ,
63+ "repr" : "representation" ,
64+ "param" : "parameter" ,
65+ "int" : "integer" ,
66+ }
67+
68+ abbreviations .update ({f"{ k } s" : f"{ v } s" for k , v in abbreviations .items ()})
69+
70+ _abbreviations_re = re .compile ("|" .join (fr"\b{ abbr } \b" for abbr in abbreviations ))
71+
72+
73+ def _humanize (s : str ) -> str :
74+ ret = inflection .humanize (s )
75+ ret = ret [0 ].lower () + ret [1 :]
76+ ret = _abbreviations_re .sub (lambda m : abbreviations [m [0 ]], ret )
77+ return ret
78+
79+
80+ _format_re = re .compile (r"\{(\w+)\}" )
81+
82+
83+ def _get_doc (cls : schema .Class , prop : schema .Property , plural = None ):
84+ if prop .doc :
85+ if plural is None :
86+ # for consistency, ignore format in non repeated properties
87+ return _format_re .sub (lambda m : m [1 ], prop .doc )
88+ format = prop .doc
89+ nouns = [m [1 ] for m in _format_re .finditer (prop .doc )]
90+ if not nouns :
91+ noun , _ , rest = prop .doc .partition (" " )
92+ format = f"{{{ noun } }} { rest } "
93+ nouns = [noun ]
94+ transform = inflection .pluralize if plural else inflection .singularize
95+ return format .format (** {noun : transform (noun ) for noun in nouns })
96+
97+ prop_name = _humanize (prop .name )
98+ class_name = cls .default_doc_name or _humanize (inflection .underscore (cls .name ))
99+ if prop .is_predicate :
100+ return f"this { class_name } { prop_name } "
101+ if plural is not None :
102+ prop_name = inflection .pluralize (prop_name ) if plural else inflection .singularize (prop_name )
103+ return f"{ prop_name } of this { class_name } "
104+
105+
58106def get_ql_property (cls : schema .Class , prop : schema .Property , prev_child : str = "" ) -> ql .Property :
59107 args = dict (
60108 type = prop .type if not prop .is_predicate else "predicate" ,
61109 qltest_skip = "qltest_skip" in prop .pragmas ,
62110 prev_child = prev_child if prop .is_child else None ,
63111 is_optional = prop .is_optional ,
64112 is_predicate = prop .is_predicate ,
113+ description = prop .description
65114 )
66115 if prop .is_single :
67116 args .update (
68117 singular = inflection .camelize (prop .name ),
69118 tablename = inflection .tableize (cls .name ),
70119 tableparams = ["this" ] + ["result" if p is prop else "_" for p in cls .properties if p .is_single ],
120+ doc = _get_doc (cls , prop ),
71121 )
72122 elif prop .is_repeated :
73123 args .update (
74124 singular = inflection .singularize (inflection .camelize (prop .name )),
75125 plural = inflection .pluralize (inflection .camelize (prop .name )),
76126 tablename = inflection .tableize (f"{ cls .name } _{ prop .name } " ),
77127 tableparams = ["this" , "index" , "result" ],
128+ doc = _get_doc (cls , prop , plural = False ),
129+ doc_plural = _get_doc (cls , prop , plural = True ),
78130 )
79131 elif prop .is_optional :
80132 args .update (
81133 singular = inflection .camelize (prop .name ),
82134 tablename = inflection .tableize (f"{ cls .name } _{ prop .name } " ),
83135 tableparams = ["this" , "result" ],
136+ doc = _get_doc (cls , prop ),
84137 )
85138 elif prop .is_predicate :
86139 args .update (
87140 singular = inflection .camelize (prop .name , uppercase_first_letter = False ),
88141 tablename = inflection .underscore (f"{ cls .name } _{ prop .name } " ),
89142 tableparams = ["this" ],
143+ doc = _get_doc (cls , prop ),
90144 )
91145 else :
92146 raise ValueError (f"unknown property kind for { prop .name } from { cls .name } " )
@@ -109,6 +163,7 @@ def get_ql_class(cls: schema.Class, lookup: typing.Dict[str, schema.Class]):
109163 properties = properties ,
110164 dir = pathlib .Path (cls .group or "" ),
111165 ipa = bool (cls .ipa ),
166+ doc = cls .doc ,
112167 ** pragmas ,
113168 )
114169
0 commit comments