lundi 12 mars 2018

Python: MVC implementation with complex model interaction

Currently I'm working on a small python (3.6.4) application (for mac os) and I'm having some trouble with implementing a controller/model relationship. I'm kind of new to python and MVC (and design patterns in general for that matter).

My program has a GUI (tKinter), so I understand how you have view/controller pairs and models that represent data that is shown on screen. No trouble there.

The program creates zip files that can be written to tape. It's meant to run as a semi-daemon. What I mean by that is that it should mainly run automatically and report the user if errors occur.

There's an input folder for the user to put projects in, a process folder that is hidden from the user and is only used by the program (so the folder structure can't be tampered with) and an output folder that is used to store the zip archives which is also invisible to the user.

  1. The program watches the input folder for "Project folders". The Project folders contain one or more "Episode folders" that each have some files in them.
  2. The program checks if the name of a project folder is present in a database (user is responsible for adding project names) and creates a matching Project folder in the process folder with a formatted name (lowercase_without_spaces).
  3. Episode folders for each episode are also checked. (In this case if they haven't been archived before)
  4. Episodes are copied from the input folder to the process folder once the files they contain are complete. (i.e. Filesize stays the same for 3 seconds)
  5. For each Project, episodes are zipped from the process folder and moved to the corresponding Project folder in the output folder.

I know this seems way to complicated but bare with me.

I have a Project and Episode class. Both classes contain their folder name from the input folder and their formatted name upon which the folder names in the process folder are based.

Each Project contains a list of Episode objects. (Probably very bad...)

There's also a MonitorController that monitors the input folder and does item 1 and 2 described above. It can also see the Project class because it holds a list of Project objects. (another bad practice I guess...)

Then there's the Database model with get_projects() which reads a plain text file with a project name on each line and returns those as a list and get_episodes() which basically does the same but it reads a file with episode names for a project. It is linked to DatabaseController which does does the comparing logic with match_project() and match_episode() and returns a boolean accordingly. It is used by MonitorController. (There I go again, linking 2 controllers together, silly me.)

I'd like to point out that to me, all of this is controller/model interaction if you ignore the fact that the input, process and output folder are in fact views and models at the same time but this is certainly the fact that confuses me the most.

So, I have a couple of questions: - Project and Episode are classes but should they be models? Should they be interacting with each other directly the way that they do now? - If controllers aren't supposed to directly communicate with each other, how am I going to check a project's name via DatabaseController before adding it to the list in MonitorController? - Should I make use of Services to make Model/Model, Model/Controller, Controller/Controller and Controller/Model connections?

Currently I violate rule #17 of The Zen of Python and probably a couple more so that's why I'm asking for help.

Sorry for the messy explanation. Please let me know if you have any questions.

Any help/advice would be great!

Aucun commentaire:

Enregistrer un commentaire