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
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright © 2011-2012 B. Clausius <barcc@gmx.de>
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
import sys, os, re
def convert(source_filename, target_filename):
with open(source_filename) as source:
lines = source.readlines()
re_field = re.compile(r'^(.*): (.+)$')
with open(target_filename, 'w') as target:
for line in lines:
match = re_field.match(line)
field, value = match.groups() if match else (None, None)
if field == 'Path':
sep = value[0]
format = '_({!r})' if sep != '@' else '{!r}'
value = '; '.join(format.format(v) for v in value.split(sep) if v)
else:
value = ''
target.write(value)
target.write('\n')
def main():
if len(sys.argv) != 2:
print 'usage: {} scriptfile'.format(os.path.basename(sys.argv[0]))
return 1
filename = sys.argv[1]
convert(filename, filename+'.py')
if __name__ == '__main__':
sys.exit(main())
|