Crombie Tools
ConfigTools.py
Go to the documentation of this file.
1 """ @file ConfigTools.py
2 Defines the CrombieTools.ConfigTools package
3 @author Daniel Abercrombie <dabercro@mit.edu>
4 
5 @package CrombieTools.ConfigTools
6 These are quick parsers for the FileConfigs for minor things that might be needed in Python.
7 @todo Use an actual FileConfigReader in here.
8 """
9 
10 import os
11 from collections import defaultdict
12 
13 FileLists = defaultdict(lambda: [])
14 
15 class Line(object):
16  lasttree = ''
17  lastentry = ''
18  lastcolor = ''
19  def __init__(self, in_line):
20  split_line = in_line.lstrip('#').split()
21  self.saved = not in_line.startswith('#')
22  self.treename = split_line[0] if split_line[0] != '.' else Line.lasttree
23  self.file = split_line[1]
24  self.xs = split_line[2]
25  self.entry = split_line[3] if split_line[3] != '.' else Line.lastentry
26  self.color = ' '.join(split_line[4:]) if split_line[4] != '.' else Line.lastcolor
27  self.raw = in_line
28 
29  Line.lasttree = self.treename
30  Line.lastentry = self.entry
31  Line.lastcolor = self.color
32 
33 
34 def TreeList(*args):
35  """Get list of tree names from a config file
36 
37  @param args are the file locations of the Configuration file to read the tree lists from
38  """
39 
40  output = set()
41 
42  for config in args:
43  if not os.path.exists(config):
44  print '%s does not exist!' % config
45  exit(40)
46 
47  with open(config, 'r') as in_file:
48  for in_line in in_file.readlines():
49 
50  split_line = in_line.split()
51 
52  if split_line:
53  raw = split_line[0]
54  treename = raw.lstrip('#')
55  if treename != '.':
56  lastname = treename
57  if raw[0] != '#':
58  output.add(lastname)
59  FileLists[lastname].append(split_line[1])
60 
61  return output