-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import os | ||
import random | ||
|
||
from seafevents.app.config import METADATA_FILE_TYPES | ||
|
||
|
||
def gen_fileext_type_map(): | ||
""" | ||
Generate previewed file extension and file type relation map. | ||
""" | ||
ext_to_type = {} | ||
for file_type in list(METADATA_FILE_TYPES.keys()): | ||
for file_ext in METADATA_FILE_TYPES.get(file_type): | ||
ext_to_type[file_ext] = file_type | ||
|
||
return ext_to_type | ||
|
||
|
||
FILEEXT_TYPE_MAP = gen_fileext_type_map() | ||
|
||
|
||
def get_file_type_by_name(filename): | ||
file_ext = os.path.splitext(filename)[1][1:].lower() | ||
file_type = FILEEXT_TYPE_MAP.get(file_ext) | ||
return file_type | ||
|
||
|
||
def gen_select_options(option_names): | ||
options = [] | ||
|
||
id_set = set() | ||
for option_name in option_names: | ||
option_id = gen_option_id(id_set) | ||
options.append({'id': option_id, 'name': option_name}) | ||
id_set.add(option_id) | ||
return options | ||
|
||
|
||
def gen_option_id(id_set): | ||
_id = str(random.random() * (10 ** 6)) | ||
|
||
while True: | ||
if _id not in id_set: | ||
return _id | ||
_id = str(random.random() * (10 ** 6)) | ||
|
||
|
||
class MetadataTable(object): | ||
def __init__(self, table_id, name): | ||
self.id = table_id | ||
self.name = name | ||
|
||
@property | ||
def columns(self): | ||
return MetadataColumns() | ||
|
||
|
||
class MetadataColumns(object): | ||
def __init__(self): | ||
self.id = MetadataColumn('_id', '_id', 'text') | ||
self.file_creator = MetadataColumn('_file_creator', '_file_creator', 'text') | ||
self.file_ctime = MetadataColumn('_file_ctime', '_file_ctime', 'date') | ||
self.file_modifier = MetadataColumn('_file_modifier', '_file_modifier', 'text') | ||
self.file_mtime = MetadataColumn('_file_mtime', '_file_mtime', 'date') | ||
self.parent_dir = MetadataColumn('_parent_dir', '_parent_dir', 'text') | ||
self.file_name = MetadataColumn('_name', '_name', 'text') | ||
self.is_dir = MetadataColumn('_is_dir', '_is_dir', 'checkbox') | ||
self.file_type = MetadataColumn('_file_type', '_file_type', 'single-select', | ||
{'options': gen_select_options(list(METADATA_FILE_TYPES.keys()))}) | ||
|
||
|
||
class MetadataColumn(object): | ||
def __init__(self, key, name, type, data=None): | ||
self.key = key | ||
self.name = name | ||
self.type = type | ||
self.data = data | ||
|
||
def to_dict(self): | ||
column_data = { | ||
'key': self.key, | ||
'name': self.name, | ||
'type': self.type, | ||
} | ||
if self.data: | ||
column_data['data'] = self.data | ||
|
||
return column_data | ||
|
||
|
||
METADATA_TABLE = MetadataTable('0001', 'Table1') |