<commit_before>#!/usr/bin/env python

import atexit
import errno
import os
import sys
import tempfile

class Locker(object):
	"""
	Filesystem-based atomic locking.
	
	Please note that this class is not designed to be thread-safe.
	"""

	class AcquireError(StandardError):
		"""The requested lock could not be acquired."""

	class StateError(StandardError):
		"""The operation is not valid in the current state."""

	_path = None
	"""The complete path to the locking directory."""

	_locked = False
	"""Whether this instance is currently holding a lock or not."""

	def __init__(self, locknow = False, directory = 'wardrobe.lock.d'):
		"""
		Initialize a new locker that will use the directory in the system's
		temp directory for locking.
		
		If locknow is set, the lock will be requested instantly; creating an
		instance will fail if it can not be acquired.
		"""
		if not isinstance(directory, str):
			raise TypeError('directory has to be a string')
		self._path = os.path.join(tempfile.gettempdir(), directory)
		if locknow:
			self.lock()

	def __del__(self):
		"""
		When this instance is unloaded, release possible locks.
		"""
		self.unlockIfLocked()

	def _lock(self):
		"""
		Acquire the lock, assuming that all state checks have already been made.
		"""
		# Try to acquire the lock.
		try:
			os.mkdir(self._path)
		except OSError, e:
			if e.errno == errno.EEXIST:
				# The directory (i.e. the lock) already exists.
				raise self.AcquireError(
					"could not acquire lock '%s'" % self._path
					)
			raise e
		# We acquired the lock successfully. Register an atexit.
		self._locked = True
		atexit.register(self.unlockIfLocked)
		return True

	def _unlock(self):
		"""
		Release the lock, assuming that all state checks have already been made.
		"""
		# Try to release the lock. OSErrors will propagate.
		# Warning: Without state checks, the directory will be removed even if
		# it was not created by this instance! (However, only empty directories
		# will be removed.)
		os.rmdir(self._path)
		self._locked = False
		return True

	def isLocked(self):
		"""
		Return whether this instance holds a lock or not.
		"""
		return self._locked<commit_msg>Make Locker.path a property<commit_after>#!/usr/bin/env python

import atexit
import errno
import os
import sys
import tempfile

class Locker(object):
	"""
	Filesystem-based atomic locking.
	
	Please note that this class is not designed to be thread-safe.
	"""

	class AcquireError(StandardError):
		"""The requested lock could not be acquired."""

	class StateError(StandardError):
		"""The operation is not valid in the current state."""

	def _getpath(self):
		"""
		The complete path to the locking directory.
		
		When setting this property to a relative path, it will be qualified by
		prepending the system's temp directory to it.
		"""
		return self._path

	def _setpath(self, value):
		if not isinstance(value, str):
			raise TypeError('path has to be a string')
		if os.path.isabs(value):
			self._path = value
		else:
			self._path = os.path.join(tempfile.gettempdir(), value)

	path = property(_getpath, _setpath)

	_locked = False
	"""Whether this instance is currently holding a lock or not."""

	def __init__(self, locknow = False, directory = 'wardrobe.lock.d'):
		"""
		Initialize a new locker that will use the directory in the system's
		temp directory for locking.
		
		If locknow is set, the lock will be requested instantly; creating an
		instance will fail if it can not be acquired.
		"""
		self.path = directory
		if locknow:
			self.lock()

	def __del__(self):
		"""
		When this instance is unloaded, release possible locks.
		"""
		self.unlockIfLocked()

	def _lock(self):
		"""
		Acquire the lock, assuming that all state checks have already been made.
		"""
		# Try to acquire the lock.
		try:
			os.mkdir(self.path)
		except OSError, e:
			if e.errno == errno.EEXIST:
				# The directory (i.e. the lock) already exists.
				raise self.AcquireError(
					"could not acquire lock '%s'" % self._path
					)
			raise e
		# We acquired the lock successfully. Register an atexit.
		self._locked = True
		atexit.register(self.unlockIfLocked)
		return True

	def _unlock(self):
		"""
		Release the lock, assuming that all state checks have already been made.
		"""
		# Try to release the lock. OSErrors will propagate.
		# Warning: Without state checks, the directory will be removed even if
		# it was not created by this instance! (However, only empty directories
		# will be removed.)
		os.rmdir(self.path)
		self._locked = False
		return True

	def isLocked(self):
		"""
		Return whether this instance holds a lock or not.
		"""
		return self._locked
{'fraction_non_alphanumeric': 0.09405940594059406, 'fraction_numerical': 0.0, 'mean_word_length': 3.4855212355212357, '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': '5341102', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1358, 'n_words': 561}