File: sdwrap.py

package info (click to toggle)
nodejs 22.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 246,928 kB
  • sloc: cpp: 1,582,349; javascript: 582,017; ansic: 82,400; python: 60,561; sh: 4,009; makefile: 2,263; asm: 1,732; pascal: 1,565; perl: 248; lisp: 222; xml: 42
file content (82 lines) | stat: -rwxr-xr-x 2,035 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python

import argparse
import sys

def wrap(args):
  l = args.input.readline(72)
  firstline = True
  while l:
    if l[-1] == '\n':
      if firstline:
        outstr = "{}".format(l)
      else:
        outstr = " {}".format(l)
        firstline = True
      l = args.input.readline(72)
    else:
      if firstline:
        outstr = "{:<71}*\n".format(l[:-1])
        firstline = False
      else:
        outstr = " {:<70}*\n".format(l[:-1])
      l = l[-1] + args.input.readline(70)
    args.output.write(outstr)

  return 0

def unwrap(args):
  l = args.input.readline()
  firstline = True
  while l:
    if len(l) > 80:
      print("Error: input line invalid (longer than 80 characters)", file=sys.stderr)
      return 1
    if not firstline and l[0] != ' ':
      print("Error: continuation line not start with blank", file=sys.stderr)
      return 1

    if len(l) > 71 and l[71] == '*':
      if firstline:
        args.output.write(l[:71])
        firstline = False
      else:
        args.output.write(l[1:71])
    else:
      if firstline:
        args.output.write(l)
      else:
        args.output.write(l[1:])
        firstline = True
    l = args.input.readline()
  return 0

def Main():
  parser = argparse.ArgumentParser(description="Wrap sidedeck source to card formats")
  parser.add_argument("-u", "--unwrap",
      help="Unwrap sidedeck cards to source formats instead", action="store_true",
      default=False)
  parser.add_argument("-i", "--input", help="input filename, default to stdin",
      action="store", default=None)
  parser.add_argument("-o", "--output", help="output filename, default to stdout",
      action="store", default=None)

  args = parser.parse_args()

  if args.input is None:
    args.input = sys.stdin
  else:
    args.input = open(args.input, 'r')

  if args.output is None:
    args.output = sys.stdout
  else:
    args.output = open(args.output, 'w')

  if args.unwrap:
    return unwrap(args)

  return wrap(args)

if __name__ == '__main__':
  sys.exit(Main())