"Fossies" - the Fresh Open Source Software Archive

Member "veusz-3.1/veusz/utils/feedback.py" (1 Mar 2018, 6020 Bytes) of package /linux/privat/veusz-3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "feedback.py" see the Fossies "Dox" file reference documentation.

    1 #    Copyright (C) 2018 Jeremy S. Sanders
    2 #    Email: Jeremy Sanders <jeremy@jeremysanders.net>
    3 #
    4 #    This program is free software; you can redistribute it and/or modify
    5 #    it under the terms of the GNU General Public License as published by
    6 #    the Free Software Foundation; either version 2 of the License, or
    7 #    (at your option) any later version.
    8 #
    9 #    This program is distributed in the hope that it will be useful,
   10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12 #    GNU General Public License for more details.
   13 #
   14 #    You should have received a copy of the GNU General Public License along
   15 #    with this program; if not, write to the Free Software Foundation, Inc.,
   16 #    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   17 ###############################################################################
   18 
   19 from __future__ import division, absolute_import, print_function
   20 from collections import defaultdict
   21 import datetime
   22 import sys
   23 import atexit
   24 import platform
   25 
   26 import sip
   27 import numpy as N
   28 from .. import qtall as qt
   29 
   30 from .utilfuncs import rrepr
   31 from .version import version
   32 from ..compat import citems, curlrequest, curlencode
   33 
   34 """Feedback module for providing information about usage.
   35 
   36 Note: careful not to send a unique identifier and to reset counts to
   37 ensure lack of traceability.
   38 """
   39 
   40 # patch this to disable any feedback
   41 disableFeedback=False
   42 
   43 # for QSettings
   44 _org='veusz.org'
   45 _app='veusz-feedback'
   46 _url='https://barmag.net/veusz-feedback/'
   47 
   48 # min send interval in days
   49 _mininterval = 7
   50 # min interval to try sending in days
   51 _minattemptinterval = 1
   52 
   53 class Feedback:
   54     """Keep track of number of activities."""
   55 
   56     def __init__(self):
   57         # counts of widget creation
   58         self.widgetcts = defaultdict(int)
   59         # counts of data import
   60         self.importcts = defaultdict(int)
   61         # counts of data export
   62         self.exportcts = defaultdict(int)
   63 
   64 # singleton
   65 feedback = Feedback()
   66 
   67 @atexit.register
   68 def updatects():
   69     """Add saved counts with values from app."""
   70     #print("running updates")
   71     setn = qt.QSettings(_org, _app)
   72 
   73     # get statistics and reset in config file
   74     widgetcts = eval(setn.value('counts/widget', '{}'))
   75     importcts = eval(setn.value('counts/import', '{}'))
   76     exportcts = eval(setn.value('counts/export', '{}'))
   77 
   78     # add existing counts
   79     for k, v in citems(feedback.widgetcts):
   80         widgetcts[k] = widgetcts.get(k, 0) + v
   81     for k, v in citems(feedback.importcts):
   82         importcts[k] = importcts.get(k, 0) + v
   83     for k, v in citems(feedback.exportcts):
   84         exportcts[k] = exportcts.get(k, 0) + v
   85 
   86     setn.setValue('counts/widget', rrepr(widgetcts))
   87     setn.setValue('counts/import', rrepr(importcts))
   88     setn.setValue('counts/export', rrepr(exportcts))
   89 
   90 class FeedbackCheckThread(qt.QThread):
   91     """Async thread to send feedback."""
   92 
   93     def run(self):
   94         from ..setting import settingdb
   95 
   96         # exit if disabled
   97         if (settingdb['feedback_disabled'] or
   98             disableFeedback or
   99             not settingdb['feedback_asked_user']):
  100             #print('disabled')
  101             return
  102 
  103         setn = qt.QSettings(_org, _app)
  104 
  105         # keep track of when we successfully sent the data (lastsent)
  106         # and when we last tried (lastattempt), so we don't send too
  107         # often
  108 
  109         today = datetime.date.today()
  110         today_tpl = (today.year, today.month, today.day)
  111 
  112         # don't try to send too often
  113         lastattempt = setn.value('last-attempt', '(2000,1,1)')
  114         lastattempt = datetime.date(*eval(lastattempt))
  115         delta_attempt = (today-lastattempt).days
  116         if delta_attempt<_minattemptinterval:
  117             #print("too soon 1")
  118             return
  119 
  120         lastsent = setn.value('last-sent')
  121         if not lastsent:
  122             delta_sent = -1
  123         else:
  124             lastsent = datetime.date(*eval(lastsent))
  125             delta_sent = (today-lastsent).days
  126 
  127             # are we within the send period
  128             if delta_sent<_mininterval:
  129                 #print("too soon 2")
  130                 return
  131 
  132         # avoid accessing url too often by updating date first
  133         setn.setValue('last-attempt', repr(today_tpl))
  134 
  135         # get statistics and reset in config file
  136         widgetcts = setn.value('counts/widget', '{}')
  137         importcts = setn.value('counts/import', '{}')
  138         exportcts = setn.value('counts/export', '{}')
  139 
  140         try:
  141             winver = str(sys.getwindowsversion())
  142         except Exception:
  143             winver = 'N/A'
  144 
  145         # construct post message - these are the data sent to the
  146         # remote server
  147         args = {
  148             'interval': str(delta_sent),
  149             'veusz-version': version(),
  150             'python-version': sys.version,
  151             'python-version_info': repr(tuple(sys.version_info)),
  152             'python-platform': sys.platform,
  153             'platform-machine': platform.machine(),
  154             'windows-version': winver,
  155             'numpy-version': N.__version__,
  156             'qt-version': qt.qVersion(),
  157             'pyqt-version': qt.PYQT_VERSION_STR,
  158             'sip-version': sip.SIP_VERSION_STR,
  159             'locale': qt.QLocale().name(),
  160             'widgetcts': widgetcts,
  161             'importcts': importcts,
  162             'exportcts': exportcts,
  163         }
  164         postdata = curlencode(args).encode('utf8')
  165 
  166         # now post the data
  167         try:
  168             f = curlrequest.urlopen(_url, postdata)
  169             retn = f.readline().decode('utf8').strip()
  170             f.close()
  171 
  172             if retn == 'ok':
  173                 #print("success")
  174                 # reset in stats file and set date last done
  175                 setn.setValue('counts/widget', '{}')
  176                 setn.setValue('counts/import', '{}')
  177                 setn.setValue('counts/export', '{}')
  178                 setn.setValue('last-sent', repr(today_tpl))
  179 
  180         except Exception as e:
  181             #print("failure",e)
  182             pass