# CLN: A simplistic CLI using XML to store commands
# Copyright (C) 2009  Niels Serup
#  
#	This file is part of CLN.
#
#	CLN 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 3 of the License, or
#	(at your option) any later version.
#
#	CLN 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 CLN.  If not, see <http://www.gnu.org/licenses/>.

# defs.py will not work on its own, as it relies on several global
# variables defined in cln.py.

def fullscreen_change():
    # Either enters or exits fullscreen mode, depending on the current mode
    
    global height, screen, background
    if fullscreen:
        height = 30
        screen = pygame.display.set_mode((width * l_width, height * l_height), FULLSCREEN)
    else:
        height = 25
        screen = pygame.display.set_mode((width * l_width, height * l_height))

    background = pygame.Surface(screen.get_size()).convert()

def elemmatching(node = None, prefs = {}):
    # Does a simple search to see if an element exists.
    
    preflen = len(prefs)
    ok = True
    for x in range(preflen):
        okey = prefs.keys()[x]
        key = okey[:]
        kf = key.find(':')
        if kf > -1:
            colon = key.split(':')
            bc = colon[0]
            ac = colon[1]
            if bc == 'attr':
                key = 'attrib["' + ac + '"]'
            else: key = key[kf + 1:]
        else: key = key[kf + 1:]
        
        if prefs[okey][0] == '!':
            nprefs = prefs[okey][1:]
            equals = False
        else:
            nprefs = prefs[okey]
            equals = True
            
        try:
            n = eval('node.' +key)
            if (equals and n != nprefs) or (not equals and n == nprefs):
                ok = False
        except: pass
    return ok

def getsubitem(node = None, prefs = {}):
    # Get the first element "below" a specified element
    
    preflen = len(prefs)
    for snode in node:
        ok = elemmatching(snode, prefs)
        if ok: break
    if ok: return snode
    else: return None

def getsubitems(node = None, prefs = {}):
    # Get the elements "below" a specified element
    
    nodes = list()
    for snode in node:
        ok = elemmatching(snode, prefs)        
        if ok: nodes.append(snode)
    return nodes

def getvars(txt = ''):
    # Read and execute variable names and variables
    
    ns = list()
    vs = list()
    ls = list()
    txt = txt.split('\n')
    # Loop through lines in text
    for x in txt:
        s = x.strip()
        if len(s) > 0 and s[0] != '#' and s.find('=') != -1:
            # Only save if equal sign is present or line is comment
            spl = s.split('=')
            n = spl[0].rstrip()
            v = spl[1].lstrip()
            ns.append(n)
            vs.append(v)
            ls.append(x)
    
    for x in range(len(ns)):
        for i in range(2):
            try:
                # Prepare variable for execution
                if i == 0:
                    et = 'global ' + ns[x] + '\n'
                else:
                    et += ns[x] + ' = ' + vs[x]
                    exec(et)
            except:
                print 'Error in variables file on line', ls[x]
                break
    
def put(txt = ''):
    # Print in CLN window
    
    text.add(txt)
    text.setstop()

def get(txt = ''):
    # Input in CLN window
    
    if txt != '': put(txt)
    text.txt = ''
    text.getting = True # CLN stops other stuff 
    global freetogo
    freetogo = True

    while text.getting and not done:
        sleep(spf/1000)
    
    return text.txt

def y(txt):
    # If yes
    
    txt = txt.lower()
    if txt == 'y' or txt == 'yes':
        return 1
    else:
        return 0

def n(txt):
    # If no
    
    txt = txt.lower()
    if txt == 'n' or txt == 'no':
        return 1
    else:
        return 0