[go: up one dir, main page]

Menu

[dfe9bc]: / dbr / workingdir.py  Maximize  Restore  History

Download this file

53 lines (39 with data), 1.5 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# ******************************************************
# * Copyright © 2016-2023 - Jordan Irwin (AntumDeluge) *
# ******************************************************
# * This software is licensed under the MIT license. *
# * See: LICENSE.txt for details. *
# ******************************************************
## @module dbr.workingdir
import os
from libdbr import config
from libdbr import paths
from libdbr.logger import Logger
logger = Logger(__name__)
## Changes working directory & writes to config
#
# This method should be called in code instead of os.chdir
# unless there is an explicit reason to do otherwise.
# \param target_dir
# \b \e str : Path to set as new working directory
def ChangeWorkingDirectory(target_dir):
if logger.debugging():
logger.debug("ChangeWorkingDirectory: {}".format(target_dir), newline=True)
print(" Working dir before: {}".format(os.getcwd()))
success = False
try:
os.chdir(target_dir)
cfg_user = config.get("user")
config_dir = cfg_user.getValue("workingdir", default=os.getcwd())
if config_dir != target_dir:
cfg_user.setValue("workingdir", target_dir)
cfg_user.save()
success = True
except OSError:
# Default to the user's home directory
dir_home = paths.getUserHome()
if os.path.isdir(dir_home):
os.chdir(dir_home)
if logger.debugging():
print(" Working dir after: {}\n".format(os.getcwd()))
return success