Package fuzzy :: Package storage :: Package fcl :: Module Reader
[hide private]
[frames] | no frames]

Source Code for Module fuzzy.storage.fcl.Reader

 1  #!/usr/bin/env python 
 2  # 
 3  # Copyright (C) 2009  Rene Liebscher 
 4  # 
 5  # This program is free software; you can redistribute it and/or modify it under 
 6  # the terms of the GNU Lesser General Public License as published by the Free  
 7  # Software Foundation; either version 3 of the License, or (at your option) any 
 8  # later version. 
 9  # 
10  # This program is distributed in the hope that it will be useful, but WITHOUT  
11  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
12  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
13  # details. 
14  #  
15  # You should have received a copy of the GNU Lesser General Public License 
16  # along with this program; if not, see <http://www.gnu.org/licenses/>.  
17  # 
18  """Load a fuzzy system from FCL file, stream or string.""" 
19   
20  __revision__ = "$Id: Reader.py,v 1.6 2013-01-09 20:10:19 rliebscher Exp $" 
21   
22  # antlr3 uses sys.maxint which is removed in python3 
23  import sys 
24  try: 
25      sys.maxint 
26  except: 
27      sys.maxint = sys.maxsize 
28   
29  import antlr3 
30   
31  try: 
32      from fuzzy.storage.fcl.FCLLexer import FCLLexer 
33  except: 
34      from fuzzy.storage.fcl.FCLLexer3 import FCLLexer 
35  from fuzzy.storage.fcl.FCLParser import FCLParser 
36   
37 -class Reader(object):
38 """Parses a FCL file to a fuzzy.System.System instance""" 39
40 - def __load(self, char_stream):
41 """Common part of load methods.""" 42 lexer = FCLLexer(char_stream) 43 tokens = antlr3.CommonTokenStream(lexer) 44 parser = FCLParser(tokens) 45 return parser.main()
46
47 - def load_from_file(self, filename):
48 """Load a fuzzy system from FCL file.""" 49 encoding = None 50 f = None 51 try: 52 # read first line 53 f = open(filename) 54 line = f.readline() 55 import re 56 # check for coding 57 result = re.search(r'coding[=:]\s*([-\w.]+)', line) 58 if result: 59 # found one and use it 60 encoding = result.group(1) 61 except: 62 # ok, then try without encoding 63 pass 64 if f: 65 f.close() 66 return self.__load(antlr3.ANTLRFileStream(filename, encoding))
67
68 - def load_from_stream(self, stream):
69 """Load a fuzzy system from FCL stream.""" 70 return self.__load(antlr3.ANTLRInputStream(stream))
71
72 - def load_from_string(self, str):
73 """Load a fuzzy system from FCL string.""" 74 return self.__load(antlr3.ANTLRStringStream(str))
75