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
2786149b
Commit
2786149b
authored
Oct 11, 2012
by
Dag Wieers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moving now from getopt to optparse
Let me know if we can move to another library instead ;-)
parent
d6b3f40c
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 @
2786149b
...
...
@@ -25,7 +25,7 @@ import json
import
ast
from
jinja2
import
Environment
,
FileSystemLoader
import
re
import
getopt
import
optparse
import
time
import
datetime
import
subprocess
...
...
@@ -169,9 +169,9 @@ def get_docstring(filename, verbose=False):
return
doc
def
return_data
(
text
,
arg
s
,
outputname
,
module
):
if
arg
s
.
output_dir
is
not
None
:
f
=
open
(
os
.
path
.
join
(
arg
s
.
output_dir
,
outputname
%
module
),
'w'
)
def
return_data
(
text
,
option
s
,
outputname
,
module
):
if
option
s
.
output_dir
is
not
None
:
f
=
open
(
os
.
path
.
join
(
option
s
.
output_dir
,
outputname
%
module
),
'w'
)
f
.
write
(
text
)
f
.
close
()
else
:
...
...
@@ -179,91 +179,80 @@ def return_data(text, args, outputname, module):
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
:
opts
,
arguments
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
'A:M:T:t:m:vo:I:GVh'
,
[
'ansible-version='
,
'module-dir='
,
'template-dir='
,
'type='
,
'module='
,
'verbose'
,
'output-dir='
,
'includes-file='
,
'generate'
,
'version'
,
'help'
,
])
except
getopt
.
error
,
e
:
print
>>
sys
.
stderr
,
'ERROR:
%
s'
%
str
(
e
)
sys
.
exit
(
1
)
for
opt
,
arg
in
opts
:
if
opt
in
(
'-A'
,
'--ansible-version'
):
args
.
ansible_version
=
arg
elif
opt
in
(
'-M'
,
'--module-dir'
):
args
.
module_dir
=
arg
elif
opt
in
(
'-T'
,
'--template-dir'
):
args
.
template_dir
=
arg
elif
opt
in
(
'-t'
,
'--type'
):
args
.
type
=
arg
if
args
.
type
not
in
type_choices
:
print
>>
sys
.
stderr
,
'ERROR: Type
%
s not in possible types
%
s.'
%
(
args
.
type
,
type_choices
)
sys
.
exit
(
1
)
elif
opt
in
(
'-m'
,
'--module'
):
args
.
module_list
.
append
(
arg
)
elif
opt
in
(
'-v'
,
'--verbose'
):
args
.
verbose
=
True
elif
opt
in
(
'-o'
,
'--output-dir'
):
args
.
output_dir
=
arg
elif
opt
in
(
'-I'
,
'--includes-file'
):
args
.
includes_file
=
arg
elif
opt
in
(
'-G'
,
'--generate'
):
args
.
do_boilerplate
=
True
elif
opt
in
(
'-V'
,
'--version'
):
print
>>
sys
.
stderr
,
'
%(prog)
s 1.0'
elif
opt
in
(
'-h'
,
'--help'
):
print
>>
sys
.
stderr
,
'''Convert Ansible module DOCUMENTATION strings to other formats
-A, --ansible-version= Ansible version number
-M, --module-dir= Ansible modules/ directory
-T, --template-dir= Directory containing Jinja2 templates
-t, --type= Output type
-m, --module= Add modules to process in module_dir
-v, --verbose Verbose
-o, --output-dir= Output directory for module files
-I, --includes-file= Create a file containing list of processed modules
-G, --generate Generate boilerplate DOCUMENTATION to stdout
'''
sys
.
exit
(
0
)
else
:
print
>>
sys
.
stderr
,
'ERROR: Option
%
s unknown to getopt'
%
opt
sys
.
exit
(
1
)
# print "M: %s" % args.module_dir
# print "t: %s" % args.type
# print "m: %s" % args.module_list
# print "v: %s" % args.verbose
if
args
.
do_boilerplate
:
p
=
optparse
.
OptionParser
(
version
=
'
%
prog 1.0'
,
usage
=
'usage:
%
prog [options] arg1 arg2'
,
description
=
'Convert Ansible module DOCUMENTATION strings to other formats'
,
)
p
.
add_option
(
"-A"
,
"--ansible-version"
,
action
=
"store"
,
dest
=
"ansible_version"
,
default
=
"unknown"
,
help
=
"Ansible version number"
)
p
.
add_option
(
"-M"
,
"--module-dir"
,
action
=
"store"
,
dest
=
"module_dir"
,
default
=
MODULEDIR
,
help
=
"Ansible modules/ directory"
)
p
.
add_option
(
"-T"
,
"--template-dir"
,
action
=
"store"
,
dest
=
"template_dir"
,
default
=
"hacking/templates"
,
help
=
"directory containing Jinja2 templates"
)
p
.
add_option
(
"-t"
,
"--type"
,
action
=
'store'
,
dest
=
'type'
,
choices
=
[
'html'
,
'latex'
,
'man'
,
'rst'
,
'json'
],
default
=
'latex'
,
help
=
"Output type"
)
p
.
add_option
(
"-m"
,
"--module"
,
action
=
'append'
,
default
=
[],
dest
=
'module_list'
,
help
=
"Add modules to process in module_dir"
)
p
.
add_option
(
"-v"
,
"--verbose"
,
action
=
'store_true'
,
default
=
False
,
help
=
"Verbose"
)
p
.
add_option
(
"-o"
,
"--output-dir"
,
action
=
"store"
,
dest
=
"output_dir"
,
default
=
None
,
help
=
"Output directory for module files"
)
p
.
add_option
(
"-I"
,
"--includes-file"
,
action
=
"store"
,
dest
=
"includes_file"
,
default
=
None
,
help
=
"Create a file containing list of processed modules"
)
p
.
add_option
(
"-G"
,
"--generate"
,
action
=
"store_true"
,
dest
=
"do_boilerplate"
,
default
=
False
,
help
=
"generate boilerplate DOCUMENTATION to stdout"
)
p
.
add_option
(
'-V'
,
action
=
'version'
)
(
options
,
args
)
=
p
.
parse_args
()
# print "M: %s" % options.module_dir
# print "t: %s" % options.type
# print "m: %s" % options.module_list
# print "v: %s" % options.verbose
if
options
.
do_boilerplate
:
boilerplate
()
sys
.
exit
(
0
)
if
not
arg
s
.
module_dir
:
if
not
option
s
.
module_dir
:
print
"Need module_dir"
sys
.
exit
(
1
)
if
not
arg
s
.
template_dir
:
if
not
option
s
.
template_dir
:
print
"Need template_dir"
sys
.
exit
(
1
)
env
=
Environment
(
loader
=
FileSystemLoader
(
arg
s
.
template_dir
),
env
=
Environment
(
loader
=
FileSystemLoader
(
option
s
.
template_dir
),
variable_start_string
=
"@{"
,
variable_end_string
=
"}@"
,
trim_blocks
=
True
,
...
...
@@ -271,25 +260,25 @@ def main():
env
.
globals
[
'xline'
]
=
rst_xline
if
arg
s
.
type
==
'latex'
:
if
option
s
.
type
==
'latex'
:
env
.
filters
[
'jpfunc'
]
=
latex_ify
template
=
env
.
get_template
(
'latex.j2'
)
outputname
=
"
%
s.tex"
includecmt
=
"
%
generated code
\n
"
includefmt
=
"
\\
input
%
s
\n
"
if
arg
s
.
type
==
'html'
:
if
option
s
.
type
==
'html'
:
env
.
filters
[
'jpfunc'
]
=
html_ify
template
=
env
.
get_template
(
'html.j2'
)
outputname
=
"
%
s.html"
includecmt
=
""
includefmt
=
""
if
arg
s
.
type
==
'man'
:
if
option
s
.
type
==
'man'
:
env
.
filters
[
'jpfunc'
]
=
man_ify
template
=
env
.
get_template
(
'man.j2'
)
outputname
=
"ansible.
%
s.3"
includecmt
=
""
includefmt
=
""
if
arg
s
.
type
==
'rst'
:
if
option
s
.
type
==
'rst'
:
env
.
filters
[
'jpfunc'
]
=
rst_ify
env
.
filters
[
'html_ify'
]
=
html_ify
env
.
filters
[
'fmt'
]
=
rst_fmt
...
...
@@ -298,28 +287,28 @@ def main():
outputname
=
"
%
s.rst"
includecmt
=
".. Generated by module_formatter
\n
"
includefmt
=
".. include:: modules/
%
s.rst
\n
"
if
arg
s
.
type
==
'json'
:
if
option
s
.
type
==
'json'
:
env
.
filters
[
'jpfunc'
]
=
json_ify
outputname
=
"
%
s.json"
includecmt
=
""
includefmt
=
""
if
arg
s
.
type
==
'js'
:
if
option
s
.
type
==
'js'
:
env
.
filters
[
'jpfunc'
]
=
js_ify
template
=
env
.
get_template
(
'js.j2'
)
outputname
=
"
%
s.js"
if
arg
s
.
includes_file
is
not
None
and
includefmt
!=
""
:
incfile
=
open
(
arg
s
.
includes_file
,
"w"
)
if
option
s
.
includes_file
is
not
None
and
includefmt
!=
""
:
incfile
=
open
(
option
s
.
includes_file
,
"w"
)
incfile
.
write
(
includecmt
)
# Temporary variable required to genrate aggregated content in 'js' format.
js_data
=
[]
for
module
in
sorted
(
os
.
listdir
(
arg
s
.
module_dir
)):
if
len
(
arg
s
.
module_list
):
if
not
module
in
arg
s
.
module_list
:
for
module
in
sorted
(
os
.
listdir
(
option
s
.
module_dir
)):
if
len
(
option
s
.
module_list
):
if
not
module
in
option
s
.
module_list
:
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
)
if
fname
.
endswith
(
".swp"
):
...
...
@@ -327,7 +316,7 @@ def main():
print
" processing module source --->
%
s"
%
fname
if
arg
s
.
type
==
'js'
:
if
option
s
.
type
==
'js'
:
if
fname
.
endswith
(
".json"
):
f
=
open
(
fname
)
j
=
json
.
load
(
f
)
...
...
@@ -335,7 +324,7 @@ def main():
js_data
.
append
(
j
)
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
:
sys
.
stderr
.
write
(
"*** ERROR: CORE MODULE MISSING DOCUMENTATION:
%
s ***
\n
"
%
module
)
...
...
@@ -346,34 +335,34 @@ def main():
doc
[
'filename'
]
=
fname
doc
[
'docuri'
]
=
doc
[
'module'
]
.
replace
(
'_'
,
'-'
)
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
)
if
arg
s
.
verbose
:
if
option
s
.
verbose
:
print
json
.
dumps
(
doc
,
indent
=
4
)
if
arg
s
.
type
==
'latex'
:
if
option
s
.
type
==
'latex'
:
if
os
.
path
.
exists
(
extra
):
f
=
open
(
extra
)
extradata
=
f
.
read
()
f
.
close
()
doc
[
'extradata'
]
=
extradata
if
arg
s
.
type
==
'json'
:
if
option
s
.
type
==
'json'
:
text
=
json
.
dumps
(
doc
,
indent
=
2
)
else
:
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
[
'json'
]
=
json
.
dumps
(
js_data
,
indent
=
2
)
text
=
template
.
render
(
docs
)
return_data
(
text
,
arg
s
,
outputname
,
'modules'
)
return_data
(
text
,
option
s
,
outputname
,
'modules'
)
#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