__init__.py 2.97 KB
Newer Older
Sylvain Thénault's avatar
cleanup    
Sylvain Thénault committed
1
# copyright 2004-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of yams.
#
# yams is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# yams 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 Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with yams. If not, see <http://www.gnu.org/licenses/>.
18
"""Object model and utilities to define generic Entities/Relations schemas.
root's avatar
root committed
19
"""
20
__docformat__ = "restructuredtext en"
21
22

from datetime import datetime, date, time
root's avatar
root committed
23

Sylvain's avatar
Sylvain committed
24
25
26
27
# set _ builtin to unicode by default, should be overriden if necessary
import __builtin__
__builtin__._ = unicode

28
29
30
31
32
33
34
from logilab.common.date import strptime, strptime_time
from logilab.common import nullobject

from yams.__pkginfo__ import version as __version__
from yams._exceptions import *

MARKER = nullobject()
35

36
BASE_TYPES = set(('String', 'Password', 'Bytes',
37
                  'Int', 'BigInt', 'Float', 'Boolean', 'Decimal',
38
39
                  'Date', 'Time', 'Datetime', 'TZTime', 'TZDatetime', 'Interval'
                  ))
Sylvain's avatar
Sylvain committed
40

41
# base groups used in permissions
Sylvain Thénault's avatar
Sylvain Thénault committed
42
BASE_GROUPS = set((_('managers'), _('users'), _('guests'), _('owners')))
43

44
45
KEYWORD_MAP = {'Datetime.NOW' : datetime.now,
               'Datetime.TODAY': datetime.today,
46
47
               'TZDatetime.NOW' : datetime.utcnow,
               'TZDatetime.TODAY': datetime.today,
48
49
50
51
52
53
               'Date.TODAY': date.today}
DATE_FACTORY_MAP = {
    'Datetime' : lambda x: ':' in x and strptime(x, '%Y/%m/%d %H:%M') or strptime(x, '%Y/%m/%d'),
    'Date' : lambda x : strptime(x, '%Y/%m/%d'),
    'Time' : strptime_time
    }
54

Nicolas Chauvat's avatar
Nicolas Chauvat committed
55
# work in progress ###
56
57
58
59
60
61
62
63
64
65
66
67
68

class _RelationRole(int):
    def __eq__(self, other):
        if isinstance(other, _RelationRole):
            return other is self
        if self:
            return other == 'object'
        return other == 'subject'
    def __nonzero__(self):
        if self is SUBJECT:
            return OBJECT
        return SUBJECT

69

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
SUBJECT = _RelationRole(0)
OBJECT  = _RelationRole(1)

from warnings import warn

def ensure_new_subjobj(val, cls=None, attr=None):
    if isinstance(val, int):
        return val
    if val == 'subject':
        msg = 'using string instead of cubicweb.SUBJECT'
        if cls:
            msg += ' for attribute %s of class %s' % (attr, cls.__name__)
        warn(DeprecationWarning, msg)
        return SUBJECT
    if val == 'object':
        msg = 'using string instead of cubicweb.OBJECT'
        if cls:
            msg += ' for attribute %s of class %s' % (attr, cls.__name__)
        warn(DeprecationWarning, msg)
89
        return SUBJECT