Some refactoring to push-pazans-to-mongo.py (also PEP-8 changes)
This commit is contained in:
@@ -1,13 +1,17 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import pymongo
|
import pymongo
|
||||||
|
|
||||||
|
|
||||||
dirWithIds = sys.argv[1]
|
dirWithIds = sys.argv[1]
|
||||||
|
|
||||||
pazansMongo = pymongo.MongoClient("equal.cf")
|
pazansMongo = pymongo.MongoClient("equal.cf")
|
||||||
pazansDb = pazansMongo['pazans']
|
pazansDb = pazansMongo['pazans']
|
||||||
pazansCollection = pazansDb['pazans']
|
pazansCollection = pazansDb['pazans']
|
||||||
|
|
||||||
# gotoMongo = pymongo.MongoClient("goto.reproducible.work")
|
# gotoMongo = pymongo.MongoClient("goto.reproducible.work")
|
||||||
@@ -20,32 +24,32 @@ ids = set()
|
|||||||
idsFile = sys.argv[3]
|
idsFile = sys.argv[3]
|
||||||
if not os.path.isfile(idsFile):
|
if not os.path.isfile(idsFile):
|
||||||
idsRegex = re.compile('\\"_id\\": (.+?),')
|
idsRegex = re.compile('\\"_id\\": (.+?),')
|
||||||
with open(usersJsonFile, "r") as fileName:
|
with open(usersJsonFile, "r") as file_name:
|
||||||
for line in fileName:
|
for line in file_name:
|
||||||
groups = idsRegex.search(line)
|
groups = idsRegex.search(line)
|
||||||
id = int(groups.group(1))
|
uid = int(groups.group(1))
|
||||||
ids.add(id)
|
ids.add(uid)
|
||||||
|
|
||||||
with open(idsFile, "w") as fileName:
|
with open(idsFile, "w") as file_name:
|
||||||
for id in ids:
|
for uid in ids:
|
||||||
fileName.write(str(id) + "\n")
|
file_name.write(str(uid) + "\n")
|
||||||
else:
|
else:
|
||||||
with open(idsFile, "r") as fileName:
|
with open(idsFile, "r") as f_ids:
|
||||||
for line in fileName:
|
for line in f_ids:
|
||||||
ids.add(int(line))
|
f_ids.add(int(line))
|
||||||
|
|
||||||
for fileName in os.listdir(dirWithIds):
|
for file_name in os.listdir(dirWithIds):
|
||||||
print("parsing", fileName)
|
print("parsing {}".format(file_name))
|
||||||
|
|
||||||
with open(os.path.join(dirWithIds, fileName), "r") as file:
|
with open(os.path.join(dirWithIds, file_name), "r") as file:
|
||||||
for line in file:
|
for line in file:
|
||||||
id = int(line)
|
id = int(line)
|
||||||
if id in ids:
|
if id in ids:
|
||||||
pazan = pazansCollection.find_one(id)
|
pazan = pazansCollection.find_one(id)
|
||||||
if pazan is None:
|
if pazan is None:
|
||||||
pazansCollection.insert_one({"_id": id, "groups": [fileName]})
|
pazansCollection.insert_one({"_id": id, "groups": [file_name]})
|
||||||
elif fileName not in pazan["groups"]:
|
elif file_name not in pazan["groups"]:
|
||||||
pazan["groups"].append(fileName)
|
pazan["groups"].append(file_name)
|
||||||
pazansCollection.update_one(
|
pazansCollection.update_one(
|
||||||
{"_id": pazan["_id"]},
|
{"_id": pazan["_id"]},
|
||||||
{"$set": {"groups": pazan["groups"]}}
|
{"$set": {"groups": pazan["groups"]}}
|
||||||
|
|||||||
Reference in New Issue
Block a user