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

Refactoring and improvement: Config class is now a singleton #1307

Merged
merged 26 commits into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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 common/askpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""What is this modul about?
"""
buhtz marked this conversation as resolved.
Show resolved Hide resolved

import os
import sys
try:
Expand All @@ -39,18 +42,23 @@
if mode == 'USER':
prompt = os.getenv('ASKPASS_PROMPT', None)
pw = password.Password(cfg)
print(pw.passwordFromUser(None, prompt = prompt))

print(pw.passwordFromUser(None, prompt=prompt))

sys.exit(0)

temp_file = os.getenv('ASKPASS_TEMP')

if temp_file is None:
#normal mode, get password from module password
# normal mode, get password from module password
pw = password.Password(cfg)
print(pw.password(None, profile_id, mode))

sys.exit(0)

#temp mode
# temp mode
fifo = password_ipc.FIFO(temp_file)
pw = fifo.read(5)

if pw:
print(pw)
43 changes: 32 additions & 11 deletions common/backintime.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from exceptions import MountException
from applicationinstance import ApplicationInstance

_=gettext.gettext
_ = gettext.gettext

RETURN_OK = 0
RETURN_ERR = 1
Expand Down Expand Up @@ -639,6 +639,7 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, 'replace', replace)
setattr(namespace, 'alias', alias)


def aliasParser(args):
"""
Call commands which where given with leading -- for backwards
Expand All @@ -656,14 +657,14 @@ def aliasParser(args):
if 'func' in dir(newArgs):
newArgs.func(newArgs)

def getConfig(args, check = True):

def getConfig(args, check=True):
"""
Load config and change to profile selected on commandline.

Args:
args (argparse.Namespace):
previously parsed arguments
check (bool): if ``True`` check if config is valid
args (argparse.Namespace): Previously parsed arguments
check (bool): Ff ``True`` check if config is valid
buhtz marked this conversation as resolved.
Show resolved Hide resolved

Returns:
config.Config: current config with requested profile selected
Expand All @@ -672,27 +673,42 @@ def getConfig(args, check = True):
SystemExit: 1 if ``profile`` or ``profile_id`` is no valid profile
2 if ``check`` is ``True`` and config is not configured
"""
cfg = config.Config(config_path = args.config, data_path = args.share_path)
cfg = config.Config(
config_path=args.config,
data_path=args.share_path
)

logger.debug('config file: %s' % cfg._LOCAL_CONFIG_PATH)
logger.debug('share path: %s' % cfg._LOCAL_DATA_FOLDER)
logger.debug('profiles: %s' % ', '.join('%s=%s' % (x, cfg.profileName(x))
for x in cfg.profiles()))
logger.debug('profiles: %s' % ', '
.join('%s=%s' % (x, cfg.profileName(x))
for x in cfg.profiles()))

if 'profile_id' in args and args.profile_id:

if not cfg.setCurrentProfile(args.profile_id):
logger.error('Profile-ID not found: %s' % args.profile_id)

sys.exit(RETURN_ERR)

if 'profile' in args and args.profile:

if not cfg.setCurrentProfileByName(args.profile):
logger.error('Profile not found: %s' % args.profile)

sys.exit(RETURN_ERR)

if check and not cfg.isConfigured():
logger.error('%(app)s is not configured!' %{'app': cfg.APP_NAME})

sys.exit(RETURN_NO_CFG)

if 'checksum' in args:
cfg.forceUseChecksum = args.checksum

return cfg


def setQuiet(args):
"""
Redirect :py:data:`sys.stdout` to ``/dev/null`` if ``--quiet`` was set on
Expand Down Expand Up @@ -737,15 +753,20 @@ def __init__(self, *args, **kwargs):
super(printDiagnostics, self).__init__(*args, **kwargs)

def __call__(self, *args, **kwargs):
"""
"""

cfg = config.Config()

# TODO Refactor into a separate functions in a new diagnostics.py when more info is added
# TODO Refactor into a separate functions in a new diagnostics.py
# when more info is added
ref, hashid = tools.gitRevisionAndHash()
git_branch = "Unknown"
git_commit = "Unknown"

if ref:
git_branch = ref
git_commit = hashid
git_branch = ref
git_commit = hashid

diagnostics = dict(
app_name=config.Config.APP_NAME,
Expand Down
Loading