def __init__(self):
     sf = NS()
     sf.db_name = config_get_section_attribute('MOODLE', 'db_name')
     sf.db_username = config_get_section_attribute('MOODLE', 'db_username')
     sf.db_password = config_get_section_attribute('MOODLE', 'db_password')
     sf.db_host = config_get_section_attribute('MOODLE', 'db_host')
     self.db = postgresql.open(sf('pq://{db_username}:{db_password}@{db_host}/{db_name}'))
     self.sql = self.db.prepare
 def delete_table_by_kwarg(self, table, **kwargs):
     """
     SELECT table WHERE kwargs
     """
     sf = NS()
     sf.table = self.convert_to_table(table)
     wheres = []
     for i in range(len(list(kwargs.keys()))):
         passed = list(kwargs.keys())[i]
         value = kwargs[passed]
         if str(value).isdigit:
             wheres.append( "{} = {}".format(passed, value) )
         else:
             wheres.append( "{} = '{}'".format(passed, value) )
     sf.wherestring = " AND ".join(wheres)
     return self.sql(sf('delete from {table} where {wherestring}'))()
def system_call(command):
    """
    WRAPPER FOR THE Popen CALL
    TURN RESULT INTO AN OBJECT AND RETURN
    DOES NOT CHECK FOR ERRORS
    DO NOT USE WITH USER INPUT

    CONVERTS TO APPROPRIATE ENCODING, RETURNS 'REGULAR' STRINGS
    """
    p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # SKIP CHECK FOR not p
    # SIMPLY RETURN THE STANDARD OUT

    sf = NS()
    encoding = sys.getdefaultencoding()
    sf.stdout, sf.stderr = p.communicate()
    sf.stdout = sf.stdout.decode(encoding).strip('\n')
    sf.stderr = sf.stderr.decode(encoding).strip('\n')
    return sf
        # RESULT NOW HAS username
        result.default_password = '******'
        result = system_call(result('echo {username}:{default_password} | /usr/sbin/chpasswd'))
        print(result)

        """
        for line in careful:
            if ":"+who+":" in line:
                with open(path, 'a') as f:
                    f.write( re.sub(':x:', ':changeme:', line) )
                system_call("/usr/sbin/newusers {}".format(path))
                os.remove(path)
        """
                
if __name__ == "__main__":

    dnet = Access()
    results = dnet.select_table('user_email_password_reset')
    # COLLAPSE SO WE ARE GUARENTEED TO BE WORKING WITH JUST ONE AT A TIME
    results = set(results)
    for row in results:
        sf = NS()
        sf.userid, sf.num, sf.fullname, sf.email = row
        print(sf('About to reset {fullname} ({num}) email password to changeme'))
        dnet.reset_email(str(sf.num))
        print(sf('Done'))
        sf.table = dnet.convert_to_table('user_email_password_reset')
        command = dnet.sql(sf("delete from {table} where powerschoolid = '{num}'"))
        command()