Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.90 KB | None | 0 0
  1. """WARNINGS TO SHARE
  2. 1. user names are not case sensitive, they must be spelled completely differently
  3. 2. comment files are case sensitive and the extension must be specified!"""
  4.  
  5. # import modules
  6. import os
  7.  
  8.  
  9. # TODO: MAKE SURE THE PATHS WORK ON WINDOWS!!!!!!!!!!!!!!!!
  10. # define the DataManager class and its methods
  11. class DataManager:
  12.  
  13.     # define class attributes
  14.     # TODO: make this private
  15.     user_profiles = []
  16.     base_dir = os.path.dirname(os.path.realpath(__file__))
  17.  
  18.     # set base directory upon instantiation & import existing users
  19.     def __init__(self):
  20.         os.chdir(self.base_dir)
  21.         self.import_users()
  22.  
  23.     # clear user profiles attribute
  24.     # TODO: make this private
  25.     def clear_users(self):
  26.         self.user_profiles.clear()
  27.  
  28.     # generate user data txt file
  29.     # TODO: make this more efficient (FUTURE RELEASE)
  30.     def gen_profile_settings(self):
  31.         try:
  32.             os.chdir('users/')
  33.             with open('user_data.txt', 'w+') as user_data:
  34.                 user_data.close()
  35.             os.chdir(self.base_dir)
  36.  
  37.         except FileNotFoundError:
  38.             with open('user_data.txt', 'w+') as user_data:
  39.                 user_data.close()
  40.             os.chdir(self.base_dir)
  41.  
  42.     # delete all user profile settings from the text file
  43.     def delete_all_profiles(self):
  44.  
  45.         # change directory and create an empty user data file
  46.         os.chdir('users/')
  47.         try:
  48.             os.remove('user_data.txt')
  49.             self.gen_profile_settings()
  50.         except FileNotFoundError:
  51.             self.gen_profile_settings()
  52.  
  53.         # move back to the base directory and notify the user
  54.         os.chdir(self.base_dir)
  55.         return 'All user data was successfully cleared!'
  56.  
  57.     # delete a user profile from the text file
  58.     def delete_profile(self, user):
  59.  
  60.         # set wd to the users folder
  61.         os.chdir('users/')
  62.  
  63.         # open the user data file and delete the profile
  64.         # read original lines in file
  65.         with open('user_data.txt', 'r+') as user_data:
  66.  
  67.             # read lines and rewrite lines that don't match the user input
  68.             lines = user_data.readlines()
  69.         user_data.close()
  70.  
  71.         # delete original user data file and write a new data file with the modified settings
  72.         os.remove('user_data.txt')
  73.         with open('user_data.txt', 'w+') as user_data:
  74.             [user_data.write(line) for line in lines if user.lower() not in line]
  75.         user_data.close()
  76.  
  77.         # return to base dir
  78.         os.chdir(self.base_dir)
  79.  
  80.         # success message
  81.         return 'Profile for user "' + user + '" successfully deleted!'
  82.  
  83.     # TODO: make a method for missing user profile file handling (FUTURE FEATURE)
  84.  
  85.     # import a user's comments
  86.     # TODO: make this private
  87.     def import_comments(self, user, file):
  88.  
  89.         # set wd to the data folder
  90.         # TODO: test if this works when moving the project's folder
  91.         os.chdir('comments/')
  92.  
  93.         # read comment file and read over blank
  94.         with open(file) as comment_file:
  95.             comment_lines = [line.strip() for line in comment_file.readlines() if line.strip()]
  96.         comment_file.close()
  97.  
  98.         # append user & comments to comments attribute
  99.         entry = {'user': user,
  100.                  'comments': comment_lines}
  101.         self.user_profiles.append(entry)
  102.  
  103.         # reset working directory to base directory
  104.         os.chdir(self.base_dir)
  105.  
  106.     # create a user
  107.     def create_user(self, user, file):
  108.  
  109.         # move to users directory
  110.         os.chdir('users/')
  111.  
  112.         # get a list of all existing user names if there's a user data file
  113.         # TODO: put this into a method
  114.         try:
  115.             with open('user_data.txt', 'r') as user_data:
  116.  
  117.                 # import raw data and skip empty lines (needed to check for duplicates)
  118.                 user_names = [name.lower()[:name.find(' - ')] for name in
  119.                               [line.strip() for line in user_data.readlines() if line.strip()]]
  120.  
  121.             # close file
  122.             user_data.close()
  123.  
  124.         # create a user data file if it doesn't exist and create an empty list of user names
  125.         # TODO: put this into a method
  126.         except FileNotFoundError:
  127.             with open('user_data.txt', 'w+') as user_data:
  128.                 user_data.close()
  129.             user_names = []
  130.  
  131.         # write user to user_data file if name doesn't exist and the text file exists
  132.         # make sure text file exists
  133.         # TODO: this can probably be more efficient (FUTURE RELEASE)
  134.         try:
  135.             os.chdir('../comments/')
  136.             with open(file, 'r') as comment_file:
  137.                 comment_file.close()
  138.             os.chdir('../users/')
  139.  
  140.         # notify the user that the text file doesn't exist
  141.         except FileNotFoundError:
  142.             return "The text file you specified doesn't exist!"
  143.  
  144.         # write user data if user's name doesn't already exist in the user data file
  145.         if user in user_names:
  146.             return 'A user with that name already exists, please use a different name!'
  147.  
  148.         # write user data
  149.         else:
  150.             with open('user_data.txt', 'a') as user_data:
  151.                 user_data.write(user.lower() + ' - ' + file + '\n')
  152.             user_data.close()
  153.  
  154.             # reset working directory to base directory
  155.             os.chdir(self.base_dir)
  156.  
  157.             # import users again
  158.             self.clear_users()
  159.             self.import_users()
  160.  
  161.             # success message
  162.             return 'User successfully added!'
  163.  
  164.     # import user data
  165.     def import_users(self):
  166.  
  167.         # clear profiles to avoid duplicates
  168.         self.clear_users()
  169.  
  170.         # change directory to user folder
  171.         os.chdir('users/')
  172.  
  173.         # create a list of arguments to supply for importing data
  174.         with open('user_data.txt', 'r') as user_data:
  175.  
  176.             # import raw data and skip empty lines
  177.             raw_data = [line.strip() for line in user_data.readlines() if line.strip()]
  178.  
  179.             # clean data and create a list of import arguments
  180.             import_arguments = []
  181.             for line in raw_data:
  182.  
  183.                 # remove newlines and unpack string into arguments
  184.                 user, file = line.split(' - ')
  185.  
  186.                 # append arguments to list
  187.                 entry = [user, file]
  188.                 import_arguments.append(entry)
  189.  
  190.         # close the file
  191.         user_data.close()
  192.  
  193.         # switch to base directory
  194.         os.chdir(self.base_dir)
  195.  
  196.         # better version of above code
  197.         for item in import_arguments:
  198.  
  199.             # define import arguments
  200.             user, file = item
  201.  
  202.             # import
  203.             self.import_comments(user, file)
  204.  
  205.         # reset working directory to base directory
  206.         os.chdir(self.base_dir)
  207.  
  208.     # print users and associated text files
  209.     # TODO: this works on my console but the formatting is messed up on Discord (INVESTIGATE, UPDATE)
  210.     def print_users(self):
  211.         # TODO: bug, make the column headers dynamic!
  212.         # move to the users directory
  213.         os.chdir('users/')
  214.  
  215.         # print the users if the user data file exists
  216.         try:
  217.             # read all lines from the user data file
  218.             with open('user_data.txt', 'r') as user_data:
  219.                 comment_lines = [line.strip() for line in user_data.readlines() if line.strip()]
  220.             user_data.close()
  221.  
  222.             # check if print_list is empty
  223.             # TODO: make this a listcomp (FUTURE RELEASE)
  224.             print_list = []
  225.             if comment_lines:
  226.                 # print users' names and associated text file
  227.                 for line in comment_lines:
  228.                     # unpack variables to print
  229.                     user, file = line.split(' - ')
  230.  
  231.                     # append variables to the print list
  232.                     print_list.append([user, file])
  233.  
  234.             # move back to the base directory
  235.             os.chdir(self.base_dir)
  236.  
  237.             # create a message file to return to the user\
  238.             msg = ''
  239.  
  240.             # print the column names w/ dynamic spacing
  241.             try:
  242.                 user_space = ' ' * (len(max([sublist[0] for sublist in print_list], key=len)) - 1)
  243.                 file_space = ' ' * (len(max([sublist[1] for sublist in print_list], key=len)) - 1)
  244.                 msg += ('User:' + user_space + 'File:' + file_space + '\n')
  245.  
  246.             # handle errors with the max() function (results from an empty list
  247.             except ValueError:
  248.                 pass
  249.  
  250.             # print all the users' names and associated text files
  251.             if print_list:
  252.                 for entry in print_list:
  253.  
  254.                     # unpack variables
  255.                     user, file = entry
  256.  
  257.                     # calculate divider spaces
  258.                     divider = ' ' * (len(max([sublist[0] for sublist in print_list], key=len)) - len(user) + 4)
  259.  
  260.                     # print the variables w/ dynamic spacing
  261.                     msg += (user + divider + file + '\n')
  262.  
  263.             # unless there is no user data
  264.             else:
  265.                 msg = 'No user settings found!'
  266.  
  267.             # return the message to the user
  268.             return msg
  269.  
  270.         # create a user data file if it doesn't exist
  271.         # TODO: put this into a method (FUTURE)
  272.         except FileNotFoundError:
  273.  
  274.             # create the file and notify the user
  275.             with open('user_data.txt', 'w+') as user_data:
  276.                 user_data.close()
  277.  
  278.             # move back to the base directory
  279.             os.chdir(self.base_dir)
  280.  
  281.             # warning message
  282.             return "User data file didn't exist. Please try again."
  283.  
  284.  
  285. # test the class (SOLELY FOR DEBUGGING, EDIT AS YOU PLEASE)
  286. if __name__ == '__main__':
  287.  
  288.     # create a data manager
  289.     dm = DataManager()
  290.  
  291.     # break
  292.     print('BREAK')
  293.  
  294.     hm = dm.print_users()
  295.     print(hm)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement