Changeset 43:b086c7d68163 in stamper


Ignore:
Timestamp:
Aug 14, 2014, 9:16:27 AM (8 years ago)
Author:
Borja Lopez <borja@…>
Branch:
default
Phase:
public
Message:

Put charts into ~/.workstamps-charts by default, ensuring the directory
exists when we create an instance of Stamper.

Cleaned up imports a bit (first imports of std library modules, then
external dependencies)

Use os.path.join to generate the paths to the chart files (instead of
manually generating them concatenating strings)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • stamper/stamper.py

    r40 r43  
    22import json
    33import re
    4 import pygal
     4import os.path
    55from datetime import datetime, date, timedelta
    6 from os.path import expanduser, exists, islink, isdir
    7 from os import symlink, remove
     6from os import symlink, remove, makedirs
    87from collections import OrderedDict
    98from operator import itemgetter
    10 
    11 
    12 STAMPS_FILE = expanduser('~/.workstamps.json')
     9import pygal
     10
     11
     12STAMPS_FILE = os.path.expanduser('~/.workstamps.json')
     13CHARTS_DIR = os.path.expanduser('~/.workstamps-charts')
    1314DATE_FORMAT = '%Y-%m-%d'
    1415TIME_FORMAT = '%H:%M:%S'
     
    2021class Stamper(object):
    2122
    22     def __init__(self, stamps_file=STAMPS_FILE):
    23         self.stamps_file = STAMPS_FILE
     23    def __init__(self, stamps_file=STAMPS_FILE, charts_dir=CHARTS_DIR):
     24        self.stamps_file = stamps_file
     25        self.charts_dir = charts_dir
    2426        self.ensure_stamps_file()
     27        self.ensure_charts_dir()
    2528        self.stamps = []
    2629
     
    4649
    4750    def ensure_stamps_file(self):
    48         if not exists(self.stamps_file):
     51        if not os.path.exists(self.stamps_file):
    4952            with open(self.stamps_file, 'w') as stamps_file:
    5053                stamps_file.write('')
     54
     55    def ensure_charts_dir(self):
     56        if not os.path.exists(self.charts_dir):
     57            makedirs(self.charts_dir)
    5158
    5259    def load_stamps(self):
     
    304311            '%Y-%m-%d_%H%M%S')
    305312        chart_symlink = 'chart-latest.svg'
    306         chart.render_to_file('graphs/' + chart_name)
    307         if islink('graphs/'+ chart_symlink):
    308             remove('graphs/'+ chart_symlink)
    309         symlink(chart_name, 'graphs/'+ chart_symlink)
     313        chart_path = os.path.join(self.charts_dir, chart_name)
     314        chart_symlink_path = os.path.join(self.charts_dir, chart_symlink)
     315
     316        chart.render_to_file(chart_path)
     317        print('Rendered chart: ' + chart_path)
     318        if os.path.islink(chart_symlink_path):
     319            remove(chart_symlink_path)
     320        symlink(chart_name, chart_symlink_path)
     321        print('Updated latest chart: ' + chart_symlink_path)
    310322
    311323    def show_stamps(self, customer=None, stamp_filter=None, verbose=False,
     
    399411        merging them into the list (removing duplicated entries)
    400412        """
    401         if not exists(filename):
     413        if not os.path.exists(filename):
    402414            print('[error] ' + filename + 'does not exist')
    403415            return
    404         if isdir(filename):
     416        if os.path.isdir(filename):
    405417            print('[error] ' + filename + 'is a directory')
    406418            return
Note: See TracChangeset for help on using the changeset viewer.