<commit_before>#!/usr/bin/env python
# coding=utf8

import os
import uuid
import logging
from datetime import datetime
import hashlib

from Crypto.Util.randpool import RandomPool

import uuidtype

import acl

from flask import url_for, send_file
from app import *

randpool = RandomPool()
debug = app.logger.debug

hashfunc = getattr(hashlib, app.config['HASHFUNC'])
hashsize = hashfunc().digest_size

class FileStorage(object):
	def __init__(self, path):
		self.path = path

	def store(self, file_):
		file_id = uuid.uuid1()

		filename = self._filename_from_uuid(file_id)
		debug("storing at %s", filename)

		file_.save(filename)

		return file_id

	def remove(self, file_id):
		filename = self._filename_from_uuid(file_id)
		debug("removing %s", filename)

		os.unlink(filename)

	def send(self, file_id, filename, mimetype):
		filepath = self._filename_from_uuid(file_id)

		return send_file(filepath, mimetype,
		                   as_attachment = True,
		                   attachment_filename = filename)

	def size_of(self, file_id):
		return os.path.getsize(self._filename_from_uuid(file_id))

	def _filename_from_uuid(self, id):
		print(repr(id))
		assert(isinstance(id, uuid.UUID))
		return os.path.join(self.path, str(id))

user_group_table = db.Table('user_group', db.metadata,
                              db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
                              db.Column('group_id', db.Integer, db.ForeignKey('groups.id'))
                           )

class User(db.Model):
	__tablename__ = 'users'
	id = db.Column(db.Integer, primary_key = True)
	groups = db.relation('Group', secondary = user_group_table, backref = 'users')

	def __str__(self):
		return "<User:%d (%s)>" % (self.id, ", ".join(map(str, self.openids)))

class Group(db.Model, acl.ACLSubjectRef):
	# strictly additive
	__tablename__ = 'groups'
	id = db.Column(db.Integer, primary_key = True)
	name = db.Column(db.String, nullable = True, unique = True)

	def __str__(self):
		return ("Group #%d" % self.id) if not self.name else self.name

def get_groups_of(user):
	if None == user: groups = [db.session.query(Group).filter_by(name = 'anonymous').one()]
	else: groups = [db.session.query(Group).filter_by(name = 'registered').one()] + user.groups

	debug('retrieved groups for user %s: %s', user, groups)
	return groups

def user_has_permission(user, verb, obj):
	# use strictly additive model
	groups = get_groups_of(user)
	for group in groups:
		perm = group.may(verb, obj)
		debug('checking group %s: %s', group, perm)
		if perm: return True
	return False

class OpenID(db.Model):
	__tablename__ = 'openids'
	id = db.Column(db.String, primary_key = True)
	user_id = db.Column(db.Integer, db.ForeignKey(User.id))
	user = db.relation(User, backref = db.backref('openids'))

	def __init__(self, url, user):
		self.id = url
		self.user = user

	def __str__(self):<commit_msg>Provided defaut argument of none for model.user_has_permission.<commit_after>#!/usr/bin/env python
# coding=utf8

import os
import uuid
import logging
from datetime import datetime
import hashlib

from Crypto.Util.randpool import RandomPool

import uuidtype

import acl

from flask import url_for, send_file
from app import *

randpool = RandomPool()
debug = app.logger.debug

hashfunc = getattr(hashlib, app.config['HASHFUNC'])
hashsize = hashfunc().digest_size

class FileStorage(object):
	def __init__(self, path):
		self.path = path

	def store(self, file_):
		file_id = uuid.uuid1()

		filename = self._filename_from_uuid(file_id)
		debug("storing at %s", filename)

		file_.save(filename)

		return file_id

	def remove(self, file_id):
		filename = self._filename_from_uuid(file_id)
		debug("removing %s", filename)

		os.unlink(filename)

	def send(self, file_id, filename, mimetype):
		filepath = self._filename_from_uuid(file_id)

		return send_file(filepath, mimetype,
		                   as_attachment = True,
		                   attachment_filename = filename)

	def size_of(self, file_id):
		return os.path.getsize(self._filename_from_uuid(file_id))

	def _filename_from_uuid(self, id):
		print(repr(id))
		assert(isinstance(id, uuid.UUID))
		return os.path.join(self.path, str(id))

user_group_table = db.Table('user_group', db.metadata,
                              db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
                              db.Column('group_id', db.Integer, db.ForeignKey('groups.id'))
                           )

class User(db.Model):
	__tablename__ = 'users'
	id = db.Column(db.Integer, primary_key = True)
	groups = db.relation('Group', secondary = user_group_table, backref = 'users')

	def __str__(self):
		return "<User:%d (%s)>" % (self.id, ", ".join(map(str, self.openids)))

class Group(db.Model, acl.ACLSubjectRef):
	# strictly additive
	__tablename__ = 'groups'
	id = db.Column(db.Integer, primary_key = True)
	name = db.Column(db.String, nullable = True, unique = True)

	def __str__(self):
		return ("Group #%d" % self.id) if not self.name else self.name

def get_groups_of(user):
	if None == user: groups = [db.session.query(Group).filter_by(name = 'anonymous').one()]
	else: groups = [db.session.query(Group).filter_by(name = 'registered').one()] + user.groups

	debug('retrieved groups for user %s: %s', user, groups)
	return groups

def user_has_permission(user, verb, obj = None):
	# use strictly additive model
	groups = get_groups_of(user)
	for group in groups:
		perm = group.may(verb, obj)
		debug('checking group %s: %s', group, perm)
		if perm: return True
	return False

class OpenID(db.Model):
	__tablename__ = 'openids'
	id = db.Column(db.String, primary_key = True)
	user_id = db.Column(db.Integer, db.ForeignKey(User.id))
	user = db.relation(User, backref = db.backref('openids'))

	def __init__(self, url, user):
		self.id = url
		self.user = user

	def __str__(self):
{'fraction_non_alphanumeric': 0.13122326775021387, 'fraction_numerical': 0.0006843455945252353, 'mean_word_length': 3.9753191489361703, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2025032', 'n_tokens_mistral': 2081, 'n_tokens_neox': 1988, 'n_words': 474}