<commit_before># encoding: utf-8

# XXX Prefer pycrypto if available
from .blowfish import Blowfish 
from xmlconfig import ContentProcessor, SimpleConstant, ContentDecoder
import hashlib, hmac

@SimpleConstant.register_processor(after=ContentDecoder)
class EncryptedContent(ContentProcessor):
    def process(self, constant, content):
        if constant.has_option("salt"):
            # Key is an SHA1 hmac hash of the key attribute of the loaded 
            # document, the salt of this element, and the namespace
            # XXX Implement password of this config document
            # XXX Try and read encoding from XML document
            key = hmac.new(constant.key.encode(), 
                (constant.options['salt'] + constant.namespace).encode(), 
                hashlib.sha1).digest()
            return Blowfish(key).decrypt(content)

# Lock / Unlock cli support
from xml.dom.minidom import parse, Text
import random, re
from base64 import standard_b64encode
from xmlconfig.cli import CliCommand
from xmlconfig import Options
import os.path

# XXX Upgrade this to argparse when we drop Python 2.6 support
from optparse import make_option

@CliCommand.register
class ConstantLockUtility(CliCommand):
    __command__ = "lock"
    __help__ = "Lock encrypted elements in a config file"
    __args__ = [
        make_option("-i","--in-place",dest="output",
            action="store_false",default=True,
            help="Overwrite input file with locked XML config rather than " \
                 "writing file to standard out"),
        make_option("-l","--salt-length", dest="saltlen",
            type="int", default=6, metavar="bytes",
            help="Set the length of the salt (in bytes) generated for the " \
                 "encrypted content. Default is 6 bytes")
    ]
    __usage__ = "%s {0} [options] file [file [...]]"

    def run(self, options, *args):
        for f in args:
            if not os.path.exists(f):
                raise ValueError("{0}: Config file does not exist".format(args[0]))<commit_msg>It's %prog for optparse usage, not %s<commit_after># encoding: utf-8

# XXX Prefer pycrypto if available
from .blowfish import Blowfish 
from xmlconfig import ContentProcessor, SimpleConstant, ContentDecoder
import hashlib, hmac

@SimpleConstant.register_processor(after=ContentDecoder)
class EncryptedContent(ContentProcessor):
    def process(self, constant, content):
        if constant.has_option("salt"):
            # Key is an SHA1 hmac hash of the key attribute of the loaded 
            # document, the salt of this element, and the namespace
            # XXX Implement password of this config document
            # XXX Try and read encoding from XML document
            key = hmac.new(constant.key.encode(), 
                (constant.options['salt'] + constant.namespace).encode(), 
                hashlib.sha1).digest()
            return Blowfish(key).decrypt(content)

# Lock / Unlock cli support
from xml.dom.minidom import parse, Text
import random, re
from base64 import standard_b64encode
from xmlconfig.cli import CliCommand
from xmlconfig import Options
import os.path

# XXX Upgrade this to argparse when we drop Python 2.6 support
from optparse import make_option

@CliCommand.register
class ConstantLockUtility(CliCommand):
    __command__ = "lock"
    __help__ = "Lock encrypted elements in a config file"
    __args__ = [
        make_option("-i","--in-place",dest="output",
            action="store_false",default=True,
            help="Overwrite input file with locked XML config rather than " \
                 "writing file to standard out"),
        make_option("-l","--salt-length", dest="saltlen",
            type="int", default=6, metavar="bytes",
            help="Set the length of the salt (in bytes) generated for the " \
                 "encrypted content. Default is 6 bytes")
    ]
    __usage__ = "%prog {0} [options] file [file [...]]"

    def run(self, options, *args):
        for f in args:
            if not os.path.exists(f):
                raise ValueError("{0}: Config file does not exist".format(args[0]))
{'fraction_non_alphanumeric': 0.09535392848455364, 'fraction_numerical': 0.006810994891753831, 'mean_word_length': 2.839402427637722, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '238688', 'n_tokens_mistral': 1121, 'n_tokens_neox': 1085, 'n_words': 387}