Skip to content

Commit

Permalink
Make this into a real package and prepare for publish on PyPi (#1)
Browse files Browse the repository at this point in the history
* preparation stuff for publishing on pypi

* follow some example on https://python-packaging.readthedocs.io/en/latest/command-line-scripts.html

* setup with a command line installer

* update the readme and cleanup code to work in module form

* simplify without named args

* handle weekends and weekdays and nicer readme

* Update README.md

* Remove codecov

* cleanup for flake
  • Loading branch information
skhg authored Jun 24, 2020
1 parent 9d9bff1 commit 4340148
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 74 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/branch-builds.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
on:
push:
branches-ignore:
- 'release*'
pull_request:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [2.7, 3.6, 3.7, 3.8]

steps:
- name: "Checkout code"
uses: actions/checkout@v2

- name: "Set up Python ${{ matrix.python-version }}"
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: "Install dependencies"
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install .
pip install flake8
pip install radon
- name: "Run flake8"
run: |
flake8 --ignore=E501
- name: "Run radon"
run: |
radon cc ./py_markdown_diary/ -a
radon mi .
radon raw . -s
42 changes: 42 additions & 0 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
on:
release:
types: [published]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6]

steps:
- name: "Checkout code"
uses: actions/checkout@v2

- name: "Set up Python ${{ matrix.python-version }}"
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: "Replace version string with release number"
run: |
sed -i "s/0.0.0/${VERSION_REF##*/}/g" setup.py
env:
VERSION_REF: ${{ github.ref }}

- name: "Install dependencies"
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install .
pip install twine
- name: "PyPi release"
run: |
python setup.py sdist
twine check dist/*
twine upload dist/*
env:
TWINE_USERNAME: ${{ secrets.TWINE_USERNAME }}
TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD }}

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ venv.bak/
.mypy_cache/

#IntelliJ
.idea/
.idea/
*.iml
13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Jack Higgins
Copyright (c) 2020 Jack Higgins

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
33 changes: 25 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
# diary
# markdown-diary

[![Build Status](https://travis-ci.org/skhg/diary.svg?branch=master)](https://travis-ci.org/skhg/diary)
![PyPI](https://img.shields.io/pypi/v/markdown-diary)

Markdown diary file generator
Diary file template generator, producing text files in [Markdown](https://en.wikipedia.org/wiki/Markdown) format.

## How to run
I use this personally to keep a simple monthly diary of my notes and activities. Since it just produces a series of text files, they can be stored in something like Dropbox, and synced everywhere easily.

`python diary.py YYYY-MM`
![Screenshot of typical usage](screenshot.png)

e.g. `python diary.py 2019-01`
## Installation
`pip install markdown-diary`

This produces a markdown file named `01 January.md` in the current directory, formatted for daily diary entries, with day and week numbers.
## How to use

`pip install` adds the command `md-diary` to your `/bin` PATH so you can run it directly from the command line.

Run `md-diary` for help.

### For a single month
Run `md-diary YYYY-mm` (e.g. `md-diary 2019-01`) which will create a file called `01 January.md`

The format looks like:

Expand Down Expand Up @@ -41,7 +49,16 @@ The format looks like:
### Tue 8
...
```

### For a whole year

Like above, but run `md-diary YYYY` (e.g. `md-diary 20202`) which will create the files `01 January.md`, `02 February.md`, `...` in the current directory.

### Options
Use the `-w` flag to output weekdays only.

## Dependencies

* python 2 or 3
* Python 2.7 or 3.x
51 changes: 0 additions & 51 deletions diary/diary.py

This file was deleted.

71 changes: 71 additions & 0 deletions py_markdown_diary/Diary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from datetime import datetime, date, timedelta
import calendar
import os


class Diary(object):

@staticmethod
def valid_month(month_str):
try:
return datetime.strptime(month_str, '%Y-%m')
except ValueError:
return None

@staticmethod
def valid_year(year_str):
try:
return datetime.strptime(year_str, '%Y')
except ValueError:
return None

@staticmethod
def write_month_file(month, filename, weekdays_only):
days_in_month = calendar.monthrange(month.year, month.month)[1]

with open(filename, 'w') as open_file:
open_file.writelines('# ' + month.strftime('%B') + '\n')
open_file.writelines('\n')

current_week = 0

for d in range(0, days_in_month):
today_date = month + timedelta(days=d)

temp_week = int(today_date.strftime('%W')) + 1

if current_week != temp_week:
current_week = temp_week
open_file.writelines('\n')
open_file.writelines('\n')
open_file.writelines('## Week ' + str(current_week) + '\n')
open_file.writelines('\n')

day_of_week = int(today_date.strftime('%w'))

is_weekend = day_of_week == 0 or day_of_week == 6

if not is_weekend or (is_weekend and not weekdays_only):
open_file.writelines('### ' + today_date.strftime('%a') + ' ' + today_date.strftime('%-d') + '\n')
open_file.writelines('\n')
open_file.writelines('\n')

@staticmethod
def create_month_file(month, weekdays_only):
filename = str(month.strftime('%m')) + ' ' + str(month.strftime('%B')) + '.md'

if not Diary.file_exists(filename):
Diary.write_month_file(month, filename, weekdays_only)

@staticmethod
def create_year_files(year, weekdays_only):
for m in range(1, 13):
Diary.create_month_file(date(year.year, m, 1), weekdays_only)

@staticmethod
def file_exists(filename):
if os.path.exists(filename):
print('Note: Diary \'' + filename + '\' already exists. Will not be modified.')
return True

return False
29 changes: 29 additions & 0 deletions py_markdown_diary/command_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python

import argparse
import sys
from . import Diary


def main():
parser = argparse.ArgumentParser(description="Create markdown diary file templates.")
parser.add_argument("when", help="Generate a diary file for the given month or year. e.g. 2018 or 2018-12",
type=str)
parser.add_argument("-w", "--weekdays", help="Only include weekdays in the produced file(s). (Excludes weekends).", action="store_true")

if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)

args = parser.parse_args()

month = Diary.Diary.valid_month(args.when)
year = Diary.Diary.valid_year(args.when)
if month:
Diary.Diary.create_month_file(month, args.weekdays)
elif year:
Diary.Diary.create_year_files(year, args.weekdays)
else:
print("Error: The value " + args.when + " is not a valid year/month combination")
parser.print_help(sys.stderr)
sys.exit(1)
Empty file added requirements.txt
Empty file.
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python

from setuptools import setup
from os import path

this_directory = path.abspath(path.dirname(__file__))
with open("README.md", "r") as fh:
long_description = fh.read()

setup(
name='markdown-diary',
version='0.0.0',
description="CLI tool to generate monthly diary files in simple Markdown format",
long_description=long_description,
long_description_content_type='text/markdown',
keywords='diary journal markdown cli',
author='Jack Higgins',
author_email='pypi@jackhiggins.ie',
url='https://github.com/skhg/markdown-diary',
packages=['py_markdown_diary'],
entry_points={
'console_scripts': ['md-diary=py_markdown_diary.command_line:main'],
},
install_requires=[
],
tests_require=[
],
license='MIT',
classifiers=[
'Development Status :: 4 - Beta',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.6'])

0 comments on commit 4340148

Please sign in to comment.