Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/data.py
Show All 10 Lines | |||||
from rest_framework.decorators import api_view | from rest_framework.decorators import api_view | ||||
from rest_framework.response import Response | from rest_framework.response import Response | ||||
from swh.indexer.fossology_license import FossologyLicenseIndexer | from swh.indexer.fossology_license import FossologyLicenseIndexer | ||||
from swh.indexer.mimetype import MimetypeIndexer | from swh.indexer.mimetype import MimetypeIndexer | ||||
from swh.indexer.ctags import CtagsIndexer | from swh.indexer.ctags import CtagsIndexer | ||||
from swh.indexer.storage import get_indexer_storage | from swh.indexer.storage import get_indexer_storage | ||||
from swh.model.from_disk import Directory | from swh.model.from_disk import Content, Directory | ||||
from swh.model.hashutil import hash_to_hex, hash_to_bytes, DEFAULT_ALGORITHMS | from swh.model.hashutil import hash_to_hex, hash_to_bytes, DEFAULT_ALGORITHMS | ||||
from swh.model.identifiers import directory_identifier | from swh.model.identifiers import directory_identifier | ||||
from swh.loader.git.from_disk import GitLoaderFromArchive | from swh.loader.git.from_disk import GitLoaderFromArchive | ||||
from swh.search import get_search | from swh.search import get_search | ||||
from swh.storage.algos.dir_iterators import dir_iterator | from swh.storage.algos.dir_iterators import dir_iterator | ||||
from swh.web import config | from swh.web import config | ||||
from swh.web.browse.utils import ( | from swh.web.browse.utils import ( | ||||
get_mimetype_and_encoding_for_content, prepare_content_for_display, | get_mimetype_and_encoding_for_content, prepare_content_for_display, | ||||
▲ Show 20 Lines • Show All 334 Lines • ▼ Show 20 Lines | Args: | ||||
data_path (str): path to a directory relative to the tests | data_path (str): path to a directory relative to the tests | ||||
folder of swh-web | folder of swh-web | ||||
data_dict (dict): the dict that will store files metadata | data_dict (dict): the dict that will store files metadata | ||||
ext_key (bool): whether to use file extensions or filenames | ext_key (bool): whether to use file extensions or filenames | ||||
as dict keys | as dict keys | ||||
""" | """ | ||||
test_contents_dir = os.path.join( | test_contents_dir = os.path.join( | ||||
os.path.dirname(__file__), data_path).encode('utf-8') | os.path.dirname(__file__), data_path).encode('utf-8') | ||||
directory = Directory.from_disk(path=test_contents_dir, data=True, | directory = Directory.from_disk(path=test_contents_dir) | ||||
save_path=True) | |||||
objects = directory.collect() | contents = [] | ||||
for c in objects['content'].values(): | for name, obj in directory.items(): | ||||
if isinstance(obj, Content): | |||||
c = obj.to_model().with_data().to_dict() | |||||
c['status'] = 'visible' | c['status'] = 'visible' | ||||
sha1 = hash_to_hex(c['sha1']) | sha1 = hash_to_hex(c['sha1']) | ||||
if ext_key: | if ext_key: | ||||
key = c['path'].decode('utf-8').split('.')[-1] | key = name.decode('utf-8').split('.')[-1] | ||||
filename = 'test.' + key | filename = 'test.' + key | ||||
else: | else: | ||||
filename = c['path'].decode('utf-8').split('/')[-1] | filename = name.decode('utf-8').split('/')[-1] | ||||
key = filename | key = filename | ||||
language = get_hljs_language_from_filename(filename) | language = get_hljs_language_from_filename(filename) | ||||
data_dict[key] = {'sha1': sha1, | data_dict[key] = {'sha1': sha1, | ||||
'language': language} | 'language': language} | ||||
del c['path'] | contents.append(c) | ||||
del c['perms'] | |||||
storage = get_tests_data()['storage'] | storage = get_tests_data()['storage'] | ||||
storage.content_add(objects['content'].values()) | storage.content_add(contents) | ||||
def _init_content_code_data_exts(): | def _init_content_code_data_exts(): | ||||
""" | """ | ||||
Fill a global dictionary which maps source file extension to | Fill a global dictionary which maps source file extension to | ||||
a code content example. | a code content example. | ||||
""" | """ | ||||
global _content_code_data_exts | global _content_code_data_exts | ||||
▲ Show 20 Lines • Show All 91 Lines • Show Last 20 Lines |