# Path: snoop/digest.py
# def digest(doc):
#     if not doc.sha1:
#         with doc.open() as f:
#             md5, sha1, fsize = _calculate_hashes(f)
#             if not doc.content_type:
#                 f.seek(0)
#                 doc.content_type = libmagic_guess_content_type(f, fsize)
#         if not doc.disk_size:
#             doc.disk_size = fsize
#         doc.sha1 = sha1
#         doc.md5 = md5
#         doc.save()
# 
#     data = {
#         'path': '//'.join(_path_bits(doc)),
#         'lang': None,
#         'sha1': doc.sha1,
#         'md5': doc.md5,
#         'filename': doc.filename,
#         'rev': doc.rev,
#     }
# 
#     if emails.is_email(doc):
#         data.update(emails.parse_email(doc))
# 
#     if 'pgp' in doc.flags:
#         data['pgp'] = doc.flags['pgp']
# 
#     if doc.container_id:
#         data['message'] = doc.container_id
# 
#     filetype = guess_filetype(doc)
#     data['type'] = filetype
# 
#     if filetype in ['text', 'html']:
#         data['text'] = text.get_text(doc)
# 
#     if filetype == 'html':
#         data['safe_html'] = html.get_safe_html(doc)
# 
#     if filetype == 'image':
#         data.update(extract_exif(doc))
# 
#     if settings.SNOOP_TIKA_SERVER_ENDPOINT and \
#             filetype in settings.SNOOP_TIKA_FILE_TYPES and \
#             doc.disk_size <= settings.SNOOP_TIKA_MAX_FILE_SIZE:
#         parsed = tika_parse(doc.sha1, doc.open)
#         data['text'] = (parsed.get('content') or '').strip()
#         data.update(extract_meta(parsed.get('metadata', {})))
# 
#     if settings.SNOOP_ANALYZE_LANG:
#         if 'text' in data and len(data['text']) > 100:
#             data['lang'] = tika_lang(data['text'])[:2]
# 
#     if 'text' in data:
#         data['word-count'] = word_count(data['text'])
# 
#     ocr_items = list(models.Ocr.objects.filter(
#         collection_id=doc.collection.id,
#         md5=doc.md5
#     ))
#     if ocr_items:
#         data['ocr'] = {ocr.tag: ocr.text for ocr in ocr_items}
# 
#     if archives.is_archive(doc):
#         archives.extract_to_base(doc)
#     if pst.is_pst_file(doc):
#         pst.extract_to_base(doc)
# 
#     return data
# 
# Path: snoop/models.py
# def cache(model, keyfunc):
#     def decorator(func):
#         def wrapper(*args, **kwargs):
#     def __str__(self):
#     def __str__(self):
#     def absolute_path(self):
#     def _open_file(self):
#     def open(self, filesystem=False):
#     def absolute_path(self):
# class EmailCache(models.Model):
# class Collection(models.Model):
# class Document(models.Model):
#     class Meta:
# class Ocr(models.Model):
#     class Meta:
# class Digest(models.Model):
# class Job(models.Model):
#     class Meta:
# class TikaCache(models.Model):
# class TikaLangCache(models.Model):
# class HtmlTextCache(models.Model):
#                     MB = 1024*1024
# 
# Path: snoop/archives.py
# KNOWN_TYPES = {
#     'application/zip',
#     'application/rar',
#     'application/x-7z-compressed',
#     'application/x-zip',
#     'application/x-gzip',
#     'application/x-zip-compressed',
#     'application/x-rar-compressed',
# }
#     CACHE_ROOT = Path(settings.SNOOP_ARCHIVE_CACHE_ROOT)
#     CACHE_ROOT = None
# class MissingArchiveFile(exceptions.BrokenDocument):
# class EncryptedArchiveFile(exceptions.BrokenDocument):
# class ExtractingFailed(exceptions.BrokenDocument):
# def _other_temps(sha1, current):
# def call_7z(archive_path, output_dir):
# def extract_to_base(doc):
# def list_children(doc):
# def open_file(doc, name):
# def is_archive(doc):
# 
# Path: snoop/content_types.py
# def guess_content_type(filename):
#     return mimetypes.guess_type(filename, strict=False)[0] or ''


import pytest
import shutil
import tempfile
from pathlib import Path
from django.conf import settings
from snoop import digest, models, archives
from snoop.content_types import guess_content_type
    'type': 'archive',
}

EML_SIMPLE = {
    'parent': None,
    'path': "eml-2-attachment/Urăsc canicula, e nașpa.eml",
    'type': 'email',
}

ZIP_ATTACHMENT = {
    'parent': EML_SIMPLE,
    'path': '3',
    'filename': 'zip-with-pdf.zip',
    'files': [
        'cap33.pdf'
    ],
    'sha1': 'cb191b36eee4b6b3a9db58199ff2bad61af2f635',
    'type': 'archive',
}

@pytest.fixture(autouse=True)
def no_models(monkeypatch):
    def disable(item, attr, default):
        def dummy(*a, **k):
            return default
        monkeypatch.setattr(item, attr, dummy)
    class DummyDocument:
        id = 0
    disable(models.Ocr.objects, 'filter', [])
    disable(models.Ocr.objects, 'all', [])

# Path: snoop/digest.py
# def digest(doc):
#     if not doc.sha1:
#         with doc.open() as f:
#             md5, sha1, fsize = _calculate_hashes(f)
#             if not doc.content_type:
#                 f.seek(0)
#                 doc.content_type = libmagic_guess_content_type(f, fsize)
#         if not doc.disk_size:
#             doc.disk_size = fsize
#         doc.sha1 = sha1
#         doc.md5 = md5
#         doc.save()
# 
#     data = {
#         'path': '//'.join(_path_bits(doc)),
#         'lang': None,
#         'sha1': doc.sha1,
#         'md5': doc.md5,
#         'filename': doc.filename,
#         'rev': doc.rev,
#     }
# 
#     if emails.is_email(doc):
#         data.update(emails.parse_email(doc))
# 
#     if 'pgp' in doc.flags:
#         data['pgp'] = doc.flags['pgp']
# 
#     if doc.container_id:
#         data['message'] = doc.container_id
# 
#     filetype = guess_filetype(doc)
#     data['type'] = filetype
# 
#     if filetype in ['text', 'html']:
#         data['text'] = text.get_text(doc)
# 
#     if filetype == 'html':
#         data['safe_html'] = html.get_safe_html(doc)
# 
#     if filetype == 'image':
#         data.update(extract_exif(doc))
# 
#     if settings.SNOOP_TIKA_SERVER_ENDPOINT and \
#             filetype in settings.SNOOP_TIKA_FILE_TYPES and \
#             doc.disk_size <= settings.SNOOP_TIKA_MAX_FILE_SIZE:
#         parsed = tika_parse(doc.sha1, doc.open)
#         data['text'] = (parsed.get('content') or '').strip()
#         data.update(extract_meta(parsed.get('metadata', {})))
# 
#     if settings.SNOOP_ANALYZE_LANG:
#         if 'text' in data and len(data['text']) > 100:
#             data['lang'] = tika_lang(data['text'])[:2]
# 
#     if 'text' in data:
#         data['word-count'] = word_count(data['text'])
# 
#     ocr_items = list(models.Ocr.objects.filter(
#         collection_id=doc.collection.id,
#         md5=doc.md5
#     ))
#     if ocr_items:
#         data['ocr'] = {ocr.tag: ocr.text for ocr in ocr_items}
# 
#     if archives.is_archive(doc):
#         archives.extract_to_base(doc)
#     if pst.is_pst_file(doc):
#         pst.extract_to_base(doc)
# 
#     return data
# 
# Path: snoop/models.py
# def cache(model, keyfunc):
#     def decorator(func):
#         def wrapper(*args, **kwargs):
#     def __str__(self):
#     def __str__(self):
#     def absolute_path(self):
#     def _open_file(self):
#     def open(self, filesystem=False):
#     def absolute_path(self):
# class EmailCache(models.Model):
# class Collection(models.Model):
# class Document(models.Model):
#     class Meta:
# class Ocr(models.Model):
#     class Meta:
# class Digest(models.Model):
# class Job(models.Model):
#     class Meta:
# class TikaCache(models.Model):
# class TikaLangCache(models.Model):
# class HtmlTextCache(models.Model):
#                     MB = 1024*1024
# 
# Path: snoop/archives.py
# KNOWN_TYPES = {
#     'application/zip',
#     'application/rar',
#     'application/x-7z-compressed',
#     'application/x-zip',
#     'application/x-gzip',
#     'application/x-zip-compressed',
#     'application/x-rar-compressed',
# }
#     CACHE_ROOT = Path(settings.SNOOP_ARCHIVE_CACHE_ROOT)
#     CACHE_ROOT = None
# class MissingArchiveFile(exceptions.BrokenDocument):
# class EncryptedArchiveFile(exceptions.BrokenDocument):
# class ExtractingFailed(exceptions.BrokenDocument):
# def _other_temps(sha1, current):
# def call_7z(archive_path, output_dir):
# def extract_to_base(doc):
# def list_children(doc):
# def open_file(doc, name):
# def is_archive(doc):
# 
# Path: snoop/content_types.py
# def guess_content_type(filename):
#     return mimetypes.guess_type(filename, strict=False)[0] or ''


# Path: testsuite/test_archives.py
import pytest
import shutil
import tempfile
from pathlib import Path
from django.conf import settings
from snoop import digest, models, archives
from snoop.content_types import guess_content_type

    'type': 'archive',
}

EML_SIMPLE = {
    'parent': None,
    'path': "eml-2-attachment/Urăsc canicula, e nașpa.eml",
    'type': 'email',
}

ZIP_ATTACHMENT = {
    'parent': EML_SIMPLE,
    'path': '3',
    'filename': 'zip-with-pdf.zip',
    'files': [
        'cap33.pdf'
    ],
    'sha1': 'cb191b36eee4b6b3a9db58199ff2bad61af2f635',
    'type': 'archive',
}

@pytest.fixture(autouse=True)
def no_models(monkeypatch):
    def disable(item, attr, default):
        def dummy(*a, **k):
            return default
        monkeypatch.setattr(item, attr, dummy)
    class DummyDocument:
        id = 0
    disable(models.Ocr.objects, 'filter', [])
    disable(models.Ocr.objects, 'all', [])

    disable(models.Document.objects, 'get_or_create', (DummyDocument(), False))