<commit_before># -*- coding: utf-8 -*-
'''
    :codauthor: :email:`Mike Place <mp@saltstack.com>`
'''

# Import Salt Testing libs
from salttesting import skipIf
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import patch, call, NO_MOCK, NO_MOCK_REASON

ensure_in_syspath('../')

# Import Python libs
import os
import shutil

# Import salt libs
import integration
from salt.fileserver import gitfs

gitfs.__opts__ = {'gitfs_remotes': [''],
                  'gitfs_root': '',
                  'fileserver_backend': 'gitfs',
                  'gitfs_base': 'master',
                  'fileserver_events': True,
}

load = {'saltenv': 'base'}

GITFS_AVAILABLE = None
try:
    import git

    GITFS_AVAILABLE = True
except ImportError:
    pass

if not gitfs.__virtual__():
    GITFS_AVAILABLE = False

# @skipIf(not GITFS_AVAILABLE, "GitFS could not be loaded. Skipping GitFS tests!")
@skipIf(NO_MOCK, NO_MOCK_REASON)
class GitFSTest(integration.ModuleCase):
    def setUp(self):
        '''
        We don't want to check in another .git dir into GH because that just gets messy.
        Instead, we'll create a temporary repo on the fly for the tests to examine.
        :return:
        '''
        self.integration_base_files = os.path.join(integration.FILES, 'file', 'base')
        self.tmp_repo_dir = os.path.join(integration.TMP, 'gitfs_root')
        self.tmp_repo_git = os.path.join(self.tmp_repo_dir, '.git')

        # Create the dir if it doesn't already exist

        try:
            shutil.copytree(self.integration_base_files, self.tmp_repo_dir + '/')
        except OSError as e:
            # We probably caught an error because files already exist. Ignore
            pass

        if not os.path.exists(self.tmp_repo_git):
            os.makedirs(self.tmp_repo_git)
        try:
            git_bin = git.Git(self.tmp_repo_git)
            git_bin.init(self.tmp_repo_dir)
            os.chdir(self.tmp_repo_dir)
            git_bin.add('.', with_keep_cwd=True)  # Is there a way to pass in a .git repo?
            git_bin.commit('-a', '-m', 'Test', with_keep_cwd=True)
        except git.GitCommandError:  # Will throw a command error if you try to init a repo that already exists
            pass

        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir']}):
            gitfs.update()

    def test_file_list(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir']}):
            ret = gitfs.file_list(load)
            self.assertIn('testfile', ret)

    def test_find_file(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir']}):
            ret = gitfs.find_file('testfile')
            expected_ret = {'path': '/tmp/salttest/cache/gitfs/refs/master/testfile',
                            'rel': 'testfile'}

            self.assertDictEqual(ret, expected_ret)

    def test_dir_list(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir']}):
            ret = gitfs.dir_list(load)
            self.assertIn('grail', ret)

    @skipIf(True, 'This test is failing and for good reason! See #9193')
    def test_file_list_emptydirs(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir']}):
            ret = gitfs.file_list_emptydirs(load)
            self.assertIn('empty_dir', ret)

    def test_envs(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir']}):
            ret = gitfs.envs()
            self.assertIn('base', ret)

    def test_file_hash_sha1(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir'],
                                         'hash_type': 'blob_sha1'}):
            tmp_load = load
            tmp_load['path'] = 'testfile'
            fnd = {'rel': 'testfile',
                   'path': 'testfile'}
            ret = gitfs.file_hash(load, fnd)
            self.assertDictEqual({'hash_type': 'blob_sha1', 'hsum': '0d234303e6451128d756c5c259175de37d767742'}, ret)

    @skipIf(True, 'This test is failing and for good reason! See #9194')
    def test_file_hash_md5(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir'],
                                         'hash_type': 'md5'}):
            tmp_load = load
            tmp_load['path'] = 'testfile'
            fnd = {'rel': 'testfile',
                   'path': 'testfile'}
            ret = gitfs.file_hash(load, fnd)
            self.assertDictEqual({'hash_type': 'md5', 'hsum': '98aa509006628302ce38ce521a7f805f'}, ret)

    def test_serve_file(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir'],
                                         'file_buffer_size': 262144}):
            fnd = {'rel': 'testfile',
                   'path': 'testfile'}

            tmp_load = load
            tmp_load['loc'] = 0

            ret = gitfs.serve_file(load, fnd)
            self.assertDictEqual({
                                     'data': 'Scene 24\n\n \n  OLD MAN:  Ah, hee he he ha!\n  ARTHUR:  And this enchanter of whom you speak, he has seen the grail?\n  OLD MAN:  Ha ha he he he he!\n  ARTHUR:  Where does he live?  Old man, where does he live?\n  OLD MAN:  He knows of a cave, a cave which no man has entered.\n  ARTHUR:  And the Grail... The Grail is there?\n  OLD MAN:  Very much danger, for beyond the cave lies the Gorge\n      of Eternal Peril, which no man has ever crossed.\n  ARTHUR:  But the Grail!  Where is the Grail!?\n  OLD MAN:  Seek you the Bridge of Death.\n  ARTHUR:  The Bridge of Death, which leads to the Grail?\n  OLD MAN:  Hee hee ha ha!\n\n',
                                     'dest': 'testfile'}, ret)

if __name__ == '__main__':
    integration.run_tests(GitFSTest)
<commit_msg>Remove unused import<commit_after># -*- coding: utf-8 -*-
'''
    :codauthor: :email:`Mike Place <mp@saltstack.com>`
'''

# Import Salt Testing libs
from salttesting import skipIf
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import patch, NO_MOCK, NO_MOCK_REASON

ensure_in_syspath('../')

# Import Python libs
import os
import shutil

# Import salt libs
import integration
from salt.fileserver import gitfs

gitfs.__opts__ = {'gitfs_remotes': [''],
                  'gitfs_root': '',
                  'fileserver_backend': 'gitfs',
                  'gitfs_base': 'master',
                  'fileserver_events': True,
}

load = {'saltenv': 'base'}

GITFS_AVAILABLE = None
try:
    import git

    GITFS_AVAILABLE = True
except ImportError:
    pass

if not gitfs.__virtual__():
    GITFS_AVAILABLE = False

# @skipIf(not GITFS_AVAILABLE, "GitFS could not be loaded. Skipping GitFS tests!")
@skipIf(NO_MOCK, NO_MOCK_REASON)
class GitFSTest(integration.ModuleCase):
    def setUp(self):
        '''
        We don't want to check in another .git dir into GH because that just gets messy.
        Instead, we'll create a temporary repo on the fly for the tests to examine.
        :return:
        '''
        self.integration_base_files = os.path.join(integration.FILES, 'file', 'base')
        self.tmp_repo_dir = os.path.join(integration.TMP, 'gitfs_root')
        self.tmp_repo_git = os.path.join(self.tmp_repo_dir, '.git')

        # Create the dir if it doesn't already exist

        try:
            shutil.copytree(self.integration_base_files, self.tmp_repo_dir + '/')
        except OSError as e:
            # We probably caught an error because files already exist. Ignore
            pass

        if not os.path.exists(self.tmp_repo_git):
            os.makedirs(self.tmp_repo_git)
        try:
            git_bin = git.Git(self.tmp_repo_git)
            git_bin.init(self.tmp_repo_dir)
            os.chdir(self.tmp_repo_dir)
            git_bin.add('.', with_keep_cwd=True)  # Is there a way to pass in a .git repo?
            git_bin.commit('-a', '-m', 'Test', with_keep_cwd=True)
        except git.GitCommandError:  # Will throw a command error if you try to init a repo that already exists
            pass

        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir']}):
            gitfs.update()

    def test_file_list(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir']}):
            ret = gitfs.file_list(load)
            self.assertIn('testfile', ret)

    def test_find_file(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir']}):
            ret = gitfs.find_file('testfile')
            expected_ret = {'path': '/tmp/salttest/cache/gitfs/refs/master/testfile',
                            'rel': 'testfile'}

            self.assertDictEqual(ret, expected_ret)

    def test_dir_list(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir']}):
            ret = gitfs.dir_list(load)
            self.assertIn('grail', ret)

    @skipIf(True, 'This test is failing and for good reason! See #9193')
    def test_file_list_emptydirs(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir']}):
            ret = gitfs.file_list_emptydirs(load)
            self.assertIn('empty_dir', ret)

    def test_envs(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir']}):
            ret = gitfs.envs()
            self.assertIn('base', ret)

    def test_file_hash_sha1(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir'],
                                         'hash_type': 'blob_sha1'}):
            tmp_load = load
            tmp_load['path'] = 'testfile'
            fnd = {'rel': 'testfile',
                   'path': 'testfile'}
            ret = gitfs.file_hash(load, fnd)
            self.assertDictEqual({'hash_type': 'blob_sha1', 'hsum': '0d234303e6451128d756c5c259175de37d767742'}, ret)

    @skipIf(True, 'This test is failing and for good reason! See #9194')
    def test_file_hash_md5(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir'],
                                         'hash_type': 'md5'}):
            tmp_load = load
            tmp_load['path'] = 'testfile'
            fnd = {'rel': 'testfile',
                   'path': 'testfile'}
            ret = gitfs.file_hash(load, fnd)
            self.assertDictEqual({'hash_type': 'md5', 'hsum': '98aa509006628302ce38ce521a7f805f'}, ret)

    def test_serve_file(self):
        with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
                                         'gitfs_remotes': ['file://' + self.tmp_repo_git],
                                         'sock_dir': self.master_opts['sock_dir'],
                                         'file_buffer_size': 262144}):
            fnd = {'rel': 'testfile',
                   'path': 'testfile'}

            tmp_load = load
            tmp_load['loc'] = 0

            ret = gitfs.serve_file(load, fnd)
            self.assertDictEqual({
                                     'data': 'Scene 24\n\n \n  OLD MAN:  Ah, hee he he ha!\n  ARTHUR:  And this enchanter of whom you speak, he has seen the grail?\n  OLD MAN:  Ha ha he he he he!\n  ARTHUR:  Where does he live?  Old man, where does he live?\n  OLD MAN:  He knows of a cave, a cave which no man has entered.\n  ARTHUR:  And the Grail... The Grail is there?\n  OLD MAN:  Very much danger, for beyond the cave lies the Gorge\n      of Eternal Peril, which no man has ever crossed.\n  ARTHUR:  But the Grail!  Where is the Grail!?\n  OLD MAN:  Seek you the Bridge of Death.\n  ARTHUR:  The Bridge of Death, which leads to the Grail?\n  OLD MAN:  Hee hee ha ha!\n\n',
                                     'dest': 'testfile'}, ret)

if __name__ == '__main__':
    integration.run_tests(GitFSTest)
<|endoftext|>


<commit_before># Copyright 2019 The Keras Tuner Authors
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

from tensorflow import keras
import kerastuner

@pytest.fixture(scope='module')
def tmp_dir(tmpdir_factory):
    return tmpdir_factory.mktemp('integration_test')

def build_model(hp):
    inputs = keras.Input(shape=(28, 28))
    x = keras.layers.Reshape((28 * 28,))(inputs)
    for i in range(hp.Range('num_layers', 1, 4)):
        x = keras.layers.Dense(
            units=hp.Range('units_' + str(i), 128, 512, 32, default=256),
            activation='relu')(x)
    x = keras.layers.Dropout(hp.Linear('dp', 0., 0.6, 0.1, default=0.5))(x)
    outputs = keras.layers.Dense(10, activation='softmax')(x)
    model = keras.Model(inputs, outputs)
    model.compile(
        optimizer=keras.optimizers.Adam(
            hp.Choice('learning_rate', [1e-2, 2e-3, 5e-4])),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])
    return model

def test_end_to_end_workflow(tmp_dir):
    (x, y), (val_x, val_y) = keras.datasets.mnist.load_data()
    x = x.astype('float32') / 255.
    val_x = val_x.astype('float32') / 255.

    x = x[:10000]
    y = y[:10000]

    tuner = kerastuner.tuners.RandomSearch(
        build_model,
        objective='val_accuracy',
        max_trials=20,
        directory=tmp_dir)

    tuner.search_space_summary()

    tuner.search(x=x,
                 y=y,
                 epochs=10,
                 batch_size=128,
                 callbacks=[keras.callbacks.EarlyStopping(patience=2)],
                 validation_data=(val_x, val_y))

    tuner.results_summary()

    best_model = tuner.get_best_models(1)[0]

    val_loss, val_acc = best_model.evaluate(val_x, val_y)
    assert val_acc > 0.96

if __name__ == '__main__':
    test_end_to_end_workflow('test_dir')
<commit_msg>Reduce sensitivity of end to end test<commit_after># Copyright 2019 The Keras Tuner Authors
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

from tensorflow import keras
import kerastuner

@pytest.fixture(scope='module')
def tmp_dir(tmpdir_factory):
    return tmpdir_factory.mktemp('integration_test')

def build_model(hp):
    inputs = keras.Input(shape=(28, 28))
    x = keras.layers.Reshape((28 * 28,))(inputs)
    for i in range(hp.Range('num_layers', 1, 4)):
        x = keras.layers.Dense(
            units=hp.Range('units_' + str(i), 128, 512, 32, default=256),
            activation='relu')(x)
    x = keras.layers.Dropout(hp.Linear('dp', 0., 0.6, 0.1, default=0.5))(x)
    outputs = keras.layers.Dense(10, activation='softmax')(x)
    model = keras.Model(inputs, outputs)
    model.compile(
        optimizer=keras.optimizers.Adam(
            hp.Choice('learning_rate', [1e-2, 2e-3, 5e-4])),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])
    return model

def test_end_to_end_workflow(tmp_dir):
    (x, y), (val_x, val_y) = keras.datasets.mnist.load_data()
    x = x.astype('float32') / 255.
    val_x = val_x.astype('float32') / 255.

    x = x[:10000]
    y = y[:10000]

    tuner = kerastuner.tuners.RandomSearch(
        build_model,
        objective='val_accuracy',
        max_trials=20,
        directory=tmp_dir)

    tuner.search_space_summary()

    tuner.search(x=x,
                 y=y,
                 epochs=10,
                 batch_size=128,
                 callbacks=[keras.callbacks.EarlyStopping(patience=2)],
                 validation_data=(val_x, val_y))

    tuner.results_summary()

    best_model = tuner.get_best_models(1)[0]

    val_loss, val_acc = best_model.evaluate(val_x, val_y)
    assert val_acc > 0.955

if __name__ == '__main__':
    test_end_to_end_workflow('test_dir')
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function, unicode_literals

__version__ = 0.1

import argparse
import os, os.path
import re
import sys
import unicodedata

p = argparse.ArgumentParser(description='')
p.add_argument('-i', '--ignorecase', '--ignore-case', action='store_true', default=False)
p.add_argument('-g', '--regexp', action='store_true', default=False)
p.add_argument('-f', '--fuzzy', action='store_true', default=False)
p.add_argument('-l', '--regex-multiline', action='store_true', default=False)
p.add_argument('-d', '--regex-dotall', action='store_true', default=False)
p.add_argument('anon_pattern', metavar='pattern', type=str, nargs=1)
args = p.parse_args()

pattern = args.anon_pattern[0]
pattern = pattern.decode('utf-8')
pattern = unicodedata.normalize('NFC', pattern)
if args.fuzzy:
    pattern = '(' + '.*'.join(re.escape(c) for c in pattern) + ')'
elif args.regexp:
    pattern = '(' + pattern.replace('(', r'\(').replace(')', r'\)') + ')'
else:
    pattern = '(' + re.escape(pattern) + ')'

flags = re.UNICODE
if args.ignorecase:
    flags |= re.IGNORECASE
if args.regex_multiline:
    flags |= re.MULTILINE
if args.regex_dotall:
    flags |= re.DOTALL
pattern = re.compile(pattern, flags)

data = sys.stdin.read()
data = data.decode('utf-8')
data = unicodedata.normalize('NFC', data)
data = pattern.sub(r'\033[32m\1\033[0m', data)
print(data)

<commit_msg>added module docstring<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" highlight - Highlight the found text.
    http://mysz.github.io/highlight
    Author: Marcin Sztolcman (marcin@urzenia.net)

    Get help with: highlight --help
    Information about version: highlight --version
"""

from __future__ import print_function, unicode_literals

__version__ = 0.1

import argparse
import os, os.path
import re
import sys
import unicodedata

p = argparse.ArgumentParser(description='')
p.add_argument('-i', '--ignorecase', '--ignore-case', action='store_true', default=False)
p.add_argument('-g', '--regexp', action='store_true', default=False)
p.add_argument('-f', '--fuzzy', action='store_true', default=False)
p.add_argument('-l', '--regex-multiline', action='store_true', default=False)
p.add_argument('-d', '--regex-dotall', action='store_true', default=False)
p.add_argument('anon_pattern', metavar='pattern', type=str, nargs=1)
args = p.parse_args()

pattern = args.anon_pattern[0]
pattern = pattern.decode('utf-8')
pattern = unicodedata.normalize('NFC', pattern)
if args.fuzzy:
    pattern = '(' + '.*'.join(re.escape(c) for c in pattern) + ')'
elif args.regexp:
    pattern = '(' + pattern.replace('(', r'\(').replace(')', r'\)') + ')'
else:
    pattern = '(' + re.escape(pattern) + ')'

flags = re.UNICODE
if args.ignorecase:
    flags |= re.IGNORECASE
if args.regex_multiline:
    flags |= re.MULTILINE
if args.regex_dotall:
    flags |= re.DOTALL
pattern = re.compile(pattern, flags)

data = sys.stdin.read()
data = data.decode('utf-8')
data = unicodedata.normalize('NFC', data)
data = pattern.sub(r'\033[32m\1\033[0m', data)
print(data)

<|endoftext|>


<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# hily 2018-05-06 M.Kolodin 2018-05-06 0.5
# Highly Likely Project

# ver. 0.5. it must scan folders in hily.ini and print result (same files) into file hily.out.

# note: PEP8 change: function names are separated from parameters lists even when they are empty; it is a tets to se if it works better than traditional spacing.

import sqlite3
import os.path

# ------------------------------------ config
# file with config
CONFIG = "hily.ini"

# folders list
flist = []

# outfile with pretty printed result, with default value
outfile = "./outfile.txt"

# ------------------------------------ start

def good_config ():
    """read configuration and cry if none or wrong"""

    global flist, outfile

    print ("Looking for configuration file.")

    if os.path.exists (CONFIG):
        with open (CONFIG) as config:
            for line in config:
                line = line.strip ()
                print (line)
                if len (line):
                    cmd, data = line.split ()
                    if cmd == "in":
                        flist += [data]
                    elif cmd == "out":
                        outfile = data
        print (f"\nFolders list is {flist},\nresult will be written to {outfile}.")

        if not flist:
            print ("Folders list is empty. No work is possible. Quitting.")
            return False

    else:
        print (f"Config file {CONFIG} does not exist. Quitting.")
        return False

    return True

# ------------------------------------ init_db

def init_db ():
    """create database if it does not exists,
    connect to it and use
    """
    pass

# ------------------------------------ scan_folders

def scan_folders ():
    """make main job: scan multiple folders recursively"""
    pass

# ------------------------------------ print_result

def print_result ():
    """print result from renewed database in pleasant way"""
    print ("OK.")

# ------------------------------------ main

def main ():
    """starter"""
    print ("This is HiLy project: Highly Likely folders scanner.\n")
    if good_config ():
        init_db ()
        scan_folders ()
        print_result ()

# ------------------------------------ init

if __name__ == "__main__":
    main ()

# ------------------------------------ the end.
<commit_msg>ver. 0.6. with TODOs<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# hily 2018-05-06 M.Kolodin 2018-05-07 0.6
# Highly Likely Project

# ver. 0.6. it must scan folders in hily.ini and print result (same files) into file hily.out.

# note: PEP8 change: function names are separated from parameters lists even when they are empty; it is a tets to se if it works better than traditional spacing.

import sqlite3
import os.path

# ------------------------------------ config
# file with config
CONFIG = "hily.ini"

# folders list
flist = []

# outfile with pretty printed result, with default value
outfile = "./outfile.txt"

# ------------------------------------ start

def good_config ():
    """read configuration and cry if none or wrong.

    TODO: convert given file paths to absolute ones.
    TODO: split full 2nd subline to data.
    """

    global flist, outfile

    print ("Looking for configuration file.")

    if os.path.exists (CONFIG):
        with open (CONFIG) as config:
            for line in config:
                line = line.strip ()
                if len (line):
                    print (line)
                    cmd, data = line.split ()
                    if cmd == "in":
                        flist += [data]
                    elif cmd == "out":
                        outfile = data
        print (f"\nFolders list is {flist},\nresult will be written to {outfile}.")

        if not flist:
            print ("Folders list is empty. No work is possible. Quitting.")
            return False

    else:
        print (f"Config file {CONFIG} does not exist. Quitting.")
        return False

    return True

# ------------------------------------ init_db

def init_db ():
    """create database if it does not exists,
    connect to it and use
    """
    pass

# ------------------------------------ scan_folders

def scan_folders ():
    """make main job: scan multiple folders recursively"""
    pass

# ------------------------------------ print_result

def print_result ():
    """print result from renewed database in pleasant way"""
    print ("OK.")

# ------------------------------------ main

def main ():
    """starter"""
    print ("This is HiLy project: Highly Likely folders scanner.\n")
    if good_config ():
        init_db ()
        scan_folders ()
        print_result ()

# ------------------------------------ init

if __name__ == "__main__":
    main ()

# ------------------------------------ the end.
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Natural Language API based Segmenter.

Word segmenter module powered by
`Cloud Natural Language API <https://cloud.google.com/natural-language/>`_.
You need to enable the API in your Google Cloud Platform project before you
use this module.

Example:
  Once you enabled the API, download a service account's credentials and set
  as `GOOGLE_APPLICATION_CREDENTIALS` environment variable.

  .. code-block:: bash

     $ export GOOGLE_APPLICATION_CREDENTIALS='/path/to/credentials.json'

  Alternatively, you can also pass the path to your credentials file to the
  module.

  .. code-block:: python

     segmenter = budou.segmenter.NLAPISegmenter(
         credentials_path='/path/to/credentials.json')

This module is equipped with caching system not to make multiple requests for
the same source sentence because making request to the API may incur costs.
The caching system is provided by `budou.cachefactory`, and a proper caching
system is chosen to be used based on the environment.

"""

from __future__ import unicode_literals
from builtins import str
import logging
import hashlib
from .segmenter import Segmenter
from .cachefactory import load_cache
from .chunk import Chunk, ChunkList

_DEPENDENT_LABEL = (
    'P', 'SNUM', 'PRT', 'AUX', 'SUFF', 'AUXPASS', 'RDROP', 'NUMBER', 'NUM',
    'PREF')
""" list of str: Labels dependent to other parts.
"""

def _memorize(func):
  """Decorator to cache the given function's output.
  """

  def _wrapper(self, *args, **kwargs):
    """Wrapper to cache the function's output.
    """
    if self.use_cache:
      cache = load_cache(self.cache_filename)
      original_key = ':'.join([
          self.__class__.__name__,
          func.__name__,
          '_'.join([str(a) for a in args]),
          '_'.join([str(w) for w in kwargs.values()])])
      cache_key = hashlib.md5(original_key.encode('utf-8')).hexdigest()
      cached_val = cache.get(cache_key)
      if cached_val:
        return cached_val
    val = func(self, *args, **kwargs)
    if self.use_cache:
      cache.set(cache_key, val)
    return val
  return _wrapper

class NLAPISegmenter(Segmenter):
  """Natural Language API Segmenter.

  Attributes:
    service: A resource object for interacting with Cloud Natural Language API.
    cache_filename (str): File path to the cache file.
    supported_languages (list of str): List of supported languages' codes.

  Args:
      cache_filename (:obj:`str`, optional): File path to the pickle file for
          caching. The file is created automatically if not exist. If the
          environment is Google App Engine Standard Environment and memcache
          service is available, it is used for caching and the pickle file
          won't be generated.
      credentials_path (:obj:`str`, optional): File path to the service
          account's credentials file. If no file path is specified, it tries
          to authenticate with default credentials.
      use_entity (:obj:`bool`, optional): Whether to use entity analysis
          results to wrap entity names in the output.
      use_cache (:obj:`bool`, optional): Whether to use a cache system.
  """

  supported_languages = {'ja', 'ko', 'zh', 'zh-TW', 'zh-CN', 'zh-HK'}

  def __init__(self, cache_filename, credentials_path, use_entity, use_cache):

    self.cache_filename = cache_filename
    self.credentials_path = credentials_path
    self.use_entity = use_entity
    self.use_cache = use_cache
    self._authenticate()

  def _authenticate(self):

    import google_auth_httplib2
    import googleapiclient.discovery

    scope = ['https://www.googleapis.com/auth/cloud-platform']
    if self.credentials_path:
      try:
        from google.oauth2 import service_account
        credentials = service_account.Credentials.from_service_account_file(
            self.credentials_path)
        scoped_credentials = credentials.with_scopes(scope)
      except ImportError:
        logging.error('Failed to load google.oauth2.service_account module. '
                      'If you are running this script in Google App Engine '
                      'environment, you can initialize the segmenter with '
                      'default credentials.')

    else:
      import google.auth
      scoped_credentials, _ = google.auth.default(scope)
    authed_http = google_auth_httplib2.AuthorizedHttp(scoped_credentials)
    service = googleapiclient.discovery.build(
        'language', 'v1beta2', http=authed_http)
    self.service = service

  def segment(self, source, language=None):
    """Returns a chunk list from the given sentence.

    Args:
      source (str): Source string to segment.
      language (:obj:`str`, optional): A language code.

    Returns:
      A chunk list. (:obj:`budou.chunk.ChunkList`)

    Raises:
      ValueError: If :obj:`language` is given and it is not included in
                  :obj:`supported_languages`.
    """
    if language and not language in self.supported_languages:
      raise ValueError(
          'Language {} is not supported by NLAPI segmenter'.format(language))

    chunks, language = self._get_source_chunks(source, language=language)
    if self.use_entity:
      entities = self._get_entities(source, language=language)
      chunks = self._group_chunks_by_entities(chunks, entities)
    chunks.resolve_dependencies()
    return chunks

  def _get_source_chunks(self, input_text, language=None):
    """Returns a chunk list retrieved from Syntax Analysis results.

    Args:
      input_text (str): Text to annotate.
      language (:obj:`str`, optional): Language of the text.

    Returns:
      A chunk list. (:obj:`budou.chunk.ChunkList`)
    """
    chunks = ChunkList()
    seek = 0
    result = self._get_annotations(input_text, language=language)
    tokens = result['tokens']
    language = result['language']
    for i, token in enumerate(tokens):
      word = token['text']['content']
      begin_offset = token['text']['beginOffset']
      label = token['dependencyEdge']['label']
      pos = token['partOfSpeech']['tag']
      if begin_offset > seek:
        chunks.append(Chunk.space())
        seek = begin_offset
      chunk = Chunk(word, pos, label)
      if chunk.label in _DEPENDENT_LABEL:
        # Determining concatenating direction based on syntax dependency.
        chunk.dependency = i < token['dependencyEdge']['headTokenIndex']
      if chunk.is_punct():
        chunk.dependency = chunk.is_open_punct()
      chunks.append(chunk)
      seek += len(word)
    return chunks, language

  def _group_chunks_by_entities(self, chunks, entities):
    """Groups chunks by entities retrieved from NL API Entity Analysis.

    Args:
      chunks (:obj:`budou.chunk.ChunkList`): List of chunks to be processed.
      entities (:obj:`list` of :obj:`dict`): List of entities.

    Returns:
      A chunk list. (:obj:`budou.chunk.ChunkList`)
    """
    for entity in entities:
      chunks_to_concat = chunks.get_overlaps(
          entity['beginOffset'], len(entity['content']))
      if not chunks_to_concat:
        continue
      new_chunk_word = u''.join([chunk.word for chunk in chunks_to_concat])
      new_chunk = Chunk(new_chunk_word)
      chunks.swap(chunks_to_concat, new_chunk)
    return chunks

  @_memorize
  def _get_annotations(self, text, language=''):
    """Returns the list of annotations retrieved from the given text.

    Args:
      text (str): Input text.
      language (:obj:`str`, optional): Language code.

    Returns:
      Results in a dictionary. :code:`tokens` contains the list of annotations
      and :code:`language` contains the inferred language from the input.
    """
    body = {
        'document': {
            'type': 'PLAIN_TEXT',
            'content': text,
        },
        'features': {
            'extract_syntax': True,
        },
        'encodingType': 'UTF32',
    }
    if language:
      body['document']['language'] = language

    request = self.service.documents().annotateText(body=body)
    response = request.execute()
    tokens = response.get('tokens', [])
    language = response.get('language')

    return {'tokens': tokens, 'language': language}

  @_memorize
  def _get_entities(self, text, language=''):
    """Returns the list of entities retrieved from the given text.

    Args:
      text (str): Input text.
      language (:obj:`str`, optional): Language code.

    Returns:
      List of entities.
    """
    body = {
        'document': {
            'type': 'PLAIN_TEXT',
            'content': text,
        },
        'encodingType': 'UTF32',
    }
    if language:
      body['document']['language'] = language

    request = self.service.documents().analyzeEntities(body=body)
    response = request.execute()
    result = []
    for entity in response.get('entities', []):
      mentions = entity.get('mentions', [])
      if not mentions:
        continue
      entity_text = mentions[0]['text']
      offset = entity_text['beginOffset']
      for word in entity_text['content'].split():
        result.append({'content': word, 'beginOffset': offset})
        offset += len(word)
    return result
<commit_msg>Add zh-Hant in supported languages for NLAPI.<commit_after># -*- coding: utf-8 -*-
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Natural Language API based Segmenter.

Word segmenter module powered by
`Cloud Natural Language API <https://cloud.google.com/natural-language/>`_.
You need to enable the API in your Google Cloud Platform project before you
use this module.

Example:
  Once you enabled the API, download a service account's credentials and set
  as `GOOGLE_APPLICATION_CREDENTIALS` environment variable.

  .. code-block:: bash

     $ export GOOGLE_APPLICATION_CREDENTIALS='/path/to/credentials.json'

  Alternatively, you can also pass the path to your credentials file to the
  module.

  .. code-block:: python

     segmenter = budou.segmenter.NLAPISegmenter(
         credentials_path='/path/to/credentials.json')

This module is equipped with caching system not to make multiple requests for
the same source sentence because making request to the API may incur costs.
The caching system is provided by `budou.cachefactory`, and a proper caching
system is chosen to be used based on the environment.

"""

from __future__ import unicode_literals
from builtins import str
import logging
import hashlib
from .segmenter import Segmenter
from .cachefactory import load_cache
from .chunk import Chunk, ChunkList

_DEPENDENT_LABEL = (
    'P', 'SNUM', 'PRT', 'AUX', 'SUFF', 'AUXPASS', 'RDROP', 'NUMBER', 'NUM',
    'PREF')
""" list of str: Labels dependent to other parts.
"""

def _memorize(func):
  """Decorator to cache the given function's output.
  """

  def _wrapper(self, *args, **kwargs):
    """Wrapper to cache the function's output.
    """
    if self.use_cache:
      cache = load_cache(self.cache_filename)
      original_key = ':'.join([
          self.__class__.__name__,
          func.__name__,
          '_'.join([str(a) for a in args]),
          '_'.join([str(w) for w in kwargs.values()])])
      cache_key = hashlib.md5(original_key.encode('utf-8')).hexdigest()
      cached_val = cache.get(cache_key)
      if cached_val:
        return cached_val
    val = func(self, *args, **kwargs)
    if self.use_cache:
      cache.set(cache_key, val)
    return val
  return _wrapper

class NLAPISegmenter(Segmenter):
  """Natural Language API Segmenter.

  Attributes:
    service: A resource object for interacting with Cloud Natural Language API.
    cache_filename (str): File path to the cache file.
    supported_languages (list of str): List of supported languages' codes.

  Args:
      cache_filename (:obj:`str`, optional): File path to the pickle file for
          caching. The file is created automatically if not exist. If the
          environment is Google App Engine Standard Environment and memcache
          service is available, it is used for caching and the pickle file
          won't be generated.
      credentials_path (:obj:`str`, optional): File path to the service
          account's credentials file. If no file path is specified, it tries
          to authenticate with default credentials.
      use_entity (:obj:`bool`, optional): Whether to use entity analysis
          results to wrap entity names in the output.
      use_cache (:obj:`bool`, optional): Whether to use a cache system.
  """

  supported_languages = {'ja', 'ko', 'zh', 'zh-TW', 'zh-CN', 'zh-HK', 'zh-Hant'}

  def __init__(self, cache_filename, credentials_path, use_entity, use_cache):

    self.cache_filename = cache_filename
    self.credentials_path = credentials_path
    self.use_entity = use_entity
    self.use_cache = use_cache
    self._authenticate()

  def _authenticate(self):

    import google_auth_httplib2
    import googleapiclient.discovery

    scope = ['https://www.googleapis.com/auth/cloud-platform']
    if self.credentials_path:
      try:
        from google.oauth2 import service_account
        credentials = service_account.Credentials.from_service_account_file(
            self.credentials_path)
        scoped_credentials = credentials.with_scopes(scope)
      except ImportError:
        logging.error('Failed to load google.oauth2.service_account module. '
                      'If you are running this script in Google App Engine '
                      'environment, you can initialize the segmenter with '
                      'default credentials.')

    else:
      import google.auth
      scoped_credentials, _ = google.auth.default(scope)
    authed_http = google_auth_httplib2.AuthorizedHttp(scoped_credentials)
    service = googleapiclient.discovery.build(
        'language', 'v1beta2', http=authed_http)
    self.service = service

  def segment(self, source, language=None):
    """Returns a chunk list from the given sentence.

    Args:
      source (str): Source string to segment.
      language (:obj:`str`, optional): A language code.

    Returns:
      A chunk list. (:obj:`budou.chunk.ChunkList`)

    Raises:
      ValueError: If :obj:`language` is given and it is not included in
                  :obj:`supported_languages`.
    """
    if language and not language in self.supported_languages:
      raise ValueError(
          'Language {} is not supported by NLAPI segmenter'.format(language))

    chunks, language = self._get_source_chunks(source, language=language)
    if self.use_entity:
      entities = self._get_entities(source, language=language)
      chunks = self._group_chunks_by_entities(chunks, entities)
    chunks.resolve_dependencies()
    return chunks

  def _get_source_chunks(self, input_text, language=None):
    """Returns a chunk list retrieved from Syntax Analysis results.

    Args:
      input_text (str): Text to annotate.
      language (:obj:`str`, optional): Language of the text.

    Returns:
      A chunk list. (:obj:`budou.chunk.ChunkList`)
    """
    chunks = ChunkList()
    seek = 0
    result = self._get_annotations(input_text, language=language)
    tokens = result['tokens']
    language = result['language']
    for i, token in enumerate(tokens):
      word = token['text']['content']
      begin_offset = token['text']['beginOffset']
      label = token['dependencyEdge']['label']
      pos = token['partOfSpeech']['tag']
      if begin_offset > seek:
        chunks.append(Chunk.space())
        seek = begin_offset
      chunk = Chunk(word, pos, label)
      if chunk.label in _DEPENDENT_LABEL:
        # Determining concatenating direction based on syntax dependency.
        chunk.dependency = i < token['dependencyEdge']['headTokenIndex']
      if chunk.is_punct():
        chunk.dependency = chunk.is_open_punct()
      chunks.append(chunk)
      seek += len(word)
    return chunks, language

  def _group_chunks_by_entities(self, chunks, entities):
    """Groups chunks by entities retrieved from NL API Entity Analysis.

    Args:
      chunks (:obj:`budou.chunk.ChunkList`): List of chunks to be processed.
      entities (:obj:`list` of :obj:`dict`): List of entities.

    Returns:
      A chunk list. (:obj:`budou.chunk.ChunkList`)
    """
    for entity in entities:
      chunks_to_concat = chunks.get_overlaps(
          entity['beginOffset'], len(entity['content']))
      if not chunks_to_concat:
        continue
      new_chunk_word = u''.join([chunk.word for chunk in chunks_to_concat])
      new_chunk = Chunk(new_chunk_word)
      chunks.swap(chunks_to_concat, new_chunk)
    return chunks

  @_memorize
  def _get_annotations(self, text, language=''):
    """Returns the list of annotations retrieved from the given text.

    Args:
      text (str): Input text.
      language (:obj:`str`, optional): Language code.

    Returns:
      Results in a dictionary. :code:`tokens` contains the list of annotations
      and :code:`language` contains the inferred language from the input.
    """
    body = {
        'document': {
            'type': 'PLAIN_TEXT',
            'content': text,
        },
        'features': {
            'extract_syntax': True,
        },
        'encodingType': 'UTF32',
    }
    if language:
      body['document']['language'] = language

    request = self.service.documents().annotateText(body=body)
    response = request.execute()
    tokens = response.get('tokens', [])
    language = response.get('language')

    return {'tokens': tokens, 'language': language}

  @_memorize
  def _get_entities(self, text, language=''):
    """Returns the list of entities retrieved from the given text.

    Args:
      text (str): Input text.
      language (:obj:`str`, optional): Language code.

    Returns:
      List of entities.
    """
    body = {
        'document': {
            'type': 'PLAIN_TEXT',
            'content': text,
        },
        'encodingType': 'UTF32',
    }
    if language:
      body['document']['language'] = language

    request = self.service.documents().analyzeEntities(body=body)
    response = request.execute()
    result = []
    for entity in response.get('entities', []):
      mentions = entity.get('mentions', [])
      if not mentions:
        continue
      entity_text = mentions[0]['text']
      offset = entity_text['beginOffset']
      for word in entity_text['content'].split():
        result.append({'content': word, 'beginOffset': offset})
        offset += len(word)
    return result
<|endoftext|>


<commit_before>#!/usr/bin/env python3

import sqlite3
import sys
import glob
import os
import re
import json
import shutil
import errno

import address_storage

dbpath = os.path.expanduser("~/.mozilla/firefox/*.default/places.sqlite")
dbpaths = glob.glob(dbpath)
config_directory_path = os.path.expanduser("~/.config/histfetch")
config_path = "/".join([config_directory_path, "config.json"])
default_config_path = "default_config.json"

def isSQLite3(filename):
    from os.path import isfile, getsize

    if not isfile(filename):
        return False
    if getsize(filename) < 100: # SQLite database file header is 100 bytes
        return False

    with open(filename, "rb") as sq_file:
        header = sq_file.read(100)

    return header[:16] == b"SQLite format 3\x00"

def import_from_database(path, address_storage):
    if not isSQLite3(path):
        print("No such SQLite3 database:", path, file=sys.stderr)
        sys.exit(1)
    connection = sqlite3.connect(path)
    cursor = connection.cursor()
    cmd = "SELECT url,last_visit_date FROM moz_places"
    cursor.execute(cmd)
    for response in cursor:
        address_storage.feed(response[0], response[1])

def compile_address_patterns(config):
    address_patterns = []
    for pattern_pair in config["patterns"]:
        address_patterns.append(
            (
                re.compile(pattern_pair["dictionary_address"]),
                [re.compile(word_pattern)
                    for word_pattern in pattern_pair["word_patterns"]]
            )
        )
    return address_patterns

def read_config_file(config_path):
    with open(config_path, "rt") as config_file:
        config = json.load(config_file)
    return compile_address_patterns(config)

def main():
    if not os.path.isfile(config_path):
        print("No config fine found; create default", file=sys.stderr)
        try:
            os.makedirs(config_directory_path)
        except OSError as exception:
            if exception.errno != errno.EEXIST:
                print("Can't create default config:", sys.exc_info(),
                        file=sys.stderr)
                exit(1)
        shutil.copyfile(default_config_path, config_path)
    try:
        address_patterns = read_config_file(config_path)
    except:
        print("Can't read config file:", sys.exc_info(), file=sys.stderr)
        exit(1)

    storage = address_storage.AddressStore(address_patterns)
    if not dbpaths:
        print("No paths to import words from", file=sys.stderr)
        sys.exit(1)
    for path in dbpaths:
        import_from_database(path, storage)
    storage.print_all()

if __name__ == "__main__":
    main()
<commit_msg>Store last used config<commit_after>#!/usr/bin/env python3

import sqlite3
import sys
import glob
import os
import re
import json
import shutil
import errno

import address_storage

dbpath = os.path.expanduser("~/.mozilla/firefox/*.default/places.sqlite")
dbpaths = glob.glob(dbpath)
config_directory_path = os.path.expanduser("~/.config/histfetch")
config_path = "/".join([config_directory_path, "config.json"])
default_config_path = "default_config.json"
last_config_path = "/".join([config_directory_path, "last_used_config.json"])

def isSQLite3(filename):
    from os.path import isfile, getsize

    if not isfile(filename):
        return False
    if getsize(filename) < 100: # SQLite database file header is 100 bytes
        return False

    with open(filename, "rb") as sq_file:
        header = sq_file.read(100)

    return header[:16] == b"SQLite format 3\x00"

def import_from_database(path, address_storage):
    if not isSQLite3(path):
        print("No such SQLite3 database:", path, file=sys.stderr)
        sys.exit(1)
    connection = sqlite3.connect(path)
    cursor = connection.cursor()
    cmd = "SELECT url,last_visit_date FROM moz_places"
    cursor.execute(cmd)
    for response in cursor:
        address_storage.feed(response[0], response[1])

def compile_address_patterns(config):
    address_patterns = []
    for pattern_pair in config["patterns"]:
        address_patterns.append(
            (
                re.compile(pattern_pair["dictionary_address"]),
                [re.compile(word_pattern)
                    for word_pattern in pattern_pair["word_patterns"]]
            )
        )
    return address_patterns

def read_config_file(config_path):
    with open(config_path, "rt") as config_file:
        config = json.load(config_file)
    return config

def create_default_config():
    try:
        os.makedirs(config_directory_path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            print("Can't create default config:", sys.exc_info(),
                    file=sys.stderr)
            exit(1)
    shutil.copyfile(default_config_path, config_path)

def main():
    if not os.path.isfile(config_path):
        print("No config fine found; create default", file=sys.stderr)
        create_default_config()
    try:
        config = read_config_file(config_path)
        address_patterns = compile_address_patterns(config)
    except:
        print("Can't read config file:", sys.exc_info(), file=sys.stderr)
        exit(1)

    storage = address_storage.AddressStore(address_patterns)
    if not dbpaths:
        print("No paths to import words from", file=sys.stderr)
        sys.exit(1)
    for path in dbpaths:
        import_from_database(path, storage)
    with open(last_config_path, "wt") as last_config_file:
        json.dump(config, last_config_file)
    storage.print_all()

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

def add_users_to_account_admin(apps, schema_editor):
    LilyUser = apps.get_model('users', 'LilyUser')
    Tenant = apps.get_model('tenant', 'Tenant')
    Group = apps.get_model('auth', 'Group')
    account_admin = Group.objects.get_or_create(name='account_admin')[0]

    for tenant in Tenant.objects.all():
        users = LilyUser.objects.filter(tenant=tenant)
        first_user = users.order_by('date_joined').first()

        if first_user:
            first_user.groups.add(account_admin)

    for superuser in LilyUser.objects.filter(is_superuser=True):
        superuser.groups.add(account_admin)

def remove_users_from_account_admin(apps, schema_editor):
    LilyUser = apps.get_model('users', 'LilyUser')
    Tenant = apps.get_model('tenant', 'Tenant')
    Group = apps.get_model('auth', 'Group')
    account_admin = Group.objects.get(name='account_admin')

    for tenant in Tenant.objects.all():
        users = LilyUser.objects.filter(tenant=tenant)
        first_user = users.order_by('date_joined').first()

        if first_user:
            if first_user.groups.filter(name='account_admin').exists():
                first_user.groups.remove(account_admin)

    for superuser in LilyUser.objects.filter(is_superuser=True):
        superuser.groups.remove(account_admin)

    account_admin.delete()

class Migration(migrations.Migration):

    dependencies = [
        ('users', '0020_set_user_info'),
        ('tenant', '0004_tenant_currency'),
    ]

    operations = [
        migrations.RunPython(add_users_to_account_admin, remove_users_from_account_admin),
    ]
<commit_msg>LILY-2254: Added a dependency to the auth app.<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

def add_users_to_account_admin(apps, schema_editor):
    LilyUser = apps.get_model('users', 'LilyUser')
    Tenant = apps.get_model('tenant', 'Tenant')
    Group = apps.get_model('auth', 'Group')
    account_admin = Group.objects.get_or_create(name='account_admin')[0]

    for tenant in Tenant.objects.all():
        users = LilyUser.objects.filter(tenant=tenant)
        first_user = users.order_by('date_joined').first()

        if first_user:
            first_user.groups.add(account_admin)

    for superuser in LilyUser.objects.filter(is_superuser=True):
        superuser.groups.add(account_admin)

def remove_users_from_account_admin(apps, schema_editor):
    LilyUser = apps.get_model('users', 'LilyUser')
    Tenant = apps.get_model('tenant', 'Tenant')
    Group = apps.get_model('auth', 'Group')
    account_admin = Group.objects.get(name='account_admin')

    for tenant in Tenant.objects.all():
        users = LilyUser.objects.filter(tenant=tenant)
        first_user = users.order_by('date_joined').first()

        if first_user:
            if first_user.groups.filter(name='account_admin').exists():
                first_user.groups.remove(account_admin)

    for superuser in LilyUser.objects.filter(is_superuser=True):
        superuser.groups.remove(account_admin)

    account_admin.delete()

class Migration(migrations.Migration):

    dependencies = [
        ('users', '0020_set_user_info'),
        ('tenant', '0004_tenant_currency'),
        ('auth', '0006_require_contenttypes_0002')
    ]

    operations = [
        migrations.RunPython(add_users_to_account_admin, remove_users_from_account_admin),
    ]
<|endoftext|>


<commit_before>from buffer.response import ResponseObject

PATHS = {
  'GET_UPDATE': 'updates/%s.json',
  'GET_INTERACTIONS': 'updates/%s/interactions.json',
  'EDIT': 'updates/%s/update.json',
  'PUBLISH': 'updates/%s/share.json',
  'DELETE': 'updates/%s/destroy.json',
  'MOVE_TO_TOP': 'updates/%s/move_to_top.json',
}

class Update(ResponseObject):

  def __init__(self, api, id=None, raw_response=None):

    if id and not raw_response:
      raw_response = api.get(url=PATHS['GET_UPDATE'] % id)

    super(Update, self).__init__(raw_response)

    self.api = api
    self.__interactions = []

  @property
  def interactions(self):
    '''
      Returns the detailed information on individual interactions with the social
      media update such as favorites, retweets and likes.
    '''

    interactions = []
    url = PATHS['GET_INTERACTIONS'] % self.id

    response = self.api.get(url=url)
    for interaction in response['interactions']:
      interactions.append(ResponseObject(interaction))

    self.__interactions = interactions

    return self.__interactions

  def edit(self, text, media=None, utc=None, now=None):
    '''
      Edit an existing, individual status update.
    '''

    url = PATHS['EDIT'] % self.id

    post_data = "text=%s&" % text

    if now:
      post_data += "now=%s&" % now

    if utc:
      post_data += "utc=%s&" % utc

    if media:
      media_format = "media[%s]=%s&"

      for media_type, media_item in media.iteritems():
        post_data += media_format % (media_type, media_item)

    return Update(api=self.api, raw_response=self.api.post(url=url,
      data=post_data)['update'])

  def publish(self):
    '''
      Immediately shares a single pending update and recalculates times for
      updates remaining in the queue.
    '''

    url = PATHS['PUBLISH'] % self.id
    return self.api.post(url=url)

  def delete(self):
    '''
      Permanently delete an existing status update.
    '''

    url = PATHS['DELETE'] % self.id
    return self.api.post(url=url)

  def move_to_top(self):
    '''
      Move an existing status update to the top of the queue and recalculate
      times for all updates in the queue. Returns the update with its new
      posting time.
    '''

    url = PATHS['MOVE_TO_TOP'] % self.id

    response = self.api.post(url=url)
    return Update(api=self.api, raw_response=response)
<commit_msg>Break post_data in 2 statements<commit_after>from buffer.response import ResponseObject

PATHS = {
  'GET_UPDATE': 'updates/%s.json',
  'GET_INTERACTIONS': 'updates/%s/interactions.json',
  'EDIT': 'updates/%s/update.json',
  'PUBLISH': 'updates/%s/share.json',
  'DELETE': 'updates/%s/destroy.json',
  'MOVE_TO_TOP': 'updates/%s/move_to_top.json',
}

class Update(ResponseObject):

  def __init__(self, api, id=None, raw_response=None):

    if id and not raw_response:
      raw_response = api.get(url=PATHS['GET_UPDATE'] % id)

    super(Update, self).__init__(raw_response)

    self.api = api
    self.__interactions = []

  @property
  def interactions(self):
    '''
      Returns the detailed information on individual interactions with the social
      media update such as favorites, retweets and likes.
    '''

    interactions = []
    url = PATHS['GET_INTERACTIONS'] % self.id

    response = self.api.get(url=url)
    for interaction in response['interactions']:
      interactions.append(ResponseObject(interaction))

    self.__interactions = interactions

    return self.__interactions

  def edit(self, text, media=None, utc=None, now=None):
    '''
      Edit an existing, individual status update.
    '''

    url = PATHS['EDIT'] % self.id

    post_data = "text=%s&" % text

    if now:
      post_data += "now=%s&" % now

    if utc:
      post_data += "utc=%s&" % utc

    if media:
      media_format = "media[%s]=%s&"

      for media_type, media_item in media.iteritems():
        post_data += media_format % (media_type, media_item)

    response = self.api.post(url=url, data=post_data)

    return Update(api=self.api, raw_response=response['update'])

  def publish(self):
    '''
      Immediately shares a single pending update and recalculates times for
      updates remaining in the queue.
    '''

    url = PATHS['PUBLISH'] % self.id
    return self.api.post(url=url)

  def delete(self):
    '''
      Permanently delete an existing status update.
    '''

    url = PATHS['DELETE'] % self.id
    return self.api.post(url=url)

  def move_to_top(self):
    '''
      Move an existing status update to the top of the queue and recalculate
      times for all updates in the queue. Returns the update with its new
      posting time.
    '''

    url = PATHS['MOVE_TO_TOP'] % self.id

    response = self.api.post(url=url)
    return Update(api=self.api, raw_response=response)
<|endoftext|>


<commit_before>import unittest

from tests.delete_test_db import delete_test_db  # module that deletes the DB :)
import database.main
from tests.create_test_db import engine, session, Base

database.main.engine = engine
database.main.session = session
database.main.Base = Base
import models.main
from models.spells.paladin_spells_template import PaladinSpellsSchema
from spells import PaladinSpell
from models.items.item_template import ItemTemplateSchema
from models.spells.spell_dots import DotSchema
from buffs import BeneficialBuff, DoT

class PaladinSpellsSchemaTests(unittest.TestCase):
    def setUp(self):
        """
        Test that the values in the Schema are as expected
        And that the convert_to_paladin_spell_object function works
        """
        self.spell_entry = 4
        self.spell_name = 'Melting Strike'
        self.dot = DoT(name='Melting', damage_tick=2, duration=2, caster_lvl=0)
        self.expected_spell = PaladinSpell(name=self.spell_name, rank=1, damage1=3, damage2=0, damage3=0,
                                           heal1=0, heal2=0, heal3=0, mana_cost=6, cooldown=3,
                                           beneficial_effect=None, harmful_effect=self.dot)

    def test_schema_values(self):
        """ Load a schema object and assert that every value is as expected"""
        loaded_schema: PaladinSpellsSchema = session.query(PaladinSpellsSchema).get(self.spell_entry)

        self.assertTrue(isinstance(loaded_schema.id, int))
        self.assertTrue(isinstance(loaded_schema.name, str))
        self.assertTrue(isinstance(loaded_schema.rank, int))
        self.assertTrue(isinstance(loaded_schema.level_required, int))
        self.assertTrue(isinstance(loaded_schema.damage1, int))
        self.assertTrue(isinstance(loaded_schema.damage2, int))
        self.assertTrue(isinstance(loaded_schema.damage3, int))
        self.assertTrue(isinstance(loaded_schema.heal1, int))
        self.assertTrue(isinstance(loaded_schema.heal2, int))
        self.assertTrue(isinstance(loaded_schema.heal3, int))
        self.assertTrue(isinstance(loaded_schema.mana_cost, int))
        self.assertIsNone(loaded_schema.beneficial_effect)
        self.assertTrue(isinstance(loaded_schema.harmful_effect, int))
        self.assertTrue(isinstance(loaded_schema.cooldown, int))
        self.assertTrue(isinstance(loaded_schema.comment, str))
        self.assertIsNone(loaded_schema.buff)
        self.assertTrue(isinstance(loaded_schema.dot, DotSchema))

    def test_convert_to_paladin_spell_object(self):
        """
        See if the convert_to_paladin_spell_object works properly
        """
        loaded_quest: PaladinSpell = session.query(PaladinSpellsSchema).get(self.spell_entry).convert_to_paladin_spell_object()
        self.assertEqual(vars(loaded_quest), vars(self.expected_spell))

def tearDownModule():
    delete_test_db()

if __name__ == '__main__':
    unittest.main()
<commit_msg>Fixed a problem in the paladin spells test<commit_after>import unittest

from tests.delete_test_db import delete_test_db  # module that deletes the DB :)
import database.main
from tests.create_test_db import engine, session, Base

database.main.engine = engine
database.main.session = session
database.main.Base = Base
import models.main
from models.spells.paladin_spells_template import PaladinSpellsSchema
from spells import PaladinSpell
from models.items.item_template import ItemTemplateSchema
from models.spells.spell_dots import DotSchema
from buffs import BeneficialBuff, DoT
from damage import Damage

class PaladinSpellsSchemaTests(unittest.TestCase):
    def setUp(self):
        """
        Test that the values in the Schema are as expected
        And that the convert_to_paladin_spell_object function works
        """
        self.spell_entry = 4
        self.spell_name = 'Melting Strike'
        self.dot = DoT(name='Melting', damage_tick=Damage(magic_dmg=2), duration=2, caster_lvl=0)
        self.expected_spell = PaladinSpell(name=self.spell_name, rank=1, damage1=3, damage2=0, damage3=0,
                                           heal1=0, heal2=0, heal3=0, mana_cost=6, cooldown=3,
                                           beneficial_effect=None, harmful_effect=self.dot)

    def test_schema_values(self):
        """ Load a schema object and assert that every value is as expected"""
        loaded_schema: PaladinSpellsSchema = session.query(PaladinSpellsSchema).get(self.spell_entry)

        self.assertTrue(isinstance(loaded_schema.id, int))
        self.assertTrue(isinstance(loaded_schema.name, str))
        self.assertTrue(isinstance(loaded_schema.rank, int))
        self.assertTrue(isinstance(loaded_schema.level_required, int))
        self.assertTrue(isinstance(loaded_schema.damage1, int))
        self.assertTrue(isinstance(loaded_schema.damage2, int))
        self.assertTrue(isinstance(loaded_schema.damage3, int))
        self.assertTrue(isinstance(loaded_schema.heal1, int))
        self.assertTrue(isinstance(loaded_schema.heal2, int))
        self.assertTrue(isinstance(loaded_schema.heal3, int))
        self.assertTrue(isinstance(loaded_schema.mana_cost, int))
        self.assertIsNone(loaded_schema.beneficial_effect)
        self.assertTrue(isinstance(loaded_schema.harmful_effect, int))
        self.assertTrue(isinstance(loaded_schema.cooldown, int))
        self.assertTrue(isinstance(loaded_schema.comment, str))
        self.assertIsNone(loaded_schema.buff)
        self.assertTrue(isinstance(loaded_schema.dot, DotSchema))

    def test_convert_to_paladin_spell_object(self):
        """
        See if the convert_to_paladin_spell_object works properly
        """
        loaded_quest: PaladinSpell = session.query(PaladinSpellsSchema).get(self.spell_entry).convert_to_paladin_spell_object()
        self.assertEqual(vars(loaded_quest), vars(self.expected_spell))

def tearDownModule():
    delete_test_db()

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>from plugins.classes.requester import Requester
from plugins.classes.specializedMatchers import MD5Matcher, StringMatcher, RegexMatcher

class CMSReq(Requester):
	def __init__(self, host, cache, results):
		super().__init__(host, cache, results)
		self.category = "CMS"
		self.match_class = None

	def prepare_results(self, matches):
		data = []
		for m in matches:
			url = m['response'].url
			version = m['output']
			weight = m['weight'] if 'weight' in m else 1

			for d in data:
				if d['url'] == url:
					d['count'] += w
					break
			else:
				data.append( {'url': url, 'count': w, 'version': version} )

		return data

	def run(self):
		# make requests
		requested = self.request_uniq()

		# find matches
		matcher = self.match_class(requested)
		matches = matcher.get_matches()

		# add to results
		intermediate_results = self.prepare_results(matches)
		self.add_results(intermediate_results)

class CMSReqMD5(CMSReq):
	def __init__(self, host, cache, results):
		super().__init__(host, cache, results)
		self.match_class = MD5Matcher

class CMSReqString(CMSReq):
	def __init__(self, host, cache, results):
		super().__init__(host, cache, results)
		self.match_class = StringMatcher

class CMSReqRegex(CMSReq):
	def __init__(self, host, cache, results):
		super().__init__(host, cache, results)
		self.match_class = RegexMatcher<commit_msg>fixed typo<commit_after>from plugins.classes.requester import Requester
from plugins.classes.specializedMatchers import MD5Matcher, StringMatcher, RegexMatcher

class CMSReq(Requester):
	def __init__(self, host, cache, results):
		super().__init__(host, cache, results)
		self.category = "CMS"
		self.match_class = None

	def prepare_results(self, matches):
		data = []
		for m in matches:
			url = m['response'].url
			version = m['output']
			w = m['weight'] if 'weight' in m else 1

			for d in data:
				if d['url'] == url:
					d['count'] += w
					break
			else:
				data.append( {'url': url, 'count': w, 'version': version} )

		return data

	def run(self):
		# make requests
		requested = self.request_uniq()

		# find matches
		matcher = self.match_class(requested)
		matches = matcher.get_matches()

		# add to results
		intermediate_results = self.prepare_results(matches)
		self.add_results(intermediate_results)

class CMSReqMD5(CMSReq):
	def __init__(self, host, cache, results):
		super().__init__(host, cache, results)
		self.match_class = MD5Matcher

class CMSReqString(CMSReq):
	def __init__(self, host, cache, results):
		super().__init__(host, cache, results)
		self.match_class = StringMatcher

class CMSReqRegex(CMSReq):
	def __init__(self, host, cache, results):
		super().__init__(host, cache, results)
		self.match_class = RegexMatcher<|endoftext|>


<commit_before>"""
Test cases for the template loaders

Note: This test requires setuptools!
"""

from django.conf import settings

if __name__ == '__main__':
    settings.configure()

import unittest
import sys
import pkg_resources
import imp
import StringIO
import os.path

from django.template import TemplateDoesNotExist, Context
from django.template.loaders.eggs import load_template_source as lts_egg
from django.template import loader

# Mock classes and objects for pkg_resources functions.
class MockProvider(pkg_resources.NullProvider):
    def __init__(self, module):
        pkg_resources.NullProvider.__init__(self, module)
        self.module = module

    def _has(self, path):
        return path in self.module._resources

    def _isdir(self,path):
        return False

    def get_resource_stream(self, manager, resource_name):
        return self.module._resources[resource_name]

    def _get(self, path):
        return self.module._resources[path].read()

class MockLoader(object):
    pass

def create_egg(name, resources):
    """
    Creates a mock egg with a list of resources.

    name: The name of the module.
    resources: A dictionary of resources. Keys are the names and values the data.
    """
    egg = imp.new_module(name)
    egg.__loader__ = MockLoader()
    egg._resources = resources
    sys.modules[name] = egg

class EggLoader(unittest.TestCase):
    def setUp(self):
        pkg_resources._provider_factories[MockLoader] = MockProvider

        self.empty_egg = create_egg("egg_empty", {})
        self.egg_1 = create_egg("egg_1", {
            os.path.normcase('templates/y.html') : StringIO.StringIO("y"),
            os.path.normcase('templates/x.txt') : StringIO.StringIO("x"),
        })
        self._old_installed_apps = settings.INSTALLED_APPS
        settings.INSTALLED_APPS = []

    def tearDown(self):
        settings.INSTALLED_APPS = self._old_installed_apps

    def test_empty(self):
        "Loading any template on an empty egg should fail"
        settings.INSTALLED_APPS = ['egg_empty']
        self.assertRaises(TemplateDoesNotExist, lts_egg, "not-existing.html")

    def test_non_existing(self):
        "Template loading fails if the template is not in the egg"
        settings.INSTALLED_APPS = ['egg_1']
        self.assertRaises(TemplateDoesNotExist, lts_egg, "not-existing.html")

    def test_existing(self):
        "A template can be loaded from an egg"
        settings.INSTALLED_APPS = ['egg_1']
        contents, template_name = lts_egg("y.html")
        self.assertEqual(contents, "y")
        self.assertEqual(template_name, "egg:egg_1:templates/y.html")

    def test_not_installed(self):
        "Loading an existent template from an egg not included in INSTALLED_APPS should fail"
        settings.INSTALLED_APPS = []
        self.assertRaises(TemplateDoesNotExist, lts_egg, "y.html")

class CachedLoader(unittest.TestCase):
    def setUp(self):
        self.old_TEMPLATE_LOADERS = settings.TEMPLATE_LOADERS
        settings.TEMPLATE_LOADERS = (
            ('django.template.loaders.cached.Loader', (
                    'django.template.loaders.filesystem.Loader',
                )
            ),
        )
    def tearDown(self):
        settings.TEMPLATE_LOADERS = self.old_TEMPLATE_LOADERS

    def test_templatedir_caching(self):
        "Check that the template directories form part of the template cache key. Refs #13573"
        # Retrive a template specifying a template directory to check
        t1, name = loader.find_template('test.html', (os.path.join(os.path.dirname(__file__), 'templates', 'first'),))
        # Now retrieve the same template name, but from a different directory
        t2, name = loader.find_template('test.html', (os.path.join(os.path.dirname(__file__), 'templates', 'second'),))

        # The two templates should not have the same content
        self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))

if __name__ == "__main__":
    unittest.main()
<commit_msg>Modified the egg template loader tests; firstly to ensure that we are testing the new, non-deprecated interface; secondly to ensure that the old deprecated interface is also tested, but deprecation warnings are silenced.<commit_after>"""
Test cases for the template loaders

Note: This test requires setuptools!
"""

from django.conf import settings

if __name__ == '__main__':
    settings.configure()

import unittest
import sys
import pkg_resources
import imp
import StringIO
import os.path
import warnings

from django.template import TemplateDoesNotExist, Context
from django.template.loaders.eggs import load_template_source as lts_egg
from django.template.loaders.eggs import Loader as EggLoader
from django.template import loader

# Mock classes and objects for pkg_resources functions.
class MockProvider(pkg_resources.NullProvider):
    def __init__(self, module):
        pkg_resources.NullProvider.__init__(self, module)
        self.module = module

    def _has(self, path):
        return path in self.module._resources

    def _isdir(self,path):
        return False

    def get_resource_stream(self, manager, resource_name):
        return self.module._resources[resource_name]

    def _get(self, path):
        return self.module._resources[path].read()

class MockLoader(object):
    pass

def create_egg(name, resources):
    """
    Creates a mock egg with a list of resources.

    name: The name of the module.
    resources: A dictionary of resources. Keys are the names and values the data.
    """
    egg = imp.new_module(name)
    egg.__loader__ = MockLoader()
    egg._resources = resources
    sys.modules[name] = egg

class DeprecatedEggLoaderTest(unittest.TestCase):
    "Test the deprecated load_template_source interface to the egg loader"
    def setUp(self):
        pkg_resources._provider_factories[MockLoader] = MockProvider

        self.empty_egg = create_egg("egg_empty", {})
        self.egg_1 = create_egg("egg_1", {
            os.path.normcase('templates/y.html') : StringIO.StringIO("y"),
            os.path.normcase('templates/x.txt') : StringIO.StringIO("x"),
        })
        self._old_installed_apps = settings.INSTALLED_APPS
        settings.INSTALLED_APPS = []
        warnings.simplefilter("ignore", PendingDeprecationWarning)

    def tearDown(self):
        settings.INSTALLED_APPS = self._old_installed_apps
        warnings.resetwarnings()

    def test_existing(self):
        "A template can be loaded from an egg"
        settings.INSTALLED_APPS = ['egg_1']
        contents, template_name = lts_egg("y.html")
        self.assertEqual(contents, "y")
        self.assertEqual(template_name, "egg:egg_1:templates/y.html")

class EggLoaderTest(unittest.TestCase):
    def setUp(self):
        pkg_resources._provider_factories[MockLoader] = MockProvider

        self.empty_egg = create_egg("egg_empty", {})
        self.egg_1 = create_egg("egg_1", {
            os.path.normcase('templates/y.html') : StringIO.StringIO("y"),
            os.path.normcase('templates/x.txt') : StringIO.StringIO("x"),
        })
        self._old_installed_apps = settings.INSTALLED_APPS
        settings.INSTALLED_APPS = []

    def tearDown(self):
        settings.INSTALLED_APPS = self._old_installed_apps

    def test_empty(self):
        "Loading any template on an empty egg should fail"
        settings.INSTALLED_APPS = ['egg_empty']
        egg_loader = EggLoader()
        self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")

    def test_non_existing(self):
        "Template loading fails if the template is not in the egg"
        settings.INSTALLED_APPS = ['egg_1']
        egg_loader = EggLoader()
        self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")

    def test_existing(self):
        "A template can be loaded from an egg"
        settings.INSTALLED_APPS = ['egg_1']
        egg_loader = EggLoader()
        contents, template_name = egg_loader.load_template_source("y.html")
        self.assertEqual(contents, "y")
        self.assertEqual(template_name, "egg:egg_1:templates/y.html")

    def test_not_installed(self):
        "Loading an existent template from an egg not included in INSTALLED_APPS should fail"
        settings.INSTALLED_APPS = []
        egg_loader = EggLoader()
        self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "y.html")

class CachedLoader(unittest.TestCase):
    def setUp(self):
        self.old_TEMPLATE_LOADERS = settings.TEMPLATE_LOADERS
        settings.TEMPLATE_LOADERS = (
            ('django.template.loaders.cached.Loader', (
                    'django.template.loaders.filesystem.Loader',
                )
            ),
        )
    def tearDown(self):
        settings.TEMPLATE_LOADERS = self.old_TEMPLATE_LOADERS

    def test_templatedir_caching(self):
        "Check that the template directories form part of the template cache key. Refs #13573"
        # Retrive a template specifying a template directory to check
        t1, name = loader.find_template('test.html', (os.path.join(os.path.dirname(__file__), 'templates', 'first'),))
        # Now retrieve the same template name, but from a different directory
        t2, name = loader.find_template('test.html', (os.path.join(os.path.dirname(__file__), 'templates', 'second'),))

        # The two templates should not have the same content
        self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))

if __name__ == "__main__":
    unittest.main()
<|endoftext|>