Skip to content

Commit e026ca0

Browse files
committed
refactor: extract code as method from do_import
This started by trying to fix a resource error where the os.scandir iterator was not closed for some reason (it should have been exhausted and close). While the ResourceWarning is fixed with: with os.scandir() as ...: indented existing code it breaks under python2. Trying a python2 friendly option: dirscan = os.scandir() ... if hasattr(dirscan,'close'): dirscan.close() doesn't fix the resource warning. Using the 'with os.scandir()...' indented a nested block too far so I extracted it into another method. It is only this method that survives 8-(.
1 parent 9649afa commit e026ca0

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

roundup/admin.py

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,29 +1245,8 @@ class colon_separated(csv.excel):
12451245

12461246
cl = self.get_class(classname)
12471247

1248-
# ensure that the properties and the CSV file headings match
1249-
with open(os.path.join(import_dir, filename), 'r') as f:
1250-
reader = csv.reader(f, colon_separated, lineterminator='\n')
1251-
file_props = None
1252-
maxid = 1
1253-
# loop through the file and create a node for each entry
1254-
for n, r in enumerate(reader):
1255-
if file_props is None:
1256-
file_props = r
1257-
continue
1258-
1259-
if self.verbose:
1260-
sys.stdout.write('\rImporting %s - %s' % (classname, n))
1261-
sys.stdout.flush()
1262-
1263-
# do the import and figure the current highest nodeid
1264-
nodeid = cl.import_list(file_props, r)
1265-
if hasattr(cl, 'import_files') and import_files:
1266-
cl.import_files(import_dir, nodeid)
1267-
maxid = max(maxid, int(nodeid))
1268-
1269-
# (print to sys.stdout here to allow tests to squash it .. ugh)
1270-
print(file=sys.stdout)
1248+
maxid = self.import_class(dir_entry.path, colon_separated, cl,
1249+
import_dir, import_files)
12711250

12721251
# import the journals
12731252
with open(os.path.join(import_dir, classname + '-journals.csv'), 'r') as f:
@@ -1283,6 +1262,46 @@ class colon_separated(csv.excel):
12831262
self.db_uncommitted = True
12841263
return 0
12851264

1265+
def import_class(self, filepath, csv_format_class, hyperdb_class,
1266+
import_dir, import_files):
1267+
"""Import class given csv class filepath, csv_format_class and
1268+
hyperdb_class, directory for import, and boolean to import
1269+
files.
1270+
1271+
Optionally import files as well if import_files is True
1272+
otherwise just import database data.
1273+
1274+
Returns: maxid seen in csv file
1275+
"""
1276+
1277+
maxid = 1
1278+
1279+
# ensure that the properties and the CSV file headings match
1280+
with open(filepath, 'r') as f:
1281+
reader = csv.reader(f, csv_format_class, lineterminator='\n')
1282+
file_props = None
1283+
# loop through the file and create a node for each entry
1284+
for n, r in enumerate(reader):
1285+
if file_props is None:
1286+
file_props = r
1287+
continue
1288+
1289+
if self.verbose:
1290+
sys.stdout.write('\rImporting %s - %s' % (
1291+
hyperdb_class.classname, n))
1292+
sys.stdout.flush()
1293+
1294+
# do the import and figure the current highest nodeid
1295+
nodeid = hyperdb_class.import_list(file_props, r)
1296+
if hasattr(hyperdb_class, 'import_files') and import_files:
1297+
hyperdb_class.import_files(import_dir, nodeid)
1298+
maxid = max(maxid, int(nodeid))
1299+
1300+
# (print to sys.stdout here to allow tests to squash it .. ugh)
1301+
print(file=sys.stdout)
1302+
1303+
return maxid
1304+
12861305
def do_importtables(self, args):
12871306
''"""Usage: importtables export_dir
12881307
This imports the database tables exported using exporttables.

0 commit comments

Comments
 (0)