X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=migrations%2Fextract-views.py;h=4a204651f96b81873673bb3b7f64210e8895edab;hb=bc7bd41556e1fd137acf4df415e5dc0f6d5e02c4;hp=4fd5090e8d24b14d277ffd9ef81098d7c112a669;hpb=fdc1bf78189188df147a882721805e7ed5654459;p=plcapi.git diff --git a/migrations/extract-views.py b/migrations/extract-views.py index 4fd5090..4a20465 100755 --- a/migrations/extract-views.py +++ b/migrations/extract-views.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import sys import re @@ -10,48 +10,47 @@ class Schema: self.output=output # left part is non-greedy - comment=re.compile("(.*?)--.*") - spaces=re.compile("^\s+(\S.*)") - view=re.compile("(?i)\s*create\s+(or\s+replace)?\s+view.*") + comment = re.compile("(.*?)--.*") + spaces = re.compile("^\s+(\S.*)") + view = re.compile("(?i)\s*create\s+(or\s+replace)?\s+view.*") def parse (self): if self.output: outfile = open(self.output, "a") else: outfile = sys.stdout - contents = file(self.input).read() - parts=contents.split(";") + with open(self.input) as feed: + contents = feed.read() + parts = contents.split(";") for part in parts: # normalize: remove comments, linebreaks, trailing spaces.. - normalized='' - lines=part.split('\n'); - out_lines=[] + lines = part.split('\n') + out_lines = [] for line in lines: # remove comment - match=Schema.comment.match(line) + match = Schema.comment.match(line) if match: - line=match.group(1) + line = match.group(1) out_lines.append(line) # get them together out_line = " ".join(out_lines) # remove trailing spaces - match=Schema.spaces.match(out_line) + match = Schema.spaces.match(out_line) if match: - out_line=match.group(1) - match=Schema.view.match(out_line) + out_line = match.group(1) + match = Schema.view.match(out_line) if match: outfile.write("{};\n".format(out_line)) if outfile != sys.stdout: outfile.close() if __name__ == '__main__': - if len(sys.argv) not in [2,3]: - print 'Usage:',sys.argv[0],'input [output]' + if len(sys.argv) not in [2, 3]: + print('Usage:', sys.argv[0], 'input [output]') sys.exit(1) - input=sys.argv[1] + input = sys.argv[1] try: - output=sys.argv[2] - except: - output=None - Schema(input,output).parse() - + output = sys.argv[2] + except Exception: + output = None + Schema(input, output).parse()