Skip to content

Commit

Permalink
Merge pull request #91 from koodaamo/add-typing
Browse files Browse the repository at this point in the history
Introduce type annotations
  • Loading branch information
petri authored Nov 26, 2020
2 parents f5b7b17 + fc3e0bc commit 805e1b3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ tnefparse 1.4.0 (unreleased)
- drop Python 2 support
- drop Python 3.5 support (jugmac00)
- add Python 3.9 support (jugmac00)
- introduce using type annotations (jugmac00)

tnefparse 1.3.1 (2020-09-30)
=============================
Expand Down
4 changes: 2 additions & 2 deletions tnefparse/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
help="extract a json dump of the tnef contents")


def tnefparse():
def tnefparse() -> None:
"command-line script"

if len(sys.argv) == 1:
Expand Down Expand Up @@ -97,7 +97,7 @@ def tnefparse():
sys.stderr.write("Successfully wrote %i files\n" % len(t.attachments))
sys.exit()

def print_body(attr, description):
def print_body(attr: str, description: str) -> None:
body = getattr(t, attr)
if body is None:
sys.exit("No %s found" % description)
Expand Down
10 changes: 6 additions & 4 deletions tnefparse/codepage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional, Union

# https://docs.microsoft.com/en-us/windows/desktop/intl/code-page-identifiers
CODEPAGE_MAP = {
20127: 'ascii',
Expand All @@ -9,25 +11,25 @@


class Codepage:
def __init__(self, codepage):
def __init__(self, codepage: int) -> None:
self.cp = codepage

def best_guess(self):
def best_guess(self) -> Optional[str]:
if CODEPAGE_MAP.get(self.cp):
return CODEPAGE_MAP.get(self.cp)
elif self.cp <= 1258: # max cpXXXX page in python
return 'cp%d' % self.cp
else:
return None

def codepage(self):
def codepage(self) -> str:
bg = self.best_guess()
if bg:
return bg
else:
return 'cp%d' % self.cp

def decode(self, byte_str):
def decode(self, byte_str: Union[str, bytes]) -> str:
if isinstance(byte_str, bytes):
return byte_str.decode(self.best_guess() or FALLBACK)
else:
Expand Down
7 changes: 4 additions & 3 deletions tnefparse/tnef.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ def __init__(self, codepage):
self.data = b''

@property
def name(self):
def name(self) -> str:
if isinstance(self._name, bytes):
return self._name.decode().strip('\x00')
else:
return self._name.strip('\x00')

def long_filename(self):
def long_filename(self) -> str:
atname = Attribute.MAPI_ATTACH_LONG_FILENAME
name = [a.data for a in self.mapi_attrs if a.name == atname]
if name:
Expand Down Expand Up @@ -326,7 +326,8 @@ def has_body(self):
def rtfbody(self):
if self._rtfbody:
try:
from compressed_rtf import decompress
# compressed_rtf is not typed yet
from compressed_rtf import decompress # type: ignore
return decompress(self._rtfbody + b'\x00')
except ImportError:
logger.warning("Returning compressed RTF. Install compressed_rtf to decompress")
Expand Down
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ deps =
commands =
check-manifest

[testenv:mypy]
deps =
mypy
commands =
# do not lint tests yet
mypy tnefparse {posargs}

[flake8]
# The GitHub editor is 127 chars wide.
max-line-length = 127

0 comments on commit 805e1b3

Please sign in to comment.