-
Notifications
You must be signed in to change notification settings - Fork 1
/
odootools.el
151 lines (117 loc) · 4.54 KB
/
odootools.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
;;; odootools.el --- Odoo tools for development
;; Copyright © 2017 Erick Navarro
;; Author: Erick Navarro <erick@navarro.io>
;;; Commentary:
;; Find Odoo security groups and views
;;; Code:
(require 'f)
(require 'helm)
(defvar odootools-addons-path "")
(defconst odootools--groups-db (f-join odootools-addons-path ".groups"))
(defconst odootools--views-db (f-join odootools-addons-path ".views"))
(defconst odootools--package-root (file-name-directory load-file-name))
(defconst odootools--analyzer-path (f-join odootools--package-root "analyzer.py"))
(defun odootools--read-groups-db ()
"Read groups db."
(let ((data (f-read-text odootools--groups-db)))
(split-string data "\n")))
(defun odootools--read-views-db ()
"Read views db."
(let ((data (f-read-text odootools--views-db)))
(split-string data "\n")))
(defun odootools--build-db ()
"Analyze files in odoo addons path."
(if (f-exists? odootools-addons-path)
(shell-command (format "python %s %s" odootools--analyzer-path odootools-addons-path))
(message "Please setup the Odoo addons path variable")))
;; HELM functions
(defun odootools--insert-group-id (candidate)
"Insert group ID selected as CANDIDATE."
(let ((group-id (car (cdr (split-string candidate ";")))))
(insert group-id)))
(defun odootools--insert-view-id (candidate)
"Insert view ID selected as CANDIDATE."
(let ((view-id (car (cdr (split-string candidate ";")))))
(insert view-id)))
(defun odootools--open-view-file (candidate)
"Open buffer with selected file as CANDIDATE."
(let ((path (car (split-string candidate ";")))
(view-id (car (cdr (split-string candidate ";")))))
(find-file-existing path)
(goto-line 0)
(search-forward (concat "id=\"" view-id "\""))))
(defun odootools--group-transformer (candidate)
"Format candidate as CANDIDATE."
(let ((splited (split-string candidate ";")))
(let ((name (car splited))
(group-id (car (cdr splited))))
(format "%s:%s" name group-id))))
(defun odootools--view-transformer (candidate)
"Format candidate as CANDIDATE."
(message candidate)
(let ((splited (split-string candidate ";")))
(let ((path (f-relative (car splited) odootools-addons-path))
(view-id (car (cdr splited))))
(format "%s:%s" path view-id))))
(defun odootools--groups-source ()
"Group IDs source."
(helm-build-sync-source "Security groups IDs"
:candidates (lambda ()
(odootools--read-groups-db))
:real-to-display 'odootools--group-transformer
:action '(("Insert this group ID `C-c'" . odootools--insert-group-id))))
(defun odootools--views-source ()
"View IDs source."
(helm-build-sync-source "Views IDs"
:candidates (lambda ()
(odootools--read-views-db))
:real-to-display 'odootools--view-transformer
:action '(("Insert this view ID `C-c'" . odootools--insert-view-id))))
(defun odootools--views-open-source ()
"View IDs source."
(helm-build-sync-source "Views"
:candidates (lambda ()
(odootools--read-views-db))
:real-to-display 'odootools--view-transformer
:action '(("Open the view file `C-c'" . odootools--open-view-file))))
;; Public API
(defun odootools-rebuild-db ()
"Rebuild the addons index."
(interactive)
(odootools--build-db))
(defun odootools-find-group-id ()
"Find group ID."
(interactive)
(if (not (f-exists? odootools--groups-db))
(odootools--build-db))
(helm :sources (odootools--groups-source)
:buffer "*helm for search group ID*"))
(defun odootools-find-view-id ()
"Find view ID."
(interactive)
(if (not (f-exists? odootools--views-db))
(odootools--build-db))
(helm :sources (odootools--views-source)
:buffer "*helm for search view ID*"))
(defun odootools-find-view-file ()
"Find view file."
(interactive)
(if (not (f-exists? odootools--views-db))
(odootools--build-db))
(helm :sources (odootools--views-open-source)
:buffer "*helm for search view file*"))
;; Yasnippets conf
(defun odootools--yas-capitalize-dot-name (name)
"Allow convert a dot model name to camelcase as NAME."
(mapconcat 'identity (mapcar (lambda (n) (capitalize n)) (split-string name "\\.")) ""))
(defun odootools--initialize-snippets ()
"Load snippets."
(let ((snip-dir (expand-file-name "snippets" odootools--package-root)))
(when (boundp 'yas-snippet-dirs)
(add-to-list 'yas-snippet-dirs snip-dir t))
(yas-load-directory snip-dir)))
(eval-after-load 'yasnippet
'(odootools--initialize-snippets))
(require 'yasnippet)
(provide 'odootools)
;;; odootools.el ends here