def user_agent(site=None, format_string=None): """ Generate the user agent string for a given site and format. @param site: The site for which this user agent is intended. May be None. @type site: BaseSite @param format_string: The string to which the values will be added using str.format. Is using config.user_agent_format when it is None. @type format_string: basestring @return: The formatted user agent @rtype: str """ values = USER_AGENT_PRODUCTS.copy() script_name = calledModuleName() values['script'] = script_name # TODO: script_product should add the script version, if known values['script_product'] = script_name script_comments = [] username = '' if config.user_agent_description: script_comments.append(config.user_agent_description) if site: script_comments.append(str(site)) # TODO: there are several ways of identifying a user, and username # is not the best for a HTTP header if the username isn't ASCII. if site.username(): username = user_agent_username(site.username()) script_comments.append('User:'******'family': site.family.name if site else '', 'code': site.code if site else '', 'lang': site.code if site else '', # TODO: use site.lang, if known 'site': str(site) if site else '', 'username': username, 'script_comments': '; '.join(script_comments) }) if not format_string: format_string = config.user_agent_format formatted = _USER_AGENT_FORMATTER.format(format_string, **values) # clean up after any blank components formatted = formatted.replace('()', '').replace(' ', ' ').strip() return formatted
def user_agent(site=None, format_string=None): """ Generate the user agent string for a given site and format. @param site: The site for which this user agent is intended. May be None. @type site: BaseSite @param format_string: The string to which the values will be added using str.format. Is using config.user_agent_format when it is None. @type format_string: basestring @return: The formatted user agent @rtype: unicode """ values = USER_AGENT_PRODUCTS.copy() script_name = calledModuleName() values['script'] = script_name # TODO: script_product should add the script version, if known values['script_product'] = script_name script_comments = [] username = '' if site: script_comments.append(str(site)) # TODO: there are several ways of identifying a user, and username # is not the best for a HTTP header if the username isnt ASCII. if site.username(): username = user_agent_username(site.username()) script_comments.append( 'User:'******'family': site.family.name if site else '', 'code': site.code if site else '', 'lang': site.code if site else '', # TODO: use site.lang, if known 'site': str(site) if site else '', 'username': username, 'script_comments': '; '.join(script_comments) }) if not format_string: format_string = config.user_agent_format formatted = _USER_AGENT_FORMATTER.format(format_string, **values) # clean up after any blank components formatted = formatted.replace(u'()', u'').replace(u' ', u' ').strip() return formatted