/
pypredefgen_gimp.py
executable file
·167 lines (124 loc) · 4.67 KB
/
pypredefgen_gimp.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""This is a GIMP plug-in that generates predefined completions for PyDev
(Eclipse IDE plug-in) for GIMP modules.
"""
from __future__ import absolute_import, print_function, division, unicode_literals
import inspect
import io
import os
import sys
# Fix GIMP failing to import modules from subdirectories in the `plug-ins` directory.
current_module_dirpath = os.path.dirname(inspect.getfile(inspect.currentframe()))
if current_module_dirpath not in sys.path:
sys.path.append(current_module_dirpath)
import importlib
import gimp
import gimpfu
from pypredefgen_gimp import pypredefgen
from pypredefgen_gimp import pypredefgen_pdb
PLUGIN_DIRPATH = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
MODULES_FILEPATH = os.path.join(PLUGIN_DIRPATH, "pypredefgen_gimp", "modules.txt")
PYPREDEF_FILES_DIRNAME = "pypredefs"
PYPREDEF_FILES_DIRPATH = os.path.join(gimp.directory, PYPREDEF_FILES_DIRNAME)
MODULES_FOR_WHICH_TO_IGNORE_DOCSTRINGS = [
"_gimpui",
"gtk._gtk",
"gtk.gdk",
"gobject._gobject",
"cairo._cairo",
"pango",
"pangocairo",
"atk",
"glib._glib",
"gio._gio",
]
MODULES_TO_SKIP_FOR_WINDOWS = [
"_gimpui",
]
def generate_predefined_completions_for_pydev(output_dirpath=PYPREDEF_FILES_DIRPATH):
if not output_dirpath:
output_dirpath = PYPREDEF_FILES_DIRPATH
module_names = _get_module_names(MODULES_FILEPATH)
gimp_progress = GimpProgress(_get_num_progress_items(module_names))
gimp_progress.initialize()
_make_dirs(output_dirpath)
_generate_for_modules(output_dirpath, module_names, gimp_progress)
_generate_for_pdb(output_dirpath, gimp_progress)
def _generate_for_modules(output_dirpath, module_names, gimp_progress):
pypredefgen.module_specific_processing_functions.update({
module_name: [pypredefgen.remove_class_docstrings]
for module_name in MODULES_FOR_WHICH_TO_IGNORE_DOCSTRINGS
})
for module_name in module_names:
if ((gimp.version >= (2, 10) and gimp.version <= (2, 10, 8))
and os.name == "nt"
and module_name in MODULES_TO_SKIP_FOR_WINDOWS):
continue
module = importlib.import_module(module_name)
pypredefgen.generate_predefined_completions(output_dirpath, module)
gimp_progress.update()
def _generate_for_pdb(output_dirpath, gimp_progress):
pypredefgen_pdb.generate_predefined_completions_for_gimp_pdb(output_dirpath)
gimp_progress.update()
def _get_module_names(modules_file_path):
if os.path.isfile(modules_file_path):
with io.open(
modules_file_path, "r",
encoding=pypredefgen.TEXT_FILE_ENCODING) as modules_file:
return [line.strip() for line in modules_file.readlines()]
else:
return []
def _make_dirs(path):
"""
Recursively create directories from the specified path. Do not raise exception
if the path already exists.
"""
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == os.errno.EEXIST and os.path.isdir(path):
pass
elif exc.errno == os.errno.EACCES and os.path.isdir(path):
# This can happen if `os.makedirs` is called on a root directory
# in Windows (e.g. `os.makedirs("C:\\")`).
pass
else:
raise
class GimpProgress(object):
def __init__(self, num_total_tasks=0):
self.num_total_tasks = num_total_tasks
self._num_finished_tasks = 0
@property
def num_finished_tasks(self):
return self._num_finished_tasks
def initialize(self, message=None):
gimp.progress_init(message if message is not None else "")
def update(self, num_tasks=1):
if self._num_finished_tasks + num_tasks > self.num_total_tasks:
raise ValueError("number of finished tasks exceeds the number of total tasks")
self._num_finished_tasks += num_tasks
gimp.progress_update(self._num_finished_tasks / self.num_total_tasks)
def _get_num_progress_items(module_names):
return len(module_names) + 1
gimpfu.register(
proc_name="generate_predefined_completions_for_pydev",
blurb=("Generate predefined completions for GIMP modules for PyDev "
"(Eclipse IDE plug-in)"),
help=('This plug-in generates separate .pypredef files for each module and '
'for the GIMP procedural database in the "{0}" subdirectory '
'of the directory where this plug-in is located.'
.format(PYPREDEF_FILES_DIRNAME)),
author="Kamil Burda",
copyright="",
date="",
label="Generate Predefined Completions for PyDev",
imagetypes="",
params=[
(gimpfu.PF_STRING, "output_folder", "Output folder", PYPREDEF_FILES_DIRPATH),
],
results=[],
function=generate_predefined_completions_for_pydev,
menu="<Image>/Filters/Languages/Python-Fu")
if __name__ == "__main__":
gimpfu.main()