-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpmatch.py
132 lines (100 loc) · 3.19 KB
/
rpmatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/python -tt
# -*- coding: UTF-8 -*-
# Copyright (C) 2017 Denis Ollier
#
# This module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
"""
Python implementation of the rpmatch(3) libc function.
It can be imported and used in python scripts or directly run as a script.
Usage example in python scripts:
from rpmatch import rpmatch
response = raw_input("Do you agree? ")
if rpmatch(response) == 1:
# User agreed
...
else:
# User did not agree
...
Usage as a script from command line:
> python -m rpmatch response
When used as a script, the following exit status codes are used:
- 0 for a recognized affirmative response
- 1 for a recognized negative response
- 2 for an unrecognized response
"""
__all__ = ['rpmatch']
__author__ = 'Denis Ollier'
__copyright__ = 'Copyright (C) 2017 Denis Ollier'
__license__ = 'GPL-3.0-or-later'
__version__ = '1.0.0'
import locale
import re
def rpmatch(response):
"""
Determine if the answer to a question is affirmative or negative
Returns:
0 for a recognized negative response,
1 for a recognized affirmative response,
-1 for an unrecognized response
"""
# locale.nl_langinfo is not supported for all OSes (e.g: windows)
try:
yes = locale.nl_langinfo(locale.YESEXPR)
no = locale.nl_langinfo(locale.NOEXPR)
except AttributeError:
yes = "^[yY]"
no = "^[nN]"
if re.match(yes, response):
result = 1
elif re.match(no, response):
result = 0
else:
result = -1
return result
# Make module runnable as a script
if __name__ == "__main__":
import optparse # argparse is better but not included in python 2.6 stdlib
import os
import sys
def process_command_line():
"""Process program command line arguments"""
# Setup option parser
usage = (
"\n %prog response"
"\n %prog option"
)
description = (
"Determine if the answer to a question is affirmative or negative"
)
optparser = optparse.OptionParser(usage=usage, description=description)
# Register options
optparser.add_option(
"-V", "--version",
action="store_true",
help="show version information and exit"
)
# Convert arguments to local variables
(keyword_args, positional_args) = optparser.parse_args()
# Handle keyword arguments
if keyword_args.version:
sys.stdout.write(
"%s %s\n" % (os.path.basename(sys.argv[0]), __version__)
)
sys.exit(os.EX_OK)
# Look for response in positional args
try:
response = positional_args[0]
except IndexError:
optparser.print_help(file=sys.stderr)
sys.exit(3)
return response
#
# Main
#
locale.setlocale(locale.LC_ALL, "")
response = process_command_line()
sys.exit(1 - rpmatch(response))
# vi: set ft=python et sw=4 ts=4: