Mercurial > touhou
annotate pytouhou/utils/xdg.py @ 728:414f8611f344
ecl: Add support for bullet sounds, instruction 84.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 30 Oct 2019 16:27:43 +0100 |
parents | b2269b9c6119 |
children |
rev | line source |
---|---|
567
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 # -*- encoding: utf-8 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 """ |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 This module is based on a rox module (LGPL): |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 http://cvs.sourceforge.net/viewcvs.py/rox/ROX-Lib2/python/rox/basedir.py?rev=1.9&view=log |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 The freedesktop.org Base Directory specification provides a way for |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 applications to locate shared data and configuration: |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 http://standards.freedesktop.org/basedir-spec/ |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 (based on version 0.6) |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 This module can be used to load and save from and to these directories. |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 Typical usage: |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 from rox import basedir |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 for dir in basedir.load_config_paths('mydomain.org', 'MyProg', 'Options'): |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 print "Load settings from", dir |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 dir = basedir.save_config_path('mydomain.org', 'MyProg') |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 print >>file(os.path.join(dir, 'Options'), 'w'), "foo=2" |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 Note: see the rox.Options module for a higher-level API for managing options. |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
27 """ |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 import os |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 _home = os.path.expanduser('~') |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 xdg_config_home = os.environ.get('XDG_CONFIG_HOME') or \ |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 os.path.join(_home, '.config') |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 xdg_config_dirs = [xdg_config_home] + \ |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 (os.environ.get('XDG_CONFIG_DIRS') or '/etc/xdg').split(':') |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 xdg_config_dirs = [x for x in xdg_config_dirs if x] |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 def save_config_path(*resource): |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 resource = os.path.join(*resource) |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 assert not resource.startswith('/') |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 path = os.path.join(xdg_config_home, resource) |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 if not os.path.isdir(path): |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 os.makedirs(path, 0o700) |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 return path |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 def load_config_paths(*resource): |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 resource = os.path.join(*resource) |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 for config_dir in xdg_config_dirs: |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 path = os.path.join(config_dir, resource) |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 if os.path.exists(path): |
b2269b9c6119
Add a configuration parser, and pass those options to argparse as defaults. Also include an xdg helper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 yield path |