Skip to content

Commit

Permalink
Add CLI, add version to hash for invalidation in case of new features (
Browse files Browse the repository at this point in the history
…#54)

* Add CLI, add version to hash for invalidation in case of new features

* Pay attention to py2
  • Loading branch information
hfaran authored Mar 21, 2018
1 parent 9843ed6 commit c81730f
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 30 deletions.
6 changes: 6 additions & 0 deletions cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python

from slackviewer.cli import cli

if __name__ == '__main__':
cli()
29 changes: 20 additions & 9 deletions slackviewer/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import os
import zipfile
import glob
import tempfile
import io

import slackviewer
from slackviewer.constants import SLACKVIEWER_TEMP_PATH
from slackviewer.message import Message
from slackviewer.utils.six import to_unicode, to_bytes


def get_channel_list(path):
Expand Down Expand Up @@ -162,9 +164,16 @@ def get_mpims(path):
return {}


def SHA1_file(filepath):
def SHA1_file(filepath, extra=""):
"""Returns hex digest of SHA1 hash of file at filepath
:param str filepath: File to hash
:param bytes extra: Extra content added to raw read of file before taking hash
:return: hex digest of hash
:rtype: str
"""
with io.open(filepath, 'rb') as f:
return hashlib.sha1(f.read()).hexdigest()
return hashlib.sha1(f.read() + extra).hexdigest()


def extract_archive(filepath):
Expand All @@ -176,8 +185,13 @@ def extract_archive(filepath):
# Misuse of TypeError? :P
raise TypeError("{} is not a zipfile".format(filepath))

archive_sha = SHA1_file(filepath)
extracted_path = os.path.join(tempfile.gettempdir(), "_slackviewer", archive_sha)
archive_sha = SHA1_file(
filepath=filepath,
# Add version of slackviewer to hash as well so we can invalidate the cached copy
# if there are new features added
extra=to_bytes(slackviewer.__version__)
)
extracted_path = os.path.join(SLACKVIEWER_TEMP_PATH, archive_sha)
if os.path.exists(extracted_path):
print("{} already exists".format(extracted_path))
else:
Expand All @@ -201,10 +215,7 @@ def extract_archive(filepath):
), 'w+', encoding="utf-8"
) as f:
s = json.dumps(archive_info, ensure_ascii=False)
try:
s = unicode(s) # py2
except NameError:
pass # py3
s = to_unicode(s)
f.write(s)

return extracted_path
Expand Down
22 changes: 22 additions & 0 deletions slackviewer/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import click
import shutil

from slackviewer.constants import SLACKVIEWER_TEMP_PATH
from slackviewer.utils.click import envvar, flag_ennvar


@click.group()
def cli():
pass


@cli.command(help="Cleans up any temporary files (including cached output by slack-export-viewer)")
@click.option("--wet", "-w", is_flag=True,
default=flag_ennvar("SEV_CLEAN_WET"),
help="Actually performs file deletion")
def clean(wet):
if wet:
print("Removing {}...".format(SLACKVIEWER_TEMP_PATH))
shutil.rmtree(SLACKVIEWER_TEMP_PATH)
else:
print("Run with -w to remove {}".format(SLACKVIEWER_TEMP_PATH))
4 changes: 4 additions & 0 deletions slackviewer/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import tempfile
import os

SLACKVIEWER_TEMP_PATH = os.path.join(tempfile.gettempdir(), "_slackviewer")
15 changes: 1 addition & 14 deletions slackviewer/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import webbrowser

import click
Expand All @@ -19,19 +18,7 @@
compile_dm_users, \
compile_mpims, \
compile_mpim_users


def envvar(name, default):
"""Create callable environment variable getter
:param str name: Name of environment variable
:param default: Default value to return in case it isn't defined
"""
return lambda: os.environ.get(name, default)


def flag_ennvar(name):
return os.environ.get(name) == '1'
from slackviewer.utils.click import envvar, flag_ennvar


def configure_app(app, archive, debug):
Expand Down
18 changes: 11 additions & 7 deletions slackviewer/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,22 @@ def msg(self):
message.append(text)

file_link = self._message.get("file", {})
# we would like to show file if it is image type
if file_link and "url_private" in file_link and "mimetype" in file_link \
and file_link["mimetype"].split('/')[0] == 'image':
# We would like to show file if it is image type
if (
file_link and
"url_private" in file_link and
"mimetype" in file_link and
file_link["mimetype"].split('/')[0] == 'image'
):
html = "<br><a href=\"{url}\"><img src=\"{url}\" height=\"200\"></a><br>" \
.format(url=file_link["url_private"])
.format(url=file_link["url_private"])
message.append(html)

if message:
if not message[0].strip():
message = message[1:]
return "<br />".join(message).strip()


@property
def img(self):
try:
Expand Down Expand Up @@ -158,7 +161,8 @@ def _render_text(self, message):
message = emoji.emojize(message, use_aliases=True)

# Adding <pre> tag for preformated code
message = re.sub(r"```(.*?)```",r'<pre style="background-color: #E6E5DF; white-space: pre-wrap;">\1</pre>', message)
message = re.sub(r"```(.*?)```", r'<pre style="background-color: #E6E5DF; white-space: pre-wrap;">\1</pre>',
message)

return message

Expand All @@ -174,7 +178,7 @@ def _sub_mention(self, matchobj):
)
except KeyError:
# In case this identifier is not in __USER_DATA, we fallback to identifier
return matchobj.group(0)[2:-1]
return matchobj.group(0)[2:-1]

def _sub_annotated_mention(self, matchobj):
return "@{}".format((matchobj.group(0)[2:-1]).split("|")[1])
Expand Down
Empty file added slackviewer/utils/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions slackviewer/utils/click.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os


def envvar(name, default):
"""Create callable environment variable getter
:param str name: Name of environment variable
:param default: Default value to return in case it isn't defined
"""
return lambda: os.environ.get(name, default)


def flag_ennvar(name):
return os.environ.get(name) == '1'
30 changes: 30 additions & 0 deletions slackviewer/utils/six.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Poor man's version Six"""

import sys


PY_VERSION = sys.version_info[0]


def to_unicode(s):
"""Convert str s to unicode
:param str s: string
:return: "unicode" version of s (unicode in py2, str in py3)
"""
if PY_VERSION == 2:
s = unicode(s)

return s


def to_bytes(s, encoding="utf8"):
"""Converts str s to bytes"""
if PY_VERSION == 2:
b = bytes(s)
elif PY_VERSION == 3:
b = bytes(s, encoding)
else:
raise ValueError("Is Python 4 out already?")

return b

0 comments on commit c81730f

Please sign in to comment.