Skip to content

Commit 40929b7

Browse files
Sébastien Barthélémyvincent.palancher
authored andcommitted
examples: Make authentication CLI parsing non-intrusive.
This ensures `make_application()` can be imported and used by different programs, which will also parse their own CLI options. Change-Id: I0df932210a2c350ce2721153494b252d4f8df89e Reviewed-on: http://gerrit2.aldebaran.lan/1562 Reviewed-by: jmonnon <jmonnon@aldebaran.com> Reviewed-by: vincent.palancher <vincent.palancher@softbankrobotics.com> Tested-by: vincent.palancher <vincent.palancher@softbankrobotics.com>
1 parent ea3f422 commit 40929b7

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

examples/authentication_with_application.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,60 @@ def newAuthenticator(self):
2828

2929

3030
# Reads a file containing the username on the first line and the password on
31-
# the second line.
31+
# the second line. This is the format used by qilaunch.
3232
def read_auth_file(path):
3333
with open(path) as f:
3434
username = f.readline().strip()
3535
password = f.readline().strip()
3636
return (username, password)
3737

3838

39-
def make_application():
40-
app = qi.Application(sys.argv)
41-
parser = argparse.ArgumentParser()
39+
def make_application(argv=sys.argv):
40+
"""
41+
Create and return the qi.Application, with authentication set up
42+
according to the command line options.
43+
"""
44+
# create the app and edit `argv` in place to remove the consumed
45+
# arguments.
46+
# As a side effect, if "-h" is in the list, it is replaced with "--help".
47+
app = qi.Application(argv)
48+
49+
# Setup a non-intrusive parser, behaving like `qi.Application`'s own
50+
# parser:
51+
# * don't complain about unknown arguments
52+
# * consume known arguments
53+
# * if the "--help" option is present:
54+
# * print its own options help
55+
# * do not print the main app usage
56+
# * do not call `sys.exit()`
57+
parser = argparse.ArgumentParser(add_help=False, usage=argparse.SUPPRESS)
4258
parser.add_argument(
4359
"-a", "--authfile",
4460
help="Path to the authentication config file. This file must "
4561
"contain the username on the first line and the password on the "
4662
"second line.")
47-
args = parser.parse_args(sys.argv[1:])
63+
if "--help" in argv:
64+
parser.print_help()
65+
return app
66+
args, unparsed_args = parser.parse_known_args(argv[1:])
4867
logins = read_auth_file(args.authfile) if args.authfile else ("nao", "nao")
4968
factory = AuthenticatorFactory(*logins)
5069
app.session.setClientAuthenticatorFactory(factory)
70+
# edit argv in place.
71+
# Note: this might modify sys.argv, like qi.Application does.
72+
argv[1:] = unparsed_args
5173
return app
5274

5375

5476
if __name__ == "__main__":
77+
parser = argparse.ArgumentParser()
78+
parser.add_argument("--msg", default="Hello python")
5579
app = make_application()
80+
args = parser.parse_args()
5681
logger = qi.Logger("authentication_with_application")
5782
logger.info("connecting session")
5883
app.start()
5984
logger.info("fetching ALTextToSpeech service")
6085
tts = app.session.service("ALTextToSpeech")
6186
logger.info("Saying something")
62-
tts.call("say", "Hello python")
87+
tts.call("say", args.msg)

0 commit comments

Comments
 (0)