← Back to team overview

oerppy-hackers team mailing list archive

[Branch ~oerppy-hackers/oerppy/trunk] Rev 22: Added highly extendible options support to all scripting classes.

 

------------------------------------------------------------
revno: 22
committer: duncan@xxxxxxxxxx
branch nick: trunk
timestamp: Wed 2011-06-01 12:49:40 -0600
message:
  Added highly extendible options support to all scripting classes.
modified:
  Makefile
  bin/export_month_timesheets
  oerppy/scripting/base.py
  oerppy/scripting/export.py
  oerppy/util.py


--
lp:oerppy
https://code.launchpad.net/~oerppy-hackers/oerppy/trunk

Your team oerppy Hackers is subscribed to branch lp:oerppy.
To unsubscribe from this branch go to https://code.launchpad.net/~oerppy-hackers/oerppy/trunk/+edit-subscription
=== modified file 'Makefile'
--- Makefile	2011-06-01 06:12:47 +0000
+++ Makefile	2011-06-01 18:49:40 +0000
@@ -2,6 +2,7 @@
 	find ./ -name "*~" -exec rm {} \;
 	find ./ -name "*.pyc" -exec rm {} \;
 	find ./ -name "*.pyo" -exec rm {} \;
+	find . -name "*.sw[op]" -exec rm {} \;
 
 check:
 	trial oerppy

=== modified file 'bin/export_month_timesheets'
--- bin/export_month_timesheets	2011-06-01 16:58:57 +0000
+++ bin/export_month_timesheets	2011-06-01 18:49:40 +0000
@@ -3,8 +3,8 @@
 from oerppy.scripting import export
 
 
+# get configuration; option parsing is handled by the script class(es)
 config_data = get_config_data()
-# XXX get/parse options
-options = None
-monthly_export = export.Monthly(config_data, options)
+# run the exporter
+monthly_export = export.Monthly(config_data)
 monthly_export.run()

=== modified file 'oerppy/scripting/base.py'
--- oerppy/scripting/base.py	2011-06-01 16:58:57 +0000
+++ oerppy/scripting/base.py	2011-06-01 18:49:40 +0000
@@ -1,4 +1,32 @@
+from optparse import OptionParser
+
+
 class Script(object):
     """
     The common code for all scripting classes.
     """
+    def get_option_parser(self):
+        """
+        Options common to all scripts.
+        """
+        parser = OptionParser(usage=self.get_usage())
+        parser.add_option(
+            "-H", "--host", dest="host", action="store",
+            help="the host to perform the config lookup for")
+        parser.add_option(
+            "-D", "--database", dest="dbname", action="store",
+            help="the db name to perform the config lookup for")
+        return parser
+
+    def get_usage(self):
+        usage = "Usage: %prog [options]"
+        description = self.__doc__
+        return "%s\n%s" % (usage, description)
+
+    def get_options(self):
+        parser = self.get_option_parser()
+        (options, args) = parser.parse_args()
+        if None in [options.host, options.dbname]:
+            parser.error("Options -H and -D are both required. "
+                         "For more information, use --help.")
+        return options

=== modified file 'oerppy/scripting/export.py'
--- oerppy/scripting/export.py	2011-06-01 16:58:57 +0000
+++ oerppy/scripting/export.py	2011-06-01 18:49:40 +0000
@@ -1,28 +1,63 @@
+from datetime import datetime
+
 from oerppy import config, scripting, service
 
 
-class Monthly(scripting.Script):
-    """
-    """
-    def __init__(self, config_data, options):
-        self.set_service(config_data, options)
+class ExportScript(scripting.Script):
+    """
+    """
+    def get_option_parser(self):
+        """
+        Options common to all export classes.
+        """
+        parser = super(ExportScript, self).get_option_parser()
+        parser.add_option(
+            "-o", "--output-file", dest="output_filename", action="store",
+            default="stdout",
+            help="the file to write the export to")
+        parser.add_option(
+            "-f", "--format", dest="export_format", action="store",
+            default="csv",
+            help="the format to export the data in")
+        return parser
+
+
+class Monthly(ExportScript):
+    """
+    Generate an export of a given month's timesheet data.
+    """
+    def __init__(self, config_data):
+        self.set_service(config_data)
         # XXX get the month for the timesheet from the options
-        self.month = None
+        self.month = options.month
         # XXX get the year for the timesheet from the options
-        self.year = None
-        # XXX get the output file frome the options
-        self.output_filename = ""
-
-    def set_service(self, config_data, options):
-        # XXX get host from options
-        host = ""
-        # XXX get dbname from options
-        dbname = ""
-        server_config = config.get_server(host, dbname)
+        self.year = options.year
+        # XXX get the output file from the options
+        self.output_filename = options.output_filename
+
+    def get_option_parser(self):
+        """
+        Options common to all export classes.
+        """
+        now = datetime.now()
+        parser = super(Monthly, self).get_option_parser()
+        parser.add_option(
+            "-m", "--month", dest="month", action="store", type="int",
+            default=now.month,
+            help="the month to export")
+        parser.add_option(
+            "-y", "--year", dest="year", action="store", type="int",
+            default=now.year,
+            help="the year to export")
+        return parser
+
+    def set_service(self, config_data):
+        options = self.get_options()
+        server_config = config.get_server(options.host, options.dbname)
         url = server_config.get("url")
-        user = server_config.get("user")
+        user = server_config.get("username")
         password = server_config.get("password")
-        self.service = service.Service(url, dbname, user, password)
+        self.service = service.Service(url, options.dbname, user, password)
 
     def run(self):
         data = self.service.get_monthly_timesheets(self.month, self.year)

=== modified file 'oerppy/util.py'
--- oerppy/util.py	2011-06-01 07:46:26 +0000
+++ oerppy/util.py	2011-06-01 18:49:40 +0000
@@ -24,6 +24,7 @@
     dest = open(dest_file, "w+")
     dest.write(data)
     dest.close()
+    # XXX set the perms on it to 600
 
 
 def read_config(default_config="", dest_dir=""):