Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
69ad124b
Commit
69ad124b
authored
Oct 11, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1298 from dagwieers/doc-optparse
Moving now from getopt to optparse
parents
5849ab31
2786149b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
90 additions
and
101 deletions
+90
-101
hacking/module_formatter.py
+90
-101
No files found.
hacking/module_formatter.py
View file @
69ad124b
...
@@ -25,7 +25,7 @@ import json
...
@@ -25,7 +25,7 @@ import json
import
ast
import
ast
from
jinja2
import
Environment
,
FileSystemLoader
from
jinja2
import
Environment
,
FileSystemLoader
import
re
import
re
import
getopt
import
optparse
import
time
import
time
import
datetime
import
datetime
import
subprocess
import
subprocess
...
@@ -169,9 +169,9 @@ def get_docstring(filename, verbose=False):
...
@@ -169,9 +169,9 @@ def get_docstring(filename, verbose=False):
return
doc
return
doc
def
return_data
(
text
,
arg
s
,
outputname
,
module
):
def
return_data
(
text
,
option
s
,
outputname
,
module
):
if
arg
s
.
output_dir
is
not
None
:
if
option
s
.
output_dir
is
not
None
:
f
=
open
(
os
.
path
.
join
(
arg
s
.
output_dir
,
outputname
%
module
),
'w'
)
f
=
open
(
os
.
path
.
join
(
option
s
.
output_dir
,
outputname
%
module
),
'w'
)
f
.
write
(
text
)
f
.
write
(
text
)
f
.
close
()
f
.
close
()
else
:
else
:
...
@@ -179,91 +179,80 @@ def return_data(text, args, outputname, module):
...
@@ -179,91 +179,80 @@ def return_data(text, args, outputname, module):
def
main
():
def
main
():
class
Object
(
object
):
pass
type_choices
=
[
'html'
,
'latex'
,
'man'
,
'rst'
,
'json'
]
args
=
Object
()
args
.
ansible_version
=
'unknown'
args
.
module_dir
=
MODULEDIR
args
.
template_dir
=
'hacking/templates'
args
.
type
=
'latex'
args
.
module_list
=
[]
args
.
verbose
=
False
args
.
output_dir
=
None
args
.
includes_file
=
None
args
.
do_boilerplate
=
False
try
:
p
=
optparse
.
OptionParser
(
opts
,
arguments
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
'A:M:T:t:m:vo:I:GVh'
,
version
=
'
%
prog 1.0'
,
[
'ansible-version='
,
'module-dir='
,
'template-dir='
,
'type='
,
usage
=
'usage:
%
prog [options] arg1 arg2'
,
'module='
,
'verbose'
,
'output-dir='
,
'includes-file='
,
description
=
'Convert Ansible module DOCUMENTATION strings to other formats'
,
'generate'
,
'version'
,
'help'
,
])
)
except
getopt
.
error
,
e
:
print
>>
sys
.
stderr
,
'ERROR:
%
s'
%
str
(
e
)
p
.
add_option
(
"-A"
,
"--ansible-version"
,
sys
.
exit
(
1
)
action
=
"store"
,
dest
=
"ansible_version"
,
for
opt
,
arg
in
opts
:
default
=
"unknown"
,
if
opt
in
(
'-A'
,
'--ansible-version'
):
help
=
"Ansible version number"
)
args
.
ansible_version
=
arg
p
.
add_option
(
"-M"
,
"--module-dir"
,
elif
opt
in
(
'-M'
,
'--module-dir'
):
action
=
"store"
,
args
.
module_dir
=
arg
dest
=
"module_dir"
,
elif
opt
in
(
'-T'
,
'--template-dir'
):
default
=
MODULEDIR
,
args
.
template_dir
=
arg
help
=
"Ansible modules/ directory"
)
elif
opt
in
(
'-t'
,
'--type'
):
p
.
add_option
(
"-T"
,
"--template-dir"
,
args
.
type
=
arg
action
=
"store"
,
if
args
.
type
not
in
type_choices
:
dest
=
"template_dir"
,
print
>>
sys
.
stderr
,
'ERROR: Type
%
s not in possible types
%
s.'
%
(
args
.
type
,
type_choices
)
default
=
"hacking/templates"
,
sys
.
exit
(
1
)
help
=
"directory containing Jinja2 templates"
)
elif
opt
in
(
'-m'
,
'--module'
):
p
.
add_option
(
"-t"
,
"--type"
,
args
.
module_list
.
append
(
arg
)
action
=
'store'
,
elif
opt
in
(
'-v'
,
'--verbose'
):
dest
=
'type'
,
args
.
verbose
=
True
choices
=
[
'html'
,
'latex'
,
'man'
,
'rst'
,
'json'
],
elif
opt
in
(
'-o'
,
'--output-dir'
):
default
=
'latex'
,
args
.
output_dir
=
arg
help
=
"Output type"
)
elif
opt
in
(
'-I'
,
'--includes-file'
):
p
.
add_option
(
"-m"
,
"--module"
,
args
.
includes_file
=
arg
action
=
'append'
,
elif
opt
in
(
'-G'
,
'--generate'
):
default
=
[],
args
.
do_boilerplate
=
True
dest
=
'module_list'
,
elif
opt
in
(
'-V'
,
'--version'
):
help
=
"Add modules to process in module_dir"
)
print
>>
sys
.
stderr
,
'
%(prog)
s 1.0'
p
.
add_option
(
"-v"
,
"--verbose"
,
elif
opt
in
(
'-h'
,
'--help'
):
action
=
'store_true'
,
print
>>
sys
.
stderr
,
'''Convert Ansible module DOCUMENTATION strings to other formats
default
=
False
,
help
=
"Verbose"
)
-A, --ansible-version= Ansible version number
p
.
add_option
(
"-o"
,
"--output-dir"
,
-M, --module-dir= Ansible modules/ directory
action
=
"store"
,
-T, --template-dir= Directory containing Jinja2 templates
dest
=
"output_dir"
,
-t, --type= Output type
default
=
None
,
-m, --module= Add modules to process in module_dir
help
=
"Output directory for module files"
)
-v, --verbose Verbose
p
.
add_option
(
"-I"
,
"--includes-file"
,
-o, --output-dir= Output directory for module files
action
=
"store"
,
-I, --includes-file= Create a file containing list of processed modules
dest
=
"includes_file"
,
-G, --generate Generate boilerplate DOCUMENTATION to stdout
default
=
None
,
'''
help
=
"Create a file containing list of processed modules"
)
sys
.
exit
(
0
)
p
.
add_option
(
"-G"
,
"--generate"
,
else
:
action
=
"store_true"
,
print
>>
sys
.
stderr
,
'ERROR: Option
%
s unknown to getopt'
%
opt
dest
=
"do_boilerplate"
,
sys
.
exit
(
1
)
default
=
False
,
help
=
"generate boilerplate DOCUMENTATION to stdout"
)
# print "M: %s" % args.module_dir
p
.
add_option
(
'-V'
,
action
=
'version'
)
# print "t: %s" % args.type
# print "m: %s" % args.module_list
(
options
,
args
)
=
p
.
parse_args
()
# print "v: %s" % args.verbose
# print "M: %s" % options.module_dir
if
args
.
do_boilerplate
:
# print "t: %s" % options.type
# print "m: %s" % options.module_list
# print "v: %s" % options.verbose
if
options
.
do_boilerplate
:
boilerplate
()
boilerplate
()
sys
.
exit
(
0
)
sys
.
exit
(
0
)
if
not
arg
s
.
module_dir
:
if
not
option
s
.
module_dir
:
print
"Need module_dir"
print
"Need module_dir"
sys
.
exit
(
1
)
sys
.
exit
(
1
)
if
not
arg
s
.
template_dir
:
if
not
option
s
.
template_dir
:
print
"Need template_dir"
print
"Need template_dir"
sys
.
exit
(
1
)
sys
.
exit
(
1
)
env
=
Environment
(
loader
=
FileSystemLoader
(
arg
s
.
template_dir
),
env
=
Environment
(
loader
=
FileSystemLoader
(
option
s
.
template_dir
),
variable_start_string
=
"@{"
,
variable_start_string
=
"@{"
,
variable_end_string
=
"}@"
,
variable_end_string
=
"}@"
,
trim_blocks
=
True
,
trim_blocks
=
True
,
...
@@ -271,25 +260,25 @@ def main():
...
@@ -271,25 +260,25 @@ def main():
env
.
globals
[
'xline'
]
=
rst_xline
env
.
globals
[
'xline'
]
=
rst_xline
if
arg
s
.
type
==
'latex'
:
if
option
s
.
type
==
'latex'
:
env
.
filters
[
'jpfunc'
]
=
latex_ify
env
.
filters
[
'jpfunc'
]
=
latex_ify
template
=
env
.
get_template
(
'latex.j2'
)
template
=
env
.
get_template
(
'latex.j2'
)
outputname
=
"
%
s.tex"
outputname
=
"
%
s.tex"
includecmt
=
"
%
generated code
\n
"
includecmt
=
"
%
generated code
\n
"
includefmt
=
"
\\
input
%
s
\n
"
includefmt
=
"
\\
input
%
s
\n
"
if
arg
s
.
type
==
'html'
:
if
option
s
.
type
==
'html'
:
env
.
filters
[
'jpfunc'
]
=
html_ify
env
.
filters
[
'jpfunc'
]
=
html_ify
template
=
env
.
get_template
(
'html.j2'
)
template
=
env
.
get_template
(
'html.j2'
)
outputname
=
"
%
s.html"
outputname
=
"
%
s.html"
includecmt
=
""
includecmt
=
""
includefmt
=
""
includefmt
=
""
if
arg
s
.
type
==
'man'
:
if
option
s
.
type
==
'man'
:
env
.
filters
[
'jpfunc'
]
=
man_ify
env
.
filters
[
'jpfunc'
]
=
man_ify
template
=
env
.
get_template
(
'man.j2'
)
template
=
env
.
get_template
(
'man.j2'
)
outputname
=
"ansible.
%
s.3"
outputname
=
"ansible.
%
s.3"
includecmt
=
""
includecmt
=
""
includefmt
=
""
includefmt
=
""
if
arg
s
.
type
==
'rst'
:
if
option
s
.
type
==
'rst'
:
env
.
filters
[
'jpfunc'
]
=
rst_ify
env
.
filters
[
'jpfunc'
]
=
rst_ify
env
.
filters
[
'html_ify'
]
=
html_ify
env
.
filters
[
'html_ify'
]
=
html_ify
env
.
filters
[
'fmt'
]
=
rst_fmt
env
.
filters
[
'fmt'
]
=
rst_fmt
...
@@ -298,28 +287,28 @@ def main():
...
@@ -298,28 +287,28 @@ def main():
outputname
=
"
%
s.rst"
outputname
=
"
%
s.rst"
includecmt
=
".. Generated by module_formatter
\n
"
includecmt
=
".. Generated by module_formatter
\n
"
includefmt
=
".. include:: modules/
%
s.rst
\n
"
includefmt
=
".. include:: modules/
%
s.rst
\n
"
if
arg
s
.
type
==
'json'
:
if
option
s
.
type
==
'json'
:
env
.
filters
[
'jpfunc'
]
=
json_ify
env
.
filters
[
'jpfunc'
]
=
json_ify
outputname
=
"
%
s.json"
outputname
=
"
%
s.json"
includecmt
=
""
includecmt
=
""
includefmt
=
""
includefmt
=
""
if
arg
s
.
type
==
'js'
:
if
option
s
.
type
==
'js'
:
env
.
filters
[
'jpfunc'
]
=
js_ify
env
.
filters
[
'jpfunc'
]
=
js_ify
template
=
env
.
get_template
(
'js.j2'
)
template
=
env
.
get_template
(
'js.j2'
)
outputname
=
"
%
s.js"
outputname
=
"
%
s.js"
if
arg
s
.
includes_file
is
not
None
and
includefmt
!=
""
:
if
option
s
.
includes_file
is
not
None
and
includefmt
!=
""
:
incfile
=
open
(
arg
s
.
includes_file
,
"w"
)
incfile
=
open
(
option
s
.
includes_file
,
"w"
)
incfile
.
write
(
includecmt
)
incfile
.
write
(
includecmt
)
# Temporary variable required to genrate aggregated content in 'js' format.
# Temporary variable required to genrate aggregated content in 'js' format.
js_data
=
[]
js_data
=
[]
for
module
in
sorted
(
os
.
listdir
(
arg
s
.
module_dir
)):
for
module
in
sorted
(
os
.
listdir
(
option
s
.
module_dir
)):
if
len
(
arg
s
.
module_list
):
if
len
(
option
s
.
module_list
):
if
not
module
in
arg
s
.
module_list
:
if
not
module
in
option
s
.
module_list
:
continue
continue
fname
=
os
.
path
.
join
(
arg
s
.
module_dir
,
module
)
fname
=
os
.
path
.
join
(
option
s
.
module_dir
,
module
)
extra
=
os
.
path
.
join
(
"inc"
,
"
%
s.tex"
%
module
)
extra
=
os
.
path
.
join
(
"inc"
,
"
%
s.tex"
%
module
)
if
fname
.
endswith
(
".swp"
):
if
fname
.
endswith
(
".swp"
):
...
@@ -327,7 +316,7 @@ def main():
...
@@ -327,7 +316,7 @@ def main():
print
" processing module source --->
%
s"
%
fname
print
" processing module source --->
%
s"
%
fname
if
arg
s
.
type
==
'js'
:
if
option
s
.
type
==
'js'
:
if
fname
.
endswith
(
".json"
):
if
fname
.
endswith
(
".json"
):
f
=
open
(
fname
)
f
=
open
(
fname
)
j
=
json
.
load
(
f
)
j
=
json
.
load
(
f
)
...
@@ -335,7 +324,7 @@ def main():
...
@@ -335,7 +324,7 @@ def main():
js_data
.
append
(
j
)
js_data
.
append
(
j
)
continue
continue
doc
=
get_docstring
(
fname
,
verbose
=
arg
s
.
verbose
)
doc
=
get_docstring
(
fname
,
verbose
=
option
s
.
verbose
)
if
doc
is
None
and
module
not
in
BLACKLIST_MODULES
:
if
doc
is
None
and
module
not
in
BLACKLIST_MODULES
:
sys
.
stderr
.
write
(
"*** ERROR: CORE MODULE MISSING DOCUMENTATION:
%
s ***
\n
"
%
module
)
sys
.
stderr
.
write
(
"*** ERROR: CORE MODULE MISSING DOCUMENTATION:
%
s ***
\n
"
%
module
)
...
@@ -346,34 +335,34 @@ def main():
...
@@ -346,34 +335,34 @@ def main():
doc
[
'filename'
]
=
fname
doc
[
'filename'
]
=
fname
doc
[
'docuri'
]
=
doc
[
'module'
]
.
replace
(
'_'
,
'-'
)
doc
[
'docuri'
]
=
doc
[
'module'
]
.
replace
(
'_'
,
'-'
)
doc
[
'now_date'
]
=
datetime
.
date
.
today
()
.
strftime
(
'
%
Y-
%
m-
%
d'
)
doc
[
'now_date'
]
=
datetime
.
date
.
today
()
.
strftime
(
'
%
Y-
%
m-
%
d'
)
doc
[
'ansible_version'
]
=
arg
s
.
ansible_version
doc
[
'ansible_version'
]
=
option
s
.
ansible_version
if
arg
s
.
includes_file
is
not
None
and
includefmt
!=
""
:
if
option
s
.
includes_file
is
not
None
and
includefmt
!=
""
:
incfile
.
write
(
includefmt
%
module
)
incfile
.
write
(
includefmt
%
module
)
if
arg
s
.
verbose
:
if
option
s
.
verbose
:
print
json
.
dumps
(
doc
,
indent
=
4
)
print
json
.
dumps
(
doc
,
indent
=
4
)
if
arg
s
.
type
==
'latex'
:
if
option
s
.
type
==
'latex'
:
if
os
.
path
.
exists
(
extra
):
if
os
.
path
.
exists
(
extra
):
f
=
open
(
extra
)
f
=
open
(
extra
)
extradata
=
f
.
read
()
extradata
=
f
.
read
()
f
.
close
()
f
.
close
()
doc
[
'extradata'
]
=
extradata
doc
[
'extradata'
]
=
extradata
if
arg
s
.
type
==
'json'
:
if
option
s
.
type
==
'json'
:
text
=
json
.
dumps
(
doc
,
indent
=
2
)
text
=
json
.
dumps
(
doc
,
indent
=
2
)
else
:
else
:
text
=
template
.
render
(
doc
)
text
=
template
.
render
(
doc
)
return_data
(
text
,
arg
s
,
outputname
,
module
)
return_data
(
text
,
option
s
,
outputname
,
module
)
if
arg
s
.
type
==
'js'
:
if
option
s
.
type
==
'js'
:
docs
=
{}
docs
=
{}
docs
[
'json'
]
=
json
.
dumps
(
js_data
,
indent
=
2
)
docs
[
'json'
]
=
json
.
dumps
(
js_data
,
indent
=
2
)
text
=
template
.
render
(
docs
)
text
=
template
.
render
(
docs
)
return_data
(
text
,
arg
s
,
outputname
,
'modules'
)
return_data
(
text
,
option
s
,
outputname
,
'modules'
)
#def boilerplate():
#def boilerplate():
#
#
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment