Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support python3 (macOS 12.3+) #2

Merged
merged 1 commit into from
May 8, 2022
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
12 changes: 10 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,19 @@ venv/
ENV/
env.bak/
venv.bak/
.envrc

# ide
.vscode
.idea

# poetry
/poetry.toml

# user
link.sh
info.plist.bak
/link.sh
/info.plist.bak
/*/.site-packages
/build/
/requirements.txt
*.alfredworkflow
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-executables-have-shebangs

- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
additional_dependencies: [flake8-bugbear]

- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
2 changes: 1 addition & 1 deletion .versionrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"scripts": {
"postbump": "sed -i.bak \"s#<string>[0-9]*.[0-9]*.[0-9]*</string>#<string>$(cat version)</string>#\" info.plist",
"postbump": "poetry version $(cat version) && sed -i.bak \"s#<string>[0-9]*.[0-9]*.[0-9]*</string>#<string>$(cat version)</string>#\" info.plist",
"precommit": "git add info.plist"

}
Expand Down
Empty file modified LICENSE
100755 → 100644
Empty file.
46 changes: 41 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
NAME := alfred-coin-ticker
WORKFLOW_FILENAME := $(NAME).alfredworkflow
VERSION_FILE := version
TARGET_FILES := $(shell cat includes.list)
TARGET_FILES := $(shell cat include.list)

default: build
# must use system python
PYTHON := /usr/bin/python3

default: clean build

.PHONY: .venv
.venv:
poetry env use $(PYTHON)
poetry install

.PHONY: build
build: $(WORKFLOW_FILENAME)

$(WORKFLOW_FILENAME): $(TARGET_FILES)
requirements.txt:
poetry export --without-hashes > requirements.txt

%/.site-packages:
$(PYTHON) -m pip install \
--prefer-binary \
--upgrade \
--target=$@ \
${PACKAGES}

deps: requirements.txt
deps: PACKAGES=-r requirements.txt
deps: build/.site-packages
deps: $(TARGET_FILES)

$(WORKFLOW_FILENAME): deps
@echo "> Packaging..."
./build.sh
rm -f $(WORKFLOW_FILENAME)
BUILD_DIR=$(BUILD_DIR) WORKFLOW_FILENAME=$(WORKFLOW_FILENAME) ./build.sh

.PHONY: test
test:
python -m unittest discover -s tests -v

install: $(WORKFLOW_FILENAME)
open $(WORKFLOW_FILENAME)

.PHONY: clean
clean:
rm -f $(WORKFLOW_FILENAME)
rm -rf \
requirements.txt \
./build/ \
.mypy_cache/ \
$(WORKFLOW_FILENAME)

.PHONY: bump_version
bump_version:
Expand All @@ -29,6 +60,7 @@ bump_version:
@echo Current version: $(shell cat $(VERSION_FILE))
@(read -e -p "Bump to version $(version)? [y/N]: " ans && case "$$ans" in [yY]) true;; *) false;; esac)
@echo $(version) > version
@potry version $(version)
@sed -i.bak 's#<string>[0-9]*.[0-9]*.[0-9]*</string>#<string>$(version)</string>#' info.plist

.PHONY: release
Expand All @@ -46,3 +78,7 @@ release-minor:
.PHONY: release-patch
release-patch:
@standard-version -a -s -t "" --release-as patch

EXECUTABLES = $(PYTHON) plutil open rsync poetry
K := $(foreach exec,$(EXECUTABLES),\
$(if $(shell which $(exec)),some string,$(error "No $(exec) in PATH")))
6 changes: 3 additions & 3 deletions README.md
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Coin Ticker for Alfred Workflow
</h1>

An [Alfred Workflow](http://www.alfredapp.com/) that provides the current price
An [Alfred Workflow](http://www.alfredapp.com/) that provides the current price
and status about cryptocurrency from [cryptocompare.com].

Supports Alfred 3 and Alfred 4 on macOS 10.7+ (Python 2.7).
Expand Down Expand Up @@ -52,8 +52,8 @@ Please use with caution.

The code is released under the MIT license. See [LICENSE](LICENSE) for details.

Awesome [alfred-workflow](https://github.com/deanishe/alfred-workflow) library
by [@deanishe](https://github.com/deanishe) is also released under
Awesome [alfred-workflow](https://github.com/deanishe/alfred-workflow) library
by [@deanishe](https://github.com/deanishe) is also released under
[MIT License](alfred-workflow/LICENCE.txt).

[cryptocompare.com]: https://www.cryptocompare.com/
Expand Down
1 change: 0 additions & 1 deletion alfred-workflow
Submodule alfred-workflow deleted from 8110e8
101 changes: 52 additions & 49 deletions api.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
#!/usr/bin/python
#!/usr/bin/env python3
# encoding: utf-8
#
# Copyright (c) 2022 Bumsoo Kim <bskim45@gmail.com>
#
# MIT Licence http://opensource.org/licenses/MIT

from __future__ import print_function, unicode_literals
from __future__ import annotations

import os
from collections import namedtuple

import requests as requests

from utils import clean_price_string
from workflow import web


class CoinTick(object):
def __init__(
self,
ticker, # type: unicode
symbol, # type: unicode
image_url, # type: unicode
fiat, # type: unicode
fiat_symbol, # type: unicode
price, # type: unicode
price_24h_high, # type: unicode
price_24h_low, # type: unicode
price_change_24h, # type: unicode
price_change_24h_percent, # type: unicode
total_volume_24h, # type: unicode
total_volume_24h_fiat, # type: unicode
ticker, # type: str
symbol, # type: str
image_url, # type: str
fiat, # type: str
fiat_symbol, # type: str
price, # type: str
price_24h_high, # type: str
price_24h_low, # type: str
price_change_24h, # type: str
price_change_24h_percent, # type: str
total_volume_24h, # type: str
total_volume_24h_fiat, # type: str
):
self.ticker = ticker
self.symbol = symbol
Expand All @@ -47,32 +48,31 @@ def __eq__(self, o):
if not isinstance(o, CoinTick):
return False

return all((
self.ticker == o.ticker,
self.symbol == o.symbol,
self.image_url == o.image_url,
self.fiat == o.fiat,
self.fiat_symbol == o.fiat_symbol,
self.price == o.price,
self.price_24h_high == o.price_24h_high,
self.price_24h_low == o.price_24h_low,
self.price_change_24h == o.price_change_24h,
self.price_change_24h_percent == o.price_change_24h_percent,
self.total_volume_24h == o.total_volume_24h,
self.total_volume_24h_fiat == o.total_volume_24h_fiat,
))

def __unicode__(self):
return '{0} {1} ({2}{3})'.format(
self.symbol, self.ticker, self.fiat_symbol, self.price)
return all(
(
self.ticker == o.ticker,
self.symbol == o.symbol,
self.image_url == o.image_url,
self.fiat == o.fiat,
self.fiat_symbol == o.fiat_symbol,
self.price == o.price,
self.price_24h_high == o.price_24h_high,
self.price_24h_low == o.price_24h_low,
self.price_change_24h == o.price_change_24h,
self.price_change_24h_percent == o.price_change_24h_percent,
self.total_volume_24h == o.total_volume_24h,
self.total_volume_24h_fiat == o.total_volume_24h_fiat,
)
)

def __str__(self):
return unicode(self).encode('utf-8')
return '{0} {1} ({2}{3})'.format(
self.symbol, self.ticker, self.fiat_symbol, self.price
)


CoinInfo = namedtuple(
'CoinInfo',
['name', 'ticker', 'symbol', 'image_url', 'url']
'CoinInfo', ['name', 'ticker', 'symbol', 'image_url', 'url']
)


Expand All @@ -84,31 +84,30 @@ class TickClient(object):

@classmethod
def get_cache_key(cls, query=None):
return '{0}-{1}'.format(cls.CACHE_KEY, query) \
.replace(os.sep, '_')
return '{0}-{1}'.format(cls.CACHE_KEY, query).replace(os.sep, '_')

def get(self, path, params):
# type: (unicode, dict) -> dict
r = web.get(self.API_BASE_URL + path, params)
# type: (str, dict) -> dict
r = requests.get(self.API_BASE_URL + path, params)
r.raise_for_status()

result = r.json()
return result

def get_coin_prices(self, tickers, fiat):
# type: (list[unicode], unicode) -> list[CoinTick] # noqa
# type: (list[str], str) -> list[CoinTick] # noqa
raise NotImplementedError()

def get_top_market_cap(self, limit, fiat):
# type: (int, unicode) -> list[CoinTick] # noqa
# type: (int, str) -> list[CoinTick] # noqa
raise NotImplementedError()

def get_coin_info(self, ticker):
# type: (unicode) -> CoinInfo # noqa
# type: (str) -> CoinInfo # noqa
raise NotImplementedError()

def get_ticker_web_url(self, ticker, fiat):
# type: (unicode, unicode) -> unicode
# type: (str, str) -> str
raise NotImplementedError()


Expand Down Expand Up @@ -168,9 +167,11 @@ def get_ticker_web_url(self, ticker, fiat):
def coin_info_from_api_repr(cls, data):
# type: (dict) -> CoinInfo
return CoinInfo(
data['CoinName'], data['Symbol'], None,
data['CoinName'],
data['Symbol'],
None,
cls.WEB_BASE_URL + data['ImageUrl'],
cls.WEB_BASE_URL + data['Url']
cls.WEB_BASE_URL + data['Url'],
)

@classmethod
Expand All @@ -183,9 +184,11 @@ def tick_from_api_repr(cls, raw, display):
fiat_symbol = display.get('TOSYMBOL')

return CoinTick(
ticker, symbol,
ticker,
symbol,
cls.WEB_BASE_URL + display.get('IMAGEURL'),
fiat, fiat_symbol,
fiat,
fiat_symbol,
clean_price_string(fiat_symbol, display.get('PRICE')),
clean_price_string(fiat_symbol, display.get('HIGH24HOUR')),
clean_price_string(fiat_symbol, display.get('LOW24HOUR')),
Expand Down Expand Up @@ -222,5 +225,5 @@ def get_coin_web_url(cls, coin_name):

@staticmethod
def normalize_to_underscore(name):
# type: (unicode) -> unicode
# type: (str) -> str
return name.replace('.', '-').replace(' ', '-')
41 changes: 26 additions & 15 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
#!/usr/bin/env bash

WORKFLOW_NAME=alfred-coin-ticker.alfredworkflow
set -euo pipefail

SCRIPT_HOME=$(dirname "$(realpath "$0")")
VERSION=$(cat version)
: "${BUILD_DIR:="$SCRIPT_HOME/build"}"
: "${WORKFLOW_FILENAME:=alfred-coin-ticker.alfredworkflow}"

echo "Home path: $SCRIPT_HOME"
echo "Version: $VERSION"
echo "Build dir: $BUILD_DIR"
echo "Workflow: $WORKFLOW_FILENAME"

#read -p "Continue? [y/N]" -n 1 -r
#echo # new line
#if [[ $REPLY =~ ^[Yy]$ ]]; then
# # pass
# echo "Building..."
#else
# echo "abort"
# exit 1
#fi

cd "$SCRIPT_HOME/alfred-workflow/workflow" || exit
git clean -nx
rsync --archive --verbose \
--filter '- *.pyc' \
--filter '- *.egg-info' \
--filter '- *.dist-info' \
--filter '- __pycache__' \
"$BUILD_DIR/.site-packages/" "$BUILD_DIR/"

read -p "Continue? [y/N]" -n 1 -r
echo # new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
git clean -fx
else
echo "abort"
exit 1
fi
# shellcheck disable=SC2046
cp $(<include.list) "$BUILD_DIR/"
cd "$BUILD_DIR" || exit
rm -rf "./.site-packages"

cd "$SCRIPT_HOME" || exit
rm -f $WORKFLOW_NAME
zip -r $WORKFLOW_NAME -@ <includes.list
zip -r "$SCRIPT_HOME/$WORKFLOW_FILENAME" . -x@"$SCRIPT_HOME/exclude.list"
Loading