<commit_before>import json, types

from theExceptions import (CreationError, DeletionError, UpdateError)

__all__ = ["Document", "Edge"]

class Document(object) :
	"""The class that represents a document. Documents are meant to be instanciated by collections"""

	def __init__(self, collection, jsonFieldInit = {}) :
		self.reset(collection, jsonFieldInit)
		self.typeName = "ArangoDoc"

	def reset(self, collection, jsonFieldInit = {}) :
		"""replaces the current values in the document by those in jsonFieldInit"""
		self.collection = collection
		self.connection = self.collection.connection
		self.documentsURL = self.collection.documentsURL
		
		self._store = {}
		self._patchStore = {}

		self._id, self._rev, self._key = None, None, None
		self.URL = None

		self.set(jsonFieldInit)
		self.modified = True

	def setPrivates(self, fieldDict) :
		"""will set self._id, self._rev and self._key field. Private fields (starting by '_') are all accessed using the self. interface,
		other fields are accessed through self[fielName], the same as regular dictionnary in python"""
		try :
			self._id = fieldDict["_id"]
			self.URL = "%s/%s" % (self.documentsURL, self._id)
			del(fieldDict["_id"])
		
			self._rev = fieldDict["_rev"]
			del(fieldDict["_rev"])
		
			self._key = fieldDict["_key"]
			del(fieldDict["_key"])
		except KeyError :
			self._id, self._rev, self._key = None, None, None
			self.URL = None

	def set(self, fieldDict = None) :
		"""Sets the document according to values contained in the dictinnary fieldDict. This will also set self._id/_rev/_key"""
		
		if fieldDict and self._id is None :
			self.setPrivates(fieldDict)

		if self.collection._validation['on_set']:
			for k in fieldDict.keys() :
				self[k] = fieldDict[k]
		else :
			self._store.update(fieldDict)

	def save(self, waitForSync = False, **docArgs) :
		"""Saves the document to the database by either performing a POST (for a new document) or a PUT (complete document overwrite).
		If you want to only update the modified fields use the .path() function.
		Use docArgs to put things such as 'waitForSync = True' (for a full list cf ArangoDB's doc).
		It will only trigger a saving of the document if it has been modified since the last save. If you want to force the saving you can use forceSave()"""

		if self.modified :
			if self.collection._validation['on_save'] :
				self.validate(patch = False)

			params = dict(docArgs)
			params.update({'collection': self.collection.name, "waitForSync" : waitForSync })
			payload = {} 
			payload.update(self._store)
			
			if self.URL is None :
				if self._key is not None :
					payload["_key"] = self._key
				payload = json.dumps(payload)
				r = self.connection.session.post(self.documentsURL, params = params, data = payload)
				update = False
			else :
				payload = json.dumps(payload)
				r = self.connection.session.put(self.URL, params = params, data = payload)
				update = True

			data = r.json()
			if (r.status_code == 201 or r.status_code == 202) and not data['error'] :
				if update :
					self._rev = data['_rev']
				else :
					self.setPrivates(data)
			else :
				if update :
					raise UpdateError(data['errorMessage'], data)<commit_msg>another 3.0 fix<commit_after>import json, types

from theExceptions import (CreationError, DeletionError, UpdateError)

__all__ = ["Document", "Edge"]

class Document(object) :
	"""The class that represents a document. Documents are meant to be instanciated by collections"""

	def __init__(self, collection, jsonFieldInit = {}) :
		self.reset(collection, jsonFieldInit)
		self.typeName = "ArangoDoc"

	def reset(self, collection, jsonFieldInit = {}) :
		"""replaces the current values in the document by those in jsonFieldInit"""
		self.collection = collection
		self.connection = self.collection.connection
		self.documentsURL = self.collection.documentsURL
		
		self._store = {}
		self._patchStore = {}

		self._id, self._rev, self._key = None, None, None
		self.URL = None

		self.set(jsonFieldInit)
		self.modified = True

	def setPrivates(self, fieldDict) :
		"""will set self._id, self._rev and self._key field. Private fields (starting by '_') are all accessed using the self. interface,
		other fields are accessed through self[fielName], the same as regular dictionnary in python"""
		try :
			self._id = fieldDict["_id"]
			self.URL = "%s/%s" % (self.documentsURL, self._id)
			del(fieldDict["_id"])
		
			self._rev = fieldDict["_rev"]
			del(fieldDict["_rev"])
		
			self._key = fieldDict["_key"]
			del(fieldDict["_key"])
		except KeyError :
			self._id, self._rev, self._key = None, None, None
			self.URL = None

	def set(self, fieldDict = None) :
		"""Sets the document according to values contained in the dictinnary fieldDict. This will also set self._id/_rev/_key"""
		
		if fieldDict and self._id is None :
			self.setPrivates(fieldDict)

		if self.collection._validation['on_set']:
			for k in fieldDict.keys() :
				self[k] = fieldDict[k]
		else :
			self._store.update(fieldDict)

	def save(self, waitForSync = False, **docArgs) :
		"""Saves the document to the database by either performing a POST (for a new document) or a PUT (complete document overwrite).
		If you want to only update the modified fields use the .path() function.
		Use docArgs to put things such as 'waitForSync = True' (for a full list cf ArangoDB's doc).
		It will only trigger a saving of the document if it has been modified since the last save. If you want to force the saving you can use forceSave()"""

		if self.modified :
			if self.collection._validation['on_save'] :
				self.validate(patch = False)

			params = dict(docArgs)
			params.update({'collection': self.collection.name, "waitForSync" : waitForSync })
			payload = {} 
			payload.update(self._store)
			
			if self.URL is None :
				if self._key is not None :
					payload["_key"] = self._key
				payload = json.dumps(payload)
				r = self.connection.session.post(self.documentsURL, params = params, data = payload)
				update = False
			else :
				payload = json.dumps(payload)
				r = self.connection.session.put(self.URL, params = params, data = payload)
				update = True

			data = r.json()
			if (r.status_code == 201 or r.status_code == 202) and "error" not in data.keys() :
				if update :
					self._rev = data['_rev']
				else :
					self.setPrivates(data)
			else :
				if update :
					raise UpdateError(data['errorMessage'], data)
{'fraction_non_alphanumeric': 0.11425003901982207, 'fraction_numerical': 0.0021851100358982363, 'mean_word_length': 3.65359477124183, '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': '7618926', 'n_tokens_mistral': 2259, 'n_tokens_neox': 1935, 'n_words': 639}