Last active
February 8, 2017 18:26
-
-
Save molcay/d61d9182fa7b133318ae12c60c6c9547 to your computer and use it in GitHub Desktop.
Extract information from csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # -*- coding: utf-8 -*- | |
| import os, sys, subprocess | |
| # Constant | |
| INPUT_DIR = "input" | |
| ACCEPTED_FILE_FORMAT = ["csv", "txt", "list"] | |
| def asciify(string): | |
| if string.find("(") != -1: | |
| string = string[:string.find("(")] + string[string.find(")")+1:] | |
| return string.replace("İ", "I").replace("Ö", "O").replace("Ü", "U").replace("Ç", "C").replace("Ğ", "G").replace("Ş", "S") | |
| students = [] | |
| input_files = [inp for inp in os.listdir(INPUT_DIR) if inp.split(".")[-1] in ACCEPTED_FILE_FORMAT] | |
| for filename in input_files: | |
| with open(os.path.join(INPUT_DIR, filename), 'r') as inp: | |
| lines = inp.readlines() | |
| for line in lines: | |
| raw_name = line.split(",")[2] # MUHAMMED OLCAY TERCANLI | |
| student_id = line.split(",")[1] | |
| (first_two, last_two) = (int(student_id[:2]), int(student_id[-2:])) | |
| asciified_name = asciify(raw_name).lower() | |
| username = asciified_name.split()[-2] + "." + asciified_name.split()[-1] # olcay.tercanli | |
| firstname = " ".join(asciified_name.split()[-2::-1][::-1]).title() # Muhammed Olcay | |
| lastname = asciified_name.split()[-1].upper() # TERCANLI | |
| password = "{0}{1}".format(username, first_two+last_two) | |
| student = { | |
| 'username': username, | |
| 'firstname': firstname, | |
| 'lastname': lastname, | |
| 'password': password | |
| } | |
| # print(student) | |
| students.append(student) | |
| for student in students: | |
| # ipa user-add olcay.tercanli --first='Muhammed Olcay' --last='TERCANLI' --displayname='Muhammed Olcay TERCANLI' | |
| command = "ipa user-add {0} --first='{1}' --last='{2}' --displayname='{1} {2}'".format(student['username'], student['firstname'], student['lastname']) | |
| # ipa passwd olcay.tercanli password_for_olcay.tercanli | |
| command2 = "ipa passwd {0} {1}".format(student['username'], student['password']) | |
| # For random password | |
| # command = "ipa user-add {0} --first='{1}' --last='{2}' --displayname='{1} {2}' --random".format(student['username'], student['firstname'], student['lastname']) | |
| print(command) | |
| print(command2) | |
| if len(sys.argv) > 1 and sys.argv[1] == "--exec": | |
| return_code = subprocess.call(command, shell=True) | |
| if return_code == 0: | |
| subprocess.call(command2, shell=True) | |
| if len(sys.argv) == 1: | |
| print("# The program only prints command. If you want execute them you can run script with this options:\n# \t./scr.py --exec") | |
| # output format | |
| # ipa user-add USERNAME --first=FIRSTNAME --last=LASTNAME --displayname="FIRSTNAME LASTNAME" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment