Add NODE_TYPE_ constants instead of using magic numbers.
This commit is contained in:
parent
5c2aa09db4
commit
7384ed9caa
1 changed files with 32 additions and 37 deletions
|
@ -28,6 +28,11 @@ from .crypto import (
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
NODE_TYPE_FILE = 0
|
||||||
|
NODE_TYPE_DIR = 1
|
||||||
|
NODE_TYPE_ROOT = 2
|
||||||
|
NODE_TYPE_INBOX = 3
|
||||||
|
NODE_TYPE_TRASH = 4
|
||||||
|
|
||||||
class Mega:
|
class Mega:
|
||||||
def __init__(self, options=None):
|
def __init__(self, options=None):
|
||||||
|
@ -80,7 +85,7 @@ class Mega:
|
||||||
self._login_user(email, password)
|
self._login_user(email, password)
|
||||||
else:
|
else:
|
||||||
self.login_anonymous()
|
self.login_anonymous()
|
||||||
self._trash_folder_node_id = self.get_node_by_type(4)[0]
|
self._trash_folder_node_id = self.get_node_by_type(NODE_TYPE_TRASH)[0]
|
||||||
logger.info('Login complete')
|
logger.info('Login complete')
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@ -201,7 +206,7 @@ class Mega:
|
||||||
raise RequestError('Url key missing')
|
raise RequestError('Url key missing')
|
||||||
|
|
||||||
def _process_file(self, file, shared_keys):
|
def _process_file(self, file, shared_keys):
|
||||||
if file['t'] == 0 or file['t'] == 1:
|
if file['t'] in [NODE_TYPE_FILE, NODE_TYPE_DIR]:
|
||||||
keys = dict(
|
keys = dict(
|
||||||
keypart.split(':', 1)
|
keypart.split(':', 1)
|
||||||
for keypart in file['k'].split('/')
|
for keypart in file['k'].split('/')
|
||||||
|
@ -237,15 +242,13 @@ class Mega:
|
||||||
key = decrypt_key(encrypted_key, shared_key)
|
key = decrypt_key(encrypted_key, shared_key)
|
||||||
file['shared_folder_key'] = shared_key
|
file['shared_folder_key'] = shared_key
|
||||||
if key is not None:
|
if key is not None:
|
||||||
# file
|
if file['t'] == NODE_TYPE_FILE:
|
||||||
if file['t'] == 0:
|
|
||||||
k = (
|
k = (
|
||||||
key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6],
|
key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6],
|
||||||
key[3] ^ key[7]
|
key[3] ^ key[7]
|
||||||
)
|
)
|
||||||
file['iv'] = key[4:6] + (0, 0)
|
file['iv'] = key[4:6] + (0, 0)
|
||||||
file['meta_mac'] = key[6:8]
|
file['meta_mac'] = key[6:8]
|
||||||
# folder
|
|
||||||
else:
|
else:
|
||||||
k = key
|
k = key
|
||||||
file['key'] = key
|
file['key'] = key
|
||||||
|
@ -256,13 +259,13 @@ class Mega:
|
||||||
# other => wrong object
|
# other => wrong object
|
||||||
elif file['k'] == '':
|
elif file['k'] == '':
|
||||||
file['a'] = False
|
file['a'] = False
|
||||||
elif file['t'] == 2:
|
elif file['t'] == NODE_TYPE_ROOT:
|
||||||
self.root_id = file['h']
|
self.root_id = file['h']
|
||||||
file['a'] = {'n': 'Cloud Drive'}
|
file['a'] = {'n': 'Cloud Drive'}
|
||||||
elif file['t'] == 3:
|
elif file['t'] == NODE_TYPE_INBOX:
|
||||||
self.inbox_id = file['h']
|
self.inbox_id = file['h']
|
||||||
file['a'] = {'n': 'Inbox'}
|
file['a'] = {'n': 'Inbox'}
|
||||||
elif file['t'] == 4:
|
elif file['t'] == NODE_TYPE_TRASH:
|
||||||
self.trashbin_id = file['h']
|
self.trashbin_id = file['h']
|
||||||
file['a'] = {'n': 'Rubbish Bin'}
|
file['a'] = {'n': 'Rubbish Bin'}
|
||||||
return file
|
return file
|
||||||
|
@ -457,10 +460,14 @@ class Mega:
|
||||||
|
|
||||||
def get_files_in_node(self, target):
|
def get_files_in_node(self, target):
|
||||||
"""
|
"""
|
||||||
Get all files in a given target, e.g. 4=trash
|
Get all files in a given target.
|
||||||
|
Params:
|
||||||
|
target: a node's id string, or one of the special nodes
|
||||||
|
e.g. NODE_TYPE_TRASH.
|
||||||
"""
|
"""
|
||||||
if type(target) == int:
|
if type(target) == int:
|
||||||
# convert special nodes (e.g. trash)
|
if target in [NODE_TYPE_FILE, NODE_TYPE_DIR]:
|
||||||
|
raise TypeError('Can\'t use file or dir node type.')
|
||||||
node_id = self.get_node_by_type(target)
|
node_id = self.get_node_by_type(target)
|
||||||
else:
|
else:
|
||||||
node_id = [target]
|
node_id = [target]
|
||||||
|
@ -542,7 +549,7 @@ class Mega:
|
||||||
"""
|
"""
|
||||||
Delete a file by its public handle
|
Delete a file by its public handle
|
||||||
"""
|
"""
|
||||||
return self.move(public_handle, 4)
|
return self.move(public_handle, NODE_TYPE_TRASH)
|
||||||
|
|
||||||
def delete_url(self, url):
|
def delete_url(self, url):
|
||||||
"""
|
"""
|
||||||
|
@ -551,7 +558,7 @@ class Mega:
|
||||||
path = self._parse_url(url).split('!')
|
path = self._parse_url(url).split('!')
|
||||||
public_handle = path[0]
|
public_handle = path[0]
|
||||||
file_id = self.get_id_from_public_handle(public_handle)
|
file_id = self.get_id_from_public_handle(public_handle)
|
||||||
return self.move(file_id, 4)
|
return self.move(file_id, NODE_TYPE_TRASH)
|
||||||
|
|
||||||
def destroy(self, file_id):
|
def destroy(self, file_id):
|
||||||
"""
|
"""
|
||||||
|
@ -576,7 +583,7 @@ class Mega:
|
||||||
|
|
||||||
def empty_trash(self):
|
def empty_trash(self):
|
||||||
# get list of files in rubbish out
|
# get list of files in rubbish out
|
||||||
files = self.get_files_in_node(4)
|
files = self.get_files_in_node(NODE_TYPE_TRASH)
|
||||||
|
|
||||||
# make a list of json
|
# make a list of json
|
||||||
if files != {}:
|
if files != {}:
|
||||||
|
@ -617,7 +624,7 @@ class Mega:
|
||||||
node = self.find(path)
|
node = self.find(path)
|
||||||
|
|
||||||
node_data = self._node_data(node)
|
node_data = self._node_data(node)
|
||||||
is_file_node = node_data['t'] == 0
|
is_file_node = node_data['t'] == NODE_TYPE_FILE
|
||||||
if is_file_node:
|
if is_file_node:
|
||||||
return self._export_file(node)
|
return self._export_file(node)
|
||||||
if node:
|
if node:
|
||||||
|
@ -880,7 +887,7 @@ class Mega:
|
||||||
'n': [
|
'n': [
|
||||||
{
|
{
|
||||||
'h': completion_file_handle,
|
'h': completion_file_handle,
|
||||||
't': 0,
|
't': NODE_TYPE_FILE,
|
||||||
'a': encrypt_attribs,
|
'a': encrypt_attribs,
|
||||||
'k': encrypted_key
|
'k': encrypted_key
|
||||||
}
|
}
|
||||||
|
@ -907,7 +914,7 @@ class Mega:
|
||||||
'n': [
|
'n': [
|
||||||
{
|
{
|
||||||
'h': 'xxxxxxxx',
|
'h': 'xxxxxxxx',
|
||||||
't': 1,
|
't': NODE_TYPE_DIR,
|
||||||
'a': encrypt_attribs,
|
'a': encrypt_attribs,
|
||||||
'k': encrypted_key
|
'k': encrypted_key
|
||||||
}
|
}
|
||||||
|
@ -969,27 +976,16 @@ class Mega:
|
||||||
def move(self, file_id, target):
|
def move(self, file_id, target):
|
||||||
"""
|
"""
|
||||||
Move a file to another parent node
|
Move a file to another parent node
|
||||||
params:
|
|
||||||
a : command
|
|
||||||
n : node we're moving
|
|
||||||
t : id of target parent node, moving to
|
|
||||||
i : request id
|
|
||||||
|
|
||||||
targets
|
Params:
|
||||||
2 : root
|
file_id: the file to move.
|
||||||
3 : inbox
|
target: a node's id string, or one of the special nodes
|
||||||
4 : trash
|
e.g. NODE_TYPE_TRASH, or the structure returned by find().
|
||||||
|
|
||||||
or...
|
|
||||||
target's id
|
|
||||||
or...
|
|
||||||
target's structure returned by find()
|
|
||||||
"""
|
"""
|
||||||
|
if isinstance(target, int):
|
||||||
# determine target_node_id
|
|
||||||
if type(target) == int:
|
|
||||||
target_node_id = str(self.get_node_by_type(target)[0])
|
target_node_id = str(self.get_node_by_type(target)[0])
|
||||||
elif type(target) in (str, ):
|
|
||||||
|
elif isinstance(target, str):
|
||||||
target_node_id = target
|
target_node_id = target
|
||||||
else:
|
else:
|
||||||
file = target[1]
|
file = target[1]
|
||||||
|
@ -1085,8 +1081,7 @@ class Mega:
|
||||||
"""
|
"""
|
||||||
# Providing dest_node spare an API call to retrieve it.
|
# Providing dest_node spare an API call to retrieve it.
|
||||||
if dest_node is None:
|
if dest_node is None:
|
||||||
# Get '/Cloud Drive' folder no dest node specified
|
dest_node = self.get_node_by_type(NODE_TYPE_ROOT)[1]
|
||||||
dest_node = self.get_node_by_type(2)[1]
|
|
||||||
|
|
||||||
# Providing dest_name spares an API call to retrieve it.
|
# Providing dest_name spares an API call to retrieve it.
|
||||||
if dest_name is None:
|
if dest_name is None:
|
||||||
|
@ -1107,7 +1102,7 @@ class Mega:
|
||||||
'n': [
|
'n': [
|
||||||
{
|
{
|
||||||
'ph': file_handle,
|
'ph': file_handle,
|
||||||
't': 0,
|
't': NODE_TYPE_FILE,
|
||||||
'a': encrypted_name,
|
'a': encrypted_name,
|
||||||
'k': encrypted_key
|
'k': encrypted_key
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue