Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Radek Hušek
cups-pdf-postprocess
Commits
f80031c5
Commit
f80031c5
authored
Oct 18, 2017
by
Radek Hušek
Browse files
make use of config
parent
e36154f0
Changes
1
Hide whitespace changes
Inline
Side-by-side
cups-pdf-postprocess.py
View file @
f80031c5
...
...
@@ -64,24 +64,45 @@ class CPP(QWidget):
super
().
__init__
()
self
.
_pdf
=
pdf
self
.
_config
=
config
self
.
_defaults
=
self
.
_config
.
get
(
"defaults"
,
{})
self
.
init_printers
()
self
.
init_gui
()
self
.
show
()
def
init_printers
(
self
):
self
.
_save_as_file
=
object
()
self
.
_printers
=
self
.
_config
.
get
(
"printers"
,
[])
self
.
_printers
.
append
((
"Save as ..."
,
self
.
_save_as_file
))
for
l
in
sp
.
check_output
([
"lpstat"
,
"-a"
]).
decode
(
"UTF-8"
).
split
(
"
\n
"
):
p
=
l
.
split
(
" "
)[
0
]
if
p
==
""
:
continue
self
.
_printers
.
append
((
p
,
[
"lp"
,
"-d"
,
p
]))
def
init_gui
(
self
):
vbox
=
QVBoxLayout
()
vbox
.
addStretch
(
1
)
o
=
QLabel
(
"Printing %s"
%
(
pdf
,),
self
)
o
=
QLabel
(
"Printing %s"
%
(
self
.
_
pdf
,),
self
)
o
.
setMinimumWidth
(
400
)
o
.
setMaximumWidth
(
800
)
o
.
setWordWrap
(
True
)
vbox
.
addWidget
(
o
)
def
margin_spinbox
(
name
,
layout
):
def
margin_spinbox
(
name
,
layout
,
margin_id
):
hbox
=
QHBoxLayout
()
hbox
.
addWidget
(
QLabel
(
name
))
s
=
QSpinBox
(
self
)
s
.
setMinimum
(
0
)
s
.
setMaximum
(
100
)
s
.
setSingleStep
(
1
)
s
.
setValue
(
20
)
s
.
setValue
(
self
.
_defaults
.
get
(
"crop_margins"
,
[
20
,
20
,
20
,
20
])[
margin_id
]
)
s
.
setEnabled
(
False
)
hbox
.
addWidget
(
s
)
layout
.
addLayout
(
hbox
)
...
...
@@ -93,10 +114,10 @@ class CPP(QWidget):
_vbox
=
QVBoxLayout
()
_vbox
.
addWidget
(
QLabel
(
"Margins [mm]"
))
self
.
_crop_margin
=
(
margin_spinbox
(
"Left"
,
_vbox
),
margin_spinbox
(
"Top"
,
_vbox
),
margin_spinbox
(
"Right"
,
_vbox
),
margin_spinbox
(
"Bottom"
,
_vbox
),
margin_spinbox
(
"Left"
,
_vbox
,
0
),
margin_spinbox
(
"Top"
,
_vbox
,
1
),
margin_spinbox
(
"Right"
,
_vbox
,
2
),
margin_spinbox
(
"Bottom"
,
_vbox
,
3
),
)
hbox
.
addLayout
(
_vbox
)
vbox
.
addLayout
(
hbox
)
...
...
@@ -105,6 +126,9 @@ class CPP(QWidget):
for
m
in
self
.
_crop_margin
:
m
.
setEnabled
(
checked
)
self
.
_crop
.
clicked
[
bool
].
connect
(
_crop_changed
)
if
self
.
_defaults
.
get
(
"crop"
,
False
):
self
.
_crop
.
setChecked
(
True
)
_crop_changed
(
True
)
hbox
=
QHBoxLayout
()
self
.
_compact
=
QCheckBox
(
'Co&mpact'
,
self
)
...
...
@@ -112,7 +136,7 @@ class CPP(QWidget):
self
.
_compact_copies
.
setMinimum
(
1
)
self
.
_compact_copies
.
setMaximum
(
1000
)
self
.
_compact_copies
.
setSingleStep
(
1
)
self
.
_compact_copies
.
setValue
(
1
)
self
.
_compact_copies
.
setValue
(
self
.
_defaults
.
get
(
"copies"
,
1
)
)
self
.
_compact_copies
.
setEnabled
(
False
)
def
_compact_changed
(
enabled
):
...
...
@@ -122,11 +146,12 @@ class CPP(QWidget):
self
.
_duplex
.
setCurrentIndex
(
1
)
self
.
_pdfbook
.
setChecked
(
False
)
_pdfbook_changed
(
False
)
else
:
self
.
_compact_copies
.
setValue
(
1
)
self
.
_duplex
.
setEnabled
(
not
enabled
)
self
.
_compact
.
clicked
[
bool
].
connect
(
_compact_changed
)
if
self
.
_defaults
.
get
(
"compact"
,
False
):
self
.
_compact
.
setChecked
(
True
)
_compact_changed
(
True
)
hbox
.
addWidget
(
self
.
_compact
)
hbox
.
addWidget
(
self
.
_compact_copies
)
...
...
@@ -148,17 +173,21 @@ class CPP(QWidget):
self
.
_duplex
.
setEnabled
(
not
enabled
)
if
enabled
:
self
.
_duplex
.
setCurrentIndex
(
1
)
else
:
self
.
_staple
.
setChecked
(
False
)
self
.
_pdfbook
.
clicked
[
bool
].
connect
(
_pdfbook_changed
)
if
self
.
_defaults
.
get
(
"pdfbook"
,
False
):
self
.
_pdfbook
.
setChecked
(
True
)
_pdfbook_changed
(
True
)
if
self
.
_defaults
.
get
(
"staple"
,
False
):
self
.
_staple
.
setChecked
(
True
)
hbox
.
addWidget
(
self
.
_duplex
)
vbox
.
addLayout
(
hbox
)
vbox
.
addWidget
(
self
.
_pdfbook
)
vbox
.
addWidget
(
self
.
_staple
)
o
=
QComboBox
(
self
)
for
(
p
,
cmd
)
in
PRINTERS
:
for
(
p
,
cmd
)
in
self
.
_printers
:
o
.
addItem
(
p
,
cmd
)
vbox
.
addWidget
(
o
)
self
.
_printer
=
o
...
...
@@ -180,7 +209,6 @@ class CPP(QWidget):
self
.
setLayout
(
vbox
)
self
.
setWindowTitle
(
'CUPS pdf printer postprocessor'
)
self
.
show
()
def
print
(
self
):
...
...
@@ -207,7 +235,7 @@ class CPP(QWidget):
pipeline
.
append
([
"%s/print_compact.sh"
%
DIR
,
"%i"
%
self
.
_compact_copies
.
value
()
])
if
self
.
_printer
.
currentData
()
is
SAVE_AS_FILE
:
if
self
.
_printer
.
currentData
()
is
self
.
_save_as_file
:
f
=
QFileDialog
.
getSaveFileName
(
self
,
'Save as ...'
)
if
not
f
[
0
]:
self
.
close
()
...
...
@@ -223,6 +251,7 @@ class CPP(QWidget):
exec_pipeline
(
pipeline
,
self
.
_pdf
,
err_callback
=
logMsg
)
self
.
_button_cancel
.
setText
(
"&Close"
)
DIR
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
sys
.
argv
[
0
]))
if
__name__
==
'__main__'
:
...
...
@@ -236,16 +265,12 @@ if __name__ == '__main__':
with
open
(
CONFIG_FILE
)
as
config
:
CONFIG
=
ast
.
literal_eval
(
"{
\n
%s
\n
}"
%
config
.
read
())
SAVE_AS_FILE
=
object
()
PRINTERS
=
CONFIG
.
get
(
"printers"
,
[])
PRINTERS
.
append
((
"Save as ..."
,
SAVE_AS_FILE
))
for
l
in
sp
.
check_output
([
"lpstat"
,
"-a"
]).
decode
(
"UTF-8"
).
split
(
"
\n
"
):
p
=
l
.
split
(
" "
)[
0
]
if
p
==
""
:
continue
PRINTERS
.
append
((
p
,
[
"lp"
,
"-d"
,
p
]))
# env must be fixed before we create Qt stuff
for
var
,
val
in
CONFIG
.
get
(
"enviroment"
,
{}).
items
():
if
val
is
None
:
del
os
.
environ
[
var
]
else
:
os
.
environ
[
var
]
=
val
app
=
QApplication
(
args
[
2
:])
ex
=
CPP
(
CONFIG
,
args
[
1
],
args
[
2
])
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment