<commit_before>#
# JavaScript Tools
# Copyright 2010 <NAME>
#

import os
import logging

from js.Dependencies import collect
from js.Parser import parse
from js.Compressor import compress

uniqueId = 0

class JsClass():
    def __init__(self, path, rel, project):
        global uniqueId
        
        self.path = path
        self.rel = rel
        self.project = project
        self.name = os.path.splitext(self.rel)[0].replace("/", ".")
        self.id = uniqueId
        
        uniqueId += 1

        self.__cache = self.project.cache
        self.__mtime = os.stat(path).st_mtime

        self.__treeKey = "tree[%s]-" % self.rel
        self.__depKey = "deps[%s]-" % self.rel
        self.__breakKey = "breaks[%s]-" % self.rel
        self.__compressedKey = "compressed[%s]" % self.rel


    def getName(self):
        return self.name
        
    def getModificationTime(self):
        return self.__mtime

    def getText(self):
        return open(self.path, mode="r", encoding="utf-8").read()

    def getTree(self, permutation=None):
        field = "tree[%s]-%s" % (self.rel, permutation)
        tree = self.__cache.read(field, self.__mtime)
        if tree == None:
            logging.debug("%s: Generating tree..." % self.name)
            tree = parse(self.getText(), self.path)
            self.__cache.store(field, tree, self.__mtime)
            
        return tree

    def getDependencies(self, permutation=None):
        field = "deps[%s]-%s" % (self.rel, permutation)
        deps = self.__cache.read(field, self.__mtime)
        if deps == None:
            logging.debug("%s: Collecting dependencies..." % self.name)
            deps, breaks = collect(self.getTree(permutation), self.getName())
            self.__cache.store(field, deps, self.__mtime)
            
            field = "breaks[%s]-%s" % (self.rel, permutation)
            self.__cache.store(field, breaks, self.__mtime)
        
        return deps
            
    def getBreakDependencies(self, permutation=None):
        field = "breaks[%s]-%s" % (self.rel, permutation)
        breaks = self.__cache.read(field, self.__mtime)
        if breaks == None:
            self.getDependencies()
            breaks = self.__cache.read(field, self.__mtime)
            
        return breaks
        
    def getCompressed(self, permutation=None):
        compressed = self.__cache.read(self.__compressedKey, self.__mtime)
        if compressed == None:
            logging.debug("%s: Compressing tree..." % self.name)
            tree = self.getTree(permutation)
            compressed = compress(tree)
            self.__cache.store(self.__compressedKey, compressed, self.__mtime)
            
        return compressed
            
    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name
        
        <commit_msg>More work on integration of permutation support. Some cleanups.<commit_after>#
# JavaScript Tools
# Copyright 2010 <NAME>
#

import os
import logging
import copy

from js.Dependencies import collect
from js.Parser import parse
from js.Compressor import compress
from js.optimizer import Variants

uniqueId = 0

class JsClass():
    def __init__(self, path, rel, project):
        global uniqueId
        
        self.path = path
        self.rel = rel
        self.project = project
        self.name = os.path.splitext(self.rel)[0].replace("/", ".")
        self.id = uniqueId
        
        uniqueId += 1

        self.__cache = self.project.cache
        self.__mtime = os.stat(path).st_mtime


    def getName(self):
        return self.name
        
    def getModificationTime(self):
        return self.__mtime

    def getText(self):
        return open(self.path, mode="r", encoding="utf-8").read()
        
        


    def getTree(self, permutation=None):
        field = "tree[%s]-%s" % (self.rel, permutation)
        tree = self.__cache.read(field, self.__mtime)
        if tree == None:
            # generate the tree, cache it and return it
            if not permutation:
                logging.debug("%s: Generating tree..." % self.name)
                tree = parse(self.getText(), self.path)
                self.__cache.store(field, tree, self.__mtime)
                
            # otherwise: read unmodified tree, copy it, modify it, cache it, return it
            else:
                tree = copy.copy(self.getTree())
                
                logging.info("%s: Optimizing tree..." % self.name)
                Variants.replace(tree, permutation)
                Variants.optimize(tree)
            
            
            
        return tree

    def getDependencies(self, permutation=None):
        field = "deps[%s]-%s" % (self.rel, permutation)
        deps = self.__cache.read(field, self.__mtime)
        if deps == None:
            logging.debug("%s: Collecting dependencies..." % self.name)
            deps, breaks = collect(self.getTree(permutation), self.getName())
            self.__cache.store(field, deps, self.__mtime)
            
            field = "breaks[%s]-%s" % (self.rel, permutation)
            self.__cache.store(field, breaks, self.__mtime)
        
        return deps
            
    def getBreakDependencies(self, permutation=None):
        field = "breaks[%s]-%s" % (self.rel, permutation)
        breaks = self.__cache.read(field, self.__mtime)
        if breaks == None:
            self.getDependencies(permutation)
            breaks = self.__cache.read(field, self.__mtime)
            
        return breaks
        
    def getCompressed(self, permutation=None):
        field = "compressed[%s]-%s" % (self.rel, permutation)
        compressed = self.__cache.read(field, self.__mtime)
        if compressed == None:
            logging.debug("%s: Compressing tree..." % self.name)
            tree = self.getTree(permutation)
            compressed = compress(tree)
            self.__cache.store(field, compressed, self.__mtime)
            
        return compressed
            
    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name
        
        
{'fraction_non_alphanumeric': 0.1151278293437551, 'fraction_numerical': 0.0026054388536069046, 'mean_word_length': 1.9095215537659878, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1411986', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1725, 'n_words': 403}