Skip to main content

Framework for simple interactive console.

Project description

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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sicfw-0.0.3.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sicfw-0.0.3-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file sicfw-0.0.3.tar.gz.

File metadata

  • Download URL: sicfw-0.0.3.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for sicfw-0.0.3.tar.gz
Algorithm Hash digest
SHA256 13bf504a17deb06d7a2231c3971f0a31e05588f7b07a4523fb125cb43659aa65
MD5 270d6fcbec2d9187485a0003c4084bd4
BLAKE2b-256 fa4d471001ae6c367868d49c7e2f9affed263024c2fff1965617da95ed28c674

See more details on using hashes here.

File details

Details for the file sicfw-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: sicfw-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for sicfw-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9bf411dce9e520462cfd5129294228f86fa95376d6a743971d1fe48e04ba0eab
MD5 2192a5ed8ea26a56d872bf67180d46b4
BLAKE2b-256 bf741266fae70b6851e246cb1f982aedecdb75d1864e48712588c83f2ee70397

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page