def show_return(data, caller=None, extra=None): """Log the data returned as the result of a query. This was written to allow inspection of data returned from the API server. The data parameter is a dict object containing keys 'ret', 'inf', 'url' and 'err'. The data of interest are 'ret' and 'err' con- taining the response text and error information. """ from os import environ from logger import getLogLevel from logging import DEBUG if getLogLevel() > DEBUG: return #data = {'ret','inf','url','err'} if not caller: caller = 'send_request' if extra: log.info("{0:s}: information about this request: {1:s}".format(caller,extra)) ret = data['ret'].encode('utf-8') try: wid = environ['COLUMNS'] except KeyError: wid = 80 ret = format_lines(ret, margin=' ret >', col=wid) log.info(" >> 'ret':\n{0:s}".format(ret)) if data['inf']: lines = " 'inf' > " + str(data['inf']) else: lines = " 'inf' > None" log.info("{0:s}".format(lines)) log.info(" >> 'redirect' : {0:s}".format(str(data['url']))) log.info(" >> 'error': {0:s}".format(str(data['err'])))
def team_users(api, team_name, request=None, eid=None, save_xml=None, debug=False): """Retrieve details of team members. Parameters: team_name - Name of team. Should be identical to a team returned by company_teams(api, 'company name') request - List which limits the amount of information returned. Must be a subset of: ['last_name', 'first_name', 'status', 'id', 'reference', 'is_provider', 'timezone', 'timezone_offset'] These are the keys in the dict object(s) returned. eid - oDesk user id. This is the same as the 'id' above. If given a list of one dict object for a team member matching 'id' is returned. Return: A list of one dict object if 'eid' parameter is given, or a list of N dict objects where N equals the number of people on the named team. If 'request' is not given the dict object contains all information for each team member. Otherwise each item in 'request' which exactly matchs one of the valid identifiers given above is returned in each dict object generated. """ userlist = [] (team_ref, parent_team_ref, company_ref) = \ team_reference_IDs(api, team_name, save_xml) if not team_ref: log.warning("No results from team_users(api, '{0:s}')".format(team_name)) return userlist log.info("fetching team {0:s} users".format(team_name)) url = urls.get_API_URL('team_users', team_ref=team_ref) log.debug('URL: {0:s}'.format(url)) if save_xml: save_xml = 'team_users.xml' response = send_GET(api, url, save_xml=save_xml) if response is None: log.error("request failed: send_GET(api, {0:s}".format(url)) return userlist tmplist = list_from_xml(response, 'user', debug=debug) if eid: for user in tmplist: if user['id'] == eid: userlist.append(user) break else: userlist = tmplist[:] all_info = ['last_name', 'first_name', 'status', 'id', 'reference', 'is_provider', 'timezone', 'timezone_offset'] if not request: requested_info = all_info[:] else: requested_info = list_intersect(list(request), all_info) return dict_subset(userlist, requested_info)
def login(self, user, password): params = [('login',user), ('password',password), ('action','login')] url = get_auth_URL('login') + self.merge_params_to_uri(None, params, False) data = self.send_request(url, 'post', caller=func()) self.user_authorized = self.request_ok(data) if self.user_authorized: log.info("Logged in as {0:s}".format(user)) self.set_user_and_password(user, password) return self.user_authorized
def company_users(api, company_name, request=None, save_xml=None, debug=False): """Retrieve list of contractors working for 'company_name' Parameters: company_name - Target company of query. Should be identical to a name returned by odesk.api.query.organization:companies(api, request) request - List which limits the amount of information returned for each user. Must be a subset of: [timezone, reference, status, timezone_offset, id, is_provider, last_name, first_name] This ultimately depends on the success of api.query.company_details(). That GET depends on the permissions granted to the authenticated user of this process- the oDesk user and password used to access the API. Return: List of dictionary objects- one per employee. """ userlist = [] details = company_details(api, company_name, save_xml=False) if not details: warn = "No results from company_details(api, '{0:s}')" log.warning(warn.format(company_name)) return userlist log.info("fetching company {0:s} users".format(company_name)) url = urls.get_API_URL('company_users', company_ref=details['reference']) log.debug('URL: {0:s}'.format(url)) if save_xml: save_xml = 'company_users.xml' response = send_GET(api, url, save_xml=save_xml) if response is None: log.error("request failed: send_GET(api, {0:s}".format(url)) return userlist userlist = list_from_xml(response, 'user', debug=debug) all_info = ['timezone', 'reference', 'status', 'timezone_offset', 'id', 'is_provider', 'last_name', 'first_name'] if not request: requested_info = all_info[:] else: requested_info = list_intersect(request, all_info) return dict_subset(userlist, requested_info)
api.clean_exit(1) log.debug("app_main() returning {0!s}".format(argv2)) return (api, argv2) # This is intended only for testing. if __name__ == '__main__': from odapi.logger import initLogger usage = """ usage: $ {0:s} [OPTIONS] [OPTIONS]""" initLogger(logLevel='debug',logDisk=False, logConsole=True) (api, argv) = app_main(sys.argv[0], sys.argv, usage) api.pprint() stdout("api.log_level = {0:d}\n".format(api.log_level)) setLogLevel("info", api) stdout('setLogLevel("info")\n') stdout("api.log_level = {0:d}\n".format(api.log_level)) log.debug("log debug test") log.info("log info test") log.warn("log warn test") log.error("log error test") stderr("remaining in argv:\n\t {0!s}\n".format(argv)) api.clean_exit(0)
job_file = path.join(popts['out'], job_file) else: job_file = path.join(data.htmldir, job_file) outdir = path.dirname(job_file) if not path.isdir(outdir): makedirs(outdir) #------------------------------------------------- # Joblisting results have been going back 30 days days = 3 #------------------------------------------------- job_list = job_search(api, since=days, JobCategory=category) if popts['text']: with open('/var/tmp/{0:s}_job_listing.txt'.format(category), 'w') as fh: print_query_list(fh, job_list) log.info('{0:d} (unfiltered) jobs in list'.format(len(job_list))) log.info('title_filter: {0!r}'.format(title_filter)) log.info('skills_filter: {0!r}'.format(skills_filter)) log.info('HTML out: {0:s}'.format(job_file)) base_fname = 'unfiltered_{0:s}.html'.format(popts['category'].lower()) unfiltered_file = path.join(path.dirname(job_file), base_fname) log.info('unfiltered out: {0:s}'.format(unfiltered_file)) try: create_listing(page_title, job_list, job_file, unfiltered_file, title_filter=title_filter, skills_filter=skills_filter) except AppException, e: stderr(e) api.clean_exit(0)