comparison i18n/hggettext @ 38815:617ae7e33a65

i18n: format warning of hggettext in standard compiler error style Now, hggettext specific warning messages are formatted in: FILENAME:LINENO:MESSAGE This allows editors to jump into corresponded line easily.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Mon, 11 Jun 2018 13:00:15 +0900
parents 0585337ea787
children 47ef023d0165
comparison
equal deleted inserted replaced
38814:96b2e66dfa74 38815:617ae7e33a65
61 'msgid %s\n' % normalize(s) + 61 'msgid %s\n' % normalize(s) +
62 'msgstr ""\n') 62 'msgstr ""\n')
63 63
64 doctestre = re.compile(r'^ +>>> ', re.MULTILINE) 64 doctestre = re.compile(r'^ +>>> ', re.MULTILINE)
65 65
66 def offset(src, doc, name, default): 66 def offset(src, doc, name, lineno, default):
67 """Compute offset or issue a warning on stdout.""" 67 """Compute offset or issue a warning on stdout."""
68 # remove doctest part, in order to avoid backslash mismatching 68 # remove doctest part, in order to avoid backslash mismatching
69 m = doctestre.search(doc) 69 m = doctestre.search(doc)
70 if m: 70 if m:
71 doc = doc[:m.start()] 71 doc = doc[:m.start()]
74 end = src.find(doc.replace('\\', '\\\\')) 74 end = src.find(doc.replace('\\', '\\\\'))
75 if end == -1: 75 if end == -1:
76 # This can happen if the docstring contains unnecessary escape 76 # This can happen if the docstring contains unnecessary escape
77 # sequences such as \" in a triple-quoted string. The problem 77 # sequences such as \" in a triple-quoted string. The problem
78 # is that \" is turned into " and so doc wont appear in src. 78 # is that \" is turned into " and so doc wont appear in src.
79 sys.stderr.write("warning: unknown offset in %s, assuming %d lines\n" 79 sys.stderr.write("%s:%d:warning:"
80 % (name, default)) 80 " unknown docstr offset, assuming %d lines\n"
81 % (name, lineno, default))
81 return default 82 return default
82 else: 83 else:
83 return src.count('\n', 0, end) 84 return src.count('\n', 0, end)
84 85
85 86
104 """ 105 """
105 mod = importpath(path) 106 mod = importpath(path)
106 if not path.startswith('mercurial/') and mod.__doc__: 107 if not path.startswith('mercurial/') and mod.__doc__:
107 with open(path) as fobj: 108 with open(path) as fobj:
108 src = fobj.read() 109 src = fobj.read()
109 lineno = 1 + offset(src, mod.__doc__, path, 7) 110 lineno = 1 + offset(src, mod.__doc__, path, 1, 7)
110 print(poentry(path, lineno, mod.__doc__)) 111 print(poentry(path, lineno, mod.__doc__))
111 112
112 functions = list(getattr(mod, 'i18nfunctions', [])) 113 functions = list(getattr(mod, 'i18nfunctions', []))
113 functions = [(f, True) for f in functions] 114 functions = [(f, True) for f in functions]
114 115
127 if funcmod.__package__ == funcmod.__name__: 128 if funcmod.__package__ == funcmod.__name__:
128 extra = '/__init__' 129 extra = '/__init__'
129 actualpath = '%s%s.py' % (funcmod.__name__.replace('.', '/'), extra) 130 actualpath = '%s%s.py' % (funcmod.__name__.replace('.', '/'), extra)
130 131
131 src = inspect.getsource(func) 132 src = inspect.getsource(func)
132 name = "%s.%s" % (actualpath, func.__name__)
133 lineno = inspect.getsourcelines(func)[1] 133 lineno = inspect.getsourcelines(func)[1]
134 doc = docobj.__doc__ 134 doc = docobj.__doc__
135 origdoc = getattr(docobj, '_origdoc', '') 135 origdoc = getattr(docobj, '_origdoc', '')
136 if rstrip: 136 if rstrip:
137 doc = doc.rstrip() 137 doc = doc.rstrip()
138 origdoc = origdoc.rstrip() 138 origdoc = origdoc.rstrip()
139 if origdoc: 139 if origdoc:
140 lineno += offset(src, origdoc, name, 1) 140 lineno += offset(src, origdoc, actualpath, lineno, 1)
141 else: 141 else:
142 lineno += offset(src, doc, name, 1) 142 lineno += offset(src, doc, actualpath, lineno, 1)
143 print(poentry(actualpath, lineno, doc)) 143 print(poentry(actualpath, lineno, doc))
144 144
145 145
146 def rawtext(path): 146 def rawtext(path):
147 with open(path) as f: 147 with open(path) as f: