Skip to content

Commit

Permalink
feat/more_file_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Nov 17, 2021
1 parent 837ad46 commit 718e0b3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
41 changes: 40 additions & 1 deletion ovos_utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,50 @@
import os
from os import walk
from os.path import splitext, join, dirname

import tempfile
from ovos_utils.bracket_expansion import expand_options
from ovos_utils.log import LOG


def get_temp_path(*args):
"""Generate a valid path in the system temp directory.
This method accepts one or more strings as arguments. The arguments are
joined and returned as a complete path inside the systems temp directory.
Importantly, this will not create any directories or files.
Example usage: get_temp_path('mycroft', 'audio', 'example.wav')
Will return the equivalent of: '/tmp/mycroft/audio/example.wav'
Args:
path_element (str): directories and/or filename
Returns:
(str) a valid path in the systems temp directory
"""
try:
path = os.path.join(tempfile.gettempdir(), *args)
except TypeError:
raise TypeError("Could not create a temp path, get_temp_path() only "
"accepts Strings")
return path


def get_cache_directory(folder):
# optional import to use ram for cache
# does not work in windows!
try:
from memory_tempfile import MemoryTempfile
except ImportError:
MemoryTempfile = None
if os.name == 'nt' or not MemoryTempfile:
path = get_temp_path(folder)
else:
path = join(MemoryTempfile(fallback=True).gettempdir(), folder)
os.makedirs(path, exist_ok=True)
return path


def resolve_ovos_resource_file(res_name):
"""Convert a resource into an absolute filename.
used internally for ovos resources
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='ovos_utils',
version='0.0.14a1',
version='0.0.14a2',
packages=['ovos_utils',
'ovos_utils.intents',
'ovos_utils.sound',
Expand Down

0 comments on commit 718e0b3

Please sign in to comment.