1

I've been reading stackoverflow for years, and this is my first post. I've tried searching, but I can't really find anything that I both understand, and matches my scenario. Yes, I'm total OOP newbie, so please explain things as plainly as possible.

OK, I'm trying to write a Python script that calls Rsync to make a backup.

I'm essentially calling the script from root's crontab. Because this opens up security issues, I'm going to be reading in the directories that need to be backed up (and a few options for the rsync command for each directory) from a configuration file in a form that the ConfigParser module will understand.

So, I'm at a point where I want to create objects to represent backup directory. My question is thus:

Do I make a separate object factory class, and send it all of the relevant information that was gleaned while parsing the config file? Alternatively, do I put all the object creation stuff in a method in my existing configuration parsing class?

I hope at least some of that makes sense.

Please note this is both for production use and a learning project for me to learn Object Oriented programming and design, so yes, it is probably overkill to go OOP on this, but I want to learn this stuff!

Thanks for your advice and help!

Here's some of what I'm got so far (pseudo code):

    import ConfigParser
    import Logging
    import os

    class ParseConf(object):
        [set up logging facilities]
        def __init__(self,conffile = "/etc/pybackup/pyback.conf"):
            self.conffile = conffile
            self.confvals = {}

        def getdirs():
            create configparser instance
            read config file
            add values to dictionary self.confvals

        def getdirobj():
            either create a list of objects here or send self.confvals
            to a factory object that returns a list of objects.              
  • 2
    First rule of developing in production: Don't do it. Second rule of developing in production: Set up a sandbox where, if you accidentally an entire critical directory, it's **okay**. As for the specific problem, my mind goes towards bash and using the system to do much of the work - are you sure you want to use Python for this? – Makoto May 02 '13 at 19:16
  • Personally, would use a factory (if there are a lot of config parameters). Handling the configuration loading inside your class sounds bad. If you only have a few config parameters, just pass them to the constructor though. – Aleph May 02 '13 at 19:21
  • 2
    @Makoto Heh, I'm inexperienced at programming, not system administration. While the script will be run as a production script, I'm developing it in a test environment. Again, I want to learn object oriented programming. Could I use Bash? Yes. Would it teach me object oriented programming, and be as easy to add functions to as it grows? No. – Herald Storm May 02 '13 at 19:27
  • @AnotherTest Thanks. There aren't a great deal of config params at this point, but I have a feeling as this gets put out to the user base, feature requests will inevitably come in, and I'll need to add to the configuration parameters. Even now, there are more things that would need to be passed as parameters than I'm comfortable with. – Herald Storm May 02 '13 at 19:29
  • 1
    Can you pass a `dict` of parameters? – Fred Foo May 02 '13 at 19:31
  • @larsmans Great idea! I'll definitely implement that, thanks. But I'm still back to my original question, do I create a method to create the directory objects inside the class I'm parsing the config file with, or do I create a separate class to create the directory objects and pass the parsed data to that? I'll need a way of looping through the objects, too, but I'm thinking of using the id attribute. – Herald Storm May 02 '13 at 19:34
  • 2
    @HeraldStorm: I'd pass the dict to `__init__` and let that handle as much as possible. Factories and other OO contraptions are rarely needed in small programs and distract from the actual program logic. But it's hard to tell without seeing some (pseudo-)code. – Fred Foo May 02 '13 at 19:55
  • OK for the object iteration, I'll probably do something like Amber mentions here[link](http://stackoverflow.com/a/4726554/2344412). As far as pseudo code, I'll try and put what I've got so far into the original question. – Herald Storm May 02 '13 at 20:00

0 Answers0