<commit_before># encoding: utf-8

"""

Asset controller core JSON API methods.

"""

from datetime import datetime

import web.core



log = __import__('logging').getLogger(__name__)
__all__ = ['CoreMethods']



class CoreMethods(web.core.Controller):
    def __init__(self, controller):
        self.controller = controller
    
    def alterChildIndex(self, oldIndex, newIndex, **kw):
        # TODO: Security.
        
        try:
            old = int(oldIndex)
            new = int(newIndex)
            
            if old == new: return 'json:', dict(status="success", message="No change.")
            
            asset = self.controller.asset
            count = len(asset.children)
            
            if old >= count or new >= count: raise ValueError('Invalid index.')
            
            child = asset.children.pop(old)
            asset.children.insert(new, child)
            asset.save()
        
        except:
            log.exception("Error ordering child from index %s to index %s.", old, new)
            return 'json:', dict(status="error", message="Unable to re-order asset.")
        
        return 'json:', dict(status="success")
    
    def getChildren(self, kind=None, tag=None, values=None, **kw):
        # TODO: Security.
        
        if kind is not None:
            kind = kind.lower().split(',')
        
        if values is None: values = 'name', 'title', 'description', 'path'
        else: values = values.split(',')
        
        for i in values:
            if i not in ('name', 'title', 'description', 'path', 'kind', 'rendered', 'tags'):
                return 'json:', dict(status="error", message="Forbidden.")
        
        children = []
        
        try:
            for child in self.controller.asset.children:
                if kind is not None and child.__class__.__name__.lower() not in kind: continue
                if tag is not None and tag not in child.tags: continue
                
                data = dict()
                
                for value in values:
                    if value == 'kind':
                        data[value] = child.__class__.__name__.lower()
                        continue
                    
                    data[value] = getattr(child, value, '')
                
                children.append(data)
        
        except:
            log.exception("Error iterating children.")
            return 'json:', dict(status="error", message="Error determining asset contents.")
        
        return 'json:', dict(status="success", children=children)


"""
class CoreMethods(Controller):
    def __init__(self, controller):<commit_msg>Allow for tag exclusions (vs. the current inclusions).<commit_after># encoding: utf-8

"""

Asset controller core JSON API methods.

"""

from datetime import datetime

import web.core



log = __import__('logging').getLogger(__name__)
__all__ = ['CoreMethods']



class CoreMethods(web.core.Controller):
    def __init__(self, controller):
        self.controller = controller
    
    def alterChildIndex(self, oldIndex, newIndex, **kw):
        # TODO: Security.
        
        try:
            old = int(oldIndex)
            new = int(newIndex)
            
            if old == new: return 'json:', dict(status="success", message="No change.")
            
            asset = self.controller.asset
            count = len(asset.children)
            
            if old >= count or new >= count: raise ValueError('Invalid index.')
            
            child = asset.children.pop(old)
            asset.children.insert(new, child)
            asset.save()
        
        except:
            log.exception("Error ordering child from index %s to index %s.", old, new)
            return 'json:', dict(status="error", message="Unable to re-order asset.")
        
        return 'json:', dict(status="success")
    
    def getChildren(self, kind=None, tag=None, values=None, **kw):
        # TODO: Security.
        
        if kind is not None:
            kind = kind.lower().split(',')
        
        if values is None: values = 'name', 'title', 'description', 'path'
        else: values = values.split(',')
        
        for i in values:
            if i not in ('name', 'title', 'description', 'path', 'kind', 'rendered', 'tags'):
                return 'json:', dict(status="error", message="Forbidden.")
        
        children = []
        
        try:
            for child in self.controller.asset.children:
                if kind is not None and child.__class__.__name__.lower() not in kind: continue
                if tag is not None:
                    if tag[0] != '!' and tag not in child.tags: continue
                    else if tag[0] == '!' and tag in child.tags: continue
                
                data = dict()
                
                for value in values:
                    if value == 'kind':
                        data[value] = child.__class__.__name__.lower()
                        continue
                    
                    data[value] = getattr(child, value, '')
                
                children.append(data)
        
        except:
            log.exception("Error iterating children.")
            return 'json:', dict(status="error", message="Error determining asset contents.")
        
        return 'json:', dict(status="success", children=children)


"""
class CoreMethods(Controller):
    def __init__(self, controller):
{'fraction_non_alphanumeric': 0.11682157827592492, 'fraction_numerical': 0.0007289958082741025, 'mean_word_length': 1.6499275712216321, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '521741', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1369, 'n_words': 408}