From: Remington Steed Date: Thu, 24 Oct 2019 13:45:37 +0000 (-0400) Subject: LP#1848524: Docs: Treat code include as an Antora "example" X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=5aff7b3eceadefa657f8a9b0870629876fb791ce;p=working%2FEvergreen.git LP#1848524: Docs: Treat code include as an Antora "example" This seems like a case that matches Antora's idea of an "example", rather than a "partial". Signed-off-by: Remington Steed Signed-off-by: Galen Charlton --- diff --git a/docs-antora/modules/development/examples/python_client.py b/docs-antora/modules/development/examples/python_client.py new file mode 100644 index 0000000000..d0c6dfcdbd --- /dev/null +++ b/docs-antora/modules/development/examples/python_client.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +"""OpenSRF client example in Python""" +import osrf.system +import osrf.ses + +def osrf_substring(session, text, sub): + """substring: Accepts a string and a number as input, returns a string""" + request = session.request('opensrf.simple-text.substring', text, sub) + + # Retrieve the response from the method + # The timeout parameter is optional + response = request.recv(timeout=2) + + request.cleanup() + # The results are accessible via content() + return response.content() + +def osrf_split(session, text, delim): + """split: Accepts two strings as input, returns an array of strings""" + request = session.request('opensrf.simple-text.split', text, delim) + response = request.recv() + request.cleanup() + return response.content() + +def osrf_statistics(session, strings): + """statistics: Accepts an array of strings as input, returns a hash""" + request = session.request('opensrf.simple-text.statistics', strings) + response = request.recv() + request.cleanup() + return response.content() + + +if __name__ == "__main__": + file = '/openils/conf/opensrf_core.xml' + + # Pull connection settings from section of opensrf_core.xml + osrf.system.System.connect(config_file=file, config_context='config.opensrf') + + # Set up a connection to the opensrf.settings service + session = osrf.ses.ClientSession('opensrf.simple-text') + + result = osrf_substring(session, "foobar", 3) + print(result) + print + + result = osrf_split(session, "This is a test", " ") + print("Received %d elements: [" % len(result)), + print(', '.join(result)), ']' + + many_strings = ( + "First I think I'll have breakfast", + "Then I think that lunch would be nice", + "And then seventy desserts to finish off the day" + ) + result = osrf_statistics(session, many_strings) + print("Length: %d" % result["length"]) + print("Word count: %d" % result["word_count"]) + + # Cleanup connection resources + session.cleanup() diff --git a/docs-antora/modules/development/pages/intro_opensrf.adoc b/docs-antora/modules/development/pages/intro_opensrf.adoc index 2bce37b784..9d245ea5e4 100644 --- a/docs-antora/modules/development/pages/intro_opensrf.adoc +++ b/docs-antora/modules/development/pages/intro_opensrf.adoc @@ -1348,7 +1348,7 @@ client: [source, python] -------------------------------------------------------------------------------- -include::python_client.py[] +include::example$python_client.py[] -------------------------------------------------------------------------------- NOTE: Python's `dnspython` module refuses to read `/etc/resolv.conf`, so to diff --git a/docs-antora/modules/development/pages/python_client.py b/docs-antora/modules/development/pages/python_client.py deleted file mode 100644 index d0c6dfcdbd..0000000000 --- a/docs-antora/modules/development/pages/python_client.py +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/env python -"""OpenSRF client example in Python""" -import osrf.system -import osrf.ses - -def osrf_substring(session, text, sub): - """substring: Accepts a string and a number as input, returns a string""" - request = session.request('opensrf.simple-text.substring', text, sub) - - # Retrieve the response from the method - # The timeout parameter is optional - response = request.recv(timeout=2) - - request.cleanup() - # The results are accessible via content() - return response.content() - -def osrf_split(session, text, delim): - """split: Accepts two strings as input, returns an array of strings""" - request = session.request('opensrf.simple-text.split', text, delim) - response = request.recv() - request.cleanup() - return response.content() - -def osrf_statistics(session, strings): - """statistics: Accepts an array of strings as input, returns a hash""" - request = session.request('opensrf.simple-text.statistics', strings) - response = request.recv() - request.cleanup() - return response.content() - - -if __name__ == "__main__": - file = '/openils/conf/opensrf_core.xml' - - # Pull connection settings from section of opensrf_core.xml - osrf.system.System.connect(config_file=file, config_context='config.opensrf') - - # Set up a connection to the opensrf.settings service - session = osrf.ses.ClientSession('opensrf.simple-text') - - result = osrf_substring(session, "foobar", 3) - print(result) - print - - result = osrf_split(session, "This is a test", " ") - print("Received %d elements: [" % len(result)), - print(', '.join(result)), ']' - - many_strings = ( - "First I think I'll have breakfast", - "Then I think that lunch would be nice", - "And then seventy desserts to finish off the day" - ) - result = osrf_statistics(session, many_strings) - print("Length: %d" % result["length"]) - print("Word count: %d" % result["word_count"]) - - # Cleanup connection resources - session.cleanup()