コード例 #1
0
ファイル: dbschema.py プロジェクト: andgein/pony
    def prepare_rename_list(schema):
        ordered_rename_list = []
        tmp_rename_list = []
        name_mapping = {}

        for table in itervalues(schema.tables):
            prev_name = obsolete(table.name)
            #
            #
            name_mapping[prev_name] = table
            if prev_name != table.name:
                tmp_rename_list.append(table)

            tmp_rename_list.sort(key=attrgetter('name.obsolete_name'))

            while tmp_rename_list:
                rest = []

                for table in tmp_rename_list:
                    if table.name not in name_mapping:
                        name_mapping.pop(obsolete(table.name))
                        name_mapping[table.name] = table
                        ordered_rename_list.append(table)
                    else:
                        rest.append(table)

                if len(rest) == len(tmp_rename_list):
                    table = rest[0]
                    throw(
                        UpgradeError,
                        'Cannot rename table `%s` to `%s`: new name is already taken'
                        % (table.name.obsolete_name, table.name))
                tmp_rename_list = rest

        for table in ordered_rename_list[:]:

            constraints = sorted(index
                                 for key, index in iteritems(table.indexes)
                                 if not index.is_pk)
            if schema.named_foreign_keys:
                constraints.extend(
                    fk for key, fk in sorted(iteritems(table.foreign_keys)))

            for constraint in constraints:
                name = constraint.name
                if name != obsolete(name):
                    ordered_rename_list.append(constraint)

        return ordered_rename_list
コード例 #2
0
 def __init__(form,
              method='GET',
              secure=DEFAULT,
              prevent_resubmit=True,
              buttons_align=None,
              **attrs):
     # Note for subclassers: __init__ can be called twice!
     object.__setattr__(form, '_pickle_entire_form', False)
     object.__setattr__(form, '_cleared', False)
     object.__setattr__(form, '_validated', False)
     object.__setattr__(form, '_error_text', None)
     object.__setattr__(form, '_request', http.request)
     object.__setattr__(
         form, 'attrs',
         dict((name.lower(), str(value))
              for name, value in iteritems(attrs)))
     object.__setattr__(form, 'fields', [])
     object.__setattr__(form, 'hidden_fields', [])
     object.__setattr__(form, 'submit_fields', [])
     object.__setattr__(form, '_secure', False)
     form._set_method(method)
     if secure is DEFAULT: secure = (method == 'POST')
     form._set_secure(secure)
     object.__setattr__(form, 'prevent_resubmit', prevent_resubmit)
     object.__setattr__(form, 'buttons_align', buttons_align)
     if form.__class__ is not Form and 'name' not in attrs:
         form.attrs['name'] = form.__class__.__name__
     name = form.attrs.get('name')
     if name: form._f = Hidden(name)
コード例 #3
0
ファイル: dbschema.py プロジェクト: andgein/pony
    def rename_columns(table, renamed_columns):
        for prev_name, new_name in iteritems(renamed_columns):
            table.rename_column(prev_name, new_name, with_constraints=False)

        provider = table.schema.provider
        for index in itervalues(table.indexes):
            if any(name in renamed_columns for name in index.col_names):
                assert not index.is_pk
                new_index_col_names = tuple(
                    renamed_columns.get(name, name)
                    for name in index.col_names)
                new_index_name = table.make_index_name(
                    new_index_col_names,
                    is_pk=False,
                    is_unique=index.is_unique,
                    m2m=index.m2m,
                    provided_name=index.provided_name)
                if new_index_name != index.name: index.rename(new_index_name)

        for fk in itervalues(table.foreign_keys):
            if any(name in renamed_columns for name in fk.col_names):
                new_fk_col_names = tuple(
                    renamed_columns.get(name, name) for name in fk.col_names)
                new_fk_name = provider.get_default_fk_name(
                    table.name, new_fk_col_names)
                if new_fk_name != fk.name: fk.rename(new_fk_name)
コード例 #4
0
 def to_dict(bag):
     bag.dicts.clear()
     for entity, objects in iteritems(bag.objects):
         for obj in objects:
             dicts = bag.dicts[entity]
             if obj not in dicts: bag._process_object(obj)
     result = defaultdict(dict)
     for entity, dicts in iteritems(bag.dicts):
         composite_pk = len(entity._pk_columns_) > 1
         for obj, d in iteritems(dicts):
             pk = obj._get_raw_pkval_()
             if composite_pk: pk = bag._reduce_composite_pk(pk)
             else: pk = pk[0]
             result[entity.__name__][pk] = d
     bag.dicts.clear()
     return result
コード例 #5
0
 def __deepcopy__(self, memo):
     if getattr(self, '_hash', None) is not None:
         return self
     return HashableDict({
         deepcopy(key, memo): deepcopy(value, memo)
         for key, value in iteritems(self)
     })
コード例 #6
0
ファイル: serialization.py プロジェクト: buhtigexa/Nerit
 def to_dict(bag):
     bag.dicts.clear()
     for entity, objects in iteritems(bag.objects):
         for obj in objects:
             dicts = bag.dicts[entity]
             if obj not in dicts: bag._process_object(obj)
     result = defaultdict(dict)
     for entity, dicts in iteritems(bag.dicts):
         composite_pk = len(entity._pk_columns_) > 1
         for obj, d in iteritems(dicts):
             pk = obj._get_raw_pkval_()
             if composite_pk: pk = bag._reduce_composite_pk(pk)
             else: pk = pk[0]
             result[entity.__name__][pk] = d
     bag.dicts.clear()
     return result
コード例 #7
0
ファイル: oracle.py プロジェクト: timedcy/pony
def set_input_sizes(cursor, arguments):
    if type(arguments) is dict:
        input_sizes = {}
        for name, arg in iteritems(arguments):
            size = get_inputsize(arg)
            if size is not None: input_sizes[name] = size
        cursor.setinputsizes(**input_sizes)
    elif type(arguments) is tuple:
        input_sizes = map(get_inputsize, arguments)
        cursor.setinputsizes(*input_sizes)
    else: assert False, type(arguments)  # pragma: no cover
コード例 #8
0
def set_input_sizes(cursor, arguments):
    if type(arguments) is dict:
        input_sizes = {}
        for name, arg in iteritems(arguments):
            size = get_inputsize(arg)
            if size is not None: input_sizes[name] = size
        cursor.setinputsizes(**input_sizes)
    elif type(arguments) is tuple:
        input_sizes = map(get_inputsize, arguments)
        cursor.setinputsizes(*input_sizes)
    else: assert False, type(arguments)  # pragma: no cover
コード例 #9
0
 def new_func(self, *args, **kwargs):
     obj = self.obj_ref()
     attr = self.attr
     if obj is not None:
         args = tuple(TrackedValue.make(obj, attr, arg) for arg in args)
         if kwargs:
             kwargs = {
                 key: TrackedValue.make(obj, attr, value)
                 for key, value in iteritems(kwargs)
             }
     result = func(self, *args, **kwargs)
     self._changed_()
     return result
コード例 #10
0
	def deserialize(x):
		t = type(x)
		if t is list: return list(imap(deserialize, x))
		if t is dict:
			if '_id_' not in x:
				return dict((key, deserialize(val)) for key, val in iteritems(x))
			obj = objmap.get(x['_id_'])
			if obj is None:
				entity_name = x['class']
				entity = db.entities[entity_name]
				pk = x['_pk_']
				obj = entity[pk]
			return obj
		return x
コード例 #11
0
def str2date(s):
    s = s.strip().lower()
    for date_re in date_re_list:
        match = date_re.match(s)
        if match is not None: break
    else: raise ValueError('Unrecognized date format')
    dict = match.groupdict()
    year = dict['year']
    day = dict['day']
    month = dict.get('month')
    if month is None:
        for key, value in iteritems(month_dict):
            if key in s: month = value; break
        else: raise ValueError('Unrecognized date format')
    return date(int(year), int(month), int(day))
コード例 #12
0
def str2datetime(s):
    s = s.strip().lower()
    for datetime_re in datetime_re_list:
        match = datetime_re.match(s)
        if match is not None: break
    else: raise ValueError('Unrecognized datetime format')

    dict = match.groupdict()
    year, day, month = dict['year'], dict['day'], dict.get('month')

    if month is None:
        for key, value in iteritems(month_dict):
            if key in s: month = value; break
        else: raise ValueError('Unrecognized datetime format')

    hh, mm, ss, mcs = _extract_time_parts(dict)
    return datetime(int(year), int(month), int(day), hh, mm, ss, mcs)
コード例 #13
0
ファイル: httputils.py プロジェクト: buhtigexa/Nerit
def parse_accept_language(s):
    if not s: return []
    languages = {}
    for lang in s.lower().split(','):
        lang = lang.strip()
        if not lang: continue
        if ';' not in lang: languages[lang.strip()] = 1
        else:
            lang, params = lang.split(';', 1)
            q = 1
            for params in params.split(';'):
                match = q_re.match(params)
                if match is not None:
                    try: q = float(match.group(1))
                    except: pass
            lang = lang.strip()
            if lang: languages[lang] = max(q, languages.get(lang))
    languages = sorted((q, lang) for lang, q in iteritems(languages))
    languages.reverse()
    return [ lang for q, lang in languages ]
コード例 #14
0
ファイル: forms.py プロジェクト: buhtigexa/Nerit
 def __init__(form, method='GET', secure=DEFAULT,
              prevent_resubmit=True, buttons_align=None, **attrs):
     # Note for subclassers: __init__ can be called twice!
     object.__setattr__(form, '_pickle_entire_form', False)
     object.__setattr__(form, '_cleared', False)
     object.__setattr__(form, '_validated', False)
     object.__setattr__(form, '_error_text', None)
     object.__setattr__(form, '_request', http.request)
     object.__setattr__(form, 'attrs', dict((name.lower(), str(value))
                                            for name, value in iteritems(attrs)))
     object.__setattr__(form, 'fields', [])
     object.__setattr__(form, 'hidden_fields', [])
     object.__setattr__(form, 'submit_fields', [])
     object.__setattr__(form, '_secure', False)
     form._set_method(method)
     if secure is DEFAULT: secure = (method=='POST')
     form._set_secure(secure)
     object.__setattr__(form, 'prevent_resubmit', prevent_resubmit)
     object.__setattr__(form, 'buttons_align', buttons_align)
     if form.__class__ is not Form and 'name' not in attrs: form.attrs['name'] = form.__class__.__name__
     name = form.attrs.get('name')
     if name: form._f = Hidden(name)
コード例 #15
0
ファイル: httputils.py プロジェクト: buhtigexa/Nerit
def parse_accept_language(s):
    if not s: return []
    languages = {}
    for lang in s.lower().split(','):
        lang = lang.strip()
        if not lang: continue
        if ';' not in lang: languages[lang.strip()] = 1
        else:
            lang, params = lang.split(';', 1)
            q = 1
            for params in params.split(';'):
                match = q_re.match(params)
                if match is not None:
                    try:
                        q = float(match.group(1))
                    except:
                        pass
            lang = lang.strip()
            if lang: languages[lang] = max(q, languages.get(lang))
    languages = sorted((q, lang) for lang, q in iteritems(languages))
    languages.reverse()
    return [lang for q, lang in languages]
コード例 #16
0
    def debugging_middleware(app, environ):
        if not options.DEBUG: return app(environ)
        query = environ.get('QUERY_STRING', '')
        if not debug_re.search(query): return app(environ)

        env = dict(
            (key, value) for key, value in iteritems(environ)
            if isinstance(key, basestring) and isinstance(value, basestring))
        env['wsgi.version'] = environ['wsgi.version']
        env['wsgi.url_scheme'] = environ['wsgi.url_scheme']
        env['wsgi.multithread'] = environ['wsgi.multithread']
        env['wsgi.multiprocess'] = environ['wsgi.multiprocess']
        env['wsgi.run_once'] = environ['wsgi.run_once']
        content_length = int(environ.get('CONTENT_LENGTH', '0'))
        input_data = environ['wsgi.input'].read(content_length)
        env['wsgi.input'] = cStringIO.StringIO(input_data)
        env['wsgi.errors'] = cStringIO.StringIO()
        file_wrapper = environ.get('wsgi.file_wrapper')
        if file_wrapper is not None: env['wsgi.file_wrapper'] = file_wrapper

        url = httputils.reconstruct_url(environ)

        command = debug_re.search(url).group(1)
        url = debug_re.sub('', url)
        if url.endswith('&'): url = url[:-1]

        expr_match = expr_re.search(url)
        if expr_match is not None:
            expr = unquote_plus(expr_match.group(1))
            url = expr_re.sub('', url)
            if url.endswith('&'): url = url[:-1]
        else: expr = None

        result_holder = []
        queue.put((local.lock, app, env, result_holder, url, command, expr))
        local.lock.acquire()
        return result_holder[0]
コード例 #17
0
    def debugging_middleware(app, environ):
        if not options.DEBUG: return app(environ)
        query = environ.get('QUERY_STRING', '')
        if not debug_re.search(query): return app(environ)

        env = dict((key, value) for key, value in iteritems(environ)
                                if isinstance(key, basestring) and isinstance(value, basestring))
        env['wsgi.version'] = environ['wsgi.version']
        env['wsgi.url_scheme'] = environ['wsgi.url_scheme']
        env['wsgi.multithread'] = environ['wsgi.multithread']
        env['wsgi.multiprocess'] = environ['wsgi.multiprocess']
        env['wsgi.run_once'] = environ['wsgi.run_once']
        content_length = int(environ.get('CONTENT_LENGTH', '0'))
        input_data = environ['wsgi.input'].read(content_length)
        env['wsgi.input'] = cStringIO.StringIO(input_data)
        env['wsgi.errors'] = cStringIO.StringIO()
        file_wrapper = environ.get('wsgi.file_wrapper')
        if file_wrapper is not None: env['wsgi.file_wrapper'] = file_wrapper

        url = httputils.reconstruct_url(environ)

        command = debug_re.search(url).group(1)
        url = debug_re.sub('', url)
        if url.endswith('&'): url = url[:-1]

        expr_match = expr_re.search(url)
        if expr_match is not None:
            expr = unquote_plus(expr_match.group(1))
            url = expr_re.sub('', url)
            if url.endswith('&'): url = url[:-1]
        else: expr = None

        result_holder = []
        queue.put((local.lock, app, env, result_holder, url, command, expr))
        local.lock.acquire()
        return result_holder[0]
コード例 #18
0
 def set_multi(cache, mapping, time=0, key_prefix=''):
     for key, value in iteritems(mapping):
         cache._set(key_prefix + key, value, time)
     return []
コード例 #19
0
def update(trans1, trans2):
    for key, d2 in iteritems(trans2):
        trans1.setdefault(key, {}).update(d2)
コード例 #20
0
ファイル: asttranslation.py プロジェクト: hellhavn/pony
def create_extractors(code_key,
                      tree,
                      globals,
                      locals,
                      special_functions,
                      const_functions,
                      additional_internal_names=()):
    result = None
    getattr_extractors = getattr_cache.get(code_key)
    if getattr_extractors:
        getattr_attrnames = HashableDict({
            src: eval(code, globals, locals)
            for src, code in iteritems(getattr_extractors)
        })
        extractors_key = HashableDict(code_key=code_key,
                                      getattr_attrnames=getattr_attrnames)
        try:
            result = extractors_cache.get(extractors_key)
        except TypeError:
            pass  # unhashable type
        if not result:
            tree = copy_ast(tree)

    if not result:
        pretranslator = PreTranslator(tree, globals, locals, special_functions,
                                      const_functions,
                                      additional_internal_names)

        extractors = {}
        for node in pretranslator.externals:
            src = node.src = ast2src(node)
            if src == '.0': code = None
            else: code = compile(src, src, 'eval')
            extractors[src] = code

        getattr_extractors = {}
        getattr_attrnames = HashableDict()
        for node in pretranslator.getattr_nodes:
            if node in pretranslator.externals:
                src = node.src
                code = extractors[src]
                getattr_extractors[src] = code
                attrname_value = eval(code, globals, locals)
                getattr_attrnames[src] = attrname_value
            elif isinstance(node, ast.Const):
                attrname_value = node.value
            else:
                throw(
                    TypeError,
                    '`%s` should be either external expression or constant.' %
                    ast2src(node))
            if not isinstance(attrname_value, basestring):
                throw(
                    TypeError, '%s: attribute name must be string. Got: %r' %
                    (ast2src(node.parent_node), attrname_value))
            node._attrname_value = attrname_value
        getattr_cache[code_key] = getattr_extractors

        extractors_key = HashableDict(code_key=code_key,
                                      getattr_attrnames=getattr_attrnames)
        result = extractors_cache[
            extractors_key] = extractors, tree, extractors_key
    return result
コード例 #21
0
 def __init__(self, obj, attr, value):
     TrackedValue.__init__(self, obj, attr)
     dict.__init__(
         self,
         {key: self.make(obj, attr, val)
          for key, val in iteritems(value)})
コード例 #22
0
 def normalize_vars(provider, vars, vartypes):
     for name, value in iteritems(vars):
         if value == '':
             vars[name] = None
             vartypes[name] = NoneType
コード例 #23
0
 def set_multi(cache, mapping, time=0, key_prefix=''):
     for key, value in iteritems(mapping):
         cache._set(key_prefix + key, value, time)
     return []
コード例 #24
0
 def replace_multi(cache, mapping, time=0, key_prefix=''):
     result = []
     for key, value in iteritems(mapping):
         if not cache._replace(key_prefix + key, value, time):
             result.append(key)
     return result
コード例 #25
0
def save_changes(db, changes, observer=None):
	changes = json.loads(changes)

	import pprint;

	pprint.pprint(changes)

	objmap = {}
	for diff in changes['objects']:
		if diff['_status_'] == 'c': continue
		pk = diff['_pk_']
		pk = (pk,) if type(pk) is not list else tuple(pk)
		entity_name = diff['class']
		entity = db.entities[entity_name]
		obj = entity._get_by_raw_pkval_(pk, from_db=False)
		oid = diff['_id_']
		objmap[oid] = obj

	def id2obj(attr, val):
		return objmap[val] if attr.reverse and val is not None else val

		# def user_has_no_rights_to(operation, obj):

	# user_groups = get_current_user_groups()
	#     throw(PermissionError, 'The current user %s which belongs to groups %s '
	#                            'has no rights to %s the object %s on the frontend'
	#                            % (get_current_user(), sorted(user_groups), operation, obj))

	for diff in changes['objects']:
		entity_name = diff['class']
		entity = db.entities[entity_name]
		dbvals = {}
		newvals = {}
		for name, val in diff.items():
			if name not in ('class', '_pk_', '_id_', '_status_'):
				attr = entity._adict_[name]
				if not attr.is_collection:
					if type(val) is dict:
						if 'old' in val: dbvals[attr] = attr.validate(id2obj(attr, val['old']))
						if 'new' in val: newvals[attr.name] = attr.validate(id2obj(attr, val['new']))
					else:
						newvals[attr.name] = attr.validate(id2obj(attr, val))
		oid = diff['_id_']
		status = diff['_status_']
		if status == 'c':
			assert not dbvals
			obj = entity(**newvals)
			if observer:
				flush()  # in order to get obj.id
				observer('create', obj, newvals)
			objmap[oid] = obj
			# if not can_edit(obj): user_has_no_rights_to('create', obj)
		else:
			obj = objmap[oid]
			if status == 'd':
				# if not can_delete(obj): user_has_no_rights_to('delete', obj)
				if observer: observer('delete', obj)
				obj.delete()
			elif status == 'u':
				# if not can_edit(obj): user_has_no_rights_to('update', obj)
				if newvals:
					assert dbvals
					if observer:
						oldvals = dict((attr.name, val) for attr, val in iteritems(dbvals))
						observer('update', obj, newvals, oldvals)
					obj._db_set_(dbvals)  # dbvals can be modified here
					for attr in dbvals: attr.__get__(obj)
					obj.set(**newvals)
				else:
					assert not dbvals
				objmap[oid] = obj
	flush()
	for diff in changes['objects']:
		if diff['_status_'] == 'd': continue
		obj = objmap[diff['_id_']]
		entity = obj.__class__
		for name, val in diff.items():
			if name not in ('class', '_pk_', '_id_', '_status_'):
				attr = entity._adict_[name]
				if attr.is_collection and attr.reverse.is_collection and attr < attr.reverse:
					removed = [objmap[oid] for oid in val.get('removed', ())]
					added = [objmap[oid] for oid in val.get('added', ())]
					collection = attr.__get__(obj)
					if removed:
						observer('remove', obj, {name: removed})
						collection.remove(removed)
					if added:
						observer('add', obj, {name: added})
						collection.add(added)
	flush()

	def deserialize(x):
		t = type(x)
		if t is list: return list(imap(deserialize, x))
		if t is dict:
			if '_id_' not in x:
				return dict((key, deserialize(val)) for key, val in iteritems(x))
			obj = objmap.get(x['_id_'])
			if obj is None:
				entity_name = x['class']
				entity = db.entities[entity_name]
				pk = x['_pk_']
				obj = entity[pk]
			return obj
		return x

	return deserialize(changes['data'])
コード例 #26
0
 def normalize_vars(provider, vars, vartypes):
     for key, value in iteritems(vars):
         vartype = vartypes[key]
         if isinstance(vartype, QueryType):
             vartypes[key], vars[key] = value._normalize_var(vartype)
コード例 #27
0
 def normalize_vars(provider, vars, vartypes):
     DBAPIProvider.normalize_vars(provider, vars, vartypes)
     for key, value in iteritems(vars):
         if value == '':
             vars[key] = None
             vartypes[key] = NoneType
コード例 #28
0
 def replace_multi(cache, mapping, time=0, key_prefix=''):
     result = []
     for key, value in iteritems(mapping):
         if not cache._replace(key_prefix + key, value, time):
             result.append(key)
     return result
コード例 #29
0
ファイル: oracle.py プロジェクト: hellhavn/pony
 def normalize_vars(provider, vars, vartypes):
     for name, value in iteritems(vars):
         if value == '':
             vars[name] = None
             vartypes[name] = NoneType
コード例 #30
0
ファイル: i18n.py プロジェクト: buhtigexa/Nerit
def update(trans1, trans2):
    for key, d2 in iteritems(trans2):
        trans1.setdefault(key, {}).update(d2)