Completed
Push — master ( dfeea4...b9163a )
by John
03:32
created

migrate_logs()   A

Complexity

Conditions 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
#!/usr/bin/env python3
2 5
"""This module is used for generic configuration parsing."""
3
4 5
import configparser  # config parsing, duh
5 5
import os  # path work
6 5
import shutil  # moving
7 5
from appdirs import AppDirs  # folders
0 ignored issues
show
Configuration introduced by
The import appdirs could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
8
9 5
__author__ = "Thurask"
10 5
__license__ = "WTFPL v2"
11 5
__copyright__ = "2016-2018 Thurask"
12
13
14 5
def config_emptysection(config, section):
15
    """
16
    Create empty configuration section.
17
18
    :param config: Configuration dictionary.
19
    :type config: configparser.ConfigParser
20
21
    :param section: Section of ini file to return.
22
    :type section: str
23
    """
24 5
    if not config.has_section(section):
25 5
        config[section] = {}
26 5
    return config
27
28
29 5
def config_homepath(homepath, logpath=False):
30
    """
31
    Fix path for ini file.
32
33
    :param homepath: Path to ini file.
34
    :type homepath: str
35
36
    :param logpath: True if processing log folder, False if processing data files. Default is False.
37
    :type logpath: bool
38
    """
39 5
    if homepath is None:
40 5
        rawpath = config_rawdir(homepath, logpath)
41 5
        homepath = process_homepath(rawpath, logpath)
42 5
    return homepath
43
44
45 5
def config_rawdir(homepath, logpath=False):
0 ignored issues
show
Unused Code introduced by
The argument homepath seems to be unused.
Loading history...
46
    """
47
    Get new config dir.
48
49
    :param homepath: Path to data directory.
50
    :type homepath: str
51
52
    :param logpath: True if processing log folder, False if processing data files. Default is False.
53
    :type logpath: bool
54
    """
55 5
    apdi = AppDirs("bbarchivist", "bbarchivist")
56 5
    if logpath:
57 5
        rawpath = apdi.user_log_dir
58
    else:
59 5
        rawpath = apdi.user_data_dir
60 5
    return rawpath
61
62
63 5
def process_homepath(homepath, logpath=False):
64
    """
65
    Prepare homepath if it doesn't exist.
66
67
    :param homepath: Path to data directory.
68
    :type homepath: str
69
70
    :param logpath: True if processing log folder, False if processing data files. Default is False.
71
    :type logpath: bool
72
    """
73 5
    if not os.path.exists(homepath):
74 5
        os.makedirs(homepath)
75 5
    if logpath:
76 5
        migrate_logs(homepath)
77
    else:
78 5
        migrate_files(homepath)
79 5
    return homepath
80
81
82 5
def migrate_files(homepath):
83
    """
84
    Prepare ini file and SQL DB for new homepath.
85
86
    :param homepath: Path to data directory.
87
    :type homepath: str
88
    """
89 5
    files = ("bbarchivist.ini", "bbarchivist.db")
90 5
    for file in files:
91 5
        oldfile = os.path.join(os.path.expanduser("~"), file)
92 5
        newfile = os.path.join(homepath, file)
93 5
        conditional_move(oldfile, newfile)
94
95
96 5
def migrate_logs(homepath):
97
    """
98
    Prepare log directory for new homepath.
99
100
    :param homepath: Path to data directory.
101
    :type homepath: str
102
    """
103 5
    olddir = os.path.join(os.path.expanduser("~"), "lookuplogs")
104 5
    if os.path.exists(olddir):
105 5
        log_move(olddir, homepath)
106 5
        shutil.rmtree(olddir)
107
108
109 5
def conditional_move(oldfile, newfile):
110
    """
111
    Migrate from user directory to dedicated appdata/.local dir.
112
113
    :param oldfile: Path to old config file.
114
    :type oldfile: str
115
116
    :param newfile: Path to new config file.
117
    :type newfile: str
118
    """
119 5
    if os.path.exists(oldfile):
120 5
        if os.path.exists(newfile):
121 5
            os.remove(oldfile)  # remove old
122
        else:
123 5
            shutil.move(oldfile, newfile)  # migrate to new
124
125
126 5
def log_move(olddir, homepath):
127
    """
128
    Migrate logs from user directory subfolder to dedicated appdata/.local dir.
129
130
    :param olddir: Path to old log dir.
131
    :type olddir: str
132
133
    :param homepath: Path to new log directory.
134
    :type homepath: str
135
    """
136 5
    oldlogs = [os.path.join(olddir, logfile) for logfile in os.listdir(olddir)]
137 5
    if oldlogs:
138 5
        for log in oldlogs:
139 5
            shutil.move(log, os.path.abspath(homepath))
140
141
142 5
def config_conffile(conffile):
143
    """
144
    Create ini file if it doesn't exist.
145
146
    :param conffile: Path to config ini file.
147
    :type conffile: str
148
    """
149 5
    if not os.path.exists(conffile):
150 5
        open(conffile, 'w').close()
151
152
153 5
def config_location(homepath=None):
154
    """
155
    Return config location.
156
157
    :param homepath: Folder containing ini file. Default is user directory.
158
    :type homepath: str
159
    """
160 5
    homepath = config_homepath(homepath)
161 5
    conffile = os.path.join(homepath, "bbarchivist.ini")
162 5
    return conffile
163
164
165 5
def generic_preamble(section, homepath=None):
166
    """
167
    Read a ConfigParser file, return whole config.
168
169
    :param section: Section of ini file to return.
170
    :type section: str
171
172
    :param homepath: Folder containing ini file. Default is user directory.
173
    :type homepath: str
174
    """
175 5
    config = configparser.ConfigParser()
176 5
    conffile = config_location(homepath)
177 5
    config_conffile(conffile)
178 5
    config.read(conffile)
179 5
    config = config_emptysection(config, section)
180 5
    return config
181
182
183 5
def generic_loader(section, homepath=None):
184
    """
185
    Read a ConfigParser file, return section.
186
187
    :param section: Section of ini file to return.
188
    :type section: str
189
190
    :param homepath: Folder containing ini file. Default is user directory.
191
    :type homepath: str
192
    """
193 5
    config = generic_preamble(section, homepath)
194 5
    ini = config[section]
195 5
    return ini
196
197
198 5
def generic_writer(section, resultdict, homepath=None):
199
    """
200
    Write a ConfigParser file.
201
202
    :param section: Section of ini file to write.
203
    :type section: str
204
205
    :param resultdict: Dictionary of configs: {key: value}
206
    :type resultdict: dict({str, bool})
207
208
    :param homepath: Folder containing ini file. Default is user directory.
209
    :type homepath: str
210
    """
211 5
    config = generic_preamble(section, homepath)
212 5
    for key, value in resultdict.items():
213 5
        config.set(section, key, str(value))
214 5
    conffile = config_location(homepath)
215 5
    with open(conffile, "w") as configfile:
216
        config.write(configfile)
217