
'''
This software is licensed under the GPL (GNU General Public License) version 2
as it appears here: http://www.gnu.org/copyleft/gpl.html
It is also included with this archive as `gpl.txt <gpl.txt>`_.
'''

#----------------------------- configuration.py ------------------------------
#----------------------- Settings for configuring PyPE -----------------------

collapse_style = 0

#CTRL-T swaps two lines.  Setting the below to 1 disables this hotkey.
REM_SWAP = 1

import os
import sys
from wx.stc import STC_EOL_CRLF, STC_EOL_LF, STC_EOL_CR
import zlib

startup_path = os.getcwd()

fmt_mode = {"\r\n":STC_EOL_CRLF,
              "\n":STC_EOL_LF,
              "\r":STC_EOL_CR}
fmt_Rmode = {}
for i,j in fmt_mode.items():
    fmt_Rmode[j] = i

eol = os.linesep
eolmode = fmt_mode[eol]

def fp(path):
    if sys.platform=='win32':
        if ' ' in path:
            return '"%s"'%path
        return path
    return path.replace(' ', '\\ ')

def fixpath(path):
    return os.path.normpath(fp(path))

try:
    sorted
except NameError:
    def sorted(foo):
        a = list(foo)
        a.sort()
        return a

se = fixpath(sys.executable)
spawnargs = [se]

if sys.executable[-8:].lower() == 'pype.exe':
    runme = se
    runpath = se
else:
    runpath = os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
    b = fixpath(os.path.join(runpath, sys.argv[0]))
    runme = "%s %s"%(se,b)
    spawnargs.append(b)
    runpath = __file__

#toss all the " marks in path names if necessary
runpath = runpath.replace('"', '')
runpath = os.path.dirname(os.path.normpath(os.path.abspath(runpath)))

nosocket = os.path.exists(os.path.join(runpath, 'nosocket'))

stylefile = os.path.join(runpath, 'stc-styles.rc.cfg')

#for open/save dialogs
wildcard = "All python files (*.py *.pyw)|*.py;*.pyw|"\
           "Pyrex files (*.pyi *.pyx)|*.pyi;*.pyx|"\
           "HTML/XML files (*.htm* *.shtm* *.xml)|*.htm*;*.shtm*;*.xml|"\
           "C/C++ files (*.c* *.h)|*.c*;*.h|"\
           "Text files (*.txt)|*.txt|"\
           "Latex files (*.tex *.bib)|*.tex;*.bib|"\
           "All Files (*.*)|*.*"

#for style mappings from extensions
extns = {'py' : 'python',
        'pyw' : 'python',
        'spt' : 'python', # spitfire templates look better as Python than as html :P
       'tmpl' : 'python', # ditto for cheetah templates
        'pyi' : 'pyrex',
        'pyx' : 'pyrex',
          'c' : 'cpp',
         'cc' : 'cpp',
        'cpp' : 'cpp',
        'c++' : 'cpp',
        'cxx' : 'cpp',
          'h' : 'cpp',
       'java' : 'cpp',
         'js' : 'cpp',
        'htm' : 'html',
       'html' : 'html',
       'shtm' : 'html',
      'shtml' : 'html',
        'xml' : 'xml',
        'tex' : 'tex',
        'bib' : 'tex',
        'ini' : 'text',
        'txt' : 'text'}

default_homedir = os.path.dirname(os.path.abspath(__file__))
if hasattr(sys, 'frozen'):
    default_homedir = os.path.split(default_homedir)[0]

sa = 0
try:
    sa = getattr(_pype, '_standalone', 0)
except:
    pass

if sa:
    homedir = os.path.join(default_homedir, ".pype")
else:
    try:
        #all user-based OSes
        thd = os.path.expanduser("~")
        if thd == "~": raise
        homedir = os.path.join(thd, ".pype")
    except:
        try:
            #windows NT,2k,XP,etc. fallback
            homedir = os.path.join(os.environ['USERPROFILE'], ".pype")
        except:
            try:
                #windows NT,2k,XP,etc. fallback #2
                homedir = os.path.join(os.environ['HOMEDRIVE'], os.environ['HOMEPATH'], ".pype")
            except:
                try:
                    #*nix fallback
                    homedir = os.path.join(os.environ['HOME'], ".pype")
                except:
                    #What os are people using?
                    homedir = os.path.join(default_homedir, ".pype")

try:
    # create the config directory if it
    # doesn't already exist
    def expandfull(var, rem=3):
        if not rem:
            return os.path.expandvars(var)
        a = os.path.expandvars(var)
        b = []
        d = [b.extend(i.split('\\')) for i in a.split('/')]
        c = []
        for i in b:
            if '%' in i:
                c.append(expandfull(i, rem-1))
            else:
                c.append(i)
        return '\\'.join(c)
    if eol == "\r\n" and '%' in homedir:
        homedir = expandfull(homedir)
    if not os.path.exists(homedir):
        os.mkdir(homedir)
except:
    raise Exception("Unable to create config directory: %r"%(homedir))

for fil in os.listdir(homedir):
    if fil.find('.tmp.') > -1:
        try: os.remove(os.path.join(homedir, fil))
        except: pass

def validate(value, orig):
    try:
        a = int(value)
    except:
        a = 0
    if a < 1:
        return orig
    return a

def getData():
    a = zlib.decompress(
'x\xda\x01\xf4\x04\x0b\xfb\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \
\x00\x00\x00 \x08\x06\x00\x00\x00szz\xf4\x00\x00\x00\x04sBIT\x08\x08\x08\x08\
|\x08d\x88\x00\x00\x04\xabIDATx\x9c\xb5\x97\xcdk\x1bG\x18\xc6\x7f\x93f\x9d \
\xc5\xd2\xacI\x94\x14Y\x91\xe3B\x1a\xb0\x14(-!\x10\xd5\xd4\xa4\x1fid\x8a/=\
\xd4\xb9\xf4Tj\x9f\n=\xd4\xfe\x0b\xd2K\xae=\xe4n\xe5TP\x8b\x95@\xd3\x90b\xcb\
\xd4\x18\xd2\x84:\x854\x07G\xb6%bKP\xad\x94nh\xbd\x84\xe9a\xbd\xab]}Y\xf9\
\xe8\x03\x02\xed\xec\xec<\xcf\xfb\xcc;\xef\xcc\x08)%\x9d\x106\x0cU\x03\xc2@M\
J\xd1\xb1\xe3K`\x7f7\xf2\x82R\xee\xf3\x90\x10\xea\xff\x10\xd1V@39@A)\x86\x84\
\xf05>\xaf \xcb\n\xa9Xt\x9c\xc8\x91\x14\xba~\x1a\xd3\\o\x15\x106\x0c\x97D\
\x17\xf6\xf8\xd5]15\xcf\x7f\xe8\xdd\x15\xcb\n\xa9\xe4\xc8,\xc3C\x93\x9e\xb6:\
Ec\xb5!\xc0%\xceBm\x02\x1f\xb1#\xa6\xda\xc1\x95n"\x06dZ\x9d=\xf3\x1d\x00\xe5\
J\x9e\xb5G\x19\xb6+\x8bhZ]\xc0\xee\x14\x84\rC\x91\x85\xf5c\xf6G\xf2f{BGH\xb3\
\xb8n\x08\x06\x8f\x03\xb0\xbc2\xcd_FN\x00\xf4i1\xa5\xcbw\xd5ve\xb1\xe1\x80C\
\x0e`\xf47D4\xa3\x17\xe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Y\xbf\x90\xe4\xd4\xd84\xe5J\x1e\xc5\xa68y\x1c\xf5\xe7?\xf6\xbbn\
\xd1\xc3n\xd2yv\xd1\xf8V\x07\x01\xa6\xe9/kN\xf4\xfa\x85$\xef\x8f]\x07\xe0\
\xce\xdd\x19_\x9fn\xe4n\t\xf6\x9c3\xe2[\xc0\x84\xff>\xb1\xe7\x91\xac\xaf/\
\xcc\xdf\xe6\x06\xcb+_\x12\xb2\xa7A\x91\xdd\x83|7J\xdf9\x83F\x9b\xb7oG\x07\
\x9c\x13\xd2\xf6\x8fynp\x0e\x80\xea^ji\x8dr\xaf\x8bK\xc7\xf3@\xf3]\xa1\x17R\
\x07\xcfse\xebz \xf1\xde\x96\x9a\x85\xbc\x0ci\xcf\x02\xbcB\x9aK\xcf\xab\xba\
\xb2\xff\x07\n$<\xcc\x9c\x11r\xaf\x00\x00\x00\x00IEND\xaeB`\x82_YY\xda' )
    return a

# The remainder of this file is in the public domain.
# This version acquired from:
# http://www.cherrypy.org/browser/branches/ticket-132/cherrypy/lib/unrepr.py?rev=194
# JC - 7/18/2007 - Added support for True/False, added StringBuilder variant
#    for string-only unrepr operations.
# JC - 9/14/2007 - Added support for negative values.
import compiler

def getObj(s):
    s="a="+s
    return compiler.parse(s).getChildren()[1].getChildren()[0].getChildren()[1]

class UnknownType(Exception):
    pass

class _Builder:
    def build(self, o):
        m = getattr(self, 'build_'+o.__class__.__name__, None)
        if m is None:
            raise UnknownType(o.__class__.__name__)
        return m(o)

class StringBuilder(_Builder):
    def build_Const(self, o):
        if type(o.value) in (str, unicode):
            return o.value
        raise Exception

class Builder(_Builder):
    def build_List(self, o):
        return map(self.build, o.getChildren())
    def build_Const(self, o):
        return o.value
    def build_Dict(self, o):
        d = {}
        i = iter(map(self.build, o.getChildren()))
        for el in i:
            d[el] = i.next()
        return d
    def build_Tuple(self, o):
        return tuple(self.build_List(o))
    def build_Name(self, o):
        if o.name == 'None':
            return None
        elif o.name == 'True':
            return True
        elif o.name == 'False':
            return False
        raise UnknownType('Name: %r'%(o,))
    def build_Add(self, o):
        real, imag = map(self.build_Const, o.getChildren())
        try:
            real = float(real)
        except TypeError:
            raise UnknownType('Add')
        if not isinstance(imag, complex) or imag.real != 0.0:
            raise UnknownType('Add')
        return real+imag
    def build_UnarySub(self, o):
        return -self.build_Const(*o.getChildren())

def unrepr(s):
    return Builder().build(getObj(s))

#variant of 'unreprWrapper' by robinson
def str_unrepr(s):
    if not s:
        return s
    try:
        return StringBuilder().build(getObj(s))
    except:
        return s

class __LINE__(object):
    def __str__(self):
        return str(sys._getframe(1).f_lineno)
__LINE__ = __LINE__()
