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

import glob,json,os,sys,traceback

#import simplejson as json

import openerpweb

from xml.etree import ElementTree

class Xml2Json:
    # xml2json-direct
    # Simple and straightforward XML-to-JSON converter in Python
    # New BSD Licensed
    #
    # URL: http://code.google.com/p/xml2json-direct/
    @staticmethod
    def convert_to_json(s):
        return json.dumps(Xml2Json.convert_to_structure(s), sort_keys=True, indent=4)

    @staticmethod
    def convert_to_structure(s):
        root = ElementTree.fromstring(s)
        return Xml2Json.convert_element(root)

    @staticmethod
    def convert_element(el, skip_whitespaces=True):
        res = {}
        if el.tag[0]=="{":
            ns, name = el.tag.rsplit("}",1) 
            res["tag"] = name
            res["namespace"] = ns[1:]
        else:
            res["tag"] = el.tag
        res["attrs"] = {}
        for k,v in el.items():
            res["attrs"][k] = v
        kids = []
        if el.text and (not skip_whitespaces or el.text.strip() != ''):
            kids.append(el.text)
        for kid in el:
            kids.append(Xml2Json.convert_element(kid))
            if kid.tail and (not skip_whitespaces or kid.tail.strip() != ''):
                 kids.append(kid.tail)
        if len(kids):
            res["children"] = kids
        return res

#----------------------------------------------------------
# OpenERP Web base Controllers
#----------------------------------------------------------

class Hello(openerpweb.Controller):
    _cp_path = "/base/hello"

    def index(self):
        return "hello world"

    @openerpweb.jsonrequest
    def ajax_hello_world(self,req):
        return {"welcome":"hello world"}

    @openerpweb.jsonrequest
    def ajax_hello_error(self,req):
        raise Exception("You suck")

class Connection(openerpweb.Controller):
    _cp_path = "/base/connection"
    def filesofmodulemap(self, ):
        # TODO root should be a global config
        files=[]
        for j in cssfiles.get(i,[]):
            files.append(j)

    def fileconcat(self, file_list):
        """ Concatenate file content
        return (concat,timestamp)
        concat: concatenation of file content
        timestamp: max(os.path.getmtime of file_list)
        """
        root = openerpweb.path_root
        files_content = []
        files_timestamp = 0
        for i in file_list:
            fname = os.path.join(root,i)
            ftime = os.path.getmtime(fname)
            if ftime > files_timestamp:
                files_timestamp = ftime
            files_content = open(fname).read()
        files_concat = "".join(files_content)
        return files_concat

    def css(self, req):
        # TODO http get argument mods is a comma seprated value of modules
        mods = 'base,base_hello'
        files = []
        for i in mods.split(','):
            globlist = openerpweb.addons_manifest.get(i,{})
            for j in globlist:
                tmp = glob.glob(os.path.join(path_addons,i,j))
                print tmp
                files.append(tmp)

        # TODO modules should list their css and js in __openerp__
        # TODO cssfiles should be a global registry of files
        cssfiles = {
            'base': ['base/static/openerp/base.css']
        }
        files = filesofmodulemap(mods)
        (concat, timestamp) = self.fileconcat(files)
        # TODO request set the Date of last modif and Etag
        return concat
    css.exposed=1

    def js(self, req):
        # TODO http get argument mods is a comma seprated value of modules
        mods = 'base,base_calendar'
        # TODO modules should list their css and js in __openerp__
        # TODO cssfiles should be a global registry of files
        jsfiles = {
            'base': ['base/static/openerp/openerp.base.js']
        }
        files = filesofmodulemap(mods)
        (concat, timestamp) = self.fileconcat(files)
        # TODO request set the Date of last modif and Etag
        return concat
    js.exposed=1

class Session(openerpweb.Controller):
    _cp_path = "/base/session"

    @openerpweb.jsonrequest
    def login(self, req, db, login, password):
        req.session.login('trunk', login, password)
        res = {
            "session_id" : req.session_id,
            "uid": req.session._uid,
        }
        return res

    def modules(self, req):
        # TODO return the list of all modules
        res={}
        res["modules"] = ["base","base_hello"]
        return res

class Menu(openerpweb.Controller):
    _cp_path = "/base/menu"

    @openerpweb.jsonrequest
    def load(self,req):
        m = req.session.model('ir.ui.menu')
        # menus are loaded fully unlike a regular tree view, cause there are
        # less than 512 items
        menu_ids = m.search([])
        menu_items = m.read(menu_ids,['name','sequence','parent_id'])
        menu_root = {'id':False, 'name':'root', 'parent_id':[-1,'']}
        menu_items.append(menu_root)
        # make a tree using parent_id
        for i in menu_items:
            i['children'] = []
        d = dict([(i["id"],i) for i in menu_items])
        for i in menu_items:
            if i['parent_id'] == False:
                pid = False
            else:
                pid = i['parent_id'][0]
            if pid in d:
                d[pid]['children'].append(i)
        # sort by sequence a tree using parent_id
        for i in menu_items:
            i['children'].sort(key = lambda x:x["sequence"])
        res={}
        res['data']=menu_root
        return res

    @openerpweb.jsonrequest
    def action(self,req,menu_id):
        m = req.session.model('ir.values')
        r = m.get('action', 'tree_but_open', [('ir.ui.menu', menu_id)], False, {})
        res={"action":r}
        return res

class DataSet(openerpweb.Controller):
    _cp_path = "/base/dataset"

    @openerpweb.jsonrequest
    def fields(self,req,model):
        return {'fields': req.session.model(model).fields_get(False)}

    @openerpweb.jsonrequest
    def load(self,req,model,domain=[],fields=['id']):
        m = req.session.model(model)
        ids = m.search(domain)
        values = m.read(ids, fields)
        res = {}
        res['ids'] = ids
        res['values'] = values
        return res

class FormView(openerpweb.Controller):
    _cp_path = "/base/formview"
    @openerpweb.jsonrequest
    def load(self,req,model,view_id):
        m = req.session.model(model)
        r = m.fields_view_get(view_id,'form')
        r["arch"]=Xml2Json.convert_to_structure(r["arch"])
        return {'fields_view':r}

class ListView(openerpweb.Controller):
    _cp_path = "/base/listview"
    @openerpweb.jsonrequest
    def load(self,req,model,view_id):
        m = req.session.model(model)
        r = m.fields_view_get(view_id,'tree')
        r["arch"]=Xml2Json.convert_to_structure(r["arch"])
        return {'fields_view':r}

class SearchView(openerpweb.Controller):
    _cp_path = "/base/searchview"
    @openerpweb.jsonrequest
    def load(self,req,model,view_id):
        m = req.session.model(model)
        r = m.fields_view_get(view_id,'search')
        r["arch"]=Xml2Json.convert_to_structure(r["arch"])
        return {'fields_view':r}

class Action(openerpweb.Controller):
    _cp_path = "/base/action"

    @openerpweb.jsonrequest
    def load(self,req,action_id):
        #m = req.session.model('ir.ui.menu')
        res={}
        return res

#
<commit_msg>addons loading part2<commit_after># -*- coding: utf-8 -*-

import glob,json,os,sys,traceback

#import simplejson as json

import openerpweb

from xml.etree import ElementTree

class Xml2Json:
    # xml2json-direct
    # Simple and straightforward XML-to-JSON converter in Python
    # New BSD Licensed
    #
    # URL: http://code.google.com/p/xml2json-direct/
    @staticmethod
    def convert_to_json(s):
        return json.dumps(Xml2Json.convert_to_structure(s), sort_keys=True, indent=4)

    @staticmethod
    def convert_to_structure(s):
        root = ElementTree.fromstring(s)
        return Xml2Json.convert_element(root)

    @staticmethod
    def convert_element(el, skip_whitespaces=True):
        res = {}
        if el.tag[0]=="{":
            ns, name = el.tag.rsplit("}",1) 
            res["tag"] = name
            res["namespace"] = ns[1:]
        else:
            res["tag"] = el.tag
        res["attrs"] = {}
        for k,v in el.items():
            res["attrs"][k] = v
        kids = []
        if el.text and (not skip_whitespaces or el.text.strip() != ''):
            kids.append(el.text)
        for kid in el:
            kids.append(Xml2Json.convert_element(kid))
            if kid.tail and (not skip_whitespaces or kid.tail.strip() != ''):
                 kids.append(kid.tail)
        if len(kids):
            res["children"] = kids
        return res

#----------------------------------------------------------
# OpenERP Web base Controllers
#----------------------------------------------------------

class Hello(openerpweb.Controller):
    _cp_path = "/base/hello"

    def index(self):
        return "hello world"

    @openerpweb.jsonrequest
    def ajax_hello_world(self,req):
        return {"welcome":"hello world"}

    @openerpweb.jsonrequest
    def ajax_hello_error(self,req):
        raise Exception("You suck")

class Connection(openerpweb.Controller):
    _cp_path = "/base/connection"

    def manifest_glob(self, modlist, key):
        files = []
        for i in mods.split(','):
            globlist = openerpweb.addons_manifest.get(i,{}).get('css',[])
            for j in globlist:
                tmp = glob.glob(os.path.join(path_addons,i,j))
                files.append(tmp)
        print modlist, key, files
        return files

    def concat_files(self, file_list):
        """ Concatenate file content
        return (concat,timestamp)
        concat: concatenation of file content
        timestamp: max(os.path.getmtime of file_list)
        """
        root = openerpweb.path_root
        files_content = []
        files_timestamp = 0
        for i in file_list:
            fname = os.path.join(root,i)
            ftime = os.path.getmtime(fname)
            if ftime > files_timestamp:
                files_timestamp = ftime
            files_content = open(fname).read()
        files_concat = "".join(files_content)
        return files_concat

    def list_modules(self, req):
        return 

    @openerpweb.jsonrequest
    def manifest_files(self, req, key, mods):
        files = self.manifest_glob(mods, key)
        return {'files': files}

    def css(self, req):
        # TODO http get argument mods is a comma seprated value of modules
        mods = 'base,base_hello'
        files = self.manifest_glob(mods.split(','), 'css')
        (concat, timestamp) = self.concat_files(files)
        # TODO request set the Date of last modif and Etag
        return concat
    css.exposed=1

    def js(self, req):
        # TODO http get argument mods is a comma seprated value of modules
        mods = 'base,base_hello'
        files = self.manifest_glob(mods.split(','), 'js')
        (concat, timestamp) = self.concat_files(files)
        # TODO request set the Date of last modif and Etag
        return concat
    js.exposed=1

class Session(openerpweb.Controller):
    _cp_path = "/base/session"

    @openerpweb.jsonrequest
    def login(self, req, db, login, password):
        req.session.login('trunk', login, password)
        res = {
            "session_id" : req.session_id,
            "uid": req.session._uid,
        }
        return res

    def modules(self, req):
        # TODO return the list of all modules
        res={}
        res["modules"] = ["base","base_hello"]
        return res

class Menu(openerpweb.Controller):
    _cp_path = "/base/menu"

    @openerpweb.jsonrequest
    def load(self,req):
        m = req.session.model('ir.ui.menu')
        # menus are loaded fully unlike a regular tree view, cause there are
        # less than 512 items
        menu_ids = m.search([])
        menu_items = m.read(menu_ids,['name','sequence','parent_id'])
        menu_root = {'id':False, 'name':'root', 'parent_id':[-1,'']}
        menu_items.append(menu_root)
        # make a tree using parent_id
        for i in menu_items:
            i['children'] = []
        d = dict([(i["id"],i) for i in menu_items])
        for i in menu_items:
            if i['parent_id'] == False:
                pid = False
            else:
                pid = i['parent_id'][0]
            if pid in d:
                d[pid]['children'].append(i)
        # sort by sequence a tree using parent_id
        for i in menu_items:
            i['children'].sort(key = lambda x:x["sequence"])
        res={}
        res['data']=menu_root
        return res

    @openerpweb.jsonrequest
    def action(self,req,menu_id):
        m = req.session.model('ir.values')
        r = m.get('action', 'tree_but_open', [('ir.ui.menu', menu_id)], False, {})
        res={"action":r}
        return res

class DataSet(openerpweb.Controller):
    _cp_path = "/base/dataset"

    @openerpweb.jsonrequest
    def fields(self,req,model):
        return {'fields': req.session.model(model).fields_get(False)}

    @openerpweb.jsonrequest
    def load(self,req,model,domain=[],fields=['id']):
        m = req.session.model(model)
        ids = m.search(domain)
        values = m.read(ids, fields)
        res = {}
        res['ids'] = ids
        res['values'] = values
        return res

class FormView(openerpweb.Controller):
    _cp_path = "/base/formview"
    @openerpweb.jsonrequest
    def load(self,req,model,view_id):
        m = req.session.model(model)
        r = m.fields_view_get(view_id,'form')
        r["arch"]=Xml2Json.convert_to_structure(r["arch"])
        return {'fields_view':r}

class ListView(openerpweb.Controller):
    _cp_path = "/base/listview"
    @openerpweb.jsonrequest
    def load(self,req,model,view_id):
        m = req.session.model(model)
        r = m.fields_view_get(view_id,'tree')
        r["arch"]=Xml2Json.convert_to_structure(r["arch"])
        return {'fields_view':r}

class SearchView(openerpweb.Controller):
    _cp_path = "/base/searchview"
    @openerpweb.jsonrequest
    def load(self,req,model,view_id):
        m = req.session.model(model)
        r = m.fields_view_get(view_id,'search')
        r["arch"]=Xml2Json.convert_to_structure(r["arch"])
        return {'fields_view':r}

class Action(openerpweb.Controller):
    _cp_path = "/base/action"

    @openerpweb.jsonrequest
    def load(self,req,action_id):
        #m = req.session.model('ir.ui.menu')
        res={}
        return res

#

{'fraction_non_alphanumeric': 0.13799673380511704, 'fraction_numerical': 0.0030620577027762655, 'mean_word_length': 2.1565721649484537, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5605477', 'n_tokens_mistral': 4642, 'n_tokens_neox': 4542, 'n_words': 1023}