Skip to content

Commit

Permalink
T5195: vyos.util -> vyos.utils package refactoring part #2
Browse files Browse the repository at this point in the history
  • Loading branch information
c-po committed Jul 15, 2023
1 parent 9285b9a commit 5f77ccf
Show file tree
Hide file tree
Showing 42 changed files with 265 additions and 404 deletions.
2 changes: 1 addition & 1 deletion python/vyos/configdep.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import typing
from inspect import stack

from vyos.util import load_as_module
from vyos.utils.system import load_as_module
from vyos.defaults import directories
from vyos.configsource import VyOSError
from vyos import ConfigError
Expand Down
2 changes: 1 addition & 1 deletion python/vyos/configdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def get_accel_dict(config, base, chap_secrets):
Return a dictionary with the necessary interface config keys.
"""
from vyos.util import get_half_cpus
from vyos.utils.system import get_half_cpus
from vyos.template import is_ipv4

dict = config.get_config_dict(base, key_mangling=('-', '_'),
Expand Down
19 changes: 11 additions & 8 deletions python/vyos/ifconfig/vrrp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019 VyOS maintainers and contributors <[email protected]>
# Copyright 2019-2023 VyOS maintainers and contributors <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand All @@ -21,8 +21,11 @@
from time import sleep
from tabulate import tabulate

from vyos import util
from vyos.configquery import ConfigTreeQuery
from vyos.utils.convert import seconds_to_human
from vyos.utils.file import read_file
from vyos.utils.file import wait_for_file_write_complete
from vyos.utils.process import process_running

class VRRPError(Exception):
pass
Expand Down Expand Up @@ -84,21 +87,21 @@ def decode_state(cls, code):
def is_running(cls):
if not os.path.exists(cls.location['pid']):
return False
return util.process_running(cls.location['pid'])
return process_running(cls.location['pid'])

@classmethod
def collect(cls, what):
fname = cls.location[what]
try:
# send signal to generate the configuration file
pid = util.read_file(cls.location['pid'])
util.wait_for_file_write_complete(fname,
pid = read_file(cls.location['pid'])
wait_for_file_write_complete(fname,
pre_hook=(lambda: os.kill(int(pid), cls._signal[what])),
timeout=30)

return util.read_file(fname)
return read_file(fname)
except OSError:
# raised by vyos.util.read_file
# raised by vyos.utils.file.read_file
raise VRRPNoData("VRRP data is not available (wait time exceeded)")
except FileNotFoundError:
raise VRRPNoData("VRRP data is not available (process not running or no active groups)")
Expand Down Expand Up @@ -145,7 +148,7 @@ def format(cls, data):
priority = data['effective_priority']

since = int(time() - float(data['last_transition']))
last = util.seconds_to_human(since)
last = seconds_to_human(since)

groups.append([name, intf, vrid, state, priority, last])

Expand Down
5 changes: 2 additions & 3 deletions python/vyos/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@
from requests.packages.urllib3 import PoolManager

from vyos.utils.io import ask_yes_no
from vyos.util import begin
from vyos.utils.process import cmd
from vyos.utils.io import make_incremental_progressbar
from vyos.utils.io import make_progressbar
from vyos.utils.io import print_error
from vyos.utils.misc import begin
from vyos.utils.process import cm
from vyos.version import get_version


CHUNK_SIZE = 8192

class InteractivePolicy(MissingHostKeyPolicy):
Expand Down
6 changes: 3 additions & 3 deletions python/vyos/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,19 @@ def force_to_list(value):
@register_filter('seconds_to_human')
def seconds_to_human(seconds, separator=""):
""" Convert seconds to human-readable values like 1d6h15m23s """
from vyos.util import seconds_to_human
from vyos.utils.convert import seconds_to_human
return seconds_to_human(seconds, separator=separator)

@register_filter('bytes_to_human')
def bytes_to_human(bytes, initial_exponent=0, precision=2):
""" Convert bytes to human-readable values like 1.44M """
from vyos.util import bytes_to_human
from vyos.utils.convert import bytes_to_human
return bytes_to_human(bytes, initial_exponent=initial_exponent, precision=precision)

@register_filter('human_to_bytes')
def human_to_bytes(value):
""" Convert a data amount with a unit suffix to bytes, like 2K to 2048 """
from vyos.util import human_to_bytes
from vyos.utils.convert import human_to_bytes
return human_to_bytes(value)

@register_filter('ip_from_cidr')
Expand Down
Loading

0 comments on commit 5f77ccf

Please sign in to comment.