summaryrefslogtreecommitdiff
path: root/namespace.py
blob: d7d31658aabfb02020d2cae1be1bfc8ff3dc757e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python
"""
Wraps standard input with C++ namespaces and writes it to standard output. Rather niche tool unless you rely too much
on pipes in your code editor.
"""

import argparse
import sys


def split(path):
	"""Splits namespace path into parts."""
	return path.replace("::", "/").replace(":", "/").split("/")


def wrap(in_, out, namespaces):
	"""Wraps input with C++ namespaces and writes it to output handle."""
	for ns in namespaces:
		out.write("namespace {}\n{}\n".format(ns, "{"))
	out.write("\n\n")
	for x in in_:
		out.write(x)
	out.write("\n\n")
	for ns in reversed(namespaces):
		out.write("{}  // namespace {}\n".format("}", ns))


def main():
	parser = argparse.ArgumentParser()
	parser.add_argument("namespaces")
	args = parser.parse_args()
	wrap(sys.stdin, sys.stdout, split(args.namespaces))


if __name__ == '__main__':
	main()