<s> import page . utils <EOL> import htmlutils <EOL> import dbutils <EOL> import configuration <EOL> from extensions . extension import Extension , ExtensionError <EOL> from extensions . manifest import ( ManifestError , PageRole , InjectRole , <EOL> ProcessCommitsRole , FilterHookRole , <EOL> ScheduledRole ) <EOL> def renderManageExtensions ( req , db , user ) : <EOL> if not configuration . extensions . ENABLED : <EOL> administrators = dbutils . getAdministratorContacts ( db , as_html = True ) <EOL> raise page . utils . DisplayMessage ( <EOL> title = "<STR_LIT>" , <EOL> body = ( ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % administrators ) , <EOL> html = True ) <EOL> cursor = db . cursor ( ) <EOL> what = req . getParameter ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> selected_versions = page . utils . json_decode ( req . getParameter ( "<STR_LIT>" , "<STR_LIT:{}>" ) ) <EOL> focused = req . getParameter ( "<STR_LIT>" , None ) <EOL> if what == "<STR_LIT>" : <EOL> title = "<STR_LIT>" <EOL> listed_extensions = [ ] <EOL> for extension_id , _ , _ , _ in Extension . getInstalls ( db , user ) : <EOL> try : <EOL> listed_extensions . append ( Extension . fromId ( db , extension_id ) ) <EOL> except ExtensionError as error : <EOL> listed_extensions . append ( error ) <EOL> else : <EOL> title = "<STR_LIT>" <EOL> listed_extensions = Extension . find ( db ) <EOL> req . content_type = "<STR_LIT>" <EOL> document = htmlutils . Document ( req ) <EOL> document . setTitle ( "<STR_LIT>" ) <EOL> html = document . html ( ) <EOL> head = html . head ( ) <EOL> body = html . body ( ) <EOL> def generateRight ( target ) : <EOL> target . a ( "<STR_LIT>" , href = "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> target . text ( "<STR_LIT:U+0020>" ) <EOL> target . a ( "<STR_LIT>" , href = "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> page . utils . generateHeader ( body , db , user , current_page = "<STR_LIT>" , generate_right = generateRight ) <EOL> document . addExternalStylesheet ( "<STR_LIT>" ) <EOL> document . addExternalScript ( "<STR_LIT>" ) <EOL> document . addInternalScript ( user . getJS ( ) ) <EOL> table = page . utils . PaleYellowTable ( body , title ) <EOL> def addTitleRightLink ( url , label ) : <EOL> if user . name != req . user : <EOL> url += "<STR_LIT>" % user . name <EOL> table . titleRight . text ( "<STR_LIT:U+0020>" ) <EOL> table . titleRight . a ( href = url ) . text ( "<STR_LIT:[>" + label + "<STR_LIT>" ) <EOL> if what != "<STR_LIT>" or focused : <EOL> addTitleRightLink ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if what != "<STR_LIT>" or focused : <EOL> addTitleRightLink ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> for item in listed_extensions : <EOL> if isinstance ( item , ExtensionError ) : <EOL> extension_error = item <EOL> extension = item . extension <EOL> else : <EOL> extension_error = None <EOL> extension = item <EOL> if focused and extension . getKey ( ) != focused : <EOL> continue <EOL> extension_path = extension . getPath ( ) <EOL> if extension . isSystemExtension ( ) : <EOL> hosting_user = None <EOL> else : <EOL> hosting_user = extension . getAuthor ( db ) <EOL> selected_version = selected_versions . get ( extension . getKey ( ) , False ) <EOL> installed_sha1 , installed_version = extension . getInstalledVersion ( db , user ) <EOL> universal_sha1 , universal_version = extension . getInstalledVersion ( db , None ) <EOL> installed_upgradeable = universal_upgradeable = False <EOL> if extension_error is None : <EOL> if installed_sha1 : <EOL> current_sha1 = extension . getCurrentSHA1 ( installed_version ) <EOL> installed_upgradeable = installed_sha1 != current_sha1 <EOL> if universal_sha1 : <EOL> current_sha1 = extension . getCurrentSHA1 ( universal_version ) <EOL> universal_upgradeable = universal_sha1 != current_sha1 <EOL> def massage_version ( version ) : <EOL> if version is None : <EOL> return "<STR_LIT>" <EOL> elif version : <EOL> return "<STR_LIT>" % version <EOL> else : <EOL> return None <EOL> if selected_version is False : <EOL> selected_version = installed_version <EOL> if selected_version is False : <EOL> selected_version = universal_version <EOL> install_version = massage_version ( selected_version ) <EOL> installed_version = massage_version ( installed_version ) <EOL> universal_version = massage_version ( universal_version ) <EOL> manifest = None <EOL> if extension_error is None : <EOL> try : <EOL> if selected_version is False : <EOL> manifest = extension . getManifest ( ) <EOL> else : <EOL> manifest = extension . getManifest ( selected_version ) <EOL> except ManifestError as error : <EOL> pass <EOL> elif installed_sha1 : <EOL> manifest = extension . getManifest ( installed_version , installed_sha1 ) <EOL> elif universal_sha1 : <EOL> manifest = extension . getManifest ( universal_version , universal_sha1 ) <EOL> if manifest : <EOL> if what == "<STR_LIT>" and manifest . hidden : <EOL> if extension . isSystemExtension ( ) : <EOL> if not user . hasRole ( db , "<STR_LIT>" ) : <EOL> continue <EOL> elif hosting_user != user : <EOL> continue <EOL> else : <EOL> if hosting_user != user : <EOL> continue <EOL> extension_id = extension . getExtensionID ( db , create = False ) <EOL> if not user . isAnonymous ( ) : <EOL> buttons = [ ] <EOL> if extension_id is not None : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( extension_id , user . id ) ) <EOL> if cursor . fetchone ( ) : <EOL> buttons . append ( ( "<STR_LIT>" , <EOL> ( "<STR_LIT>" <EOL> % ( htmlutils . jsify ( extension . getAuthorName ( ) ) , <EOL> htmlutils . jsify ( extension . getName ( ) ) ) ) ) ) <EOL> if not installed_version : <EOL> if manifest and install_version and install_version != universal_version : <EOL> buttons . append ( ( "<STR_LIT>" , <EOL> ( "<STR_LIT>" <EOL> % ( htmlutils . jsify ( extension . getAuthorName ( ) ) , <EOL> htmlutils . jsify ( extension . getName ( ) ) , <EOL> htmlutils . jsify ( install_version ) ) ) ) ) <EOL> else : <EOL> buttons . append ( ( "<STR_LIT>" , <EOL> ( "<STR_LIT>" <EOL> % ( htmlutils . jsify ( extension . getAuthorName ( ) ) , <EOL> htmlutils . jsify ( extension . getName ( ) ) ) ) ) ) <EOL> if manifest and ( install_version != installed_version <EOL> or ( installed_sha1 and installed_upgradeable ) ) : <EOL> if install_version == installed_version : <EOL> label = "<STR_LIT>" <EOL> else : <EOL> label = "<STR_LIT>" <EOL> buttons . append ( ( "<STR_LIT>" , <EOL> ( "<STR_LIT>" <EOL> % ( htmlutils . jsify ( extension . getAuthorName ( ) ) , <EOL> htmlutils . jsify ( extension . getName ( ) ) , <EOL> htmlutils . jsify ( install_version ) ) ) ) ) <EOL> if user . hasRole ( db , "<STR_LIT>" ) : <EOL> if not universal_version : <EOL> if manifest and install_version : <EOL> buttons . append ( ( "<STR_LIT>" , <EOL> ( "<STR_LIT>" <EOL> % ( htmlutils . jsify ( extension . getAuthorName ( ) ) , <EOL> htmlutils . jsify ( extension . getName ( ) ) , <EOL> htmlutils . jsify ( install_version ) ) ) ) ) <EOL> else : <EOL> buttons . append ( ( "<STR_LIT>" , <EOL> ( "<STR_LIT>" <EOL> % ( htmlutils . jsify ( extension . getAuthorName ( ) ) , <EOL> htmlutils . jsify ( extension . getName ( ) ) ) ) ) ) <EOL> if manifest and ( install_version != universal_version <EOL> or ( universal_sha1 and universal_upgradeable ) ) : <EOL> if install_version == universal_version : <EOL> label = "<STR_LIT>" <EOL> else : <EOL> label = "<STR_LIT>" <EOL> buttons . append ( ( label , <EOL> ( "<STR_LIT>" <EOL> % ( htmlutils . jsify ( extension . getAuthorName ( ) ) , <EOL> htmlutils . jsify ( extension . getName ( ) ) , <EOL> htmlutils . jsify ( universal_version ) ) ) ) ) <EOL> else : <EOL> buttons = None <EOL> def renderItem ( target ) : <EOL> target . span ( "<STR_LIT:name>" ) . innerHTML ( extension . getTitle ( db , html = True ) ) <EOL> if hosting_user : <EOL> is_author = manifest and manifest . isAuthor ( db , hosting_user ) <EOL> is_sole_author = is_author and len ( manifest . authors ) == <NUM_LIT:1> <EOL> else : <EOL> is_sole_author = False <EOL> if extension_error is None : <EOL> span = target . span ( "<STR_LIT>" ) <EOL> span . b ( ) . text ( "<STR_LIT>" ) <EOL> select = span . select ( "<STR_LIT>" , critic_author = extension . getAuthorName ( ) , critic_extension = extension . getName ( ) ) <EOL> select . option ( value = '<STR_LIT>' , selected = "<STR_LIT>" if selected_version is False else None ) . text ( "<STR_LIT>" ) <EOL> versions = extension . getVersions ( ) <EOL> if versions : <EOL> optgroup = select . optgroup ( label = "<STR_LIT>" ) <EOL> for version in versions : <EOL> optgroup . option ( value = "<STR_LIT>" % version , selected = "<STR_LIT>" if selected_version == version else None ) . text ( "<STR_LIT:%s>" % version . upper ( ) ) <EOL> optgroup = select . optgroup ( label = "<STR_LIT>" ) <EOL> optgroup . option ( value = '<STR_LIT>' , selected = "<STR_LIT>" if selected_version is None else None ) . text ( "<STR_LIT>" ) <EOL> if manifest : <EOL> is_installed = bool ( installed_version ) <EOL> if is_installed : <EOL> target . span ( "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> else : <EOL> is_installed = bool ( universal_version ) <EOL> if is_installed : <EOL> target . span ( "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> target . div ( "<STR_LIT:description>" ) . preformatted ( ) . text ( manifest . description , linkify = True ) <EOL> if not is_sole_author : <EOL> authors = target . div ( "<STR_LIT>" ) <EOL> authors . b ( ) . text ( "<STR_LIT>" % ( "<STR_LIT:s>" if len ( manifest . authors ) > <NUM_LIT:1> else "<STR_LIT>" ) ) <EOL> authors . text ( "<STR_LIT:U+002CU+0020>" . join ( author . name for author in manifest . getAuthors ( ) ) ) <EOL> else : <EOL> is_installed = False <EOL> div = target . div ( "<STR_LIT>" ) . preformatted ( ) <EOL> if extension_error is None : <EOL> anchor = div . a ( href = "<STR_LIT>" % extension . getKey ( ) ) <EOL> anchor . text ( "<STR_LIT>" ) <EOL> else : <EOL> div . text ( "<STR_LIT>" ) <EOL> if selected_version is False : <EOL> return <EOL> pages = [ ] <EOL> injects = [ ] <EOL> processcommits = [ ] <EOL> filterhooks = [ ] <EOL> scheduled = [ ] <EOL> if manifest : <EOL> for role in manifest . roles : <EOL> if isinstance ( role , PageRole ) : <EOL> pages . append ( role ) <EOL> elif isinstance ( role , InjectRole ) : <EOL> injects . append ( role ) <EOL> elif isinstance ( role , ProcessCommitsRole ) : <EOL> processcommits . append ( role ) <EOL> elif isinstance ( role , FilterHookRole ) : <EOL> filterhooks . append ( role ) <EOL> elif isinstance ( role , ScheduledRole ) : <EOL> scheduled . append ( role ) <EOL> role_table = target . table ( "<STR_LIT>" ) <EOL> if pages : <EOL> role_table . tr ( ) . th ( colspan = <NUM_LIT:2> ) . text ( "<STR_LIT>" ) <EOL> for role in pages : <EOL> row = role_table . tr ( ) <EOL> url = "<STR_LIT>" % ( dbutils . getURLPrefix ( db , user ) , role . pattern ) <EOL> if is_installed and "<STR_LIT:*>" not in url : <EOL> row . td ( "<STR_LIT>" ) . a ( href = url ) . text ( url ) <EOL> else : <EOL> row . td ( "<STR_LIT>" ) . text ( url ) <EOL> td = row . td ( "<STR_LIT:description>" ) <EOL> td . text ( role . description ) <EOL> if injects : <EOL> role_table . tr ( ) . th ( colspan = <NUM_LIT:2> ) . text ( "<STR_LIT>" ) <EOL> for role in injects : <EOL> row = role_table . tr ( ) <EOL> row . td ( "<STR_LIT>" ) . text ( "<STR_LIT>" % ( dbutils . getURLPrefix ( db , user ) , role . pattern ) ) <EOL> td = row . td ( "<STR_LIT:description>" ) <EOL> td . text ( role . description ) <EOL> if processcommits : <EOL> role_table . tr ( ) . th ( colspan = <NUM_LIT:2> ) . text ( "<STR_LIT>" ) <EOL> ul = role_table . tr ( ) . td ( colspan = <NUM_LIT:2> ) . ul ( ) <EOL> for role in processcommits : <EOL> li = ul . li ( ) <EOL> li . text ( role . description ) <EOL> if filterhooks : <EOL> role_table . tr ( ) . th ( colspan = <NUM_LIT:2> ) . text ( "<STR_LIT>" ) <EOL> for role in filterhooks : <EOL> row = role_table . tr ( ) <EOL> row . td ( "<STR_LIT:title>" ) . text ( role . title ) <EOL> row . td ( "<STR_LIT:description>" ) . text ( role . description ) <EOL> if scheduled : <EOL> role_table . tr ( ) . th ( colspan = <NUM_LIT:2> ) . text ( "<STR_LIT>" ) <EOL> for role in scheduled : <EOL> row = role_table . tr ( ) <EOL> row . td ( "<STR_LIT>" ) . text ( "<STR_LIT>" % ( role . frequency , role . at ) ) <EOL> td = row . td ( "<STR_LIT:description>" ) <EOL> td . text ( role . description ) <EOL> installed_by = "<STR_LIT>" <EOL> if extension_id is not None : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( extension . getExtensionID ( db , create = False ) , ) ) <EOL> user_ids = set ( user_id for user_id , in cursor . fetchall ( ) ) <EOL> if user_ids : <EOL> installed_by = "<STR_LIT>" <EOL> if None in user_ids : <EOL> installed_by += "<STR_LIT>" <EOL> user_ids . remove ( None ) <EOL> if user_ids : <EOL> installed_by += "<STR_LIT>" <EOL> if user_ids : <EOL> installed_by += ( "<STR_LIT>" <EOL> % ( len ( user_ids ) , <EOL> "<STR_LIT:s>" if len ( user_ids ) > <NUM_LIT:1> else "<STR_LIT>" ) ) <EOL> installed_by += "<STR_LIT:)>" <EOL> table . addItem ( "<STR_LIT>" , renderItem , extension_path + "<STR_LIT:/>" + installed_by , buttons ) <EOL> document . addInternalScript ( "<STR_LIT>" % page . utils . json_encode ( selected_versions ) ) <EOL> return document </s>
