summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Hatch <tim@timhatch.com>2014-10-04 00:02:54 -0700
committerTim Hatch <tim@timhatch.com>2014-10-04 00:02:54 -0700
commitc78a3e3637f6d6e8611fd254e82847058692a5f7 (patch)
treedc06e14bd5e70503f6118798b99c8e5a472b5d48
parent583750890b845f4f8537eee5feb1f77c957dda38 (diff)
downloadpygments-c78a3e3637f6d6e8611fd254e82847058692a5f7.tar.gz
Update postgres builtins regeneration logic
-rw-r--r--pygments/lexers/_postgres_builtins.py45
1 files changed, 15 insertions, 30 deletions
diff --git a/pygments/lexers/_postgres_builtins.py b/pygments/lexers/_postgres_builtins.py
index a11dd6d3..e534281b 100644
--- a/pygments/lexers/_postgres_builtins.py
+++ b/pygments/lexers/_postgres_builtins.py
@@ -15,6 +15,8 @@ try:
except ImportError:
from urllib.request import urlopen
+from pygments.util import format_lines
+
# One man's constant is another man's variable.
SOURCE_URL = 'https://github.com/postgres/postgres/raw/master'
KEYWORDS_URL = SOURCE_URL + '/doc/src/sgml/keywords.sgml'
@@ -100,38 +102,21 @@ def parse_pseudos(f):
return dt
def update_consts(filename, constname, content):
- f = open(filename)
- lines = f.readlines()
- f.close()
+ with open(filename) as f:
+ data = f.read()
# Line to start/end inserting
- re_start = re.compile(r'^%s\s*=\s*\[\s*$' % constname)
- re_end = re.compile(r'^\s*\]\s*$')
- start = [ n for n, l in enumerate(lines) if re_start.match(l) ]
- if not start:
- raise ValueError("couldn't find line containing '%s = ['" % constname)
- if len(start) > 1:
- raise ValueError("too many lines containing '%s = ['" % constname)
- start = start[0] + 1
-
- end = [ n for n, l in enumerate(lines) if n >= start and re_end.match(l) ]
- if not end:
- raise ValueError("couldn't find line containing ']' after %s " % constname)
- end = end[0]
-
- # Pack the new content in lines not too long
- content = [repr(item) for item in content ]
- new_lines = [[]]
- for item in content:
- if sum(map(len, new_lines[-1])) + 2 * len(new_lines[-1]) + len(item) + 4 > 75:
- new_lines.append([])
- new_lines[-1].append(item)
-
- lines[start:end] = [ " %s,\n" % ", ".join(items) for items in new_lines ]
-
- f = open(filename, 'w')
- f.write(''.join(lines))
- f.close()
+ re_match = re.compile(r'^%s\s*=\s*\($.*?^\s*\)$' % constname, re.M | re.S)
+ m = re_match.search(data)
+ if not m:
+ raise ValueError('Could not find existing definition for %s' %
+ (constname,))
+
+ new_block = format_lines(constname, content)
+ data = data[:m.start()] + new_block + data[m.end():]
+
+ with open(filename, 'w') as f:
+ f.write(data)
# Autogenerated: please edit them if you like wasting your time.