536b622cdef25abb46ca916e4d2e318f0fa40b82
[stack/conf/vim.git] / addons / TagHighlight / plugin / TagHighlight / module / config.py
1 #!/usr/bin/env python
2 # Tag Highlighter:
3 #   Author:  A. S. Budden <abudden _at_ gmail _dot_ com>
4 # Copyright: Copyright (C) 2009-2011 A. S. Budden
5 #            Permission is hereby granted to use and distribute this code,
6 #            with or without modifications, provided that this copyright
7 #            notice is copied with it. Like anything else that's free,
8 #            the TagHighlight plugin is provided *as is* and comes with no
9 #            warranty of any kind, either expressed or implied. By using
10 #            this plugin, you agree that in no event will the copyright
11 #            holder be liable for any damages resulting from the use
12 #            of this software.
13
14 # ---------------------------------------------------------------------
15 import sys
16 import os
17
18 from optparse import Values
19 from .utilities import TagHighlightOptionDict
20 from .loaddata import LoadFile, LoadDataFile, SetLoadDataDirectory
21 from .debug import SetDebugLogFile, SetDebugLogLevel, Debug
22
23 config = TagHighlightOptionDict()
24
25 def SetDataDirectories():
26     global config
27     if hasattr(sys, 'frozen'):
28         # Compiled variant, executable should be in
29         # plugin/TagHighlight/Compiled/Win32, so data
30         # is in ../../data relative to executable
31         config['data_directory'] = os.path.abspath(
32                 os.path.join(os.path.dirname(sys.executable),
33                 '../../data'))
34         config['version_info_dir'] = os.path.abspath(os.path.dirname(sys.executable))
35     else:
36         # Script variant: this file in
37         # plugin/TagHighlight/module, so data is in
38         # ../data relative to this file
39         config['data_directory'] = os.path.abspath(
40                 os.path.join(os.path.dirname(__file__),
41                 '../data'))
42         config['version_info_dir'] = config['data_directory']
43
44     SetLoadDataDirectory(config['data_directory'])
45
46     if not os.path.exists(config['data_directory']):
47         raise IOError("Data directory doesn't exist, have you installed the main distribution?")
48
49 def LoadVersionInfo():
50     global config
51     data = LoadDataFile('release.txt')
52     config['release'] = data['release']
53
54     try:
55         config['version'] = LoadFile(os.path.join(config['version_info_dir'],'version_info.txt'))
56     except IOError:
57         config['version'] = {
58                 'clean': 'Unreleased',
59                 'date': 'Unreleased',
60                 'revision_id': 'Unreleased',
61                 }
62
63 def SetInitialOptions(new_options):
64     global config
65     for key in new_options:
66         config[key] = new_options[key]
67     if 'debug_level' in config:
68         SetDebugLogLevel(config['debug_level'])
69     if 'debug_file' in config:
70         SetDebugLogFile(config['debug_file'])
71
72 def LoadLanguages():
73     global config
74     if 'language_handler' in config:
75         return
76     from .languages import Languages
77     config['language_handler'] = Languages(config)
78
79     full_language_list = config['language_handler'].GetAllLanguages()
80     if len(config['languages']) == 0:
81         # Include all languages
82         config['language_list'] = full_language_list
83     else:
84         config['language_list'] = [i for i in full_language_list if i in config['languages']]
85     Debug("Languages:\n\t{0!r}\n\t{1!r}".format(full_language_list, config['language_list']), "Information")
86
87 SetDataDirectories()
88 LoadVersionInfo()