Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F66474433
class_dumper_classes.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Jun 10, 18:44
Size
4 KB
Mime Type
text/x-python
Expires
Wed, Jun 12, 18:44 (2 d)
Engine
blob
Format
Raw Data
Handle
18227673
Attached To
R1692 code_skeleton
class_dumper_classes.py
View Options
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################
import
argparse
from
class_dumper
import
ClassDumper
################################################################
class
ClassDumperClasses
(
ClassDumper
):
" Class emplyed to output to classes text format "
def
__init__
(
self
,
output_file
):
ClassDumper
.
__init__
(
self
,
output_file
)
self
.
output_file
=
output_file
self
.
nb_tabulation
=
0
def
dump_class
(
self
,
_class
,
_file
):
" dumps a class into the provided file "
sstr
=
self
.
format_class_declaration
(
_class
)
self
.
_inc_tabulation
()
sstr
+=
self
.
format_constructors
(
_class
)
sstr
+=
self
.
format_methods
(
_class
)
sstr
+=
self
.
format_members
(
_class
)
sstr
+=
"
\n
"
self
.
_dec_tabulation
()
_file
.
write
(
sstr
)
@classmethod
def
format_class_declaration
(
cls
,
_class
):
" forge a str representing the class "
sstr
=
"class "
+
_class
.
name
if
_class
.
inheritance
is
not
None
:
sstr
+=
"("
+
", "
.
join
(
_class
.
inheritance
)
+
")"
sstr
+=
"
\n
"
return
sstr
def
format_constructors
(
self
,
_class
):
" forge a str representing the constructor "
sstr
=
""
for
encaps
in
[
'public'
,
'private'
,
'protected'
]:
meths
=
_class
.
getMethods
(
encaps
)
if
_class
.
name
in
meths
:
for
meth
in
meths
[
_class
.
name
]:
sstr
+=
self
.
_format_method
(
_class
,
meth
)
for
encaps
in
[
'public'
,
'private'
,
'protected'
]:
meths
=
_class
.
getMethods
(
encaps
)
if
'~'
+
_class
.
name
in
meths
:
for
meth
in
meths
[
'~'
+
_class
.
name
]:
sstr
+=
self
.
_format_method
(
_class
,
meth
)
return
sstr
def
format_methods
(
self
,
_class
):
" forge a str representing methods "
sstr
=
""
for
encaps
in
[
'public'
,
'private'
,
'protected'
]:
meths
=
_class
.
getMethods
(
encaps
)
meths_names
=
set
(
meths
.
keys
())
-
set
([
_class
.
name
,
'~'
+
_class
.
name
])
meths_names
=
list
(
meths_names
)
if
len
(
meths_names
)
is
not
0
:
for
_name
in
meths_names
:
for
meth
in
meths
[
_name
]:
sstr
+=
self
.
_format_method
(
_class
,
meth
)
return
sstr
def
format_members
(
self
,
_class
):
" forge a str representing members "
sstr
=
""
for
encaps
in
[
'public'
,
'private'
,
'protected'
]:
membs
=
_class
.
getMembers
(
encaps
)
if
len
(
membs
)
is
not
0
:
for
dummy_n
,
memb
in
membs
.
iteritems
():
sstr
+=
self
.
_format_member
(
_class
,
memb
)
sstr
+=
"
\n
"
return
sstr
def
_format_method
(
self
,
dummy_class
,
meth
):
sstr
=
self
.
_get_tabulation
()
sstr
+=
meth
.
encapsulation
+
" "
if
meth
.
static
:
sstr
+=
meth
.
static
+
" "
if
meth
.
virtual
:
sstr
+=
meth
.
virtual
+
" "
if
meth
.
ret
:
sstr
+=
meth
.
ret
+
" "
sstr
+=
meth
.
name
+
"("
sstr
+=
", "
.
join
([
a
+
" "
+
b
for
b
,
a
in
list
(
meth
.
args
.
iteritems
())])
sstr
+=
")"
if
meth
.
const
:
sstr
+=
" const"
sstr
+=
";
\n
"
self
.
_inc_tabulation
()
self
.
_dec_tabulation
()
return
sstr
def
_format_member
(
self
,
dummy_class
,
memb
):
sstr
=
self
.
_get_tabulation
()
sstr
+=
memb
.
encapsulation
+
" "
if
memb
.
static
==
'static'
:
sstr
+=
memb
.
static
sstr
+=
memb
.
type
+
" "
+
memb
.
name
+
';'
return
sstr
def
_get_tabulation
(
self
):
return
" "
*
self
.
nb_tabulation
def
_inc_tabulation
(
self
):
self
.
nb_tabulation
+=
1
def
_dec_tabulation
(
self
):
self
.
nb_tabulation
-=
1
################################################################
def
main
():
parser
=
argparse
.
ArgumentParser
(
description
=
'Classes descriptor format producer for class representation'
)
parser
.
add_argument
(
'--class_file'
,
'-c'
,
help
=
'The class file to process'
,
required
=
True
)
parser
.
add_argument
(
'--output_file'
,
'-o'
,
help
=
'The file where to put the classes description'
,
required
=
True
)
args
=
parser
.
parse_args
()
args
=
vars
(
args
)
dumper_class
=
ClassDumperClasses
(
args
[
'output_file'
])
dumper_class
.
dump
(
class_file
=
args
[
'class_file'
])
if
__name__
==
'__main__'
:
main
()
Event Timeline
Log In to Comment