Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

Lint and format #166

Merged
merged 8 commits into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions .github/workflows/python-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
branches: [master]

jobs:
build:
test:
runs-on: ubuntu-latest
strategy:
max-parallel: 5
Expand All @@ -26,7 +26,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest pylint tox tox-gh-actions
pip install pytest tox tox-gh-actions
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Test with tox
run: tox
Expand All @@ -39,3 +39,24 @@ jobs:
file: .tox/coverage.xml
fail_ci_if_error: true
verbose: true
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pylint black
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings.
flake8 . --count --exit-zero --statistics
- name: Check formatting with black
run: black --check **/*.py
17 changes: 6 additions & 11 deletions example/listener_example.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
#!/usr/bin/env python

__author__ = 'Igor Maculan <[email protected]>'
__author__ = "Igor Maculan <[email protected]>"
import logging

from pushbullet import Listener
from pushbullet import Pushbullet

from pushbullet import Listener, Pushbullet

logging.basicConfig(level=logging.DEBUG)

API_KEY = '' # YOUR API KEY
API_KEY = "" # YOUR API KEY
HTTP_PROXY_HOST = None
HTTP_PROXY_PORT = None


def on_push(data):
print('Received data:\n{}'.format(data))
print("Received data:\n{}".format(data))


def main():
pb = Pushbullet(API_KEY)

s = Listener(account=pb,
on_push=on_push,
http_proxy_host=HTTP_PROXY_HOST,
http_proxy_port=HTTP_PROXY_PORT)
s = Listener(account=pb, on_push=on_push, http_proxy_host=HTTP_PROXY_HOST, http_proxy_port=HTTP_PROXY_PORT)
try:
s.run_forever()
except KeyboardInterrupt:
s.close()


if __name__ == '__main__':
if __name__ == "__main__":
main()
42 changes: 22 additions & 20 deletions example/mirror_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
A simple example showing how to mirror notifications.
"""

import json
import hashlib
import base64
import hashlib
import json
import os
import subprocess
import os, sys
import sys
import time

from pushbullet import PushBullet, Listener
from pushbullet import Listener, PushBullet

class Mirrorer(object):

def __init__(self, auth_key, temp_folder, device_name, last_push = time.time(), device_iden=None):
class Mirrorer(object):
def __init__(self, auth_key, temp_folder, device_name, last_push=time.time(), device_iden=None):
self.temp_folder = temp_folder
if not os.path.exists(self.temp_folder):
os.makedirs(temp_folder)
Expand All @@ -32,13 +33,12 @@ def __init__(self, auth_key, temp_folder, device_name, last_push = time.time(),
if not self.device:
try:
device = self.pb.new_device(device_name)
print("Created new device:",device_name,"iden:",device.device_iden)
print("Created new device:", device_name, "iden:", device.device_iden)
self.device = device
except:
except Exception:
print("Error: Unable to create device")
raise


self.check_pushes()

def save_icon(self, b64_asset):
Expand All @@ -55,10 +55,12 @@ def save_icon(self, b64_asset):
def check_pushes(self):
pushes = self.pb.get_pushes(self.last_push)
for push in pushes:
if not isinstance(push,dict):
if not isinstance(push, dict):
# not a push object
continue
if ((push.get("target_device_iden", self.device.device_iden) == self.device.device_iden) and not (push.get("dismissed", True))):
if (push.get("target_device_iden", self.device.device_iden) == self.device.device_iden) and not (
push.get("dismissed", True)
):
self.notify(push.get("title", ""), push.get("body", ""))
self.pb.dismiss_push(push.get("iden"))
self.last_push = max(self.last_push, push.get("created"))
Expand All @@ -67,23 +69,23 @@ def watcher(self, push):
if push["type"] == "push" and push["push"]["type"] == "mirror":
print("MIRROR")
image_path = self.save_icon(push["push"]["icon"])
self.notify(push["push"]["title"],
push["push"]["body"], image_path)
self.notify(push["push"]["title"], push["push"]["body"], image_path)
elif push["type"] == "tickle":
print("TICKLE")
self.check_pushes()


def notify(self, title, body, image=None):
subprocess.Popen(["notify-send", title, body, "-i", image or ""])
print(title)
print(body)

def dump_config(self, path):
config = {"temp_folder": self.temp_folder,
"auth_key": self._auth_key,
"device_name": self.device.nickname,
"device_iden": self.device.device_iden}
config = {
"temp_folder": self.temp_folder,
"auth_key": self._auth_key,
"device_name": self.device.nickname,
"device_iden": self.device.device_iden,
}
with open(path, "w") as conf:
json.dump(config, conf)

Expand All @@ -92,7 +94,7 @@ def run(self):
self.listener.run_forever()
except KeyboardInterrupt:
self.listener.close()


def main():
config_file = sys.argv[1]
Expand All @@ -104,5 +106,5 @@ def main():
m.dump_config(config_file)


if __name__ == '__main__':
if __name__ == "__main__":
main()
15 changes: 13 additions & 2 deletions pushbullet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
from .__version__ import __version__
from .pushbullet import Pushbullet
from .device import Device
from .errors import InvalidKeyError, PushbulletError, PushError
from .listener import Listener
from .errors import PushbulletError, InvalidKeyError, PushError
from .pushbullet import Pushbullet

PushBullet = Pushbullet

__all__ = [
"__version__",
"Device",
"InvalidKeyError",
"PushbulletError",
"PushError",
"Listener",
"Pushbullet",
"PushBullet",
]
10 changes: 5 additions & 5 deletions pushbullet/_compat.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import sys

PY2 = sys.version_info[0] == 2

if PY2:
def _py2_b64encode(x):
return x.encode("base64")

standard_b64encode = lambda x: x.encode("base64")

if sys.version_info[0] == 2:
standard_b64encode = _py2_b64encode
else:

from base64 import standard_b64encode

__all__ = ['standard_b64encode']
__all__ = ["standard_b64encode"]
3 changes: 0 additions & 3 deletions pushbullet/channel.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from __future__ import unicode_literals

import warnings

from .helpers import use_appropriate_encoding


class Channel(object):

def __init__(self, account, channel_info):
self._account = account
self.channel_tag = channel_info.get("tag")
Expand Down
5 changes: 2 additions & 3 deletions pushbullet/chat.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from __future__ import unicode_literals

from .helpers import use_appropriate_encoding
from .device import Device
from .helpers import use_appropriate_encoding


class Chat(Device):

def __init__(self, account, chat_info):
self._account = account
self.iden = chat_info.get("iden")

contact_info = chat_info['with']
contact_info = chat_info["with"]
for attr in ("created", "modified", "muted"):
setattr(self, attr, chat_info.get(attr))
for attr in ("name", "email", "email_normalized", "image_url"):
Expand Down
21 changes: 15 additions & 6 deletions pushbullet/device.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
from __future__ import unicode_literals

import warnings

from .helpers import use_appropriate_encoding


class Device(object):

def __init__(self, account, device_info):
self._account = account
self.device_iden = device_info.get("iden")
if not device_info.get("icon", None):
device_info["icon"] = "system"
for attr in ("push_token", "app_version", "fingerprint", "created", "modified",
"active", "nickname", "generated_nickname", "manufacturer", "icon",
"model", "has_sms", "key_fingerprint"):
for attr in (
"push_token",
"app_version",
"fingerprint",
"created",
"modified",
"active",
"nickname",
"generated_nickname",
"manufacturer",
"icon",
"model",
"has_sms",
"key_fingerprint",
):
setattr(self, attr, device_info.get(attr))

def push_note(self, title, body):
Expand Down
7 changes: 6 additions & 1 deletion pushbullet/errors.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
class PushbulletError(Exception):
pass


class InvalidKeyError(PushbulletError):
pass


class PushError(PushbulletError):
pass


class NoEncryptionModuleError(Exception):
def __init__(self, msg):
super(NoEncryptionModuleError, self).__init__(
"cryptography is required for end-to-end encryption support and could not be imported: " + msg + "\nYou can install it by running 'pip install cryptography'")
"cryptography is required for end-to-end encryption support and could not be imported: "
+ msg
+ "\nYou can install it by running 'pip install cryptography'"
)
6 changes: 2 additions & 4 deletions pushbullet/filetype.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

try:
from magic import from_buffer as magic_from_buffer
except ImportError:
Expand All @@ -18,8 +17,7 @@ def get_file_type(file, filename):
# decode because that results in unicode on python2
def maybe_decode(s):
try:
decoded = s.decode('utf-8')
except AttributeError as e:
decoded = s.decode("utf-8")
except AttributeError:
decoded = s
return decoded

4 changes: 3 additions & 1 deletion pushbullet/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
def use_appropriate_encoding(fn):

if sys.version_info[0] < 3:

def _fn(*args, **kwargs):
return fn(*args, **kwargs).encode(sys.stdout.encoding or 'utf-8')
return fn(*args, **kwargs).encode(sys.stdout.encoding or "utf-8")

return _fn
else:
return fn
Loading