File size: 9,783 Bytes
c7f4bd0
 
 
 
1
2
3
4
5
{"nwo":"18F\/fbopen","sha":"e5a71b8d837f528050a29c5b04a8ea409bc298a3","path":"loaders\/attachments\/base\/util.py","language":"python","identifier":"create_dir","parameters":"(log, dirname=None)","argument_list":"","return_statement":"return dirname","docstring":"Create a directory without failing if it already exists.","docstring_summary":"Create a directory without failing if it already exists.","docstring_tokens":["Create","a","directory","without","failing","if","it","already","exists","."],"function":"def create_dir(log, dirname=None):\n    \"\"\"\n    Create a directory without failing if it already exists.\n    \"\"\"\n\n    if dirname and not os.path.isdir(dirname):\n        log.info(\"Checking directory... Creating {}\".format(dirname))\n        os.makedirs(dirname)\n    else:\n        log.info(\"Checking directory... {} exists.\".format(dirname))\n\n    return dirname","function_tokens":["def","create_dir","(","log",",","dirname","=","None",")",":","if","dirname","and","not","os",".","path",".","isdir","(","dirname",")",":","log",".","info","(","\"Checking directory... Creating {}\"",".","format","(","dirname",")",")","os",".","makedirs","(","dirname",")","else",":","log",".","info","(","\"Checking directory... {} exists.\"",".","format","(","dirname",")",")","return","dirname"],"url":"https:\/\/github.com\/18F\/fbopen\/blob\/e5a71b8d837f528050a29c5b04a8ea409bc298a3\/loaders\/attachments\/base\/util.py#L4-L15"}
{"nwo":"18F\/fbopen","sha":"e5a71b8d837f528050a29c5b04a8ea409bc298a3","path":"loaders\/attachments\/base\/runner.py","language":"python","identifier":"AttachmentsRunner.create_import_dir","parameters":"(self, dirname=None)","argument_list":"","return_statement":"return dirname","docstring":"Create a timestampped directory to perform an import in.\n        Returns a string containing the directory name.","docstring_summary":"Create a timestampped directory to perform an import in.\n        Returns a string containing the directory name.","docstring_tokens":["Create","a","timestampped","directory","to","perform","an","import","in",".","Returns","a","string","containing","the","directory","name","."],"function":"def create_import_dir(self, dirname=None):\n        \"\"\"\n        Create a timestampped directory to perform an import in.\n        Returns a string containing the directory name.\n        \"\"\"\n\n        if not dirname:\n            dirname = self.current_import_dirname()\n            self.log.debug(dirname)\n\n        self.create_dir(dirname=dirname)\n\n        return dirname","function_tokens":["def","create_import_dir","(","self",",","dirname","=","None",")",":","if","not","dirname",":","dirname","=","self",".","current_import_dirname","(",")","self",".","log",".","debug","(","dirname",")","self",".","create_dir","(","dirname","=","dirname",")","return","dirname"],"url":"https:\/\/github.com\/18F\/fbopen\/blob\/e5a71b8d837f528050a29c5b04a8ea409bc298a3\/loaders\/attachments\/base\/runner.py#L20-L32"}
{"nwo":"18F\/fbopen","sha":"e5a71b8d837f528050a29c5b04a8ea409bc298a3","path":"loaders\/attachments\/base\/link_extractor.py","language":"python","identifier":"LinkExtractor.get_links","parameters":"(self, doc, solnbr, shelf)","argument_list":"","return_statement":"return len(links)","docstring":"Parses the links from a doc. Returns the number of links found.","docstring_summary":"Parses the links from a doc. Returns the number of links found.","docstring_tokens":["Parses","the","links","from","a","doc",".","Returns","the","number","of","links","found","."],"function":"def get_links(self, doc, solnbr, shelf):\n        '''\n        Parses the links from a doc. Returns the number of links found.\n        '''\n        shelf[solnbr] = {}\n        links = self.collect_link_attrs(doc)\n        shelf[solnbr] = {'attachments': links}\n\n        return len(links)","function_tokens":["def","get_links","(","self",",","doc",",","solnbr",",","shelf",")",":","shelf","[","solnbr","]","=","{","}","links","=","self",".","collect_link_attrs","(","doc",")","shelf","[","solnbr","]","=","{","'attachments'",":","links","}","return","len","(","links",")"],"url":"https:\/\/github.com\/18F\/fbopen\/blob\/e5a71b8d837f528050a29c5b04a8ea409bc298a3\/loaders\/attachments\/base\/link_extractor.py#L62-L70"}
{"nwo":"18F\/fbopen","sha":"e5a71b8d837f528050a29c5b04a8ea409bc298a3","path":"loaders\/attachments\/base\/link_extractor.py","language":"python","identifier":"LinkExtractor.collect_link_attrs","parameters":"(self, doc)","argument_list":"","return_statement":"return attachments","docstring":"This would be the method to override if scraping a different site.\n        It must return a list of dicts containing keys 'filename', 'url', and 'description'. Ala:\n        [\n            { 'filename': 'example1.txt', 'url': 'http:\/\/example.com\/example1.txt', 'description': 'Just some file' },\n            { 'filename': 'example2.doc', 'url': 'http:\/\/example.com\/example2.doc', 'description': 'Some word doc' },\n        ]\n        'filename' can be left as None, but it should be defined. This will help indicate that we have some\n        more special processing to do, later down the line, than to just download a URL.","docstring_summary":"This would be the method to override if scraping a different site.\n        It must return a list of dicts containing keys 'filename', 'url', and 'description'. Ala:\n        [\n            { 'filename': 'example1.txt', 'url': 'http:\/\/example.com\/example1.txt', 'description': 'Just some file' },\n            { 'filename': 'example2.doc', 'url': 'http:\/\/example.com\/example2.doc', 'description': 'Some word doc' },\n        ]\n        'filename' can be left as None, but it should be defined. This will help indicate that we have some\n        more special processing to do, later down the line, than to just download a URL.","docstring_tokens":["This","would","be","the","method","to","override","if","scraping","a","different","site",".","It","must","return","a","list","of","dicts","containing","keys","filename","url","and","description",".","Ala",":","[","{","filename",":","example1",".","txt","url",":","http",":","\/\/","example",".","com","\/","example1",".","txt","description",":","Just","some","file","}","{","filename",":","example2",".","doc","url",":","http",":","\/\/","example",".","com","\/","example2",".","doc","description",":","Some","word","doc","}","]","filename","can","be","left","as","None","but","it","should","be","defined",".","This","will","help","indicate","that","we","have","some","more","special","processing","to","do","later","down","the","line","than","to","just","download","a","URL","."],"function":"def collect_link_attrs(self, doc):\n        '''\n        This would be the method to override if scraping a different site.\n        It must return a list of dicts containing keys 'filename', 'url', and 'description'. Ala:\n        [\n            { 'filename': 'example1.txt', 'url': 'http:\/\/example.com\/example1.txt', 'description': 'Just some file' },\n            { 'filename': 'example2.doc', 'url': 'http:\/\/example.com\/example2.doc', 'description': 'Some word doc' },\n        ]\n        'filename' can be left as None, but it should be defined. This will help indicate that we have some\n        more special processing to do, later down the line, than to just download a URL.\n        '''\n\n        attachments = []\n        for div in doc('div.notice_attachment_ro'):\n            a = {}\n            d = pq(div)\n\n            # only in cases where it's a file upload to FBO, then there's a '.file' div\n            link_tag = d.find('div.file')('a') or d.find('div')('a')\n\n            a = {\n                'filename': link_tag.text().strip(),\n                'url': urljoin(self.fbo_base_url, link_tag.attr('href')),\n                'desc': d.find('span.label').closest('div').remove('span').text().strip(),\n            }\n\n            attachments.append(a)\n\n        if not attachments:  # keep looking\n            addl_info_link = doc('div#dnf_class_values_procurement_notice__additional_info_link__widget')('a')\n            if addl_info_link:\n                a = {\n                    'filename': None,\n                    'url': addl_info_link.attr('href'),\n                    'desc': addl_info_link.text().strip(),\n                }\n\n                attachments.append(a)\n\n        return attachments","function_tokens":["def","collect_link_attrs","(","self",",","doc",")",":","attachments","=","[","]","for","div","in","doc","(","'div.notice_attachment_ro'",")",":","a","=","{","}","d","=","pq","(","div",")","# only in cases where it's a file upload to FBO, then there's a '.file' div","link_tag","=","d",".","find","(","'div.file'",")","(","'a'",")","or","d",".","find","(","'div'",")","(","'a'",")","a","=","{","'filename'",":","link_tag",".","text","(",")",".","strip","(",")",",","'url'",":","urljoin","(","self",".","fbo_base_url",",","link_tag",".","attr","(","'href'",")",")",",","'desc'",":","d",".","find","(","'span.label'",")",".","closest","(","'div'",")",".","remove","(","'span'",")",".","text","(",")",".","strip","(",")",",","}","attachments",".","append","(","a",")","if","not","attachments",":","# keep looking","addl_info_link","=","doc","(","'div#dnf_class_values_procurement_notice__additional_info_link__widget'",")","(","'a'",")","if","addl_info_link",":","a","=","{","'filename'",":","None",",","'url'",":","addl_info_link",".","attr","(","'href'",")",",","'desc'",":","addl_info_link",".","text","(",")",".","strip","(",")",",","}","attachments",".","append","(","a",")","return","attachments"],"url":"https:\/\/github.com\/18F\/fbopen\/blob\/e5a71b8d837f528050a29c5b04a8ea409bc298a3\/loaders\/attachments\/base\/link_extractor.py#L72-L111"}