Skip to content

Commit

Permalink
Move config into its own Config() object
Browse files Browse the repository at this point in the history
  • Loading branch information
volker-fr committed Jan 18, 2025
1 parent 4011a97 commit 408e60d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
15 changes: 8 additions & 7 deletions slackviewer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

from datetime import datetime

from jinja2 import Environment, PackageLoader
from slackviewer.config import Config
from slackviewer.constants import SLACKVIEWER_TEMP_PATH
from slackviewer.utils.click import envvar, flag_ennvar
from slackviewer.reader import Reader
from jinja2 import Environment, PackageLoader
from slackviewer.utils.click import envvar, flag_ennvar


@click.group()
Expand Down Expand Up @@ -40,13 +41,13 @@ def clean(wet):
@click.option("--template", default=None, type=click.File('r'), help="Custom single file export template")
@click.argument('archive')
def export(**kwargs):
config = kwargs
config = Config(kwargs)

css = pkgutil.get_data('slackviewer', 'static/viewer.css').decode('utf-8')

tmpl = Environment(loader=PackageLoader('slackviewer')).get_template("export_single.html")
if config["template"]:
tmpl = Environment(loader=PackageLoader('slackviewer')).from_string(config["template"].read())
if config.template:
tmpl = Environment(loader=PackageLoader('slackviewer')).from_string(config.template.read())
r = Reader(config)
channel_list = sorted(
[{"channel_name": k, "messages": v} for (k, v) in r.compile_channels().items()],
Expand All @@ -55,7 +56,7 @@ def export(**kwargs):

dm_list = []
mpims = []
if config["show_dms"]:
if config.show_dms:
#
# Direct DMs
dm_list = r.compile_dm_messages()
Expand Down Expand Up @@ -86,7 +87,7 @@ def export(**kwargs):
css=css,
generated_on=datetime.now(),
workspace_name=r.slack_name(),
source_file=os.path.basename(config["archive"]),
source_file=os.path.basename(config.archive),
channels=channel_list,
dms=dm_list,
mpims=mpims,
Expand Down
36 changes: 18 additions & 18 deletions slackviewer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import flask

from slackviewer.app import app
from slackviewer.reader import Reader
from slackviewer.config import Config
from slackviewer.freezer import CustomFreezer
from slackviewer.reader import Reader
from slackviewer.utils.click import envvar, flag_ennvar


def configure_app(app, config):
app.debug = config["debug"]
app.no_sidebar = config["no_sidebar"]
app.no_external_references = config["no_external_references"]
app.debug = config.debug
app.no_sidebar = config.no_sidebar
app.no_external_references = config.no_external_references
if app.debug:
print("WARNING: DEBUG MODE IS ENABLED!")
app.config["PROPAGATE_EXCEPTIONS"] = True
Expand All @@ -22,13 +23,13 @@ def configure_app(app, config):

top = flask._app_ctx_stack
top.path = reader.archive_path()
top.channels = reader.compile_channels(config["channels"])
top.channels = reader.compile_channels(config.channels)
top.groups = reader.compile_groups()
top.dms = {}
top.dm_users = []
top.mpims = {}
top.mpim_users = []
if not config["skip_dms"]:
if not config.skip_dms:
top.dms = reader.compile_dm_messages()
top.dm_users = reader.compile_dm_users()
top.mpims = reader.compile_mpim_messages()
Expand Down Expand Up @@ -74,20 +75,19 @@ def configure_app(app, config):
@click.option('--skip-dms', is_flag=True, default=False, help="Hide direct messages")
@click.option('--skip-channel-member-change', is_flag=True, default=False, envvar='SKIP_CHANNEL_MEMBER_CHANGE', help="Hide channel join/leave messages")
def main(**kwargs):
config = kwargs
if not config["archive"]:

config = Config(kwargs)
if not config.archive:
raise ValueError("Empty path provided for archive")

configure_app(app, config)

if config["html_only"]:
if config.html_only:
# We need relative URLs, otherwise channel refs do not work
app.config["FREEZER_RELATIVE_URLS"] = True

# Custom subclass of Freezer allows overwriting the output directory
freezer = CustomFreezer(app)
freezer.cf_output_dir = config["output_dir"]
freezer.cf_output_dir = config.output_dir

# This tells freezer about the channel URLs
@freezer.register_generator
Expand All @@ -97,14 +97,14 @@ def channel_name():

freezer.freeze()

if not config["no_browser"]:
if not config.no_browser:
webbrowser.open("file:///{}/index.html"
.format(os.path.abspath(config["output_dir"])))
.format(os.path.abspath(config.output_dir)))

elif not config["test"]:
if not config["no_browser"]:
webbrowser.open("http://{}:{}".format(config["ip"], config["port"]))
elif not config.test:
if not config.no_browser:
webbrowser.open("http://{}:{}".format(config.ip, config.port))
app.run(
host=config["ip"],
port=config["port"]
host=config.ip,
port=config.port
)
6 changes: 3 additions & 3 deletions slackviewer/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Reader(object):

def __init__(self, config):
self._config = config
self._PATH = extract_archive(config["archive"])
self._since = config["since"]
self._PATH = extract_archive(config.archive)
self._since = config.since
# slack name that is in the url https://<slackname>.slack.com
self._slack_name = self._get_slack_name()
# TODO: Make sure this works
Expand Down Expand Up @@ -248,7 +248,7 @@ def _build_threads(self, channel_data):

for location, message in enumerate(channel_data[channel_name]):
# remove "<user> joined/left <channel>" message
if self._config['skip_channel_member_change'] and message._message.get('subtype') in ['channel_join', 'channel_leave']:
if self._config.skip_channel_member_change and message._message.get('subtype') in ['channel_join', 'channel_leave']:
items_to_remove.append(location)
continue

Expand Down

0 comments on commit 408e60d

Please sign in to comment.