<commit_before>#    Copyright (C) 2014  Dignity Health
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    NO CLINICAL USE.  THE SOFTWARE IS NOT INTENDED FOR COMMERCIAL PURPOSES
#    AND SHOULD BE USED ONLY FOR NON-COMMERCIAL RESEARCH PURPOSES.  THE
#    SOFTWARE MAY NOT IN ANY EVENT BE USED FOR ANY CLINICAL OR DIAGNOSTIC
#    PURPOSES.  YOU ACKNOWLEDGE AND AGREE THAT THE SOFTWARE IS NOT INTENDED FOR
#    USE IN ANY HIGH RISK OR STRICT LIABILITY ACTIVITY, INCLUDING BUT NOT
#    LIMITED TO LIFE SUPPORT OR EMERGENCY MEDICAL OPERATIONS OR USES.  LICENSOR
#    MAKES NO WARRANTY AND HAS NOR LIABILITY ARISING FROM ANY USE OF THE
#    SOFTWARE IN ANY HIGH RISK OR STRICT LIABILITY ACTIVITIES.

# Borg Object Regeneration for GPI
#   This is a set of convenience functions for wrapping external binaries in
#   python.

from __future__ import absolute_import, division, print_function, unicode_literals

import os
import hashlib
import subprocess
import numpy as np

# gpi
#from .defines import GPI_SHDM_PATH
#from .logger import manager
import tempfile
GPI_SHDM_PATH = tempfile.gettempdir()

# start logger for this module
#log = manager.getLogger(__name__)
class manager(object):
    def warn(self, msg):
        print(msg)
log = manager()

class FilePath(object):
    '''Generate a tempfile-name and path based on THIS object's id.  If THIS
    object looses its reference then make sure the associated file is also
    deleted. The supplied read/writer functions can be used to write and
    retrieve the file information. If a tempfile-path and name is all that is
    needed, then this object can be instantiated without any arguments.

    path: /tmp (default GPI tmp dir)
    filename: additional to the nodeid
    suffix: additional to the nodeid (i.e. '.jpg')
    nodeid: node's location in memory (id())
    rfunc: reader function with footprint:
                data = rfunc('filename')
    wfunc: writer function with footprint:
                retcode = wfunc('filename', data)
                retcode: None or 0 for success

    If no names are specified then THIS object id is used.
    '''
    _Extern_File_Handle_Type = True

    def __init__(self, wfunc=None, wdata=None, path=None, filename=None,
                 suffix=None, nodeid=None, rfunc=None, asuffix=[]):

        self._reader = rfunc
        self._writer = wfunc
        self._output_data = wdata # data to be written

        self._suffix = ''
        if suffix is not None:
            self._suffix = suffix

        self._additional_suffix = set(asuffix + [self._suffix, '']) - set([None])

        ## build the filepath one step at a time

        self._basename_path = ''
        self._filename = ''
        if nodeid:
            self._filename += str(nodeid)

        if filename:
            if self._filename != '':
                self._filename += '_'
            self._filename += str(filename)

        # just use THIS object id if nothing is specified
        if self._filename == '':
            self._filename = str(id(self))

        if path:
            self._basename_path = os.path.join(str(path), self._filename)
        else:
            self._basename_path = os.path.join(GPI_SHDM_PATH, self._filename)

        if self._suffix:
            self._filename += self._suffix

        if self.fileExists():
            log.warn('The path: \'' + self._basename_path + '\' already exists, continuing...')

    def __str__(self):
        return self._basename_path

    def __del__(self):
        # this may not delete in a timely fashion so direct use of clear() is
        # encouraged.
        if self.fileExists():
            log.warn('The \'FilePath\' object for path: \''+self._basename_path+'\' was not closed before collection.')
            self.clear()

    def additionalSuffix(self, suf=[]):
        # in case the filename is used as a basename, this will allow more
        # files to be searched for removal.  -helpful for formats that require
        # multiple files.
        self._additional_suffix = suf

    def clear(self):
        for s in self._additional_suffix:
            if os.path.isfile(self._basename_path + s):
                os.remove(self._basename_path + s)

    def fileExists(self):
        for s in self._additional_suffix:
            if os.path.isfile(self._basename_path + s):
                return True
        return False

    def close(self):
        self.clear()

    def setReader(self, func):
        self._reader = func

    def setWriter(self, func):
        self._writer = func<commit_msg>Removed warning about closing file handles.<commit_after>#    Copyright (C) 2014  Dignity Health
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    NO CLINICAL USE.  THE SOFTWARE IS NOT INTENDED FOR COMMERCIAL PURPOSES
#    AND SHOULD BE USED ONLY FOR NON-COMMERCIAL RESEARCH PURPOSES.  THE
#    SOFTWARE MAY NOT IN ANY EVENT BE USED FOR ANY CLINICAL OR DIAGNOSTIC
#    PURPOSES.  YOU ACKNOWLEDGE AND AGREE THAT THE SOFTWARE IS NOT INTENDED FOR
#    USE IN ANY HIGH RISK OR STRICT LIABILITY ACTIVITY, INCLUDING BUT NOT
#    LIMITED TO LIFE SUPPORT OR EMERGENCY MEDICAL OPERATIONS OR USES.  LICENSOR
#    MAKES NO WARRANTY AND HAS NOR LIABILITY ARISING FROM ANY USE OF THE
#    SOFTWARE IN ANY HIGH RISK OR STRICT LIABILITY ACTIVITIES.

# Borg Oriented Rejiggering for GPI
#   This is a set of convenience functions for wrapping external binaries in
#   python.

from __future__ import absolute_import, division, print_function, unicode_literals

import os
import hashlib
import subprocess
import numpy as np

# gpi
#from .defines import GPI_SHDM_PATH
#from .logger import manager
import tempfile
GPI_SHDM_PATH = tempfile.gettempdir()

# start logger for this module
#log = manager.getLogger(__name__)
class manager(object):
    def warn(self, msg):
        print(msg)
log = manager()

class FilePath(object):
    '''Generate a tempfile-name and path based on THIS object's id.  If THIS
    object looses its reference then make sure the associated file is also
    deleted. The supplied read/writer functions can be used to write and
    retrieve the file information. If a tempfile-path and name is all that is
    needed, then this object can be instantiated without any arguments.

    path: /tmp (default GPI tmp dir)
    filename: additional to the nodeid
    suffix: additional to the nodeid (i.e. '.jpg')
    nodeid: node's location in memory (id())
    rfunc: reader function with footprint:
                data = rfunc('filename')
    wfunc: writer function with footprint:
                retcode = wfunc('filename', data)
                retcode: None or 0 for success

    If no names are specified then THIS object id is used.
    '''
    _Extern_File_Handle_Type = True

    def __init__(self, wfunc=None, wdata=None, path=None, filename=None,
                 suffix=None, nodeid=None, rfunc=None, asuffix=[]):

        self._reader = rfunc
        self._writer = wfunc
        self._output_data = wdata # data to be written

        self._suffix = ''
        if suffix is not None:
            self._suffix = suffix

        self._additional_suffix = set(asuffix + [self._suffix, '']) - set([None])

        ## build the filepath one step at a time

        self._basename_path = ''
        self._filename = ''
        if nodeid:
            self._filename += str(nodeid)

        if filename:
            if self._filename != '':
                self._filename += '_'
            self._filename += str(filename)

        # just use THIS object id if nothing is specified
        if self._filename == '':
            self._filename = str(id(self))

        if path:
            self._basename_path = os.path.join(str(path), self._filename)
        else:
            self._basename_path = os.path.join(GPI_SHDM_PATH, self._filename)

        if self._suffix:
            self._filename += self._suffix

        if self.fileExists():
            log.warn('The path: \'' + self._basename_path + '\' already exists, continuing...')

    def __str__(self):
        return self._basename_path

    def __del__(self):
        # this may not delete in a timely fashion so direct use of clear() is
        # encouraged.
        if self.fileExists():
            #log.warn('The \'FilePath\' object for path: \''+self._basename_path+'\' was not closed before collection.')
            self.clear()

    def additionalSuffix(self, suf=[]):
        # in case the filename is used as a basename, this will allow more
        # files to be searched for removal.  -helpful for formats that require
        # multiple files.
        self._additional_suffix = suf

    def clear(self):
        for s in self._additional_suffix:
            if os.path.isfile(self._basename_path + s):
                os.remove(self._basename_path + s)

    def fileExists(self):
        for s in self._additional_suffix:
            if os.path.isfile(self._basename_path + s):
                return True
        return False

    def close(self):
        self.clear()

    def setReader(self, func):
        self._reader = func

    def setWriter(self, func):
        self._writer = func
{'fraction_non_alphanumeric': 0.07619780642678468, 'fraction_numerical': 0.0011545122185876468, 'mean_word_length': 2.544152744630072, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4384956', 'n_tokens_mistral': 2968, 'n_tokens_neox': 2834, 'n_words': 1144}