-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikipediatools.py
64 lines (57 loc) · 2.23 KB
/
wikipediatools.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
#
# (C) Pywikipedia bot team, 2006-2013
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id: addb2e90dc6cd8cc539d69b8117835fef925a2ec $'
#
import os
import sys
def create_user_config_file(base_dir):
import generate_user_files
generate_user_files.create_user_config(base_dir)
def get_base_dir():
"""Return the directory in which user-specific information is stored.
This is determined in the following order -
1. If the script was called with a -dir: argument, use the directory
provided in this argument
2. If the user has a PYWIKIBOT_DIR environment variable, use the value
of it
3. If the script was started from a directory that contains a
user-config.py file, use this directory as the base
4. If all else fails, use the directory from which this module was
loaded.
5. If the user-config.py file is not found, another will be created
in the current directory, following in the footsteps of project,
language and bot username.
"""
for arg in sys.argv[1:]:
if arg.startswith("-dir:"):
base_dir = arg[5:]
break
else:
if "PYWIKIBOT_DIR" in os.environ:
base_dir = os.environ["PYWIKIBOT_DIR"]
else:
if os.path.exists('user-config.py'):
base_dir = '.'
else:
try:
base_dir = os.path.split(
sys.modules['wikipediatools'].__file__)[0]
except KeyError:
print sys.modules
base_dir = '.'
if not os.path.isabs(base_dir):
base_dir = os.path.normpath(os.path.join(os.getcwdu(), base_dir))
# make sure this path is valid and that it contains user-config file
if not os.path.isdir(base_dir):
raise RuntimeError("Directory '%s' does not exist." % base_dir)
if not os.path.exists(os.path.join(base_dir, "user-config.py")):
print("No user-config.py found in directory '%s'" % base_dir)
called = os.path.basename(sys.argv[0].strip()).split('.')[0]
if called != 'version':
print("Creating...\n")
create_user_config_file(base_dir)
return base_dir