Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
papersurfer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jacobsohn, Johann
papersurfer
Commits
be7a7369
Commit
be7a7369
authored
4 years ago
by
Johann Jacobsohn
Browse files
Options
Downloads
Patches
Plain Diff
Add interactive configuration and use channel name instead of channel id
parent
39071dba
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
papersurfer.py
+118
-18
118 additions, 18 deletions
papersurfer.py
with
118 additions
and
18 deletions
papersurfer.py
+
118
−
18
View file @
be7a7369
...
...
@@ -14,15 +14,12 @@ from dataclasses import dataclass
import
re
from
functools
import
partial
import
json
import
time
import
requests
from
mattermostdriver
import
Driver
import
urwid
import
configargparse
URL
=
"
mattermost.cen.uni-hamburg.de
"
CHANNEL
=
"
n5myem9yc7fyzb9am7ym5o41ry
"
@dataclass
class
PostDTO
:
""""
Encapsulate Mattermost Posts.
"""
...
...
@@ -117,16 +114,43 @@ class Doi:
class
Mattermost
:
"""
Provide a simplified interaction w/ mattermost api.
"""
def
__init__
(
self
,
username
,
password
):
def
__init__
(
self
,
url
,
channelname
,
username
,
password
):
self
.
mattermost
=
Driver
({
'
url
'
:
URL
,
'
url
'
:
url
,
'
login_id
'
:
username
,
'
password
'
:
password
,
'
port
'
:
443
})
try
:
self
.
mattermost
.
login
()
except
:
print
(
"
Failed to log into Mattermost.
"
)
raise
ValueError
try
:
self
.
channel
=
self
.
get_channel
(
channelname
)
except
:
print
(
"
Couldn
'
t find Mattermost channel.
"
)
raise
ValueError
self
.
reporters
=
{}
def
get_channel
(
self
,
channelname
):
""""
Try to find the paper channel by display name.
"""
teams
=
[
team
[
"
id
"
]
for
team
in
self
.
mattermost
.
teams
.
get_user_teams
(
"
me
"
)]
channels
=
[]
for
team
in
teams
:
teamchannels
=
[
channel
for
channel
in
self
.
mattermost
.
channels
.
get_channels_for_user
(
"
me
"
,
team
)
if
channel
[
"
display_name
"
]
==
channelname
]
channels
.
extend
(
teamchannels
)
# lets just hope no-one has the same channel name in multiple teams
if
len
(
channels
)
==
0
:
print
(
f
"
Channel
{
channelname
}
does not exits
"
)
raise
ValueError
return
channels
[
0
][
"
id
"
]
def
get_reporter
(
self
,
id
):
"""
Load user from mattermost api and cache.
"""
if
id
not
in
self
.
reporters
:
...
...
@@ -136,7 +160,7 @@ class Mattermost:
def
retrieve_all_messages
(
self
):
"""
Retrieve all messages from mattermost, unfiltered for papers.
"""
posts
=
self
.
mattermost
.
posts
.
get_posts_for_channel
(
CHANNEL
)
posts
=
self
.
mattermost
.
posts
.
get_posts_for_channel
(
self
.
channel
)
return
[
PostDTO
(
id
=
m
[
'
id
'
],
message
=
m
[
'
message
'
],
...
...
@@ -173,7 +197,7 @@ class PrettyButton(urwid.Button):
class
Papersurfer
:
"""
Provide UI and interface with mattermost class.
"""
def
__init__
(
self
,
username
,
password
):
def
__init__
(
self
,
url
,
channel
,
username
,
password
):
self
.
_screen
=
urwid
.
raw_display
.
Screen
()
self
.
size
=
self
.
_screen
.
get_cols_rows
()
self
.
filter
=
""
...
...
@@ -193,7 +217,7 @@ class Papersurfer:
onclick
=
self
.
on_export_clicked
)
div
=
urwid
.
Divider
(
u
'
-
'
)
self
.
mtm
=
Mattermost
(
username
,
password
)
self
.
mtm
=
Mattermost
(
url
,
channel
,
username
,
password
)
body
=
[
urwid
.
Text
(
""
)]
self
.
listcontent
=
urwid
.
SimpleFocusListWalker
(
body
)
...
...
@@ -370,24 +394,100 @@ class Papersurfer:
def
close_details
(
self
,
_
):
self
.
mainloop
.
widget
=
self
.
top
def
get_config_file_paths
():
"""
Find, load and parse a config file.
The first config file that is found is used, it is searched for (in
this order), at:
- config, if set (e.g. from the cli)
- from the default source path (./configurations/gascamcontrol.conf)
- home path
- XDG_CONFIG_HOME/gascamcontrol/gascamcontrol.conf (linux only)
- system path
- XDG_CONFIG_DIRS/gascamcontrol/gascamcontrol.conf (linux only)
>>>
c
=
Conf
()
>>>
type
(
c
.
get_config_file_paths
())
<
class
'
list
'
>
"""
import os
env = os.environ
xdg_home = None
if
'
XDG_CONFIG_HOME
'
in env:
xdg_home = env[
'
XDG_CONFIG_HOME
'
]
elif
'
HOME
'
in env:
xdg_home = env[
'
HOME
'
] +
'
/
.
config
'
xdg_config_dirs = None
if
'
XDG_CONFIG_DIRS
'
in env:
xdg_config_dirs = env[
'
XDG_CONFIG_DIRS
'
].split(
'
:
'
)
elif
'
HOME
'
in env:
xdg_config_dirs = [
'
/
etc
/
xdg
'
]
default_filename =
"
papersurfer.conf
"
default_path =
"
./
"
paths = [default_path, xdg_home]
paths.extend(xdg_config_dirs)
return [os.path.join(p, default_filename) for p in paths if p]
def interactive_configuration():
url = input(
"
Mattermost URL (eg. mattermost.example.net):
"
)
channel = input(
"
Channel (eg. Paper Club):
"
)
username = input(
"
Username (eg. JohnDoe):
"
)
password = input(
"
Password (eg. SuperSecret1):
"
)
return url, channel, username, password
def parse_args():
"""
Parse command line arguments and config file.
"""
parser = configargparse.ArgParser()
parser._default_config_files = get_config_file_paths()
parser.add(
"
-w
"
,
"
--write-out-config-file
"
,
help=
"
takes the current command line args and writes them out
"
"
to a config file at the given path
"
,
is_write_out_config_file_arg=True)
parser.add(
'
-
c
'
,
'
--
my
-
config
'
, required=False, is_config_file=True,
help
=
'
config file path
'
,
default
=
'
papersurfer.conf
'
)
parser
.
add
(
'
-
u
'
,
'
--username
'
,
required
=
Tru
e
,
help
=
'
Mattermost u
sername
'
,
default
=
"
USERNAME
"
)
parser
.
add
(
'
-
p
'
,
'
--
password
'
,
required
=
Tru
e
,
help
=
'
Mattermost
password
'
,
default
=
"
PASSWORD
"
)
help=
'
config
file
path
'
)
parser.add(
'
-
-
url
'
, required=
Fals
e, help=
'
Mattermost
u
rl
'
)
parser.add(
'
--
channel
'
, required=False, help=
'
Mattermost
channel
'
)
parser.add(
'
-
u
'
,
'
--
username
'
, required=
Fals
e, help=
'
Mattermost
username
'
)
parser.add(
'
-
p
'
,
'
--
password
'
, required=False, help=
'
Mattermost
password
'
)
options = parser.parse_args()
return
options
.
username
,
options
.
password
if not options.url:
start_interactive = input(
"
Could not load config file or read command line arguments, do you
"
"
wish to start the interactive configuration assistant? (y/n)
"
)
if start_interactive ==
"
y
"
:
url, channel, username, password = interactive_configuration()
try:
Mattermost(url, channel, username, password)
except:
print(
"
Failed to validate configuration, exiting.
"
)
exit(1)
options.url = url
options.channel = channel
options.username = username
options.password = password
configfile =
"
papersurfer.conf
"
with open(configfile,
"
w
"
) as file:
file.write(f
"
url = {url}
\n
"
)
file.write(f
"
channel = {channel}
\n
"
)
file.write(f
"
username = {username}
\n
"
)
file.write(f
"
password = {password}
\n
"
)
print(f
"
Configfile {configfile} written.
"
)
time.sleep(2)
else:
parser.print_help()
exit(0)
return options.url, options.channel, options.username, options.password
if __name__ ==
"
__main__
"
:
USERNAME
,
PASSWORD
=
parse_args
()
Papersurfer
(
USERNAME
,
PASSWORD
)
URL, CHANNEL,
USERNAME, PASSWORD = parse_args()
Papersurfer(
URL, CHANNEL,
USERNAME, PASSWORD)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment