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

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

"""Configuration management for lingpy. 

 

Various aspects of lingpy can be configured and customized by the user. This is done with 

configuration files in the user's config dir. 

 

.. seealso:: https://pypi.python.org/pypi/appdirs/ 

""" 

from __future__ import unicode_literals, print_function, absolute_import, division 

import io 

 

from appdirs import user_config_dir 

from six import PY3 

from six.moves.configparser import RawConfigParser 

from clldutils.path import Path 

 

DIR = Path(user_config_dir('lingpy')) 

 

 

class Config(RawConfigParser): 

def __init__(self, name, default=None, **kw): 

"""Initialization. 

 

:param name: Basename for the config file (suffix .ini will be appended). 

:param default: Default content of the config file. 

""" 

self.name = name 

self.default = default 

config_dir = Path(kw.pop('config_dir', None) or DIR) 

RawConfigParser.__init__(self, kw, allow_no_value=True) 

if self.default: 

if PY3: 

fp = io.StringIO(self.default) 

else: 

fp = io.BytesIO(self.default.encode('utf8')) 

self.readfp(fp) 

 

cfg_path = config_dir.joinpath(name + '.ini') 

if cfg_path.exists(): 

assert cfg_path.is_file() 

self.read(cfg_path.as_posix()) 

else: 

if not config_dir.exists(): 

try: 

config_dir.mkdir() 

except OSError: # pragma: no cover 

# this happens when run on travis-ci, by a system user. 

pass 

if config_dir.exists(): 

with open(cfg_path.as_posix(), 'w') as fp: 

self.write(fp) 

self.path = cfg_path