From 17b4d0363af767f7686a11ba828270070995120e Mon Sep 17 00:00:00 2001 From: "richard@richard.do" Date: Sat, 16 Feb 2013 00:34:16 +0000 Subject: [PATCH] added setup.py, restructured files to be a python package. fixed issue #4. updated readme --- .gitignore | 5 ++++- CONTRIBUTORS | 2 +- README.md | 13 +++++++++++- mega/__init__.py | 1 + crypto.py => mega/crypto.py | 0 errors.py => mega/errors.py | 0 mega.py => mega/mega.py | 19 +++++++++++------ setup.py | 41 +++++++++++++++++++++++++++++++++++++ tests/test.py => tests.py | 7 +++---- 9 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 mega/__init__.py rename crypto.py => mega/crypto.py (100%) rename errors.py => mega/errors.py (100%) rename mega.py => mega/mega.py (96%) create mode 100644 setup.py rename tests/test.py => tests.py (91%) diff --git a/.gitignore b/.gitignore index 48b9af1..fcbedb6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ # Hidden files # ################### -.* \ No newline at end of file +.* +# Build files # +################### +build/ \ No newline at end of file diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 9b87731..56116ee 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -1,4 +1,4 @@ cyberjujum richardasaurus jlejeune -issues: zbahoui,alyssarowan +issues: zbahoui,alyssarowan,kionez diff --git a/README.md b/README.md index dea4620..91b3437 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,17 @@ This is a work in progress, further functionality coming shortly. ## How To Use +### Install mega.py package + + - Download the source code + - Open the terminal in the source folder + - Run the following command + sudo python setup.py install + +### Import mega.py + + from mega import Mega + ### Create an instance of Mega.py mega = Mega() @@ -60,7 +71,7 @@ This is a work in progress, further functionality coming shortly. ## Tests - Test .py files can be found in /tests, run these to ensure Mega.py is working 100%. + Test .py files can be found in tests.py, run these to ensure Mega.py is working 100%. ## Contribute diff --git a/mega/__init__.py b/mega/__init__.py new file mode 100644 index 0000000..2da6f5f --- /dev/null +++ b/mega/__init__.py @@ -0,0 +1 @@ +from .mega import Mega \ No newline at end of file diff --git a/crypto.py b/mega/crypto.py similarity index 100% rename from crypto.py rename to mega/crypto.py diff --git a/errors.py b/mega/errors.py similarity index 100% rename from errors.py rename to mega/errors.py diff --git a/mega.py b/mega/mega.py similarity index 96% rename from mega.py rename to mega/mega.py index f803679..d46b434 100644 --- a/mega.py +++ b/mega/mega.py @@ -7,8 +7,8 @@ import os import random import binascii import requests -import errors -from crypto import * +from .errors import ValidationError,RequestError +from .crypto import * class Mega(object): @@ -32,7 +32,7 @@ class Mega(object): resp = self.api_request({'a': 'us', 'user': email, 'uh': uh}) #if numeric error code response if isinstance(resp, int): - raise errors.RequestError(resp) + raise RequestError(resp) self._login_process(resp, password_aes) def _login_process(self, resp, password): @@ -82,7 +82,7 @@ class Mega(object): #if numeric error code response if isinstance(json_resp, int): - raise errors.RequestError(json_resp) + raise RequestError(json_resp) return json_resp[0] def get_files(self): @@ -129,7 +129,7 @@ class Mega(object): public_handle, decrypted_key) else: - raise errors.ValidationError('File id and key must be present') + raise ValidationError('File id and key must be present') def download_url(self, url, dest_path=None): ''' @@ -154,7 +154,7 @@ class Mega(object): path = match[0] return path else: - raise errors.RequestError('Url key missing') + raise RequestError('Url key missing') def get_user(self): user_data = self.api_request({'a': 'ug'}) @@ -359,6 +359,13 @@ class Mega(object): """ if file['t'] == 0 or file['t'] == 1: key = file['k'][file['k'].index(':') + 1:] + #fix for shared folder key format {k: foo1:bar1/foo2:bar2 } + uid = file['u'] + keys = file['k'].split('/') + regex = re.compile('^%s:.*$' % uid) + for keytmp in keys: + if regex.match(keytmp): + key = keytmp[keytmp.index(':') + 1:] key = decrypt_key(base64_to_a32(key), self.master_key) if file['t'] == 0: k = (key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6], diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..c658d28 --- /dev/null +++ b/setup.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +from distutils.core import setup +import os + + +def get_packages(package): + """ + Return root package & all sub-packages. + """ + return [dirpath + for dirpath, dirnames, filenames in os.walk(package) + if os.path.exists(os.path.join(dirpath, '__init__.py'))] + +def get_package_data(package): + """ + Return all files under the root package, that are not in a + package themselves. + """ + walk = [(dirpath.replace(package + os.sep, '', 1), filenames) + for dirpath, dirnames, filenames in os.walk(package) + if not os.path.exists(os.path.join(dirpath, '__init__.py'))] + + filepaths = [] + for base, filenames in walk: + filepaths.extend([os.path.join(base, filename) + for filename in filenames]) + return {package: filepaths} + +setup( + name='mega.py', + version='0.8', + packages=get_packages('mega'), + package_data=get_package_data('mega'), + description='Python lib for the Mega.co.nz API', + author='Richard O\'Dwyer', + author_email='richard@richard.do', + license='Creative Commons Attribution-Noncommercial-Share Alike license', + long_description=open('README.md').read(), +) \ No newline at end of file diff --git a/tests/test.py b/tests.py similarity index 91% rename from tests/test.py rename to tests.py index 7df00ba..d8a4bb6 100644 --- a/tests/test.py +++ b/tests.py @@ -1,6 +1,5 @@ from mega import Mega - def test(): #user details email = 'your@email.com' @@ -23,11 +22,11 @@ def test(): print files[file] ##upload file - print(m.upload('test.py')) + print(m.upload('tests.py')) ##get file's public link #NOTE: if passing upload() function response use get_upload_link() - file = m.find('test.py') + file = m.find('tests.py') #print(m.get_upload_link(file)) print(m.get_link(file)) @@ -42,7 +41,7 @@ def test(): print(m.delete(file[1]['k'])) ##download file - #file = m.find('test.py') + #file = m.find('tests.py') #m.download(file) ##specify destination folder #m.download(file, '/home/user_name/Desktop')