<commit_before>import os

from topaz.error import error_for_oserror
from topaz.module import ClassDef
from topaz.modules.enumerable import Enumerable
from topaz.objects.objectobject import W_Object
from topaz.objects.regexpobject import RegexpCache
from topaz.utils.glob import Glob
from topaz.utils.ll_dir import opendir, readdir, closedir


class W_DirObject(W_Object):
    classdef = ClassDef("Dir", W_Object.classdef, filepath=__file__)
    classdef.include_module(Enumerable)

    def __del__(self):
        if self.open:
            closedir(self.dirp)

    def ensure_open(self, space):
        if not self.open:
            raise space.error(space.w_IOError, "closed directory")

    @classdef.method("initialize", path="str")
    def method_initialize(self, space, path):
        self.path = path
        try:
            self.dirp = opendir(path)
        except OSError as e:
            raise error_for_oserror(space, e)
        self.open = True

    @classdef.singleton_method("allocate")
    def method_allocate(self, space, args_w):
        return W_DirObject(space)

    @classdef.singleton_method("pwd")
    @classdef.singleton_method("getwd")
    def method_pwd(self, space):
        return space.newstr_fromstr(os.getcwd())

    @classdef.singleton_method("chdir", path="path")
    def method_chdir(self, space, path=None, block=None):
        if path is None:
            path = os.environ["HOME"]
        current_dir = os.getcwd()
        os.chdir(path)
        if block is not None:
            try:
                return space.invoke_block(block, [space.newstr_fromstr(path)])
            finally:
                os.chdir(current_dir)
        else:
            return space.newint(0)

    @classdef.singleton_method("delete", path="path")
    @classdef.singleton_method("rmdir", path="path")
    @classdef.singleton_method("unlink", path="path")
    def method_delete(self, space, path):
        try:
            os.rmdir(path if path else "")
        except OSError as e:
            raise error_for_oserror(space, e)
        return space.newint(0)

    @classdef.singleton_method("[]")
    def method_subscript(self, space, args_w):
        if len(args_w) == 1:
            return space.send(self, space.newsymbol("glob"), args_w)
        else:
            return space.send(self, space.newsymbol("glob"), [space.newarray(args_w)])

    @classdef.singleton_method("glob", flags="int")
    def method_glob(self, space, w_pattern, flags=0, block=None):
        if space.is_kind_of(w_pattern, space.w_array):
            patterns_w = space.listview(w_pattern)
        else:
            patterns_w = [w_pattern]

        glob = Glob(space.fromcache(RegexpCache))

        for w_pat in patterns_w:
            pattern_w = space.convert_type(w_pat, space.w_string, "to_path", raise_error=False)
            if pattern_w is space.w_nil:
                pattern = space.convert_type(w_pat, space.w_string, "to_str")
            pattern = space.str_w(pattern_w)
            if len(patterns_w) == 1:
                for pat in pattern.split("\0"):
                    glob.glob(pat, flags)
            else:
                glob.glob(pattern, flags)
<commit_msg>w_pattern, not pattern_w.<commit_after>import os

from topaz.error import error_for_oserror
from topaz.module import ClassDef
from topaz.modules.enumerable import Enumerable
from topaz.objects.objectobject import W_Object
from topaz.objects.regexpobject import RegexpCache
from topaz.utils.glob import Glob
from topaz.utils.ll_dir import opendir, readdir, closedir


class W_DirObject(W_Object):
    classdef = ClassDef("Dir", W_Object.classdef, filepath=__file__)
    classdef.include_module(Enumerable)

    def __del__(self):
        if self.open:
            closedir(self.dirp)

    def ensure_open(self, space):
        if not self.open:
            raise space.error(space.w_IOError, "closed directory")

    @classdef.method("initialize", path="str")
    def method_initialize(self, space, path):
        self.path = path
        try:
            self.dirp = opendir(path)
        except OSError as e:
            raise error_for_oserror(space, e)
        self.open = True

    @classdef.singleton_method("allocate")
    def method_allocate(self, space, args_w):
        return W_DirObject(space)

    @classdef.singleton_method("pwd")
    @classdef.singleton_method("getwd")
    def method_pwd(self, space):
        return space.newstr_fromstr(os.getcwd())

    @classdef.singleton_method("chdir", path="path")
    def method_chdir(self, space, path=None, block=None):
        if path is None:
            path = os.environ["HOME"]
        current_dir = os.getcwd()
        os.chdir(path)
        if block is not None:
            try:
                return space.invoke_block(block, [space.newstr_fromstr(path)])
            finally:
                os.chdir(current_dir)
        else:
            return space.newint(0)

    @classdef.singleton_method("delete", path="path")
    @classdef.singleton_method("rmdir", path="path")
    @classdef.singleton_method("unlink", path="path")
    def method_delete(self, space, path):
        try:
            os.rmdir(path if path else "")
        except OSError as e:
            raise error_for_oserror(space, e)
        return space.newint(0)

    @classdef.singleton_method("[]")
    def method_subscript(self, space, args_w):
        if len(args_w) == 1:
            return space.send(self, space.newsymbol("glob"), args_w)
        else:
            return space.send(self, space.newsymbol("glob"), [space.newarray(args_w)])

    @classdef.singleton_method("glob", flags="int")
    def method_glob(self, space, w_pattern, flags=0, block=None):
        if space.is_kind_of(w_pattern, space.w_array):
            patterns_w = space.listview(w_pattern)
        else:
            patterns_w = [w_pattern]

        glob = Glob(space.fromcache(RegexpCache))

        for w_pat in patterns_w:
            w_pat2 = space.convert_type(w_pat, space.w_string, "to_path", raise_error=False)
            if w_pat2 is space.w_nil:
                pattern = space.convert_type(w_pat, space.w_string, "to_str")
            pattern = space.str_w(w_pat2)
            if len(patterns_w) == 1:
                for pat in pattern.split("\0"):
                    glob.glob(pat, flags)
            else:
                glob.glob(pattern, flags)

{'fraction_non_alphanumeric': 0.1091366567070294, 'fraction_numerical': 0.0023588614562038055, 'mean_word_length': 2.577052868391451, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7558970', 'n_tokens_mistral': 2039, 'n_tokens_neox': 1994, 'n_words': 418}