Skip to content

Commit

Permalink
Merge pull request #23 from yukihirop/features/0.2.3
Browse files Browse the repository at this point in the history
Merge from features/0.2.3 into master
  • Loading branch information
yukihirop authored Feb 2, 2021
2 parents 29faacd + 4fd60b1 commit 5edae08
Show file tree
Hide file tree
Showing 27 changed files with 1,495 additions and 462 deletions.
13 changes: 7 additions & 6 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ Instead, the operation is guaranteed by executing the following command and actu
- [ ] python3 gfzs/views/search_result.py
- [ ] python3 gfzs/utils/markup.py
- [ ] python3 gfzs/utils/color.py
- [ ] python3 gfzs/utils/logger.py
- [ ] python3 gfzs/runtime/config.py
- [ ] python3 gfzs/cmd/init.py
- [ ] python3 gfzs/cmd/edit.py
- [ ] python3 gfzs/cmd/demo.py
- [ ] python3 gfzs/cmd/valid.py
- [ ] python3 gfzs/controller.py
- [ ] python3 gfzs/model.py
- [ ] cat fixtures/rust.json | bin/gfzs -s 20
- [ ] cat fixtures/rust.json | python3 -m gfzs -s 40
- [ ] bin/gfzs init
- [ ] bin/gfzs edit
- [ ] bin/gfzs demo
- [ ] bin/gfzs valid
- [ ] cat fixtures/rust.json | bin/gfzs -s 20 -l 0 -p ./tmp/gfzs.log
- [ ] cat fixtures/rust.json | python3 -m gfzs -s 40 -l 0 -p ./tmp/gfzs.log
- [ ] bin/gfzs -l 0 -p ./tmp/gfzs.log init
- [ ] bin/gfzs -l 0 -p ./tmp/gfzs.log edit
- [ ] bin/gfzs -l 0 -p ./tmp/gfzs.log demo
- [ ] bin/gfzs -l 0 -p ./tmp/gfzs.log valid
- [ ] black gfzs/**/*.py
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.2.3

2021/02/02

- `[Fix bugs]` Bug about a page turning ([#21](https://github.com/yukihirop/gfzs/pull/21))
- `[New Features]` Output a Log ([#19](https://github.com/yukihirop/gfzs/pull/19))

Please see [milestone](https://github.com/yukihirop/gfzs/milestone/5)

## 0.2.2

2021/01/27
Expand Down
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,35 @@ gfzs demo

## 📖 Usage

```bash
$ gfzs -h

usage: gfzs [-h] [--version] [--score SCORE] [--log-level LOG_LEVEL]
[--log-path LOG_PATH]
{init,edit,demo,valid} ...

Google Fuzzy Search. Pipe the search result(json) of googler and use it

optional arguments:
-h, --help show this help message and exit
--version, -v show program's version number and exit
--score SCORE, -s SCORE
fuzzywuzzy's score (default: 30). please see
https://github.com/seatgeek/fuzzywuzzy
--log-level LOG_LEVEL, -l LOG_LEVEL
Log Level (default: 1). [0: DEBUG, 1: INFO, 2: WARN,
3: ERROR, 4: FATAL, 5: UNKNOWN, 6: NULL]
--log-path LOG_PATH, -p LOG_PATH
Log Path (default: ~/gfzs.log)

SubCommands:
{init,edit,demo,valid}
init Initialize gfzs
edit Edit config
demo Play with Demo
valid Validate ~/.gfzsrc
```

Initialize first. A configuration file (`.gfzsrc`) is created in your home directory.

```bash
Expand Down Expand Up @@ -242,6 +271,29 @@ Config is valid
|`DEBUG`|You will be able to use the `debug` module.|
|`EDITOR`|Set the command to open the editor.|


## 💌 Logging

By default, logs with a `log level of INFO` or higher will be sent to `~/gfzs.log`.

|Log Level|value|desc|
|---------|-----|----|
|`DEBUG`|0|||
|`INFO`|1|default|
|`WARN`|2||
|`ERROR`|3||
|`FATAL`|4||
|`UNKNOWN`|5||
|`NULL`|6||

## 🐛 Debug

When you want to know the details of the movement or identify the cause of the error, you can change the log level to `DEBUG(0)` and debug with the log.

```bash
$ cat fixtures/python.json | bin/gfzs --log-level 0
```

## 💪 Development

First, create a virtual environment.
Expand Down
80 changes: 64 additions & 16 deletions gfzs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
from gfzs.utils import debug
from gfzs.controller import Controller
from gfzs.model import Model
from gfzs.runtime.opts import RuntimeOpts
from gfzs.runtime.config import RuntimeConfig
import gfzs.runtime.opts as runtime_opts
import gfzs.runtime.config as runtime_config
import gfzs.utils.logger as logger
import gfzs.cmd.init as cmd_init
import gfzs.cmd.edit as cmd_edit
import gfzs.cmd.demo as cmd_demo
Expand All @@ -27,8 +28,8 @@
from utils import debug
from controller import Controller
from model import Model
from runtime.opts import RuntimeOpts
from runtime.config import RuntimeConfig
import runtime.opts as runtime_opts
import runtime.config as runtime_config
import cmd.init as cmd_init
import cmd.edit as cmd_edit
import cmd.demo as cmd_demo
Expand All @@ -51,8 +52,26 @@ def init_parser():
"--score",
"-s",
type=int,
default=RuntimeOpts.default_score,
help="fuzzywuzzy's score. please see https://github.com/seatgeek/fuzzywuzzy",
default=runtime_opts.default_score,
help="fuzzywuzzy's score (default: {0}). please see https://github.com/seatgeek/fuzzywuzzy".format(
runtime_opts.default_score
),
)
parser.add_argument(
"--log-level",
"-l",
type=str,
default=logger.INFO,
help="Log Level (default: {0}). [0: DEBUG, 1: INFO, 2: WARN, 3: ERROR, 4: FATAL, 5: UNKNOWN, 6: NULL]".format(
logger.INFO
),
)
parser.add_argument(
"--log-path",
"-p",
type=str,
default=runtime_config.default_log_path,
help="Log Path (default: {0})".format(runtime_config.default_log_path),
)

subparsers = parser.add_subparsers(title="SubCommands", dest="command")
Expand All @@ -61,7 +80,9 @@ def init_parser():
subparsers.add_parser("init", help="Initialize gfzs")
subparsers.add_parser("edit", help="Edit config")
subparsers.add_parser("demo", help="Play with Demo")
subparsers.add_parser("valid", help="Validate ~/.gfzsrc")
subparsers.add_parser(
"valid", help="Validate {0}".format(runtime_config.default_config_path)
)

return parser

Expand All @@ -70,30 +91,44 @@ def exec_subcommand(parser, argv=sys.argv[1:]) -> None:
args = parser.parse_args(argv)

if args.command == "init":
cmd_init.main()
cmd_init.main(args)
elif args.command == "edit":
cmd_edit.main()
cmd_edit.main(args)
elif args.command == "demo":
cmd_demo.main(args)
elif args.command == "valid":
cmd_valid.main()
cmd_valid.main(args)


def main() -> None:

signal.signal(signal.SIGINT, signal.SIG_DFL)
# https://note.nkmk.me/python-warnings-ignore-warning/
warnings.simplefilter("ignore", FutureWarning)

parser = init_parser()
exec_subcommand(parser)
args = parser.parse_args()

progname = "gfzs"
properties = {
"progname": progname,
"severity": int(args.log_level),
"log_path": args.log_path,
}
logger.init_properties(**properties)
logger.debug("start %s" % progname)

def handle_sigint(signum, frame):
logger.debug("detect SIGINT (Ctrl-c)")
logger.debug("exit 0")
sys.exit(0)

signal.signal(signal.SIGINT, handle_sigint)

data = None
error = None
errors = []
printable_len = 100

args = parser.parse_args()
_ = RuntimeOpts.get_instance(args)
runtime_opts.init(args)
ttyname = tty.get_ttyname()

with open_tty(ttyname) as tty_f:
Expand All @@ -102,31 +137,40 @@ def main() -> None:
try:
json_str = sys.stdin.read()
data = json.loads(json_str)
runtime_config = RuntimeConfig.get_instance()
validator = Model(data)
runtime_config.init()

if not runtime_config.valid():
logger.debug("[print] Config is invalid.")
print("Config is invalid.")
errors = runtime_config.errors
return
elif not validator.valid():
errors = validator.errors
return
except json.decoder.JSONDecodeError as e:
logger.error(e)
print("Error: %s" % e)
if "[ERROR]" in json_str or len(json_str) <= printable_len:
logger.error("Input data: %s" % json_str)
print("Input data: %s" % json_str)
else:
logger.error(
"Input data (100 chars): %s ..." % json_str[:printable_len]
)
print("Input data (100 chars): %s ..." % json_str[:printable_len])

logger.debug("exit 1")
sys.exit(1)
except Exception as e:
errors.append(e)
finally:
if errors != []:
for error in errors:
logger.error(error)
print("Error: %s" % error)

logger.debug("exit 1")
sys.exit(1)

controller = Controller(data)
Expand All @@ -139,5 +183,9 @@ def main() -> None:
finally:
controller._end_curses()
if error != None:
logger.error(error)
print(error)
logger.debug("exit 1")
sys.exit(1)

logger.debug("end %s" % progname, new_line=True)
52 changes: 40 additions & 12 deletions gfzs/cmd/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import signal
import warnings
import curses
import argparse

# local

Expand All @@ -14,18 +15,20 @@
# https://codechacha.com/ja/how-to-import-python-files/
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
from controller import Controller
from runtime.config import RuntimeConfig
from runtime.opts import RuntimeOpts
import runtime.config as runtime_config
import runtime.opts as runtime_opts
import utils.logger as logger

if os.environ.get("DEBUG"):
import debug
import utils.debug as debug

# need when 「cat fixtures/rust.json | python -m gfzs」
# need when 「cat fixtures/rust.json | bin/gfzs」
else:
from gfzs.controller import Controller
from gfzs.runtime.config import RuntimeConfig
from gfzs.runtime.opts import RuntimeOpts
import gfzs.runtime.config as runtime_config
import gfzs.runtime.opts as runtime_opts
import gfzs.utils.logger as logger

if os.environ.get("DEBUG"):
import gfzs.utils.debug as debug
Expand All @@ -35,8 +38,9 @@
# https://codechacha.com/ja/how-to-import-python-files/
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname("../"))))
from controller import Controller
from runtime.config import RuntimeConfig
from runtime.opts import RuntimeOpts
import runtime.config as runtime_config
import runtime.opts as runtime_opts
import utils.logger as logger

if os.environ.get("DEBUG"):
import utils.debug as debug
Expand Down Expand Up @@ -128,15 +132,31 @@
]


def main(args=None):
signal.signal(signal.SIGINT, signal.SIG_DFL)
def main(args: argparse.Namespace):
progname = "gfzs.cmd.demo"
properties = {
"progname": progname,
"severity": int(args.log_level),
"log_path": args.log_path,
}
logger.init_properties(**properties)
logger.debug("start %s" % progname)

def handle_sigint(signum, fframe) -> None:
logger.debug("detect SIGINT (Ctrl-c)")
logger.debug("exit 0")
sys.exit(0)

signal.signal(signal.SIGINT, handle_sigint)
warnings.simplefilter("ignore", FutureWarning)

_ = RuntimeOpts.get_instance(args)
runtime_config = RuntimeConfig.get_instance()
runtime_config.init()
runtime_opts.init(args)
if not runtime_config.valid():
logger.debug("[print] 'Config is invalid.'")
print("Config is invalid.")
for error in runtime_config.errors:
logger.error(error)
print("Error: %s" % error)
sys.exit(1)

Expand All @@ -152,9 +172,17 @@ def main(args=None):
finally:
controller._end_curses()
if error != None:
logger.error(error)
print("Error: %s" % error)
logger.debug("exit 1")
sys.exit(1)

logger.debug("end %s" % progname, new_line=True)


if __name__ == "__main__":
main()
args = argparse.Namespace()
args.log_path = "./tmp/gfzs.log"
args.log_level = 0

main(args)
Loading

0 comments on commit 5edae08

Please sign in to comment.