Skip to content

Commit

Permalink
Fixed API.Task to not inherit API.Directory
Browse files Browse the repository at this point in the history
  • Loading branch information
shichao-an committed Apr 10, 2015
1 parent 62fba09 commit 37349ec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
19 changes: 0 additions & 19 deletions run_tests.sh

This file was deleted.

6 changes: 4 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,18 @@ def delete_entries(entries):
time.sleep(2)


def is_task_created(tasks, info_hash):
def is_task_created(tasks, info_hash, is_directory=True):
"""
Check if a task is created against a hash value
:param list tasks: a list of :class:`API.Task` objects
:param str info_hash: hash value of the target task
:param bool is_directory: whether this task has an associated directory
"""
for task in tasks:
if task.info_hash == info_hash:
assert task.is_directory == is_directory
return True
else:
return False
Expand Down Expand Up @@ -226,7 +228,7 @@ def test_add_task_url_http(self):
info_hash = TEST_TARGET_URL1['info_hash']
assert self.api.add_task_url(url)
tasks = self.api.get_tasks()
is_task_created(tasks, info_hash)
is_task_created(tasks, info_hash, False)

def test_add_task_url_magnet(self):
"""
Expand Down
67 changes: 52 additions & 15 deletions u115/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,9 +972,11 @@ class BaseFile(Base):
def __init__(self, api, cid, name):
"""
:param API api: associated API object
:param int cid: integer
for file: this represents the directory it belongs to;
for directory: this represents itself
:param str cid: directory id
* For file: this represents the directory it belongs to;
* For directory: this represents itself
:param str name: originally named `n`
NOTICE
Expand Down Expand Up @@ -1026,7 +1028,7 @@ class File(BaseFile):
File in a directory
:ivar int fid: file id
:ivar int cid: cid of the current directory
:ivar str cid: cid of the current directory
:ivar int size: size in bytes
:ivar str size_human: human-readable size
:ivar str file_type: originally named `ico`
Expand Down Expand Up @@ -1095,7 +1097,7 @@ def open_torrent(self):

class Directory(BaseFile):
"""
:ivar int cid: cid of this directory
:ivar str cid: cid of this directory
:ivar int pid: represents the parent directory it belongs to
:ivar int count: number of entries in this directory
:ivar datetime.datetime date_created: integer, originally named `t`
Expand Down Expand Up @@ -1229,11 +1231,21 @@ def list(self, count=30, order='user_ptime', asc=False, show_dir=True,
return res


class Task(Directory):
class Task(Base):
"""
BitTorrent or URL task
:ivar datetime.datetime add_time: added time
:ivar str cid: associated directory id, if any:
* For a directory task (e.g. BT task): this is its associated
directory's cid
* For a file task (e.g. HTTP url task): this is the cid of the
downloads directory
This value may be None if the task is failed and has no corresponding
directory
:ivar str file_id: equivalent to `cid` of :class:`.Directory`. This value
may be None if the task is failed and has no corresponding directory
:ivar str info_hash: hashed value
Expand Down Expand Up @@ -1264,8 +1276,9 @@ class Task(Directory):
def __init__(self, api, add_time, file_id, info_hash, last_update,
left_time, move, name, peers, percent_done, rate_download,
size, status, cid, pid):
super(Task, self).__init__(api, cid, name, pid)

self.api = api
self.cid = cid
self.name = name
self.add_time = add_time
self.file_id = file_id
self.info_hash = info_hash
Expand All @@ -1282,6 +1295,19 @@ def __init__(self, api, add_time, file_id, info_hash, last_update,
self._deleted = False
self._count = -1

@property
def is_directory(self):
"""
:return: whether this task is associated with a directory
:rtype: bool
"""
return self.api.downloads_directory.cid != self.cid

@property
def is_bt(self):
"""Alias of `is_directory`"""
return self.is_directory

def delete(self):
"""
Delete task (does not influence its corresponding directory)
Expand All @@ -1297,6 +1323,10 @@ def delete(self):

@property
def is_deleted(self):
"""
:return: whether this task is deleted
:rtype: bool
"""
return self._deleted

@property
Expand Down Expand Up @@ -1348,37 +1378,44 @@ def status_human(self):

@property
def directory(self):
"""Corresponding directory to this task"""
"""Associated directory, if any, with this task"""
if not self.is_directory:
msg = 'This task is a file task with no associated directory.'
raise TaskError(msg)
if self._directory is None:
if self.is_transferred:
self._directory = self.api._load_directory(self.cid)
if self._directory is None:
msg = 'No directory corresponding to this task: Task is %s.' % \
msg = 'No directory assciated with this task: Task is %s.' % \
self.status_human.lower()
raise TaskError(msg)
return self._directory

@property
def parent(self):
"""Parent directory of the corresponding directory"""
"""Parent directory of the associated directory"""
return self.directory.parent

@property
def count(self):
"""Number of entries in the corresponding directory"""
"""Number of entries in the associated directory"""
return self.directory.count

def list(self, count=30, order='user_ptime', asc=False, show_dir=True):
def list(self, count=30, order='user_ptime', asc=False, show_dir=True,
natsort=True):
"""
List files of the corresponding directory to this task.
List files of the associated directory to this task.
:param int count: number of entries to be listed
:param str order: originally named `o`
:param bool asc: whether in ascending order
:param bool show_dir: whether to show directories
"""
return self.directory.list(count, order, asc, show_dir)
return self.directory.list(count, order, asc, show_dir, natsort)

def __unicode__(self):
return self.name


class Torrent(Base):
Expand Down

0 comments on commit 37349ec

Please sign in to comment.