aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTmpecho <82368148+Tmpecho@users.noreply.github.com>2023-08-21 20:14:46 +0200
committerGitHub <noreply@github.com>2023-08-21 20:14:46 +0200
commit983dad4b748cf8b06a39e210bf030549f2a1b3db (patch)
tree8c0ac5bd4ed9cc1554440872447212009e5e3215
parent9530ce3d52e635d3813d8a35d7ebf8e10798dda7 (diff)
downloadautotiling-983dad4b748cf8b06a39e210bf030549f2a1b3db.tar.gz
Refactor autotiling argparse setup for conciseness
- Utilized f-strings for improved readability and string formatting. - Removed redundant line breaks and aligned argument definitions. - Consolidated file handling with context managers for better error resilience.
-rw-r--r--autotiling/main.py88
1 files changed, 28 insertions, 60 deletions
diff --git a/autotiling/main.py b/autotiling/main.py
index 8f7e673..2bd9fb5 100644
--- a/autotiling/main.py
+++ b/autotiling/main.py
@@ -26,23 +26,14 @@ try:
except ImportError:
__version__ = "unknown"
-
def temp_dir():
- if os.getenv("TMPDIR"):
- return os.getenv("TMPDIR")
- elif os.getenv("TEMP"):
- return os.getenv("TEMP")
- elif os.getenv("TMP"):
- return os.getenv("TMP")
-
- return "/tmp"
+ return os.getenv("TMPDIR") or os.getenv("TEMP") or os.getenv("TMP") or "/tmp"
def save_string(string, file):
try:
- file = open(file, "wt")
- file.write(string)
- file.close()
+ with open(file_path, "wt") as file:
+ file.write(string)
except Exception as e:
print(e)
@@ -65,10 +56,7 @@ def switch_splitting(i3, e, debug, outputs, workspaces, depth_limit):
# Stop, if outputs is set and current output is not in the selection
if outputs and output not in outputs:
if debug:
- print(
- "Debug: Autotiling turned off on output {}".format(output),
- file=sys.stderr,
- )
+ print(f"Debug: Autotiling turned off on output {output}", file=sys.stderr)
return
if con and not workspaces or (str(con.workspace().num) in workspaces):
@@ -119,65 +107,46 @@ def switch_splitting(i3, e, debug, outputs, workspaces, depth_limit):
if new_layout != con.parent.layout:
result = i3.command(new_layout)
if result[0].success and debug:
- print("Debug: Switched to {}".format(new_layout), file=sys.stderr)
+ print(f"Debug: Switched to {new_layout}", file=sys.stderr)
elif debug:
- print("Error: Switch failed with err {}".format(result[0].error), file=sys.stderr, )
+ print(f"Error: Switch failed with err {result[0].error}", file=sys.stderr)
elif debug:
print("Debug: No focused container found or autotiling on the workspace turned off", file=sys.stderr)
except Exception as e:
- print("Error: {}".format(e), file=sys.stderr)
+ print(f"Error: {e}", file=sys.stderr)
def main():
parser = argparse.ArgumentParser()
- parser.add_argument("-d",
- "--debug",
- action="store_true",
+
+ parser.add_argument("-d", "--debug", action="store_true",
help="print debug messages to stderr")
- parser.add_argument("-v",
- "--version",
- action="version",
- version="%(prog)s {}, Python {}".format(__version__, sys.version),
- help="display version information", )
- parser.add_argument("-o",
- "--outputs",
- help="restricts autotiling to certain output; "
- "example: autotiling --output DP-1 HDMI-0",
- nargs="*",
- type=str,
- default=[], )
- parser.add_argument("-w",
- "--workspaces",
- help="restricts autotiling to certain workspaces; example: autotiling --workspaces 8 9",
- nargs="*",
- type=str,
- default=[], )
- parser.add_argument("-l",
- "--limit",
- help='limit how often autotiling will split a container; '
- 'try "2", if you like master-stack layouts; default: 0 (no limit)',
- type=int,
- default=0, )
+ parser.add_argument("-v", "--version", action="version",
+ version=f"%(prog)s {__version__}, Python {sys.version}",
+ help="display version information")
+ parser.add_argument("-o", "--outputs", nargs="*", type=str, default=[],
+ help="restricts autotiling to certain output; example: autotiling --output DP-1 HDMI-0")
+ parser.add_argument("-w", "--workspaces", nargs="*", type=str, default=[],
+ help="restricts autotiling to certain workspaces; example: autotiling --workspaces 8 9")
+ parser.add_argument("-l", "--limit", type=int, default=0,
+ help='limit how often autotiling will split a container; try "2" if you like master-stack layouts; default: 0 (no limit)')
"""
Changing event subscription has already been the objective of several pull request. To avoid doing this again
and again, let's allow to specify them in the `--events` argument.
"""
- parser.add_argument("-e",
- "--events",
- help="list of events to trigger switching split orientation; default: WINDOW MODE",
- nargs="*",
- type=str,
- default=["WINDOW", "MODE"])
+ parser.add_argument("-e", "--events", nargs="*", type=str, default=["WINDOW", "MODE"],
+ help="list of events to trigger switching split orientation; default: WINDOW MODE")
- args = parser.parse_args()
- if args.debug and args.outputs:
- print("autotiling is only active on outputs:", ",".join(args.outputs))
+ args = parser.parse_args()
- if args.debug and args.workspaces:
- print("autotiling is only active on workspaces:", ','.join(args.workspaces))
+ if args.debug:
+ if args.outputs:
+ print(f"autotiling is only active on outputs: {','.join(args.outputs)}")
+ if args.workspaces:
+ print(f"autotiling is only active on workspaces: {','.join(args.workspaces)}")
# For use w/ nwg-panel
ws_file = os.path.join(temp_dir(), "autotiling")
@@ -202,13 +171,12 @@ def main():
for e in args.events:
try:
i3.on(Event[e], handler)
- print("{} subscribed".format(Event[e]))
+ print(f"{Event[e]} subscribed")
except KeyError:
- print("'{}' is not a valid event".format(e), file=sys.stderr)
+ print(f"'{e}' is not a valid event", file=sys.stderr)
i3.main()
if __name__ == "__main__":
- # execute only if run as a script
main()