~openerp-dev/openobject-server/trunk-missing-default-values-pza

« back to all changes in this revision

Viewing changes to openerp/tools/misc.py

  • Committer: Pooja Zankhariya (OpenERP)
  • Date: 2014-01-31 06:44:29 UTC
  • mfrom: (5013.1.35 trunk)
  • Revision ID: pza@tinyerp.com-20140131064429-0a1u9ax5cf3sslug
[MERGE]Sync with Trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
import threading
36
36
import time
37
37
import zipfile
38
 
from collections import defaultdict
 
38
from collections import defaultdict, Mapping
39
39
from datetime import datetime
40
40
from itertools import islice, izip, groupby
41
41
from lxml import etree
822
822
        '%Z': '',
823
823
}
824
824
 
 
825
POSIX_TO_LDML = {
 
826
    'a': 'E',
 
827
    'A': 'EEEE',
 
828
    'b': 'MMM',
 
829
    'B': 'MMMM',
 
830
    #'c': '',
 
831
    'd': 'dd',
 
832
    'H': 'HH',
 
833
    'I': 'hh',
 
834
    'j': 'DDD',
 
835
    'm': 'MM',
 
836
    'M': 'mm',
 
837
    'p': 'a',
 
838
    'S': 'ss',
 
839
    'U': 'w',
 
840
    'w': 'e',
 
841
    'W': 'w',
 
842
    'y': 'yy',
 
843
    'Y': 'yyyy',
 
844
    # see comments above, and babel's format_datetime assumes an UTC timezone
 
845
    # for naive datetime objects
 
846
    #'z': 'Z',
 
847
    #'Z': 'z',
 
848
}
 
849
 
 
850
def posix_to_ldml(fmt, locale):
 
851
    """ Converts a posix/strftime pattern into an LDML date format pattern.
 
852
 
 
853
    :param fmt: non-extended C89/C90 strftime pattern
 
854
    :param locale: babel locale used for locale-specific conversions (e.g. %x and %X)
 
855
    :return: unicode
 
856
    """
 
857
    buf = []
 
858
    pc = False
 
859
    quoted = []
 
860
 
 
861
    for c in fmt:
 
862
        # LDML date format patterns uses letters, so letters must be quoted
 
863
        if not pc and c.isalpha():
 
864
            quoted.append(c if c != "'" else "''")
 
865
            continue
 
866
        if quoted:
 
867
            buf.append("'")
 
868
            buf.append(''.join(quoted))
 
869
            buf.append("'")
 
870
            quoted = []
 
871
 
 
872
        if pc:
 
873
            if c == '%': # escaped percent
 
874
                buf.append('%')
 
875
            elif c == 'x': # date format, short seems to match
 
876
                buf.append(locale.date_formats['short'].pattern)
 
877
            elif c == 'X': # time format, seems to include seconds. short does not
 
878
                buf.append(locale.time_formats['medium'].pattern)
 
879
            else: # look up format char in static mapping
 
880
                buf.append(POSIX_TO_LDML[c])
 
881
            pc = False
 
882
        elif c == '%':
 
883
            pc = True
 
884
        else:
 
885
            buf.append(c)
 
886
 
 
887
    # flush anything remaining in quoted buffer
 
888
    if quoted:
 
889
        buf.append("'")
 
890
        buf.append(''.join(quoted))
 
891
        buf.append("'")
 
892
 
 
893
    return ''.join(buf)
 
894
 
825
895
def server_to_local_timestamp(src_tstamp_str, src_format, dst_format, dst_tz_name,
826
896
        tz_offset=True, ignore_unparsable_time=True):
827
897
    """
1005
1075
 
1006
1076
    def __enter__(self):
1007
1077
        for logger in self.loggers:
 
1078
            assert isinstance(logger, basestring),\
 
1079
                "A logger name must be a string, got %s" % type(logger)
1008
1080
            logging.getLogger(logger).addFilter(self)
1009
1081
 
1010
1082
    def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):
1070
1142
    return [x for i, x in enumerate(args) if not strip(args, i)]
1071
1143
 
1072
1144
 
 
1145
class ConstantMapping(Mapping):
 
1146
    """
 
1147
    An immutable mapping returning the provided value for every single key.
 
1148
 
 
1149
    Useful for default value to methods
 
1150
    """
 
1151
    __slots__ = ['_value']
 
1152
    def __init__(self, val):
 
1153
        self._value = val
 
1154
 
 
1155
    def __len__(self):
 
1156
        """
 
1157
        defaultdict updates its length for each individually requested key, is
 
1158
        that really useful?
 
1159
        """
 
1160
        return 0
 
1161
 
 
1162
    def __iter__(self):
 
1163
        """
 
1164
        same as len, defaultdict udpates its iterable keyset with each key
 
1165
        requested, is there a point for this?
 
1166
        """
 
1167
        return iter([])
 
1168
 
 
1169
    def __getitem__(self, item):
 
1170
        return self._value
 
1171
 
 
1172
 
1073
1173
def dumpstacks(sig, frame):
1074
1174
    """ Signal handler: dump a stack trace for each existing thread."""
1075
1175
    code = []
1107
1207
    _logger.info("\n".join(code))
1108
1208
 
1109
1209
 
 
1210
 
1110
1211
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: