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
14 # ---------------------------------------------------------------------
15 from __future__ import print_function
21 def SetLoadDataDirectory(directory):
23 data_directory = directory
25 def LoadFile(filename, list_entries=[]):
27 fh = open(filename, 'r')
30 if line.strip().endswith(':') and line[0] not in [' ','\t',':','#']:
31 key = line.strip()[:-1]
32 elif key is not None and line.startswith('\t'):
34 # Dict entry, split onto multiple lines
35 parts = line.strip().split(':',1)
36 if key not in results:
38 elif not isinstance(results[key], dict):
39 raise ValueError("Mixed data types in data file {file} for entry {key}".format(filename, key))
40 if parts[0] in list_entries:
41 results[key][parts[0]] = parts[1].split(',')
43 results[key][parts[0]] = parts[1]
45 # List entry, split onto multiple lines
46 if key not in results:
48 elif not isinstance(results[key], list):
49 raise ValueError("Mixed data types in data file {file} for entry {key}".format(filename, key))
50 results[key].append(line.strip())
51 elif ':' in line and line[0] not in [' ','\t',':','#']:
52 # End of previous list: was it a real list or
54 if key not in results:
55 # Empty entry: add as such
56 if key in list_entries:
61 parts = line.strip().split(':',1)
62 if parts[0] in list_entries:
63 # Registered list entry: split on commas
64 results[parts[0]] = parts[1].split(',')
67 results[parts[0]] = parts[1]
71 def LoadDataFile(relative, list_entries=[]):
72 filename = os.path.join(data_directory,relative)
73 return LoadFile(filename, list_entries)
75 def GlobData(matcher):
76 files = glob.glob(os.path.join(data_directory, matcher))
77 return [os.path.relpath(i,data_directory) for i in files]