<commit_before>import os.path
from sqlobject import SQLObjectNotFound

import wakefs.db
import wakefs.utils
import wakefs.conn

class Stats(object):
    _valid_attributes = {
            'st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid',
            'st_gid', 'st_rdev', 'st_size', 'st_blksize', 'st_blocks',
            'st_atime', 'st_mtime', 'st_ctime'
        }

    def __init__(self, dbobject):
        self._dbobject = dbobject

    def __get_db_col(self, colname):
        if self._dbobject:
            return getattr(self._dbobject, colname)
        else:
            raise AttributeError("Object was deleted")

    def __set_db_col(self, colname, value):
        if self._dbobject:
            setattr(self._dbobject, colname, value)
        else:
            raise AttributeError("Object was deleted")

    def __getattribute__(self, name):
        if name in Stats._valid_attributes:
            return self.__get_db_col(name)
        else:
            return object.__getattribute__(self,name)

    def __setattr__(self, name, value):
        if name in Stats._valid_attributes:
            self.__set_db_col(name, value)
        else:
            return object.__setattr__(self,name,value)

    def __repr__(self):
        repr = "Stats("
        for a in Stats._valid_attributes:
            repr += a + '=' + str(getattr(self,a)) + ','
        return repr[:-1] + ")"

class File(object):
    def __init__(self, name, location=None):
        wakefs.db.initialise()
        DBObjectClass = getattr(wakefs.db,type(self).__name__)
        try:
            self._dbobject = DBObjectClass.selectBy(name=name).getOne()
        except SQLObjectNotFound:
            dirname = os.path.dirname(name) or "/"
            if dirname != "/" and wakefs.db.File.selectBy(name=dirname).count() > 0:
                raise ValueError("'%s' is a file." % dirname)
            directory = Directory(
                    name=dirname,
                    location=location
                )
            stats = wakefs.utils.get_stats(name, location)
            self._dbobject = DBObjectClass(
                    directory = directory._dbobject,
                    location = location,
                    name = name,
                    **stats
                )

    def __get_db_col(self, colname):
        if self._dbobject:
            return getattr(self._dbobject, colname)
        else:
            raise AttributeError("Object was deleted")

    def __set_db_col(self, colname, value):
        if self._dbobject:
            setattr(self._dbobject, colname, value)
        else:
            raise AttributeError("Object was deleted")

    @property
    def crc(self):
        return self.__get_db_col('crc')

    @property
    def location(self):
        return self.__get_db_col('location')

    @property
    def name(self):
        return self.__get_db_col('name')

    @property
    def stat(self):
        return Stats(self._dbobject)

    def __unicode__(self):
        return self.name

    def __str__(self):
        return str(unicode(self))

    def __repr__(self):
        return unicode(self)
    
class Link(File):
    def __init__(self, name, target, location=None):
        wakefs.db.initialise()
        DBObjectClass = getattr(wakefs.db,type(self).__name__)
        self._connection = wakefs.conn.connection_factory()
        try:
            self._dbobject = DBObjectClass.selectBy(name=name).getOne()
        except SQLObjectNotFound:
            dirname = os.path.dirname(name) or "/"
            if dirname != "/" and wakefs.db.File.selectBy(name=dirname).count() > 0:
                raise ValueError("'%s' is a file." % dirname)
            directory = Directory(
                    name=dirname,
                    location=location
                )
            stats = wakefs.utils.get_stats(name, location)
            self._dbobject = wakefs.db.DBObjectClass(
                    directory = directory._dbobject,
                    location = location,
                    target = target,
                    name = name,
                    **stats
                )

    @property
    def target(self):
        return self.target

class SymLink(Link):
    pass

class Directory(File):
    @property
    def content(self):
        for c in self.__get_db_col('content'):
            yield globals()[type(c).__name__](c.name,c.location)
<commit_msg>Generate object type automatically when in Database<commit_after>import os.path
from sqlobject import SQLObjectNotFound
from sqlobject.dberrors import DuplicateEntryError

import wakefs.db
import wakefs.utils
import wakefs.conn

def get_object(filename, location=None):
    return File(filename, location)

class IsDirectoryError(Exception):
    pass

class IsFileError(Exception):
    pass

class Stats(object):
    _valid_attributes = {
            'st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid',
            'st_gid', 'st_rdev', 'st_size', 'st_blksize', 'st_blocks',
            'st_atime', 'st_mtime', 'st_ctime'
        }

    def __init__(self, dbobject):
        self._dbobject = dbobject

    def __get_db_col(self, colname):
        if self._dbobject:
            return getattr(self._dbobject, colname)
        else:
            raise AttributeError("Object was deleted")

    def __set_db_col(self, colname, value):
        if self._dbobject:
            setattr(self._dbobject, colname, value)
        else:
            raise AttributeError("Object was deleted")

    def __getattribute__(self, name):
        if name in Stats._valid_attributes:
            return self.__get_db_col(name)
        else:
            return object.__getattribute__(self,name)

    def __setattr__(self, name, value):
        if name in Stats._valid_attributes:
            self.__set_db_col(name, value)
        else:
            return object.__setattr__(self,name,value)

    def __repr__(self):
        repr = "Stats("
        for a in Stats._valid_attributes:
            repr += a + '=' + str(getattr(self,a)) + ','
        return repr[:-1] + ")"

class File(object):
    def __new__(cls, name, *args, **kwargs):
        wakefs.db.initialise()
        try:
            db_obj = wakefs.db.INode.selectBy(name=name).getOne()
            if type(db_obj).__name__ != cls.__name__:
                return globals()[type(db_obj).__name__](name, db_obj, *args, **kwargs)
        except SQLObjectNotFound:
            pass
        return super(File, cls).__new__(cls, name, *args, **kwargs)

    def __init__(self, name, db_obj=None, location=None, **kwargs):
        DBObjectClass = getattr(wakefs.db,type(self).__name__)
        self._connection = wakefs.conn.connection_factory()

        if db_obj != None:
            self._dbobject = db_obj
        elif name == "/":
            self._dbobject = wakefs.db.Directory.selectBy(name=name).getOne()
        else:
            dirname = os.path.dirname(name) or "/"
            if dirname != "/" and wakefs.db.File.selectBy(name=dirname).count() > 0:
                raise IsFileError("'%s' is a file." % dirname)
            directory = Directory(
                    name=dirname,
                    location=location
                )
            stats = wakefs.utils.get_stats(name, location)
            try:
                kwargs.update(stats)
                self._dbobject = DBObjectClass(
                        directory=directory._dbobject,
                        location=location,
                        name=name,
                        **kwargs
                   )
            except DuplicateEntryError:
                self._dbobject = DBObjectClass.selectBy(name=name).getOne()

    def __get_db_col(self, colname):
        if self._dbobject:
            return getattr(self._dbobject, colname)
        else:
            raise AttributeError("Object was deleted")

    def __set_db_col(self, colname, value):
        if self._dbobject:
            setattr(self._dbobject, colname, value)
        else:
            raise AttributeError("Object was deleted")

    @property
    def crc(self):
        return self.__get_db_col('crc')

    @property
    def location(self):
        return self.__get_db_col('location')

    @property
    def name(self):
        return self.__get_db_col('name')

    @property
    def stat(self):
        return Stats(self._dbobject)

    def __unicode__(self):
        return self.name

    def __str__(self):
        return str(unicode(self))

    def __repr__(self):
        return unicode(self)

class Link(File):
    def __init__(self, name, db_obj=None, location=None, target=None):
        if wakefs.db.Link.selectBy(name=name).count() == 0 and \
                target == None:
            raise ValueError("A link must have a target")
        super(Link, self).__init__(name, db_obj, location, target=target)

    @property
    def target(self):
        return self.target

class SymLink(Link):
    pass

class Directory(File):
    @property
    def content(self):
        for c in self.__get_db_col('content'):
            yield globals()[type(c).__name__](c.name,c.location)

{'fraction_non_alphanumeric': 0.10645801190213798, 'fraction_numerical': 0.0006612298875909191, 'mean_word_length': 2.0330882352941178, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 6, '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': '7172059', 'n_tokens_mistral': 2674, 'n_tokens_neox': 2589, 'n_words': 574}