-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxml-enclose-text.py
executable file
·65 lines (48 loc) · 1.59 KB
/
xml-enclose-text.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import codecs
import re
import os.path
from optparse import OptionParser
class LineMarker(object):
def __init__(self, opts, input_encoding):
self._opts = opts
self._input_encoding = input_encoding
self._elem_id = 1
def process_files(self, fnames):
for fname in fnames or ['-']:
self._process_file(fname)
def _process_file(self, fname):
if fname != '-':
with codecs.open(fname, 'r', encoding=self._input_encoding) as f:
self._mark_lines(f)
else:
self._mark_lines(sys.stdin)
def _mark_lines(self, file_):
for line in file_:
if line[0] in '<\n':
sys.stdout.write(line)
else:
sys.stdout.write(self._add_line_tags(line))
def _add_line_tags(self, line):
result = (u'<{elemname} id="{id}">\n{line}</{elemname}>\n'
.format(elemname=self._opts.element, line=line,
id=self._elem_id))
self._elem_id += 1
return result
def getopts():
optparser = OptionParser()
optparser.add_option('--element')
(opts, args) = optparser.parse_args()
return (opts, args)
def main():
input_encoding = 'utf-8'
output_encoding = 'utf-8'
sys.stdin = codecs.getreader(input_encoding)(sys.stdin)
sys.stdout = codecs.getwriter(output_encoding)(sys.stdout)
(opts, args) = getopts()
line_marker = LineMarker(opts, input_encoding)
line_marker.process_files(args)
if __name__ == "__main__":
main()