Skip to content

Commit

Permalink
[v0.3.0] Add file_type to File object
Browse files Browse the repository at this point in the history
Signed-off-by: Sanket Saurav <[email protected]>
  • Loading branch information
sanketsaurav committed Jun 17, 2018
1 parent 5663bf7 commit cbd1b79
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 48 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 0.3.0 (2018-06-17)
- Add `file_type` attribute on the `File` object, with the following
recognized possible values: `image`, `text`, `media`, and `binary`
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mock = "*"
twine = "*"
"boto3" = "*"
future = "*"
mimelib = "*"

[requires]
python_version = "2.7"
97 changes: 52 additions & 45 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion s3tree/__version__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__version__ = '0.2.0'
__version__ = '0.3.0'
11 changes: 10 additions & 1 deletion s3tree/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""This module contains the model objects that are used to represent
trees and files in S3Tree."""
import os
import mimelib
from future.utils import python_2_unicode_compatible
from json import dumps
from .utils import humanize_file_size, cached_property
Expand Down Expand Up @@ -59,6 +60,9 @@ def __init__(self, data, s3tree):
self.size_in_bytes = data.get('Size')
self.storage_class = data.get('StorageClass')

# create a mime object for this file
self.mime = mimelib.url(self.path)

# private attributes
self.__s3tree = s3tree

Expand All @@ -72,6 +76,11 @@ def name(self):
"""File name of this file."""
return os.path.basename(self.path)

@property
def file_type(self):
"""Returns this file's type."""
return self.mime.file_type

def read(self):
"""Read the contents of this file. This method returns a string."""
try:
Expand All @@ -87,7 +96,7 @@ def __str__(self):
@cached_property
def as_dict(self):
"""Dictionary representation of this file."""
properties = ('name', 'path', 'etag', 'size',
properties = ('name', 'path', 'etag', 'size', 'file_type',
'size_in_bytes')
data = {p: getattr(self, p) for p in properties}
data['last_modified'] = self.last_modified.isoformat()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
VERSION = None

# What packages are required for this module to be executed?
REQUIRED = ['boto3', 'future', 'six']
REQUIRED = ['boto3', 'future', 'mimelib', 'six']

here = os.path.abspath(os.path.dirname(__file__))

Expand Down
1 change: 1 addition & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_file_as_dict_property(file_data, s3tree):
file_obj = File(file_data, s3tree)
orig_data = dict(name='tooltag-add.png',
path='admin/img/tooltag-add.png',
file_type='image',
etag='"2152fd3b4a4dd92fef70a86e50e1453b"',
size_in_bytes=2048,
size='2 KB',
Expand Down

0 comments on commit cbd1b79

Please sign in to comment.