mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 01:52:56 +01:00
project-text-files.py: Add CmdAddCppNamespace
This is an old patch, tackling namespace addition. Not finished, just a cleanup commit. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
e1974118b5
commit
974a56793a
1 changed files with 69 additions and 7 deletions
|
|
@ -12,8 +12,9 @@ import subprocess
|
|||
import jwutils
|
||||
from jwutils.log import *
|
||||
|
||||
_exts_h = set([ '.h', '.H', '.hxx', '.HXX'])
|
||||
_exts_cpp = _exts_h | set([ '.cpp', '.CPP', '.c', '.C', '.cxx', '.CXX' ])
|
||||
_exts_h = set([ '.h', '.H', '.hxx', '.HXX'])
|
||||
_exts_cpp = set([ '.cpp', '.CPP', '.c', '.C', '.cxx', '.CXX' ])
|
||||
_exts_h_cpp = _exts_h | _exts_cpp
|
||||
|
||||
class Cmd(jwutils.Cmd):
|
||||
|
||||
|
|
@ -30,9 +31,6 @@ class Cmd(jwutils.Cmd):
|
|||
stopc = "^a-zA-Z0-9_"
|
||||
stopa = "(^|[" + stopc + "])"
|
||||
stope = "([" + stopc + "]|$)"
|
||||
#stopc2 = stopc.replace("\\", "\\\\")
|
||||
# return re.sub("(^|[" + stopc + "])" + src + "([" + stopc + "]|$)", "\1" + target + "\2", data)
|
||||
#f = "(^|[" + stopc + "])" + src + "([" + stopc + "]|$)"
|
||||
f = stopa + src + stope
|
||||
t = "\\1" + target + "\\2"
|
||||
done = False
|
||||
|
|
@ -85,6 +83,16 @@ class Cmd(jwutils.Cmd):
|
|||
r = r + line
|
||||
return r
|
||||
|
||||
def self._add_namespace_to_header(self, data, ns_new):
|
||||
lines = data.splitlines()
|
||||
old = None
|
||||
ns_cur = []
|
||||
for line in iter(lines):
|
||||
match = re.sub('^ *namespace[ \t]*([^ ]+)[ \t]*{.*', '\\1', line)
|
||||
if match != line:
|
||||
ns_cur = match(blah, bl
|
||||
classname = re.sub('^ *#[ \t]*ifndef[\t ]+([^ ]+)($|[ \t/;])', '\\1', line)
|
||||
|
||||
def _fix_multiple_inclusion_preventer(self, prefix, path):
|
||||
dir, name = os.path.split(path)
|
||||
if len(name) == 0:
|
||||
|
|
@ -197,7 +205,7 @@ class CmdReplaceCppSymbols(Cmd):
|
|||
self._init(args)
|
||||
slog(NOTICE, "running")
|
||||
files = []
|
||||
exts = _exts_cpp | set([ '.sh', '.py' ])
|
||||
exts = _exts_h_cpp | set([ '.sh', '.py' ])
|
||||
for root, dirs, names in os.walk(args.root):
|
||||
for name in names:
|
||||
trunc, ext = os.path.splitext(name)
|
||||
|
|
@ -222,7 +230,7 @@ class CmdReplaceCppSymbols(Cmd):
|
|||
if args.rename_files:
|
||||
for dir, name in files:
|
||||
trunc, ext = os.path.splitext(name)
|
||||
if not ext in _exts_cpp:
|
||||
if not ext in _exts_h_cpp:
|
||||
continue
|
||||
if not trunc.lower() in self.file_truncs:
|
||||
continue
|
||||
|
|
@ -238,4 +246,58 @@ class CmdReplaceCppSymbols(Cmd):
|
|||
os.rename(path, new_path)
|
||||
self._fix_multiple_inclusion_preventer(args.mip_prefix, new_path)
|
||||
|
||||
class CmdAddCppNamespace(Cmd):
|
||||
|
||||
def __init__(self):
|
||||
super(CmdAddCppNamespace, self).__init__("add-cpp-namespace", "Enclose C++ classes in namespace")
|
||||
|
||||
def add_parser(self, parsers):
|
||||
p = super(CmdAddCppNamespace, self).add_parser(parsers)
|
||||
p.add_argument('-n', '--namespace', help="Namespace", default=None)
|
||||
p.add_argument('-p', '--package', help="Package", default=None)
|
||||
return p
|
||||
|
||||
# overriding
|
||||
def run(self, args):
|
||||
if args.regex is not None:
|
||||
return super(CmdAddCppNamespace, self).run(args)
|
||||
self._init(args)
|
||||
slog(NOTICE, "running")
|
||||
files = []
|
||||
exts = _exts_h_cpp | set([ '.sh', '.py' ])
|
||||
for root, dirs, names in os.walk(args.root):
|
||||
for name in names:
|
||||
trunc, ext = os.path.splitext(name)
|
||||
if ext in exts:
|
||||
files.append((root, name))
|
||||
self.process(args, files)
|
||||
|
||||
# overriding
|
||||
def _init(self, args):
|
||||
r = super(CmdAddCppNamespace, self)._init(args)
|
||||
self.file_truncs = set()
|
||||
if self.replacements is not None:
|
||||
for patt in self.replacements:
|
||||
self.file_truncs.add(patt.lower())
|
||||
return r
|
||||
|
||||
def process(self, args, files):
|
||||
if args.namespace:
|
||||
for dir, name in files:
|
||||
path = dir + '/' + name
|
||||
with open(path) as infile:
|
||||
data = odata = infile.read()
|
||||
trunc, ext = os.path.splitext(name)
|
||||
if ext in _exts_h:
|
||||
data = self._add_namespace_to_header(data, namespace)
|
||||
elif ext in _exts_cpp:
|
||||
data = self._add_using_namespace(data, namespace)
|
||||
elif:
|
||||
continue
|
||||
if data == odata:
|
||||
continue
|
||||
tmp = path + '.' + ('rep' if args.backup is None else args.backup)
|
||||
with open(tmp, 'w') as outfile:
|
||||
outfile.write(data)
|
||||
|
||||
jwutils.run_sub_commands('process text files')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue