2
\$\begingroup\$

I have a CLI wrapper for https://yakuake.kde.org/.

Python 3.4.*.

#! /usr/bin/env python
# coding: utf-8

import click
import subprocess


def get_postfix(title):
    file_path = '/tmp/yakuake_{}_postfix'.format(title)

    try:
        with open(file_path, 'r') as f:
            postfix_number = int(f.readline())
        postfix_number += 1
    except FileNotFoundError:
        postfix_number = 1

    with open(file_path, 'w') as f:
        f.write(str(postfix_number))

    return '{:02}'.format(postfix_number)


@click.command()
@click.argument('title')
def set_yakuake_title(title):
    """
    Set Yakuake title for current active tab.
    """
    qbus_cmd = 'qdbus org.kde.yakuake'

    compound_title = '**{title}-{postfix}**'.format(
        title=title,
        postfix=get_postfix(title)
    )
    session_id_cmd = '{qbus} {active_session}'.format(
        qbus=qbus_cmd,
        active_session='/yakuake/sessions org.kde.yakuake.activeSessionId'
    )
    set_tab_title_cmd = '{qbus} {tab_title} `{session_id}` "{title}"'.format(
        qbus=qbus_cmd,
        tab_title='/yakuake/tabs setTabTitle',
        session_id=session_id_cmd,
        title=compound_title
    )

    subprocess.call(set_tab_title_cmd, shell=True)


if __name__ == '__main__':
    set_yakuake_title()

Does this code follow common best practices?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.