Skip to main content

Simple Interactive Console Framework

Framework for interactive console application.

Sample of implementation

# -*- coding:utf-8 -*-
import os
from sicfw.sicfw import Mode, set_global, get_global, start

MEMO = os.path.expanduser('~/.global_memo_v0_0_1')
GLOBAL_INPUT = 'input_lines'
GLOBAL_LINENUM = 'line_num'

class InitialMode(Mode):

    def wait_command(self):
        return 'init'

    @Mode.command('init')
    def initialize(self):
        if os.path.exists(MEMO):
            lines = []
            with open(MEMO, 'r') as f:
                for line in f:
                    lines.append(line.strip())
            set_global(GLOBAL_INPUT, lines)
        else:
            set_global(GLOBAL_INPUT, [])

        return MemoMode

    @Mode.after_command_message('init')
    def initial_message(self):
        return get_global(GLOBAL_INPUT)

class MemoMode(Mode):

    @Mode.after_command_message('--help')
    def help(self):
        message = ['Commands:',
                   '-q        : Quit gmemo.',
                   '-s --save : Save curret memo.',
                   '--replace : Replace line.',
                   '--reset   : Initialize memo.',
        ]
        return message

    @Mode.command('-q')
    def quit_command(self):
        exit(0)

    @Mode.command('--read')
    def read_command(self):
        if os.path.exists(MEMO):
            lines = []
            with open(MEMO, 'r') as f:
                for line in f:
                    lines.append(line.strip())
            set_global(GLOBAL_INPUT, lines)
        return MemoMode

    @Mode.after_command_message('--read')
    def after_read_message(self):
        message = ['----------------',
                   'read succeeded!!',
                   '----------------'
        ]
        lines = get_global(GLOBAL_INPUT)
        message.extend(lines)
        return message

    @Mode.command(('-s', '--save'))
    def save_command(self):
        lines = get_global(GLOBAL_INPUT)
        with open(MEMO, 'w') as f:
            for l in lines:
                f.write(l + '\n')
        return MemoMode

    @Mode.after_command_message(('-s', '--save'))
    def after_save_message(self):
        message = ['----------------',
                   'save succeeded!!',
                   '----------------'
        ]
        return message

    @Mode.free_input
    def free(self, command):
        lines = get_global(GLOBAL_INPUT)
        if lines:
            lines.append(command)
        else:
            lines = [command]
        set_global(GLOBAL_INPUT, lines)
        return MemoMode

    @Mode.command('--replace')
    def change2replace_mode(self):
        return ReplaceMode1

    @Mode.command(('-r', '--reset'))
    def reset(self):
        set_global(GLOBAL_INPUT, [])
        return MemoMode

class ReplaceMode1(Mode):

    def premessage(self):
        message = ['------------------------------',
                   'input line number for replace.',
                   '------------------------------'
        ]
        lines = get_global(GLOBAL_INPUT)
        lines = ['{:>3}'.format(line_num + 1) + ' | ' + line
                  for line_num, line in enumerate(lines)]
        message.extend(lines)
        return message

    @Mode.free_input
    def select_line(self, command):
        try:
            line_number = int(command)
        except:
            return ReplaceMode1

        lines = get_global(GLOBAL_INPUT)
        if line_number > 0 and line_number <= len(lines):
            set_global(GLOBAL_LINENUM, line_number)
            return ReplaceMode2
        else:
            return ReplaceMode1


class ReplaceMode2(Mode):

    def premessage(self):
        lines = get_global(GLOBAL_INPUT)
        line_num = get_global(GLOBAL_LINENUM)
        message = ['-------------------',
                   'replace target line',
                   '-------------------',
                   lines[line_num - 1]
        ]
        return message

    @Mode.free_input
    def replace_line(self, command):
        lines = get_global(GLOBAL_INPUT)
        line_num = get_global(GLOBAL_LINENUM)
        lines[line_num - 1] = command
        set_global(GLOBAL_INPUT, lines)
        return MemoMode

    @Mode.after_free_input_message
    def after_message(self, command):
        lines = get_global(GLOBAL_INPUT)
        message = ['-------------------',
                   'replace succeeded!!',
                   '-------------------'
        ]
        message.extend(lines)
        return message

def main():
    start(InitialMode)

if __name__ == '__main__':
    main()

Metadata

Release files for sicfw 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sicfw 0.0.3
File Size Uploaded Provenance
sicfw-0.0.3.tar.gz 3.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sicfw 0.0.3
File Interpreter ABI Platform
sicfw-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 6.8 kB

Release files / sicfw-0.0.3.tar.gz

Download URL sicfw-0.0.3.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
13bf504a17deb06d7a2231c3971f0a31e05588f7b07a4523fb125cb43659aa65
BLAKE2b-256 checksum
How to use checksums
fa4d471001ae6c367868d49c7e2f9affed263024c2fff1965617da95ed28c674
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Release files / sicfw-0.0.3-py3-none-any.whl

Download URL sicfw-0.0.3-py3-none-any.whl
Size 3.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9bf411dce9e520462cfd5129294228f86fa95376d6a743971d1fe48e04ba0eab
BLAKE2b-256 checksum
How to use checksums
bf741266fae70b6851e246cb1f982aedecdb75d1864e48712588c83f2ee70397
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Security for sicfw 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page