def create_root(): Taxon.create(rank=ROOT, valid_name='root', is_page_root=True)
def read_file(filename): with codecs.open(filename, mode='r') as file: reader = csv.reader(file) first_line = reader.next() # name of parent of root taxon should be in cell A1 root_name = first_line[0] if root_name: root_parent = Taxon.filter(Taxon.valid_name == root_name)[0] # maintain stack of taxa that are parents of the current taxon stack = [root_parent] else: stack = [] # current valid taxon (for synonyms) current_valid = None # whether current taxon should be marked as root of a page is_page_root = True error_occurred = False for row in reader: try: # ignore blank rows if row[3] == '' and row[0] == '': continue data = parse_row(row) if data['status'] == STATUS_VALID: # get stuff off the stack rank = data['rank'] # TODO: make this somehow unranked-clade-aware while len(stack) > 0 and rank >= stack[-1].rank: stack.pop() # create new Taxon current_valid = Taxon.create(valid_name=data['valid_name'], age=data['age'], rank=data['rank'], is_page_root=is_page_root, comments=data['comments_taxon'], data=data['data_taxon']) if len(stack) > 0: current_valid.parent = stack[-1] if is_page_root: is_page_root = False stack.append(current_valid) # create new Name data['taxon'] = current_valid assert current_valid.valid_name == data['valid_name'], \ "Valid name %s does not match expected %s" % (data['valid_name'], current_valid.valid_name) data['data'] = helpers.fix_data(data['data']) # Detect whether a name object is already present (Principle of Coordination) nm = None if data['root_name'][0:4] == 'see ': seen = data['root_name'][4:] nm = Taxon.get(Taxon.valid_name == seen).base_name # create a new Name if none was found if nm is None: nm = Name.create(**data) # set base_name field if data['status'] == STATUS_VALID: current_valid.base_name = nm except Exception: traceback.print_exc() print('Error parsing row: %s' % row) error_occurred = True # ignore error and happily go on with the next return not error_occurred