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

Use appdirs' user_cache_dir for sequence file #165

Merged
merged 1 commit into from
Jan 20, 2018
Merged
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
14 changes: 11 additions & 3 deletions miio/vacuum_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import json
import ipaddress
import time
import pathlib
from appdirs import user_cache_dir
from pprint import pformat as pf
from typing import Any # noqa: F401

Expand Down Expand Up @@ -46,7 +48,7 @@ def validate_token(ctx, param, value):
@click.option('--token', envvar="MIROBO_TOKEN", callback=validate_token)
@click.option('-d', '--debug', default=False, count=True)
@click.option('--id-file', type=click.Path(dir_okay=False, writable=True),
default='/tmp/python-mirobo.seq')
default=user_cache_dir('python-miio') + '/python-mirobo.seq')
@click.version_option()
@click.pass_context
def cli(ctx, ip: str, token: str, debug: int, id_file: str):
Expand All @@ -73,8 +75,8 @@ def cli(ctx, ip: str, token: str, debug: int, id_file: str):
start_id = x.get("seq", 0)
manual_seq = x.get("manual_seq", 0)
_LOGGER.debug("Read stored sequence ids: %s", x)
except (FileNotFoundError, TypeError, ValueError) as ex:
_LOGGER.error("Unable to read the stored msgid: %s", ex)
except (FileNotFoundError, TypeError, ValueError):
pass

vac = miio.Vacuum(ip, token, start_id, debug)

Expand All @@ -96,6 +98,12 @@ def cleanup(vac: miio.Vacuum, **kwargs):
id_file = kwargs['id_file']
seqs = {'seq': vac.raw_id, 'manual_seq': vac.manual_seqnum}
_LOGGER.debug("Writing %s to %s", seqs, id_file)
path_obj = pathlib.Path(id_file)
dir = path_obj.parents[0]
try:
dir.mkdir(parents=True)
except FileExistsError:
pass # after dropping py3.4 support, use exist_ok for mkdir
with open(id_file, 'w') as f:
json.dump(seqs, f)

Expand Down