<commit_before># -*- coding: utf-8 -*-

# Asyncode Runtime - XML framework allowing developing internet
# applications without using programming languages.
# Copyright (C) 2008-2010  <NAME>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the version 3 of GNU General Public License as published by
# the Free Software Foundation.

# 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 General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from ACR.utils import replaceVars,prepareVars, str2obj, dicttree
from ACR.components import *
from ACR.utils.xmlextras import tree2xml
from xml.sax.saxutils import escape,unescape
import pymongo
from bson import objectid
from ACR.utils.interpreter import makeTree
import time

class Mongo(Component):
	SERVER='localhost'
	PORT=27017
	DIRECTION="DESCENDING"
	def __init__(self,config):
		if not config:
			config={}
		server=config.get("server",self.SERVER)
		port=config.get("port",self.PORT)
		#self.conn=pymongo.Connection()
		#self.DEFAULT_DB=config.get("defaultdb")
		self.DEFAULT_COLL=config.get("defaultcoll")

	def update(self,acenv,config):
		D=acenv.doDebug
		params=config["params"]
		coll=acenv.app.storage[params.get("coll",self.DEFAULT_COLL)]
		where=params["where"].execute(acenv)
		o=config["content"].execute(acenv)
		if D:
			acenv.debug("where clause is %s",where)
			acenv.debug("update object is %s",o)
		coll.update(where,o,safe=True,multi=True)

	def insert(self,acenv,config):
		D=acenv.doDebug
		params=config["params"]
		coll=acenv.app.storage[params.get("coll",self.DEFAULT_COLL)]
		o=config["content"].execute(acenv)
		if D: acenv.debug("doing %s",coll.insert)
		try:
			id=coll.insert(o,safe=True)
		except Exception,e:
			return {
				"@status":"error",
				"@error":type(e),
				"@message":str(e)
			}
		if D:acenv.info("inserted: %s",o)
		ret={"@id":id,"@status":"ok"}
		#leaving space for debugging and profiling info
		return ret

	def save(self,acenv,config):
		D=acenv.doDebug
		if D: acenv.debug("START Mongo.save with: %s", config)
		params=config["params"]
		coll=acenv.app.storage[params.get("coll",self.DEFAULT_COLL)]
		o=config["content"].execute(acenv)
		if D: acenv.debug("doing %s",coll.insert)
		id=coll.save(o,safe=True)
		if D:acenv.debug("saved:\n%s",o)
		ret={"@id":id}
		#leaving space for debugging and profiling info
		return ret

	def remove(self,acenv,config):
		D=acenv.doDebug
		if D: acenv.debug("START Mongo.remove with: %s", config)
		params=config["params"]
		coll=acenv.app.storage[params.get("coll",self.DEFAULT_COLL)]
		o=config["content"].execute(acenv)
		if D: acenv.debug("doing %s",coll.insert)
		lastError=coll.remove(o,safe=True)
		if D:acenv.debug("removed:\n%s",o)
		#leaving space for debugging and profiling info
		return {}

	def count(self,acenv,config):
		return self.find(acenv,config,count=True)

	def findOne(self,acenv,config):
		return self.find(acenv,config,one=True)

	def find(self,acenv,config,count=False,one=False):
		D=acenv.doDebug
		P=acenv.doProfiling
		params=config["params"]
		coll=acenv.app.storage[params.get("coll",self.DEFAULT_COLL)]
		p={
			"spec":params.get("where", config["content"]).execute(acenv)
		}
		if D:acenv.debug("Finding objects matching:\n%s",p["spec"])
		for i in params:
			if type(params[i]) is list:
				params[i]=replaceVars(acenv,params[i])
		if params.has_key("fields"):
			p["fields"]=params["fields"]
		if not one:
			if params.has_key("skip"):
				p["skip"]=int(params["skip"])
			if params.has_key("limit"):
				p["limit"]=int(params["limit"])
			if params.has_key("sort"):
				dir=pymongo.__dict__.get(params.get("direction",self.DIRECTION).upper())
				p["sort"]=[(params["sort"],dir)]
		if P: t=time.time()
		if count:
			ret=coll.find(**p).count()
		elif one:
			ret=list(coll.find(**p).limit(-1))
			ret=ret and ret[0] or None
		else:
			ret=list(coll.find(**p))
		if P:
			acenv.profiler["dbtimer"]+=time.time()-t
			acenv.profiler["dbcounter"]+=1
		if ret:
			#if len(ret) is 1:
			#	if D:acenv.debug("END Mongo.find with %s",ret[0])
			#	return ret[0]
			if D:acenv.debug("END Mongo.find with %s",ret)
			return ret
		else:
			if D:acenv.debug("END Mongo.find with no object")
			return {"@status":"nodata"}

	def generate(self, acenv,config):
		D=acenv.doDebug<commit_msg>nodata->noData, some fixes<commit_after># -*- coding: utf-8 -*-

# Asyncode Runtime - XML framework allowing developing internet
# applications without using programming languages.
# Copyright (C) 2008-2010  <NAME>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the version 3 of GNU General Public License as published by
# the Free Software Foundation.

# 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 General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from ACR.utils import replaceVars,prepareVars, str2obj, dicttree
from ACR.components import *
from ACR.utils.xmlextras import tree2xml
from xml.sax.saxutils import escape,unescape
import pymongo
from bson import objectid
from ACR.utils.interpreter import makeTree
import time

class Mongo(Component):
	SERVER='localhost'
	PORT=27017
	DIRECTION="DESCENDING"
	def __init__(self,config):
		if not config:
			config={}
		server=config.get("server",self.SERVER)
		port=config.get("port",self.PORT)
		#self.conn=pymongo.Connection()
		#self.DEFAULT_DB=config.get("defaultdb")
		self.DEFAULT_COLL=config.get("defaultcoll")

	def update(self,acenv,config):
		D=acenv.doDebug
		params=config["params"]
		coll=acenv.app.storage[params.get("coll",self.DEFAULT_COLL)]
		where=params["where"].execute(acenv)
		o=config["content"].execute(acenv)
		if D:
			acenv.debug("where clause is %s",where)
			acenv.debug("update object is %s",o)
		coll.update(where,o,safe=True,multi=True)

	def insert(self,acenv,config):
		D=acenv.doDebug
		params=config["params"]
		coll=acenv.app.storage[params.get("coll",self.DEFAULT_COLL)]
		o=config["content"].execute(acenv)
		if D: acenv.debug("doing %s",coll.insert)
		try:
			id=coll.insert(o,safe=True)
		except Exception,e:
			return {
				"@status":"error",
				"@error":type(e),
				"@message":str(e)
			}
		if D:acenv.info("inserted: %s",o)
		ret={"@id":id,"@status":"ok"}
		#leaving space for debugging and profiling info
		return ret

	def save(self,acenv,config):
		D=acenv.doDebug
		if D: acenv.debug("START Mongo.save with: %s", config)
		params=config["params"]
		coll=acenv.app.storage[params.get("coll",self.DEFAULT_COLL)]
		o=config["content"].execute(acenv)
		if D: acenv.debug("doing %s",coll.insert)
		id=coll.save(o,safe=True)
		if D:acenv.debug("saved:\n%s",o)
		ret={"@id":id}
		#leaving space for debugging and profiling info
		return ret

	def remove(self,acenv,config):
		D=acenv.doDebug
		if D: acenv.debug("START Mongo.remove with: %s", config)
		params=config["params"]
		coll=acenv.app.storage[params.get("coll",self.DEFAULT_COLL)]
		o=config["content"].execute(acenv)
		if D: acenv.debug("doing %s",coll.insert)
		lastError=coll.remove(o,safe=True)
		if D:acenv.debug("removed:\n%s",o)
		#leaving space for debugging and profiling info
		return {}

	def count(self,acenv,config):
		return self.find(acenv,config,count=True)

	def findOne(self,acenv,config):
		return self.find(acenv,config,one=True)

	def find(self,acenv,config,count=False,one=False):
		D=acenv.doDebug
		P=acenv.doProfiling
		params=config["params"]
		coll=acenv.app.storage[params.get("coll",self.DEFAULT_COLL)]
		p={
			"spec":params.get("where", config["content"]).execute(acenv)
		}
		if D:acenv.debug("Finding objects matching:\n%s",p["spec"])
		for i in params:
			if type(params[i]) is list:
				params[i]=replaceVars(acenv,params[i])
		if params.has_key("fields"):
			p["fields"]=params["fields"]
		if not one:
			if params.has_key("skip"):
				p["skip"]=int(params["skip"])
			if params.has_key("limit"):
				p["limit"]=int(params["limit"])
			if params.has_key("sort"):
				dir=pymongo.__dict__.get(params.get("direction",self.DIRECTION).upper())
				p["sort"]=[(params["sort"],dir)]
		if P: t=time.time()
		if count:
			ret=coll.find(**p).count()
			if D:acenv.debug("Objects matching count is: %s",ret)
		elif one:
			ret=list(coll.find(**p).limit(-1))
			ret=ret and ret[0] or None
		else:
			ret=list(coll.find(**p))
		if P:
			acenv.profiler["dbtimer"]+=time.time()-t
			acenv.profiler["dbcounter"]+=1
		if ret:
			#if len(ret) is 1:
			#	if D:acenv.debug("END Mongo.find with %s",ret[0])
			#	return ret[0]
			if D:acenv.debug("END Mongo.find with %s",ret)
			return ret
		else:
			if count:
				if D:acenv.debug("END Mongo.count with 0")
				return ret
			if D:acenv.debug("END Mongo.find with no object")
			return {"@status":"noData"}

	def generate(self, acenv,config):
		D=acenv.doDebug
{'fraction_non_alphanumeric': 0.15663039965528386, 'fraction_numerical': 0.005063018420769148, 'mean_word_length': 4.898348157560355, 'pattern_counts': {'":': 16, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '3674336', 'n_tokens_mistral': 3645, 'n_tokens_neox': 3297, 'n_words': 734}