-
Notifications
You must be signed in to change notification settings - Fork 1
/
qvm-get-image-tt
executable file
·139 lines (120 loc) · 5.27 KB
/
qvm-get-image-tt
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
#!/usr/bin/python3 -O
# The Qubes OS Project, http://www.qubes-os.org
# Ali's personal Qubes OS improvements, http://www.mirjamali.com
#
# Copyright (C) 2013 Wojciech Porczyk <wojciech@porczyk.eu>
# Copyright (C) 2024 Ali Mirjamai <ali@mirjamali.com>
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import argparse
import qubesadmin
import qubesimgconvertertt
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter, \
description='Secure copy of images between virtual machines', \
epilog="Both SRC and DST may specify format in gm(1) way, i.e. " \
"png:aqq.gif\nANSI representation in xterm & xfce4-terminal is usually "\
"limited to 256 colors.")
parser.add_argument('vmname', metavar='VMNAME', help='Source VM')
parser.add_argument('color', metavar='COLOR', \
help='Color for the effects. Will be ignored for untouched & invert filters.')
parser.add_argument('src', metavar='SRC', help='Path inside source VM')
dstoransi = parser.add_argument_group(title="Output Control", \
description="Image could be saved or printed to the terminal (or both)")
dstoransi.add_argument('dst', metavar='DST', \
help='Destination path in this VM', nargs='?')
dstoransi.add_argument("--ANSI", type=str, \
choices=["pattern", "white", "black", "COLOR"], help="Print ANSI " \
"X3.64(ISO/IEC 6429) color representation of image in color-capable tty. "\
"Background could be GIMP-Style transparency representation 'pattern', " \
"'white', 'black' or 'COLOR' to apply 2nd positional argument as bg.")
effectgroup = parser.add_argument_group(title="Image Effects", \
description="Effect filter(s) to perform on the image")
effectgroup.add_argument("--filter", type=str, default="ttfilter", choices= \
["ttfilter", "tint", "overlay","thin-border", "thick-border", \
"untouched", "invert", "marker"], \
help="'ttfilter' is the default i.e.: program will look for 'ttfilter' " \
"key in VM features. If it exists, its value should indicate the effect " \
"for that qube. If 'ttfilter' feature key is missing or it contains an " \
"invalid value, Image will be untouched")
effectgroup.add_argument("--diagonal", type=str, choices= \
["tr", "br", "bl", "tl"], \
help="Diagonal fill behind image. Choices are tr, br, bl, tl for " \
"top-right, buttom-right, buttom-left, top-left")
effectgroup.add_argument("--mirror", type=str, default="none", choices=\
["vertical", "vertically", "horizontal", "horizontally", "both"], \
help="Mirror image in one or both axes.")
effectgroup.add_argument("--alphacomposite", metavar="IMAGE", type=str, \
help="Composite image on top of another image. "\
"Provide path of background image inside source VM.")
def main():
args = parser.parse_args()
app = qubesadmin.Qubes()
try:
vm = app.domains[args.vmname]
except KeyError:
vm = None
if vm is None:
parser.error('No such VM: {0}'.format(args.vmname))
if not(args.ANSI or args.dst):
parser.error("Either or both of --ANSI or DST is required")
img=qubesimgconvertertt.Image.get_from_vm(vm, args.src)
filter = args.filter
if filter == "ttfilter":
filter = vm.features.get("ttfilter", "tint")
''' Python 3.11 is nicely shipped with Q4.2, so we use match statement'''
match filter:
case "tint":
img=img.tint(args.color)
case "overlay":
img=img.overlay(args.color)
case "thin-border":
img=img.thin_border(args.color)
case "thick-border":
img=img.thick_border(args.color)
case "untouched":
img=img.untouched()
case "invert":
img=img.invert()
case "mirror":
img=img.mirror(1)
case "flip":
img=img.mirror(0)
case "marker":
img=img.marker(args.color)
if args.diagonal:
img=img.diagonal(args.color, args.diagonal)
match args.mirror:
case "vertical" | "vertically":
img=img.mirror(0)
case "horizontal" | "horizontally":
img=img.mirror(1)
case "both":
img=img.mirror((0,1))
case _:
pass
if args.alphacomposite:
back=qubesimgconvertertt.Image.get_from_vm(vm, args.alphacomposite)
img=img.alphacomposite(back)
if args.ANSI:
if args.ANSI == "COLOR":
img.ANSI(args.color)
else:
img.ANSI(args.ANSI)
if args.dst:
img.save(args.dst)
if __name__ == '__main__':
main()
# vim: ft=python sw=4 ts=4 et