from insupportable import support
if support('PY2'):
    print("You are on python 2")
else:
    print("You are on python 3")

"""
Explanation: Built-in quick support for Python 2 and 3
As a developper enable warnings, (or use python -Wd)
You support both Python 2 and Python 3 ?
End of explanation
"""


from insupportable import PY2,PY3 # PY2, PY3, for convenience and avoid typos :-) 

support.config(PY2=False)

if support(PY3):
    print("You are on python 2")
else:
    print("You are on python 3")

"""
Explanation: Drop Python 2 support:
End of explanation
"""


support.config(PY2=False, PY3=True) # this woudl be in config file

if support(PY3):
    print("You are on python 3")
else:
    print("You are on python 2")

"""
Explanation: (I'm writing that in IPython, so stack is wrong here)
works also if I test if(PY3) !
End of explanation
"""


support.config(({'WindowsPhone':True,
              'Android':True,
              'iOs':False
             },))

if support('WindowsPhone'):
    print('Click on start menu')
else:
    print('Probably Android')

"""
Explanation: Say you use to support 3 platforms,
End of explanation
"""


support.config(config=({'WindowsPhone':True,
              'Android':False,
              'iOs':False
             },))

support.config(config=({'WindowsPhone':True,
              'Android':False,
              'iOs':False
             },))
if support('WindowsPhone'):
    print('Click on start menu')
else:
    print("Probably Android - but you don't support it anymore")


if support('WindowsPhone'):
    print('Click on start menu')
else:
    print("Probably Android - but you don't support it anymore")

"""
Explanation: And actually you decide to drop android
End of explanation
"""


from insupportable import support

if support('PY1'):
    # Well, we need some work around for old versions:'
    import sys
    sys.stdout.write('in python one you had to write to sys !')

 

"""
Explanation: Don't get in your way.
Nothing more anoying that having to write the configuration before actually having working code. support assume by default that you support the feature though it will warn that it does not know it. So you can just set up everything after.
End of explanation
"""


import insupportable

insupportable.predicates

support('PY35+')

import insupportable
from insupportable import support

def jp():
    import sys
    return ('jupyter_notebook' in sys.modules)

support.s.add_feature('JUPYTER', jp, supported=True)


support('JUPYTER')

class Item(object):
    
    def __init__(self, name, predicate=True, support=True):
        """An item in a discret support set.
        
        An item is consituted of 
        - a name (string)
        - a predicate (`Bool | funtion:()->Bool`) to determine if we are typically in this case.
        
        - the support status (bool)
        
        """
        self.name = name
        if callabale(predicate):
            predicate = predicate()
        self.predicate = predicate
        self.support = support
        
    
        


support.s.predicates

"""
Explanation: It gently warn you that maybe you should tell it wether or not you stil support PY1, which let you write code and decide wether ot not to support later. 
Minor python versions
End of explanation
"""


import insupportable

def fun(a, *args):
    print(args)


fun(1,2,3)

DF

from insupportable import DiscreatFeature as DF, FeatureGroup#, FeatureTracker

PY2 = DF('PY2', lambda x: sys.version_info.major == 2)
PY3 = DF('PY3', lambda x: sys.version_info.major == 3)
JPY = DF('JUPYTER', lambda x: True)
IPY = DF('IPYTHON', lambda x: False)


group_py = FeatureGroup(PY2,PY3)
group_jpy = FeatureGroup(JPY,IPY)

vx = lambda: re.compile('^(\S+) ?(\d+)\.(\d+)(\+|-)?$')

class NumeralFeature(object):
    
    def __init__(self ,name, predicate):
        self.name = name
        self.features = []
        if callable(predicate):
            self.current_version = predicate()
        else : 
            self.current_version = predicate

import re

import sys
pyv = NumeralFeature('python', lambda :sys.version_info)

print(vx().search('foo2.3').groups())
print(vx().search('foo2.3+').groups())
print(vx().search('tornado 2.3-').groups())
print(vx().search('python 2.7+').groups())


class FeatureTracker(object):

    def __init__(self, *args):
        self.args = args

        self._known_versions = [f.name for g in args for f in g.features if type(f) == DF]
        self._known_numerals = [g.name for g in args if type(g) == NumeralFeature]
        self._numeral_map = {}
        for g in args:
            if type(g) == NumeralFeature:
                self._numeral_map[g.name] = g.current_version
            else:
                continue




    def support(self, indetifier):
        pass
    
    def know(self, identifier):
        if identifier in self._known_versions:
            return True
        elif vx().match(identifier):
            feat = vx().search(identifier).groups()
            print(tuple(map(int, feat[1:3])) >= self._numeral_map[feat[0]][:2])
            return feat[0] in self._known_numerals
        
    
        
        
ft = FeatureTracker(group_jpy, group_py, pyv)

ft._numeral_map

ft.know('python 3.5+')

FeatureTracker

"""
Explanation: technical
End of explanation
"""


