MonScoDocEssai/misc/extract_code_strings.py

39 lines
1.0 KiB
Python
Raw Normal View History

2021-01-10 11:43:17 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Extract all string litterals from our code base.
Useful to check if an API function is used in a generated web page !
Usage:
extract_code_strings.py source.py ... > string-constants.txt
2021-01-10 11:43:17 +01:00
(replace RT by an existing departement id)
E. Viennet 2021-01-09
"""
from __future__ import print_function
import sys
import ast
import types
2021-01-10 11:43:17 +01:00
L = []
for srcfilename in sys.argv[1:]:
# print("processing %s" % srcfilename, file=sys.stderr)
2021-01-10 11:43:17 +01:00
with open(srcfilename) as f:
p = ast.parse(f.read())
# L.extend(x.s.strip() for x in ast.walk(p) if x.__class__ == ast.Str)
for x in ast.walk(p):
if x.__class__ == ast.Str:
if type(x.s) == types.StringType:
s = x.s
else:
s = x.s.encode("UTF-8")
# remove tabs and cr
s = s.translate(None, "\t\n")
if len(s):
print("%s\t%s" % (srcfilename, s))
2021-01-10 11:43:17 +01:00
# L = sorted(set(L)) # uniq | sort
# print("\n".join(L))