-
Notifications
You must be signed in to change notification settings - Fork 8
/
unwrap
executable file
·159 lines (122 loc) · 3.99 KB
/
unwrap
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#! /usr/bin/python3
# unwrap (Python script) -- removes soft line breaks
#
# Version: 1.1
# Copyright: (c)2015 Alastair Irvine <[email protected]>
# Keywords: qp, text, line wrapping
# Licence: This file is released under the GNU General Public License
#
'''Description: Converts each paragraph in the input into a single line.
Input consists of file(s) or stdin (the default if no command line arguments
are present, or used instead of any filename given as "-") and its paragraphs
are separated by a blank line i.e. two line breaks. All indenting is removed.
Usage: unwrap [-q] [<files>...]
Options:
-q Decode Quoted Printable input before processing
'''
# Licence details:
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# See http://www.gnu.org/licenses/gpl-2.0.html for more information.
#
# You can find the complete text of the GPLv2 in the file
# /usr/share/common-licenses/GPL-2 on Debian systems.
# Or see the file COPYING in the same directory as this program.
#
#
# TO-DO:
import sys
import getopt
import quopri
import io
self="unwrap"
allowed_options='hdq'
allowed_long_options=['help']
# *** CLASSES ***
class Output(object):
"Collects a list of lines, and outputs them (joined by a space) when required."
def __init__(self):
self.lines = []
def addline(self, line):
self.lines.append(line)
def flush(self):
"""Returns a paragraph with a newline appended, or an empty string if no
lines are present."""
s = ""
if self.lines:
s = " ".join(self.lines) + "\n"
self.lines = []
return s
# *** FUNCTIONS ***
def show_help(dest=sys.stdout):
print(__doc__, end='', file=dest)
def report_error(*msg):
print(self + ": Error:", msg, file=sys.stderr)
def report_warning(*msg):
print(self + ": Warning:", msg, file=sys.stderr)
def report_notice(*msg):
print(self + ": Notice:", msg, file=sys.stderr)
def process_regular(f):
o = Output()
text = ""
# Process lines, detecting paragraphs from empty lines and re-inserting line breaks
for line in f.readlines():
# Used to use rstrip() here, but why??
s = line.strip()
if s == "":
text += o.flush() + "\n"
else:
o.addline(s)
return text + o.flush()
def process_quopri(f):
b = io.StringIO()
quopri.decode(f, b)
b.seek(0)
## print len(b.getvalue())
return process_regular(b)
# *** MAINLINE ***
if __name__ == '__main__':
# == Command-line parsing ==
# -- defaults --
debug = 0
# -- option handling --
try:
optlist, args = getopt.getopt(sys.argv[1:], allowed_options, allowed_long_options)
except getopt.GetoptError as e:
report_error(e)
sys.exit(1)
# Create a special dict object that defaults to False for unspecified options
from collections import defaultdict
params = defaultdict(bool)
for option, opt_arg in optlist:
if option == "-q":
params["quopri"] = True
elif option == "-d":
debug += 1
elif option == "-h" or option == "--help":
show_help()
sys.exit(0)
# -- argument checking --
## if len(args) not in (2, 3):
## report_error("Invalid command-line parameters.")
## show_help(sys.stderr)
## sys.exit(1)
# == processing ==
if len(args) == 0:
if params["quopri"]:
print(process_quopri(sys.stdin))
else:
print(process_regular(sys.stdin))
else:
for filename in args:
if filename == "-":
f = sys.stdin
else:
f = open(filename)
if params["quopri"]:
print(process_quopri(f))
else:
print(process_regular(f))