1
0
mirror of https://github.com/gryf/snipmate.vim.git synced 2025-12-22 05:47:59 +01:00

cleaned up convertSnip.py to adhere to the PEP 8 style guide

This commit is contained in:
Michael Sanders
2009-04-05 10:59:49 -04:00
parent f3777f928f
commit c254a3e8c4

View File

@@ -1,98 +1,107 @@
#!/usr/bin/python #!/usr/bin/python
# Converts command-based snippets to new file-based snippet syntax """
# NOTE: This is only meant to help, it is not perfect! Check the file File: convertSnips.py
# afterwards to make sure it's correct. Author: Michael Sanders
Description: Converts command-based snippets to new file-based snippet syntax.
NOTE: This is only meant to help, it is not perfect! Check the file afterwards
to make sure it's correct.
"""
import sys import sys, re
import re
import os
def Usage(): def usage():
"""Print usage message and exit."""
print """\ print """\
Usage: convertSnips.py -h or --help Print this help and exit Usage: convertSnips.py -h or --help Print this help and exit
or: convertSnips.py inputfile Print .snippets file or: convertSnips.py [inputfile] Print .snippets file
or: convertSnips.py inputfile outputfile Output to file""" or: convertSnips.py [inputfile] [outputfile] Output to file"""
def FindSnippet(line): def find_snippet(line):
"""\ """
Try to find a snippet in the given line. If it is found, return the Try to find a snippet in the given line. If it is found, return the
converted snippet; otherwise, return -1.\ converted snippet; otherwise, return -1.
""" """
snippet = re.search("exe ['\"](GlobalSnip|Snipp)(!)? (\S+) (.*)['\"]", line) snippet = re.search("exe ['\"](GlobalSnip|Snipp)(!)? (\S+) (.*)['\"]", line)
if not snippet: return -1 if not snippet:
return -1
trigger = snippet.group(3) trigger = snippet.group(3)
text = "\t" + snippet.group(4) text = "\t" + snippet.group(4)
if snippet.group(2): # Process multi-snippet if snippet.group(2): # Process multi-snippet
endSnip = re.search(r'\s*\\"(.*?)\\"\s*(.*)', text) end_snip = re.search(r'\s*\\"(.*?)\\"\s*(.*)', text)
if not endSnip: if not end_snip:
endSnip = re.search('\s*"(.*?)"\s*(.*)', text) end_snip = re.search('\s*"(.*?)"\s*(.*)', text)
if not endSnip: return -1 if not end_snip:
return -1
trigger += ' ' + endSnip.group(1) # Add name to snippet declaration trigger += ' ' + end_snip.group(1) # Add name to snippet declaration
text = "\t" + endSnip.group(2) text = "\t" + end_snip.group(2)
return trigger + "\n" + text return trigger + "\n" + text
newLines = [] def process_line(line, new_lines):
"""
def ProcessLine(line): Search the line for a snippet or comment, and append it to new_lines[]
"""\ if it is found.
Search the line for a snippet or comment, and append it to newLines[]
if it is found.\
""" """
snippet = FindSnippet(line) snippet = find_snippet(line)
if snippet == -1: if snippet == -1:
comment = re.match('^"(.*)', line) comment = re.match('^"(.*)', line)
if comment: newLines.append('#' + comment.group(1)) if comment:
new_lines.append('#' + comment.group(1))
else: else:
newLines.append('snippet ' + snippet) new_lines.append('snippet ' + snippet)
return snippet return snippet, new_lines
def Output(lines, file = None): def output_lines(lines, outputfile = None):
outputLines = '' """Prints or outputs to file the converted snippets."""
file_lines = ''
for snippet in lines: for snippet in lines:
for line in snippet.split("\\n"): for line in snippet.split("\\n"):
line = re.sub(r'\\t', '\t', line) line = re.sub(r'\\t', '\t', line)
line = re.sub(r'\\\\', r'\\', line) line = re.sub(r'\\\\', r'\\', line)
if not re.match('^(\#|snippet)', line): if not re.match('^(\#|snippet|$)', line):
line = "\t" + line line = "\t" + line
outputLines += line + "\n" file_lines += line + "\n"
if file: if outputfile:
try: try:
output = open(file, 'w') output = open(outputfile, 'w')
except IOError, e: except IOError, error:
raise SystemExit('convertSnips.py: %s' % e) raise SystemExit('convertSnips.py: %s' % error)
output.write(outputLines) output.write(file_lines)
else: else:
print outputLines, print file_lines,
def main(argv = None): def main(argv = None):
if argv is None: argv = sys.argv[1:] if argv is None:
argv = sys.argv[1:]
if not argv or '-h' in argv or '--help' in argv: if not argv or '-h' in argv or '--help' in argv:
Usage() usage()
return 1 return 1
try: try:
input = open(argv[0], 'r') inputfile = open(argv[0], 'r')
except IOError, e: except IOError, error:
raise SystemExit('convertSnips.py: %s' % e) raise SystemExit('convertSnips.py: %s' % error)
snippet = -1 snippet = -1
for line in input.readlines(): new_lines = []
for line in inputfile.readlines():
if snippet == -1: if snippet == -1:
snippet = ProcessLine(line) snippet, new_lines = process_line(line, new_lines)
else: else:
concat = re.search(r"^\s+(\\\s*)?(\.\s*)?['\"](.*)['\"]", line) concat = re.search(r"^\s+(\\\s*)?(\.\s*)?['\"](.*)['\"]", line)
if concat: if concat:
newLines[-1] += "\n" + concat.group(3) # Add concatenated lines new_lines[-1] += "\n" + concat.group(3) # Add concatenated lines
else: else:
snippet = ProcessLine(line) snippet, new_lines = process_line(line, new_lines)
if len(argv) == 1: Output(newLines) if len(argv) == 1:
else: Output(newLines, argv[1]) output_lines(new_lines)
else:
output_lines(new_lines, argv[1])
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())