<s> """<STR_LIT>""" <EOL> import logging <EOL> import datetime <EOL> import hurry . filesize <EOL> from framework import sentry <EOL> from framework . auth . decorators import Auth <EOL> from website import settings <EOL> from website . util import paths <EOL> from website . util import sanitize <EOL> from website . settings import DISK_SAVING_MODE <EOL> logger = logging . getLogger ( __name__ ) <EOL> FOLDER = '<STR_LIT>' <EOL> FILE = '<STR_LIT:file>' <EOL> KIND = '<STR_LIT>' <EOL> DEFAULT_PERMISSIONS = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> def format_filesize ( size ) : <EOL> return hurry . filesize . size ( size , system = hurry . filesize . alternative ) <EOL> def default_urls ( node_api , short_name ) : <EOL> return { <EOL> '<STR_LIT>' : u'<STR_LIT>' . format ( node_api = node_api , addonshort = short_name ) , <EOL> '<STR_LIT>' : u'<STR_LIT>' . format ( node_api = node_api , addonshort = short_name ) , <EOL> } <EOL> def to_hgrid ( node , auth , ** data ) : <EOL> """<STR_LIT>""" <EOL> return NodeFileCollector ( node , auth , ** data ) . to_hgrid ( ) <EOL> def build_addon_root ( node_settings , name , permissions = None , <EOL> urls = None , extra = None , buttons = None , user = None , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> permissions = permissions or DEFAULT_PERMISSIONS <EOL> if name : <EOL> name = u'<STR_LIT>' . format ( node_settings . config . full_name , name ) <EOL> else : <EOL> name = node_settings . config . full_name <EOL> if hasattr ( node_settings . config , '<STR_LIT>' ) and node_settings . config . urls : <EOL> urls = node_settings . config . urls <EOL> if urls is None : <EOL> urls = default_urls ( node_settings . owner . api_url , node_settings . config . short_name ) <EOL> forbid_edit = DISK_SAVING_MODE if node_settings . config . short_name == '<STR_LIT>' else False <EOL> if isinstance ( permissions , Auth ) : <EOL> auth = permissions <EOL> permissions = { <EOL> '<STR_LIT>' : node_settings . owner . can_view ( auth ) , <EOL> '<STR_LIT>' : ( node_settings . owner . can_edit ( auth ) <EOL> and not node_settings . owner . is_registration <EOL> and not forbid_edit ) , <EOL> } <EOL> max_size = node_settings . config . max_file_size <EOL> if user and '<STR_LIT>' in user . system_tags : <EOL> max_size = node_settings . config . high_max_file_size <EOL> ret = { <EOL> '<STR_LIT>' : node_settings . config . short_name , <EOL> '<STR_LIT>' : node_settings . config . full_name , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : node_settings . config . icon_url , <EOL> KIND : FOLDER , <EOL> '<STR_LIT>' : extra , <EOL> '<STR_LIT>' : buttons , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : permissions , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : max_size , <EOL> '<STR_LIT>' : node_settings . config . accept_extensions , <EOL> } , <EOL> '<STR_LIT>' : urls , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : node_settings . owner . _id , <EOL> '<STR_LIT>' : node_settings . owner . url , <EOL> '<STR_LIT>' : node_settings . owner . api_url , <EOL> } <EOL> ret . update ( kwargs ) <EOL> return ret <EOL> def build_addon_button ( text , action , title = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> button = { <EOL> '<STR_LIT:text>' : text , <EOL> '<STR_LIT:action>' : action , <EOL> } <EOL> if title : <EOL> button [ '<STR_LIT>' ] = '<STR_LIT>' . format ( title = title ) <EOL> return button <EOL> def sort_by_name ( hgrid_data ) : <EOL> return_value = hgrid_data <EOL> if hgrid_data is not None : <EOL> return_value = sorted ( hgrid_data , key = lambda item : item [ '<STR_LIT:name>' ] . lower ( ) ) <EOL> return return_value <EOL> class NodeFileCollector ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , node , auth , ** kwargs ) : <EOL> self . node = node <EOL> self . auth = auth <EOL> self . extra = kwargs <EOL> self . can_view = node . can_view ( auth ) <EOL> self . can_edit = node . can_edit ( auth ) and not node . is_registration <EOL> def to_hgrid ( self ) : <EOL> """<STR_LIT>""" <EOL> root = self . _serialize_node ( self . node ) <EOL> return [ root ] <EOL> def _collect_components ( self , node , visited ) : <EOL> rv = [ ] <EOL> for child in node . nodes : <EOL> if child . resolve ( ) . _id not in visited and not child . is_deleted and node . can_view ( self . auth ) : <EOL> visited . append ( child . resolve ( ) . _id ) <EOL> rv . append ( self . _serialize_node ( child , visited = visited ) ) <EOL> return rv <EOL> def _get_node_name ( self , node ) : <EOL> """<STR_LIT>""" <EOL> can_view = node . can_view ( auth = self . auth ) <EOL> if can_view : <EOL> node_name = sanitize . unescape_entities ( node . title ) <EOL> elif node . is_registration : <EOL> node_name = u'<STR_LIT>' <EOL> elif node . is_fork : <EOL> node_name = u'<STR_LIT>' <EOL> elif not node . primary : <EOL> node_name = u'<STR_LIT>' <EOL> else : <EOL> node_name = u'<STR_LIT>' <EOL> return node_name <EOL> def _serialize_node ( self , node , visited = None ) : <EOL> """<STR_LIT>""" <EOL> visited = visited or [ ] <EOL> visited . append ( node . resolve ( ) . _id ) <EOL> can_view = node . can_view ( auth = self . auth ) <EOL> if can_view : <EOL> children = self . _collect_addons ( node ) + self . _collect_components ( node , visited ) <EOL> else : <EOL> children = [ ] <EOL> return { <EOL> '<STR_LIT:name>' : self . _get_node_name ( node ) , <EOL> '<STR_LIT>' : node . category , <EOL> '<STR_LIT>' : FOLDER , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : node . can_edit ( self . auth ) and not node . is_registration , <EOL> '<STR_LIT>' : can_view , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> } , <EOL> '<STR_LIT>' : children , <EOL> '<STR_LIT>' : not node . primary , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : node . project_or_component , <EOL> '<STR_LIT>' : node . resolve ( ) . _id , <EOL> } <EOL> def _collect_addons ( self , node ) : <EOL> rv = [ ] <EOL> for addon in node . get_addons ( ) : <EOL> if addon . config . has_hgrid_files : <EOL> try : <EOL> temp = addon . config . get_hgrid_data ( addon , self . auth , ** self . extra ) <EOL> except Exception as e : <EOL> logger . warn ( <EOL> getattr ( <EOL> e , <EOL> '<STR_LIT:data>' , <EOL> "<STR_LIT>" . format ( addon . config . full_name ) <EOL> ) <EOL> ) <EOL> sentry . log_exception ( ) <EOL> rv . append ( { <EOL> KIND : FOLDER , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : addon . config . icon_url , <EOL> '<STR_LIT>' : addon . config . short_name , <EOL> '<STR_LIT>' : addon . config . full_name , <EOL> '<STR_LIT>' : { '<STR_LIT>' : False , '<STR_LIT>' : False } , <EOL> '<STR_LIT:name>' : '<STR_LIT>' . format ( addon . config . full_name ) , <EOL> } ) <EOL> continue <EOL> rv . extend ( sort_by_name ( temp ) or [ ] ) <EOL> return rv <EOL> def collect_addon_assets ( node ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : list ( collect_addon_js ( node ) ) , <EOL> '<STR_LIT>' : list ( collect_addon_css ( node ) ) , <EOL> } <EOL> def collect_addon_js ( node , visited = None , filename = '<STR_LIT>' , config_entry = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> js = [ ] <EOL> for addon_config in settings . ADDONS_AVAILABLE_DICT . values ( ) : <EOL> js . extend ( addon_config . include_js . get ( config_entry , [ ] ) ) <EOL> js_path = paths . resolve_addon_path ( addon_config , filename ) <EOL> if js_path : <EOL> js . append ( js_path ) <EOL> return js <EOL> def collect_addon_css ( node , visited = None ) : <EOL> """<STR_LIT>""" <EOL> css = [ ] <EOL> for addon_config in settings . ADDONS_AVAILABLE_DICT . values ( ) : <EOL> css . extend ( addon_config . include_css . get ( '<STR_LIT>' , [ ] ) ) <EOL> return css <EOL> def delta_date ( d ) : <EOL> diff = d - datetime . datetime . utcnow ( ) <EOL> s = diff . total_seconds ( ) <EOL> return s </s>
