Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
gargantext
Project
Project
Details
Activity
Releases
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
humanities
gargantext
Commits
85e2d95a
Commit
85e2d95a
authored
Jun 30, 2017
by
sim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a script to show nodes
parent
d902d8ac
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
109 additions
and
0 deletions
+109
-0
show_nodes.py
gargantext/tools/show_nodes.py
+109
-0
No files found.
gargantext/tools/show_nodes.py
0 → 100644
View file @
85e2d95a
# Make this a standalone script...
# Can be called this way: python3 gargantext/tools/show_nodes.py
import
sys
import
os
import
django
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
+
'/../..'
))
os
.
environ
.
setdefault
(
'DJANGO_SETTINGS_MODULE'
,
'gargantext.settings'
)
django
.
setup
()
# ...End of jiberish.
import
itertools
import
colorama
from
colorama
import
Fore
from
sqlalchemy.sql.expression
import
literal_column
from
gargantext.util.db
import
session
,
func
,
aliased
from
gargantext.models
import
Node
NODE_BULLET
=
'‣'
# https://en.wikipedia.org/wiki/Box-drawing_character
TREE_ROOT
=
'╾'
TREE_VERT
=
'│'
TREE_HORI
=
'─'
TREE_FORK
=
'├'
TREE_CORN
=
'└'
FIRST
=
0x01
LAST
=
0x02
def
nodes
(
parent
=
None
,
group_by
=
'typename'
,
order_by
=
'typename'
,
has_child
=
'check'
):
if
group_by
or
has_child
is
not
None
:
select
=
[
func
.
min
(
Node
.
id
)
.
label
(
'id'
),
func
.
min
(
Node
.
name
)
.
label
(
'name'
),
func
.
min
(
Node
.
typename
)
.
label
(
'typename'
),
func
.
count
(
Node
.
id
)
.
label
(
'cnt'
)]
else
:
select
=
[
Node
.
id
.
label
(
'id'
),
Node
.
name
.
label
(
'name'
),
Node
.
typename
.
label
(
'typename'
),
literal_column
(
'1'
)
.
label
(
'cnt'
)]
if
has_child
is
not
None
:
N
=
aliased
(
Node
)
select
.
append
(
func
.
count
(
N
.
id
)
.
label
(
'children'
))
else
:
select
.
append
(
literal_column
(
'NULL'
)
.
label
(
'children'
))
parent_id
=
getattr
(
parent
,
'id'
,
parent
)
q
=
session
.
query
(
*
select
)
.
filter_by
(
parent_id
=
parent_id
)
\
.
group_by
(
getattr
(
Node
,
group_by
if
group_by
else
'id'
))
if
has_child
is
not
None
:
q
=
q
.
outerjoin
(
N
,
N
.
parent_id
==
Node
.
id
)
.
group_by
(
N
.
parent_id
)
return
q
.
order_by
(
order_by
)
def
node_show
(
node
,
prefix
=
''
,
maxlen
=
60
):
if
node
.
children
>
0
or
node
.
cnt
==
1
:
name
=
node
.
name
[:
maxlen
]
+
'..'
if
len
(
node
.
name
)
>
maxlen
else
node
.
name
label
=
Fore
.
CYAN
+
name
+
Fore
.
RESET
else
:
label
=
Fore
.
MAGENTA
+
str
(
node
.
cnt
)
+
Fore
.
RESET
print
(
prefix
,
'
%
s
%
s
%
s'
%
(
Fore
.
GREEN
,
node
.
typename
,
label
),
sep
=
''
)
def
tree_show
(
node
,
pos
=
FIRST
|
LAST
,
level
=
0
,
prefix
=
''
,
maxlen
=
60
,
compact
=
True
):
#print('%02d %x' % (level, pos), end='')
branch
=
TREE_ROOT
if
pos
&
FIRST
and
level
==
0
else
TREE_FORK
if
not
pos
&
LAST
else
TREE_CORN
node_prefix
=
prefix
+
branch
+
2
*
TREE_HORI
+
' '
node_show
(
node
,
node_prefix
,
maxlen
)
childs
=
iter
(
nodes
(
parent
=
node
,
group_by
=
compact
and
'typename'
))
try
:
node
=
next
(
childs
)
except
StopIteration
:
return
prefix
=
prefix
+
(
' '
if
pos
&
LAST
else
TREE_VERT
)
+
' '
for
i
,
next_node
in
enumerate
(
itertools
.
chain
(
childs
,
[
None
])):
pos
=
(
FIRST
if
i
==
0
else
0
)
|
(
LAST
if
next_node
is
None
else
0
)
tree_show
(
node
,
pos
,
level
+
1
,
prefix
,
maxlen
,
compact
)
node
=
next_node
if
__name__
==
"__main__"
:
import
sys
if
len
(
sys
.
argv
)
==
1
:
compact
=
True
elif
len
(
sys
.
argv
)
==
2
and
sys
.
argv
[
1
]
in
(
'-a'
,
'--all'
):
compact
=
False
else
:
print
(
"Usage:
%
s [-a|--all]"
%
sys
.
argv
[
0
],
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
colorama
.
init
(
strip
=
False
)
for
root
in
nodes
():
tree_show
(
root
,
compact
=
compact
)
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