← Back to team overview

oerppy-hackers team mailing list archive

[Branch ~oerppy-hackers/oerppy/trunk] Rev 30: * Moved MonthlyExport export functionality from run method into new export

 

------------------------------------------------------------
revno: 30
committer: duncan@xxxxxxxxxx
branch nick: trunk
timestamp: Thu 2011-06-02 10:51:35 -0600
message:
  * Moved MonthlyExport export functionality from run method into new export
    method.
  * Changed private methods to public ones in the addon query class.
  * Added support for caching calls to get_options.
  * Added an addon-specific const module.
  * Updated various methods to use constants instead of raw strings (easier to
    maintain, test, keep track of, etc.).
added:
  oerppy/addons/canonical/const.py
modified:
  bin/export_month_timesheets
  oerppy/addons/canonical/query.py
  oerppy/addons/canonical/scripting/export.py
  oerppy/const.py
  oerppy/export.py
  oerppy/scripting/base.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 'bin/export_month_timesheets'
--- bin/export_month_timesheets	2011-06-02 15:59:48 +0000
+++ bin/export_month_timesheets	2011-06-02 16:51:35 +0000
@@ -13,5 +13,5 @@
 # setup any addons that we want
 registry.add("canonical", query.CanonicalAddOns)
 # run the exporter
-monthly_export = export.MonthlyExports(config_data)
+monthly_export = export.MonthlyExport(config_data)
 monthly_export.run()

=== added file 'oerppy/addons/canonical/const.py'
--- oerppy/addons/canonical/const.py	1970-01-01 00:00:00 +0000
+++ oerppy/addons/canonical/const.py	2011-06-02 16:51:35 +0000
@@ -0,0 +1,9 @@
+from oerppy.const import *
+
+
+TIMESHEET_HOURS = "Timesheet Hours"
+TIMESHEETS_WEEKLY = "Weekly Timesheets"
+TIMESHEET_VIEWS = [
+    TIMESHEET_HOURS,
+    TIMESHEETS_WEEKLY,
+    ]

=== modified file 'oerppy/addons/canonical/query.py'
--- oerppy/addons/canonical/query.py	2011-06-02 15:49:02 +0000
+++ oerppy/addons/canonical/query.py	2011-06-02 16:51:35 +0000
@@ -1,4 +1,4 @@
-from oerppy import const
+from oerppy.addons.canonical import const
 
 
 class CanonicalAddOns(object):
@@ -14,22 +14,21 @@
         self.user_dept = None
 
     def views(self):
-        return const.VIEWS
+        return const.TIMESHEET_VIEWS
 
-    # XXX this isn't a query method so much as a dispatch one; maybe it should
-    # be renamed?
-    # XXX on second thought... this is an addon and needs some sort of
-    # well-defined, user-sensical method name for running queries :-)
-    def query(self, report, domain):
-        """
-        Run the query and return the grid.
-        """
+    def query(self, report, domains=None):
+        """
+        A dispatch method that runs the query for the given report name, and
+        optionally, domains.
+        """
+        if not domain:
+            domain = []
         if not self.user_dept:
-            self._get_departments()
-        if report == "Timesheet Hours":
-            records = self._timesheet_hours(domain)
-        elif report == "Weekly Timesheets":
-            records = self._weekly_timesheets(domain)
+            self.get_departments()
+        if report == const.TIMESHEET_HOURS:
+            records = self.timesheet_hours(domains)
+        elif report == const.TIMESHEETS_WEEKLY:
+            records = self.weekly_timesheets(domains)
         else:
             records = []
         return records
@@ -42,8 +41,7 @@
     #   3) convert the parsed data into a model instance
     # then, of course, one is able to work with the data quite easily
 
-    # XXX let's make this public
-    def _get_departments(self):
+    def get_departments(self):
         """
         Build a lookup dictionary of department vs user_id.
         """
@@ -65,8 +63,7 @@
 
         self.user_dept = user_dept
 
-    # XXX let's make this public
-    def _timesheet_hours(self, domain):
+    def timesheet_hours(self, domain):
         recs = self.client.searchfields(
             "project.task.work", 
             domain, 
@@ -87,8 +84,7 @@
             records.append(line)
         return records
 
-    # XXX let's make this public
-    def _weekly_timesheets(self, domain):
+    def weekly_timesheets(self, domain):
         """
         Get the weekly timeentry records, highlighting the missing weeks.
         """

=== modified file 'oerppy/addons/canonical/scripting/export.py'
--- oerppy/addons/canonical/scripting/export.py	2011-06-02 15:59:48 +0000
+++ oerppy/addons/canonical/scripting/export.py	2011-06-02 16:51:35 +0000
@@ -1,6 +1,6 @@
 from datetime import datetime
 
-from oerppy import config, scripting, service
+from oerppy import config, export, scripting, service
 
 
 class MonthlyExport(scripting.ExportScript):
@@ -12,7 +12,7 @@
         Options for monthly exports.
         """
         now = datetime.now()
-        parser = super(Monthly, self).get_option_parser()
+        parser = super(MonthlyExport, self).get_option_parser()
         parser.add_option(
             "-m", "--month", dest="month", action="store", type="int",
             default=now.month,
@@ -35,6 +35,21 @@
         # currently done (the executable setting the name associated with the
         # addon when it's added to the registry).
 
+    def export(self, data):
+        options = self.get_options()
+        # create the filehandle
+        if options.output_filename == const.STDOUT_FILENAME:
+            fd = sys.stdout
+        else:
+            fd = open(options.output_filename, "w+")
+        # dispatch the appropriate export based on the format option
+        if options.export_format == const.EXPORT_RAW:
+            fd.write(data)
+        elif options.export_format == const.EXPORT_CSV:
+            export.export_csv(data, fd)
+        #elif options.export_format == const.EXPORT_GOOGLE_DOCS:
+        #    export.export_google(user, pass, data, ..., ?)
+
     def run(self):
         data = self.service.get_monthly_timesheets(self.month, self.year)
-        export.export_csv(data, self.output_filename)
+        self.export(data)

=== modified file 'oerppy/const.py'
--- oerppy/const.py	2011-06-01 08:25:27 +0000
+++ oerppy/const.py	2011-06-02 16:51:35 +0000
@@ -1,10 +1,10 @@
+STDOUT_FILENAME = "stdout"
+EXPORT_RAW = "raw"
+EXPORT_CSV = "csv"
+EXPORT_GOOGLE_DOCS = "google"
 CONFIG_FILE = "oerppy.yml"
 LOCAL_CONFIG_TEMPLATE = "./etc/%s"
 SAMPLE_CONFIG = LOCAL_CONFIG_TEMPLATE % "oerppy-sample.yml"
 CONFIG_TEMPLATE = LOCAL_CONFIG_TEMPLATE % CONFIG_FILE
 CONFIG_DIR = "~/.oerppy"
 DEFAULT_CONFIG = "%s/%s" % (CONFIG_DIR, CONFIG_FILE)
-VIEWS = [
-    'Timesheet Hours',
-    'Weekly Timesheets',
-    ]

=== modified file 'oerppy/export.py'
--- oerppy/export.py	2011-06-01 16:42:38 +0000
+++ oerppy/export.py	2011-06-02 16:51:35 +0000
@@ -1,17 +1,13 @@
-def export_csv(self, service, data=None, filename):
+def export_csv(self, data, file_handle):
     if not data:
         return
-
     # Export the records to a CSV file
-    file_handle = open(filename, 'wt')
     writer = csv.DictWriter(file_handle, data)
-
     # Create the header row
     header = {}
     for key in data.keys():
         header[key] = key
     writer.writerow(header)
-
     # Output the records
     for row in data:
         writer.writerow(row)

=== modified file 'oerppy/scripting/base.py'
--- oerppy/scripting/base.py	2011-06-02 15:59:48 +0000
+++ oerppy/scripting/base.py	2011-06-02 16:51:35 +0000
@@ -1,11 +1,14 @@
 from optparse import OptionParser
 
+from oerppy import const
+
 
 class Script(object):
     """
     The common code for all scripting classes.
     """
     def __init__(self, config_data):
+        self.options = None
         self.service = None
         self.set_service(config_data)
 
@@ -33,13 +36,16 @@
         description = self.__doc__
         return "%s\n%s" % (usage, description)
 
-    def get_options(self):
+    def get_options(self, purge=False):
+        if self.options and purge == False:
+            return self.options
         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
+        self.options = options
+        return self.options
 
 
 class ExportScript(Script):
@@ -54,10 +60,10 @@
         parser = super(ExportScript, self).get_option_parser()
         parser.add_option(
             "-o", "--output-file", dest="output_filename", action="store",
-            default="stdout",
+            default=const.STDOUT_FILENAME,
             help="the file to write the export to")
         parser.add_option(
             "-f", "--format", dest="export_format", action="store",
-            default="csv",
+            default=const.EXPORT_RAW,
             help="the format to export the data in")
         return parser