#!/bin/env python
# -*- coding: utf-8; -*-
#
# (c) 2016 FABtotum, http://www.fabtotum.com
#
# This file is part of FABUI.
#
# FABUI is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# FABUI is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FABUI.  If not, see <http://www.gnu.org/licenses/>.

# Import standard python module
import re

class Parser:
    def __init__(self):
        self.simplify_layer = re.compile('layer\s')
        self.simplify_engine = re.compile('generated by Simplify3D')
    @staticmethod
    def _is_number(s):
        try:
            float(s)
            return True
        except ValueError:
            return False
        
    def is_simplify(self, line):
        if self.simplify_engine.search(line):
            return True
        return False
        
    def process_line(self, line):
        attrs = {}
        line = line.lower()
        try:
            if line[0] == ';':       
                tags = line.strip().split(';')
                if len(tags) == 2:
                    if self.simplify_layer.search(tags[1]):
                        tag, value = tags[1].strip().split(',')[0].split(' ')
                        if(self._is_number(value.strip())):
                            attrs['layer'] = value.strip()
        except Exception, e:
            print str(e)
        return attrs
