Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

65

66

67

68

69

70

71

# -*- coding: utf-8 -*- 

 

import os 

 

import pymysql 

import yaml 

 

from pywikibot import config as pywikibot_config 

 

DEFAULT_CONFIG_FILE_NAME = 'database_config.default.yml' 

OVERRIDE_CONFIG_FILE_NAME = 'database_config.yml' 

 

 

def _get_current_directory(): 

"""Return the absolute path of the current file.""" 

return os.path.dirname(os.path.abspath(__file__)) 

 

 

def _get_database_config_file(): 

current_dir = _get_current_directory() 

config_file = os.path.join(current_dir, DEFAULT_CONFIG_FILE_NAME) 

override_config_file = os.path.join(current_dir, OVERRIDE_CONFIG_FILE_NAME) 

if os.path.isfile(override_config_file): 

config_file = override_config_file 

return config_file 

 

 

def _get_database_config(): 

config_file = _get_database_config_file() 

return yaml.safe_load(open(config_file, 'r')) 

 

 

def get_monuments_database_config(): 

return _get_database_config()['monuments_db'] 

 

 

def get_commons_database_config(): 

return _get_database_config()['commons_db'] 

 

 

def connect_to_monuments_database(): 

"""Connect to the mysql monuments database.""" 

db_config = get_monuments_database_config() 

username = db_config.get('username', pywikibot_config.db_username) 

password = db_config.get('password', pywikibot_config.db_password) 

conn = pymysql.connect( 

host=db_config['server'], db=db_config['db_name'], 

user=username, passwd=password, 

use_unicode=True, charset='utf8') 

conn.ping(True) 

cursor = conn.cursor() 

return (conn, cursor) 

 

 

def connect_to_commons_database(): 

"""Connect to the commons mysql database.""" 

db_config = get_commons_database_config() 

username = os.environ.get('DB_USERNAME') or db_config.get('username', pywikibot_config.db_username) 

password = os.environ.get('DB_PASSWORD') or db_config.get('password', pywikibot_config.db_password) 

conn = pymysql.connect( 

host=db_config['server'], db=db_config['db_name'], 

user=username, passwd=password, 

use_unicode=True, charset='latin1') 

cursor = conn.cursor() 

return (conn, cursor) 

 

 

def close_database_connection(conn, cursor): 

"""Close the cursor and commit the current transactions.""" 

conn.commit() 

cursor.close()