forked from linuxmint/xed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-plugin.py
executable file
·161 lines (132 loc) · 5.82 KB
/
generate-plugin.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# generate-plugin.py - xed plugin skeletton generator
# This file is part of xed
#
# Copyright (C) 2006 - Steve Frécinaux
#
# xed is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# xed is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with xed; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA
import re
import os
import argparse
from datetime import date
import preprocessor
TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'plugin_template')
PLUGIN_DIR = os.path.normpath(os.path.join(os.path.dirname(__file__), '../plugins'))
# Parsing command line options
parser = argparse.ArgumentParser()
parser.add_argument('-S', '--standalone', dest='standalone', action='store_true',
help='indicates that this plugin is intended to be distributed as a standalone package')
parser.add_argument('-s', '--with-side-pane', dest='side_pane', action='store_true',
help='Indicates that this plugin will use a side pane')
parser.add_argument('-b', '--with-bottom-pane', dest='bottom_pane', action='store_true',
help='Indicates that this plugin will use a bottom pane')
parser.add_argument('-m', '--with-menu', dest='menu', action='store_true',
help='Indicates that this plugin will use menu entries')
parser.add_argument('-c', '--with-config', dest='config', action='store_true',
help='Indicates that this plugin will use a configuration dialog')
parser.add_argument('-d', '--description', dest='description', default='Type here a short description of your plugin', metavar='DESC',
help='Description of the plugin')
parser.add_argument('-a', '--author', dest='author', default=os.getenv('USERNAME'), metavar='AUTH',
help='Author of the plugin')
parser.add_argument('-e', '--email', dest='email', default=os.getenv('LOGNAME') + '@email.com', metavar='EMAIL',
help='Email address of the author')
parser.add_argument('-l', '--language', dest='language', default='c', metavar='LANG',
help='Language of the plugin')
parser.add_argument('-o', '--output-directory', dest='directory', default=None, metavar='LANG',
help='Language of the plugin')
parser.add_argument('name', metavar='PLUGIN_NAME',
help='The name of the plugin')
args = parser.parse_args()
plugin_name = args.name
plugin_id = re.sub('[^a-z0-9_]', '', plugin_name.lower().replace(' ', '_'))
plugin_module = plugin_id.replace('_', '-')
directives = {
'PLUGIN_NAME' : plugin_name,
'PLUGIN_MODULE' : plugin_module,
'PLUGIN_ID' : plugin_id,
'AUTHOR_FULLNAME' : args.author,
'AUTHOR_EMAIL' : args.email,
'DATE_YEAR' : date.today().year,
'DESCRIPTION' : args.description,
}
# Files to be generated by the preprocessor, in the form "template : outfile"
output_files = {
'meson.build': 'meson.build',
'xed-plugin.desktop.in': '%s.plugin.desktop.in' % plugin_module
}
if args.language == 'c':
directives['HAS_C_FILES'] = True
output_files['xed-plugin.c'] = 'xed-%s-plugin.c' % plugin_module
output_files['xed-plugin.h'] = 'xed-%s-plugin.h' % plugin_module
if args.side_pane:
directives['WITH_SIDE_PANE'] = True
if args.bottom_pane:
directives['WITH_BOTTOM_PANE'] = True
if args.menu:
directives['WITH_MENU'] = True
if args.config:
directives['WITH_CONFIGURE_DIALOG'] = True
if args.directory is None:
directory = os.getcwd() if args.standalone else PLUGIN_DIR
elif os.path.isdir(args.directory):
directory = args.directory
else:
print('Unable to create plugin: %s does not exist or is not a directory' % args.directory)
quit(1)
directory = os.path.join(directory, plugin_module)
if os.path.exists(directory):
print('Unable to create plugin: directory %s already exists' % directory)
quit(1)
else:
os.makedirs(directory)
if not args.standalone:
with open(os.path.join(PLUGIN_DIR, 'meson.build'), 'r') as f:
contents = f.read()
lines = contents.split('\n')
start = False
for i in range(len(lines)):
line = lines[i].rstrip()
print(line)
if line.startswith('subdir('):
start = True
elif start:
break
else:
continue
if line[8:-2] > plugin_module:
break
lines.insert(i, 'subdir(\'%s\')' % plugin_module)
with open(os.path.join(PLUGIN_DIR, 'meson.build'), 'w') as f:
f.write('\n'.join(lines))
# Generate the plugin base
for infile, outfile in output_files.iteritems():
print('Generating file %s from template %s...' % (outfile, infile))
file_directives = directives.copy()
infile = os.path.join(TEMPLATE_DIR, infile)
outfile = os.path.join(directory, outfile)
if not os.path.isfile(infile):
print('Input file %s does not exist: skipping' % os.path.basename(infile))
continue
# Make sure the destination directory exists
if not os.path.isdir(os.path.split(outfile)[0]):
os.makedirs(os.path.split(outfile)[0])
# Variables relative to the generated file
file_directives['DIRNAME'], file_directives['FILENAME'] = os.path.split(outfile)
# Generate the file
preprocessor.process(infile, outfile, file_directives)
print('Done')
# ex:ts=4:et: