File: lib.py

package info (click to toggle)
vkbasalt-cli 3.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 180 kB
  • sloc: python: 273; xml: 33; makefile: 2
file content (258 lines) | stat: -rwxr-xr-x 11,097 bytes parent folder | download
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# lib.py: library supplying the logics and functions to generate configs

# Copyright (C) 2022 vkbasalt-cli Contributors

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, version 3.

# This library 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.

# SPDX Identifier: LGPL-3.0-only

from os import path, environ, system, remove
from sys import exit
from shutil import copyfile
import configparser
import logging


def parse(args, *arguments):
    # Apply default settings if possible
    if args.default:
        install_paths = [
            "/usr/lib/extensions/vulkan/vkBasalt/etc/vkBasalt",
            "/usr/local",
            "/usr/share/vkBasalt",
        ]
        for item in install_paths:
            file_path = path.join(item, "vkBasalt.conf")
            if path.isfile(file_path):
                if args.output:
                    logging.info(f"Outputting file to {file_path}")
                    copyfile(file_path, path.join(args.output, "vkBasalt.conf"))
                if args.exec:
                    environ["ENABLE_VKBASALT"] = "1"
                    environ["VKBASALT_CONFIG_FILE"] = file_path
                    system(f"{args.exec}")
                return
        logging.error(f"No such path for vkBasalt exists")
        exit(1)

    # Generate config and check for errors
    if args.effects or args.lut_file_path:
        file = []

        # --disable-on-launch
        file.append("enableOnLaunch = ")
        if args.disable_on_launch:
            logging.info("Setting Key enableOnLaunch = False")
            file.append("False\n")
        else:
            logging.info("Setting Key enableOnLaunch = True")
            file.append("True\n")

        # --toggle-key
        file.append("toggleKey = ")
        if args.toggle_key:
            file.append(f"{args.toggle_key[0]}\n")
        else:
            file.append("Home\n")

        # --cas-sharpness
        if args.cas_sharpness:
            args.cas_sharpness = round(args.cas_sharpness, 2)
            if -1 <= args.cas_sharpness <= 1:
                logging.info(f"Setting Key casSharpness = {args.cas_sharpness}")
                file.append(f"casSharpness = {args.cas_sharpness}\n")
            else:
                logging.error(f"Error: CAS sharpness must be above -1 and below 1")
                exit(1)

        # --dls-sharpness
        if args.dls_sharpness:
            args.dls_sharpness = round(args.dls_sharpness, 2)
            if 0 <= args.dls_sharpness <= 1:
                logging.info(f"Setting Key dlsSharpness = {args.dls_sharpness}")
                file.append(f"dlsSharpness = {args.dls_sharpness}\n")
            else:
                logging.error(f"Error: DLS sharpness must be above 0 and below 1")
                exit(1)

        # --dls-denoise
        if args.dls_denoise:
            args.dls_denoise = round(args.dls_denoise, 2)
            if 0 <= args.dls_denoise <= 1:
                logging.info(f"Setting Key dlsDenoise = {args.dls_denoise}")
                file.append(f"dlsDenoise = {args.dls_denoise}\n")
            else:
                logging.error(f"Error: DLS denoise must be above 0 and below 1")
                exit(1)

        # --fxaa-subpixel-quality
        if args.fxaa_subpixel_quality:
            args.fxaa_subpixel_quality = round(args.fxaa_subpixel_quality, 2)
            if 0 <= args.fxaa_subpixel_quality <= 1:
                logging.info(f"Setting Key fxaaQualitySubpix = {args.fxaa_subpixel_quality}")
                file.append(f"fxaaQualitySubpix = {args.fxaa_subpixel_quality}\n")
            else:
                logging.error(f"Error: FXAA subpixel quality must be above 0 and below 1")
                exit(1)

        # --fxaa-edge-quality-threshold
        if args.fxaa_quality_edge_threshold:
            args.fxaa_quality_edge_threshold = round(args.fxaa_quality_edge_threshold, 2)
            if 0 <= args.fxaa_quality_edge_threshold <= 1:
                logging.info(f"Setting Key fxaaQualityEdgeThreshold = {args.fxaa_quality_edge_threshold}")
                file.append(f"fxaaQualityEdgeThreshold = {args.fxaa_quality_edge_threshold}\n")
            else:
                logging.error(f"Error: FXAA edge quality threshold must be above 0 and below 1")
                exit(1)

        # --fxaa-quality-edge-threshold-min
        if args.fxaa_quality_edge_threshold_min:
            args.fxaa_quality_edge_threshold_min = round(args.fxaa_quality_edge_threshold_min, 3)
            if 0 <= args.fxaa_quality_edge_threshold_min <= 0.1:
                logging.info(f"Setting Key fxaaQualityEdgeThresholdMin = {args.fxaa_quality_edge_threshold_min}")
                file.append(f"fxaaQualityEdgeThresholdMin = {args.fxaa_quality_edge_threshold_min}\n")
            else:
                logging.error(f"Error: FXAA edge quality threshold minimum must be above 0 and below 0.1")
                exit(1)

        # --smaa-edge-detection
        if args.smaa_edge_detection:
            logging.info(f"Setting Key smaaEdgeDetection = {args.smaa_edge_detection}")
            file.append(f"smaaEdgeDetection = {args.smaa_edge_detection}\n")

        # --smaa-threshold
        if args.smaa_threshold:
            args.smaa_threshold = round(args.smaa_threshold, 3)
            if 0 <= args.smaa_threshold <= 0.5:
                logging.info(f"Setting Key smaaThreshold = {args.smaa_threshold}")
                file.append(f"smaaThreshold = {args.smaa_threshold}\n")
            else:
                logging.error(f"Error: SMAA threshold must be above 0 and below 0.5")
                exit(1)

        # --smaa-max-search-steps
        if args.smaa_max_search_steps:
            args.smaa_max_search_steps = round(args.smaa_max_search_steps)
            if 0 <= args.smaa_max_search_steps <= 112:
                logging.info(f"Setting Key smaaMaxSearchSteps = {args.smaa_max_search_steps}")
                file.append(f"smaaMaxSearchSteps = {args.smaa_max_search_steps}\n")
            else:
                logging.error(f"Error: SMAA max search steps must be above 0 and below 112")
                exit(1)

        # --smaa-max-search-steps-diagonal
        if args.smaa_max_search_steps_diagonal:
            args.smaa_max_search_steps_diagonal = round(args.smaa_max_search_steps_diagonal)
            if 0 <= args.smaa_max_search_steps_diagonal <= 20:
                logging.info(f"Setting Key smaaMaxSearchStepsDiag = {args.smaa_max_search_steps_diagonal}")
                file.append(f"smaaMaxSearchStepsDiag = {args.smaa_max_search_steps_diagonal}\n")
            else:
                logging.error(f"Error: SMAA max search steps diagonal must be above 0 and below 20")
                exit(1)

        # --smaa-corner-rounding
        if args.smaa_corner_rounding:
            args.smaa_corner_rounding = round(args.smaa_corner_rounding)
            if 0 <= args.smaa_corner_rounding <= 100:
                logging.info(f"Setting Key smaaCornerRounding = {args.smaa_corner_rounding}")
                file.append(f"smaaCornerRounding = {args.smaa_corner_rounding}\n")
            else:
                logging.error(f"Error: SMAA corner rounding must be above 0 and below 100")
                exit(1)

        # --lut-file-path
        if args.lut_file_path:
            if not " " in args.lut_file_path:
                logging.info(f"Setting Key lutFile = {args.lut_file_path}")
                file.append(f"lutFile = {args.lut_file_path}\n")
            else:
                logging.error("Error: CLUT must not contain any whitespace")
                exit(1)

        # Output file
        if args.output:
            if path.isdir(args.output):
                vkbasalt_conf = path.join(args.output, "vkBasalt.conf")
            else:
                logging.error(f"Error: No such directory")
                exit(1)
        else:
            vkbasalt_conf = "/tmp/vkBasalt.conf"
            tmp = True

        # Write and close file
        with open(vkbasalt_conf, "w") as f:
            if args.effects:
                args.effects = ':'.join(args.effects)
                logging.info(f"Setting Key effects = {args.effects}")
                file.append(f"effects = {args.effects}\n")
            logging.info(f"Writing to: {vkbasalt_conf}")
            f.write("".join(file))

        # --exec
        if args.exec:
            environ["ENABLE_VKBASALT"] = "1"
            environ["VKBASALT_CONFIG_FILE"] = vkbasalt_conf
            system(f"{args.exec}")

            if tmp:
                remove(vkbasalt_conf)

    else:
        logging.error(f"Please specify one or more effects, or a CLUT file path.")
        exit(1)

def getConfigValue(config, value):
    with open(config, "r") as f:
        file = "[config]\n"+f.read()
        config = configparser.ConfigParser(allow_no_value=True)
        config.read_string(file)
        return config['config'].get(value)

def ParseConfig(config):
    class Args:
        default = False
        effects = False
        output = False
        disable_on_launch = False
        toggle_key = False
        cas_sharpness = False
        dls_sharpness = False
        dls_denoise = False
        fxaa_subpixel_quality = False
        fxaa_quality_edge_threshold = False
        fxaa_quality_edge_threshold_min = False
        smaa_edge_detection = False
        smaa_threshold = False
        smaa_max_search_steps = False
        smaa_max_search_steps_diagonal = False
        smaa_corner_rounding = False
        lut_file_path = False
    Args.effects = getConfigValue(config, 'effects')
    Args.toggle_key = getConfigValue(config, 'toggleKey')
    Args.disable_on_launch = "True" if getConfigValue(config, 'enableOnLaunch') == "False" else "False"
    Args.cas_sharpness = getConfigValue(config, 'casSharpness')
    Args.dls_sharpness = getConfigValue(config, 'dlsSharpness')
    Args.dls_denoise = getConfigValue(config, 'dlsDenoise')
    Args.fxaa_subpixel_quality = getConfigValue(config, 'fxaaQualitySubpix')
    Args.fxaa_quality_edge_threshold = getConfigValue(config, 'fxaaQualityEdgeThreshold')
    Args.fxaa_quality_edge_threshold_min = getConfigValue(config, 'fxaaQualityEdgeThresholdMin')
    Args.smaa_edge_detection = getConfigValue(config, 'smaaEdgeDetection')
    Args.smaa_threshold = getConfigValue(config, 'smaaThreshold')
    Args.smaa_max_search_steps = getConfigValue(config, 'smaaMaxSearchSteps')
    Args.smaa_max_search_steps_diagonal = getConfigValue(config, 'smaaMaxSearchStepsDiag')
    Args.smaa_corner_rounding = getConfigValue(config, 'smaaCornerRounding')
    Args.lut_file_path = getConfigValue(config, 'lutFile')

    return(Args)