Skip to content

Commit

Permalink
fix ImportError on Python 2.x
Browse files Browse the repository at this point in the history
bundlewrap.utils.ui cannot import from both “time” (stdlib) and “bundlewrap.utils.time”
  • Loading branch information
trehn committed Oct 4, 2017
1 parent 6ab4634 commit 1b4ca3c
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 108 deletions.
2 changes: 1 addition & 1 deletion bundlewrap/cmdline/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
blue,
bold,
error_summary,
format_duration,
green,
green_unless_zero,
mark_for_translation as _,
Expand All @@ -22,7 +23,6 @@
yellow,
yellow_unless_zero,
)
from ..utils.time import format_duration
from ..utils.ui import io


Expand Down
13 changes: 10 additions & 3 deletions bundlewrap/cmdline/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
from ..lock import softlock_add, softlock_list, softlock_remove
from ..utils.cmdline import get_target_nodes
from ..utils.table import ROW_SEPARATOR, render_table
from ..utils.text import bold, error_summary, green, mark_for_translation as _, randstr, red, \
yellow
from ..utils.time import format_timestamp
from ..utils.text import (
bold,
error_summary,
format_timestamp,
green,
mark_for_translation as _,
randstr,
red,
yellow,
)
from ..utils.ui import io, page_lines


Expand Down
13 changes: 10 additions & 3 deletions bundlewrap/cmdline/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@
from ..utils import SkipList
from ..utils.cmdline import get_target_nodes
from ..utils.table import ROW_SEPARATOR, render_table
from ..utils.text import mark_for_translation as _
from ..utils.text import blue, bold, error_summary, green, red, yellow
from ..utils.time import format_duration
from ..utils.text import (
blue,
bold,
error_summary,
format_duration,
green,
mark_for_translation as _,
red,
yellow,
)
from ..utils.ui import io


Expand Down
2 changes: 1 addition & 1 deletion bundlewrap/cmdline/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
blue,
bold,
error_summary,
format_duration,
green,
green_unless_zero,
mark_for_translation as _,
red,
red_unless_zero,
)
from ..utils.time import format_duration
from ..utils.ui import io


Expand Down
12 changes: 10 additions & 2 deletions bundlewrap/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@

from .exceptions import NodeLockedException
from .utils import cached_property, tempfile
from .utils.text import blue, bold, mark_for_translation as _, red, wrap_question
from .utils.time import format_duration, format_timestamp, parse_duration
from .utils.text import (
blue,
bold,
format_duration,
format_timestamp,
mark_for_translation as _,
parse_duration,
red,
wrap_question,
)
from .utils.ui import io


Expand Down
15 changes: 12 additions & 3 deletions bundlewrap/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@
from .metadata import hash_metadata
from .utils import cached_property, names
from .utils.statedict import hash_statedict
from .utils.text import blue, bold, cyan, green, red, validate_name, yellow
from .utils.text import force_text, mark_for_translation as _
from .utils.time import format_duration
from .utils.text import (
blue,
bold,
cyan,
force_text,
format_duration,
green,
mark_for_translation as _,
red,
validate_name,
yellow,
)
from .utils.ui import io


Expand Down
51 changes: 51 additions & 0 deletions bundlewrap/utils/text.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from datetime import datetime, timedelta
from io import BytesIO
from os import environ
from os.path import normpath
Expand Down Expand Up @@ -200,3 +201,53 @@ def flush(self):
def write(self, msg):
self.buffer += msg
self.flush()


def format_duration(duration, msec=False):
"""
Takes a timedelta and returns something like "1d 5h 4m 3s".
"""
components = []
if duration.days > 0:
components.append(_("{}d").format(duration.days))
seconds = duration.seconds
if seconds >= 3600:
hours = int(seconds / 3600)
seconds -= hours * 3600
components.append(_("{}h").format(hours))
if seconds >= 60:
minutes = int(seconds / 60)
seconds -= minutes * 60
components.append(_("{}m").format(minutes))
if seconds > 0 or not components:
if msec:
seconds += duration.microseconds / 1000000.0
components.append(_("{:.3f}s").format(seconds))
else:
components.append(_("{}s").format(seconds))
return " ".join(components)


def format_timestamp(timestamp):
return datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")


def parse_duration(duration):
"""
Parses a string like "1d 5h 4m 3s" into a timedelta.
"""
days = 0
seconds = 0
for component in duration.strip().split(" "):
component = component.strip()
if component[-1] == "d":
days += int(component[:-1])
elif component[-1] == "h":
seconds += int(component[:-1]) * 3600
elif component[-1] == "m":
seconds += int(component[:-1]) * 60
elif component[-1] == "s":
seconds += int(component[:-1])
else:
raise ValueError(_("{} is not a valid duration string").format(repr(duration)))
return timedelta(days=days, seconds=seconds)
56 changes: 0 additions & 56 deletions bundlewrap/utils/time.py

This file was deleted.

12 changes: 10 additions & 2 deletions bundlewrap/utils/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@

from . import STDERR_WRITER, STDOUT_WRITER
from .table import render_table, ROW_SEPARATOR
from .text import HIDE_CURSOR, SHOW_CURSOR, ansi_clean, blue, bold, mark_for_translation as _
from .time import format_duration
from .text import (
HIDE_CURSOR,
SHOW_CURSOR,
ansi_clean,
blue,
bold,
format_duration,
mark_for_translation as _,
)


INFO_EVENT = Event()
QUIT_EVENT = Event()
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/utils_text.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from datetime import timedelta

from bundlewrap.utils.text import (
ansi_clean,
bold,
format_duration,
red,
parse_duration,
)


Expand All @@ -13,3 +17,34 @@ def test_ansi_clean():
assert len(red("test")) != len("test")
assert ansi_clean(red("test")) == "test"
assert ansi_clean(bold(red("test"))) == "test"


def test_format_duration():
assert format_duration(timedelta()) == "0s"
assert format_duration(timedelta(seconds=10)) == "10s"
assert format_duration(timedelta(minutes=10)) == "10m"
assert format_duration(timedelta(hours=10)) == "10h"
assert format_duration(timedelta(days=10)) == "10d"
assert format_duration(timedelta(days=1, hours=2, minutes=3, seconds=4)) == "1d 2h 3m 4s"


def test_parse_duration():
assert parse_duration("0s") == timedelta()
assert parse_duration("10s") == timedelta(seconds=10)
assert parse_duration("10m") == timedelta(minutes=10)
assert parse_duration("10h") == timedelta(hours=10)
assert parse_duration("10d") == timedelta(days=10)
assert parse_duration("1d 2h 3m 4s") == timedelta(days=1, hours=2, minutes=3, seconds=4)


def test_parse_format_inverse():
for duration in (
"0s",
"1s",
"1m",
"1h",
"1d",
"1d 4h",
"1d 4h 7s",
):
assert format_duration(parse_duration(duration)) == duration
37 changes: 0 additions & 37 deletions tests/unit/utils_time.py

This file was deleted.

0 comments on commit 1b4ca3c

Please sign in to comment.