Refactor¶
refactor
¶
CST/AST helpers and rewriter for Odoo model files.
Reads model files, classifies fields and methods against a project KB, and rewrites class bodies to apply canonical section headers and Google-style docstring skeletons. Pure file-I/O — no git, no CLI.
Classes:
| Name | Description |
|---|---|
ClassInfo |
Information about an Odoo model class found in a source file. |
SymbolInfo |
Information about a single field or method within an Odoo model class. |
Functions:
| Name | Description |
|---|---|
analyse_file |
Classify every Odoo model class and its symbols in a Python source file. |
rewrite_file |
Rewrite a Python source file by injecting section headers and docstring skeletons. |
ClassInfo
dataclass
¶
ClassInfo(class_name: str, model_name: Optional[str], inherit: List[str], is_new_model: bool, lineno: int, symbols: List[SymbolInfo] = (lambda: [])())
Information about an Odoo model class found in a source file.
Attributes:
| Name | Type | Description |
|---|---|---|
class_name |
str
|
Python class name. |
model_name |
Optional[str]
|
Value of |
inherit |
List[str]
|
Values of |
is_new_model |
bool
|
True when this module is the creator of the model, per the KB model_origins table. |
lineno |
int
|
Source line number of the class definition. |
symbols |
List[SymbolInfo]
|
Ordered list of fields and methods in the class. |
-
Reference
IO
Refactor
refactoranalyse_file
-
Reference
IO
Refactor
refactorrewrite_file
SymbolInfo
dataclass
¶
SymbolInfo(name: str, kind: str, section: str, lineno: int, has_docstring: bool = False, has_super: bool = False, super_methods: List[str] = (lambda: [])(), kb_entry: Optional[Dict[str, Any]] = None, is_override: bool = False, field_type: Optional[str] = None)
Information about a single field or method within an Odoo model class.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Symbol name. |
kind |
str
|
|
section |
str
|
Canonical section header (e.g. |
lineno |
int
|
Source line number of the definition. |
has_docstring |
bool
|
True if the method already has a docstring. |
has_super |
bool
|
True if the method calls |
super_methods |
List[str]
|
Names of methods called via |
kb_entry |
Optional[Dict[str, Any]]
|
Matching KB record, or |
is_override |
bool
|
True when the symbol is in the KB but has no |
field_type |
Optional[str]
|
|
analyse_file
¶
analyse_file(py_file: Path, kb: KBReader, modules_index: Dict[str, Any], custom_module: str, module_local_refs: Optional[Dict[Tuple[str, str], List[str]]] = None) -> List[ClassInfo]
Classify every Odoo model class and its symbols in a Python source file.
Reads the file, parses it with ast, and for each model class found
resolves its fields and methods against the KB. Syntax errors are logged
and produce an empty result rather than raising.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
Path to the Python source file to analyse. |
required |
|
KBReader
|
Open KB reader used for symbol and model lookups. |
required |
|
Dict[str, Any]
|
Pre-loaded modules dict from |
required |
|
str
|
Name of the module being analysed (used for KB lookups). |
required |
|
Optional[Dict[Tuple[str, str], List[str]]]
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
List[ClassInfo]
|
Ordered list of |
List[ClassInfo]
|
Empty when the file contains no Odoo model classes or fails to parse. |
Source code in src/oops/io/refactor.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
rewrite_file
¶
Rewrite a Python source file by injecting section headers and docstring skeletons.
Uses libcst for AST-preserving rewriting so comments and formatting are
retained. Returns the original source unchanged when classes is empty or
the file fails to parse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
Path to the Python source file to rewrite. |
required |
|
List[ClassInfo]
|
Analysis result from |
required |
Returns:
| Type | Description |
|---|---|
str
|
Rewritten source code as a string, or the original source on failure. |