Commit Diff


commit - 6e628374ed0a7c02cc852259000378f87e0388fb
commit + 8601dd7572e963321f1db6c475ca80c1f88262d1
blob - f36ef641004ea4e75c1f5386b690da9062156a3b
blob + 0309192ca9f3ddcd6776590a1be8696e6d96ef4e
--- .gitignore
+++ .gitignore
@@ -1 +1,2 @@
-static/times/*.json
+static/times/
+static/video/
blob - 0a271073ec0721a116c971eebb93bb99e14ae77a
blob + 3d6fdf18c7ecda9f2efaee03ae0ebd3ba6a69f0a
--- CHANGELOG.md
+++ CHANGELOG.md
@@ -9,6 +9,7 @@ change log follows the conventions of
 ### Added
 
 - Option for generating video files.
+- Option for setting a language in a build script.
 
 ### Fixed
 
blob - bb0cf7c6e2170550ec6d12988447560f8a08c0a5
blob + 481d3366e948452d5664d00f00f4d1bb12eacd96
--- Makefile
+++ Makefile
@@ -8,10 +8,14 @@ PREFIX ?= /usr
 PREFIX_MAN = ${PREFIX}/share/man
 PREFIX_DATA = ${PREFIX}/local/share
 
-QUOTES ?= $(PROJECT_DIR)/quotes/quotes_en.csv
-TARGET_DIR ?= static/times/
-JSON_DATA = ${TARGET_DIR}/*.json
+TARGET_DIR ?= static
+JSON_DIR = $(TARGET_DIR)/times
+VIDEO_DIR = $(TARGET_DIR)/video
 
+QUOTES_RU = $(PROJECT_DIR)/quotes/quotes_ru.csv
+QUOTES_EN = $(PROJECT_DIR)/quotes/quotes_en.csv
+QUOTES = ${QUOTES_RU} ${QUOTES_EN}
+
 LITCLOCK_SCRIPT = litclock
 LITCLOCK_MAN = litclock.1
 
@@ -19,18 +23,32 @@ all: build
 
 build: build_json build_video
 
-build_json: ${QUOTES}
-	@python build.py --filename $< --create-json --path ${TARGET_DIR}/times
+build_json: build_json_ru build_json_en
 
-build_video: ${QUOTES}
-	@python build.py --filename $< --create-video --path ${TARGET_DIR}/images
+build_json_ru: ${QUOTES_RU}
+	@python build.py --filename $< --language ru --create-json --path ${JSON_DIR}/ru
 
+build_json_en: ${QUOTES_EN}
+	@python build.py --filename $< --language en --create-json --path ${JSON_DIR}/en
+
+build_video: build_video_ru build_video_en
+
+build_video_ru: ${QUOTES_RU}
+	@python build.py --filename $< --language ru --create-video --path ${VIDEO_DIR}/ru
+
+build_video_en: ${QUOTES_EN}
+	@python build.py --filename $< --language en --create-video --path ${VIDEO_DIR}/en
+
 check: check-data check-man check-pep8
 
-check-data:
-	@python build.py --filename $(PROJECT_DIR)/quotes/quotes_ru.csv --dry-run --create-json --path ${TARGET_DIR}/times
-	@python build.py --filename $(PROJECT_DIR)/quotes/quotes_en.csv --dry-run --create-json --path ${TARGET_DIR}/times
+check-data: check_data_ru check_data_en
 
+check_data_ru: ${QUOTES_RU}
+	@python build.py --filename $< --language ru --dry-run --create-json
+
+check_data_en: ${QUOTES_EN}
+	@python build.py --filename $< --language en --dry-run --create-json
+
 check-man: ${LITCLOCK_MAN}
 	@mandoc -Tlint $< -W style
 
@@ -38,7 +56,7 @@ check-pep8: build.py
 	@autopep8 --diff --exit-code $<
 
 www: build_json
-	@python -m http.server 8000 --bind 127.0.0.1 -d static
+	@python -m http.server 8000 --bind 127.0.0.1 -d ${TARGET_DIR}
 
 install: ${LITCLOCK_SCRIPT} ${LITCLOCK_MAN} ${QUOTES}
 	install ${LITCLOCK_SCRIPT} ${PREFIX}/local/bin/${LITCLOCK_SCRIPT}
@@ -47,6 +65,9 @@ install: ${LITCLOCK_SCRIPT} ${LITCLOCK_MAN} ${QUOTES}
 	install -m 644 ${QUOTES} ${PREFIX_DATA}/litclock
 
 clean:
-	@rm -f ${JSON_DATA}
+	@rm -rf ${VIDEO_DIR}/* ${JSON_DIR}/*
 
-.PHONY: build build_json build_video check check-data check-man check-pep8 install www clean
+.PHONY: build build_json build_json_ru build_json_en
+.PHONY: build_video build_video_ru build_video_en
+.PHONY: check check-data check_data_ru check_data_en check-man check-pep8
+.PHONY: install www clean
blob - e90a830bb1816020a6f56e619f1ac01bbceafe81
blob + 46fe8fed42f00556f351acfe5dcdd0fc32d28554
--- build.py
+++ build.py
@@ -11,25 +11,30 @@ import textwrap
 from PIL import Image, ImageDraw, ImageFont
 import cv2
 
-DEFAULT_QUOTE_RU = {
-    "quote_first": "Счастливые часов не наблюдают.",
-    "quote_time_case": "",
-    "quote_last": "",
-    "title": "Горе от ума",
-    "author": "А. Грибоедов",
-    "sfw": "yes"
+DEFAULT_QUOTE = {
+    "ru":  [
+        {
+            "quote_first": "Счастливые часов не наблюдают.",
+            "quote_time_case": "",
+            "quote_last": "",
+            "title": "Горе от ума",
+            "author": "А. Грибоедов",
+            "sfw": "yes"
+        }
+    ],
+    "en": [
+        {
+            "quote_first": "Who knows, in happiness, how time is flying?",
+            "quote_time_case": "",
+            "quote_last": "",
+            "title": "Woe From Wit",
+            "author": "Alexander Griboyedov",
+            "sfw": "yes"
+        }
+    ]
 }
 
-DEFAULT_QUOTE_EN = {
-    "quote_first": "Who knows, in happiness, how time is flying?",
-    "quote_time_case": "",
-    "quote_last": "",
-    "title": "Woe From Wit",
-    "author": "Alexander Griboyedov",
-    "sfw": "yes"
-}
 
-
 def complement_number(num):
     if num < 10:
         return "0{}".format(num)
@@ -95,13 +100,19 @@ def generate_image(txt_quote, txt_title, txt_author, i
     img.save(image_name)
 
 
-def complement_placeholders(times):
+def random_default_quote(lang):
+    quotes_list = DEFAULT_QUOTE.get(lang)
+    return random.choice(quotes_list)
+
+
+def complement_placeholders(times, lang):
     times_with_placeholders = times.copy()
     for hours, minutes in iter_daytime():
         time_str = "{}:{}".format(hours, minutes)
         quotes_list = times_with_placeholders.get(time_str)
         if not quotes_list:
-            placeholder = DEFAULT_QUOTE_RU.copy()
+            default_quote = random_default_quote(lang)
+            placeholder = default_quote.copy()
             placeholder["time"] = time_str
             placeholder["quote_first"] = "{}: {}".format(
                 time_str, placeholder["quote_first"])
@@ -183,8 +194,11 @@ def build_dict(quote_filename, verbose=False):
 def parse_args():
     parser = argparse.ArgumentParser(prog="build_data")
     parser.add_argument("--filename",
-                        type=str,
-                        required=True)
+                        type=str)
+    parser.add_argument("-l", "--language",
+                        choices=["ru", "en"],
+                        required=True,
+                        type=str)
     parser.add_argument("-p", "--path",
                         type=str)
     parser.add_argument("-d", "--dry-run",
@@ -215,13 +229,24 @@ def build_video(image_dir, video_name):
 
     cv2.destroyAllWindows()
     video.release()
-    print("File {}.".format(result_file))
+    print("File {}".format(result_file))
 
 
 def main():
     inputs = parse_args()
-    times = build_dict(inputs.filename, inputs.verbose)
-    times_with_placeholders = complement_placeholders(times)
+    filename = inputs.filename
+    if not filename and inputs.language:
+        filename = "quotes/quotes_{}.csv".format(inputs.language)
+
+    if not os.path.isfile(filename):
+        print("File {} is not found".format(filename))
+        sys.exit(1)
+
+    if not inputs.dry_run and not os.path.isdir(inputs.path):
+        os.makedirs(inputs.path)
+
+    times = build_dict(filename, inputs.verbose)
+    times_with_placeholders = complement_placeholders(times, inputs.language)
     # Write JSON files.
     if not inputs.dry_run and inputs.create_json:
         write_files(times_with_placeholders, inputs.path)
@@ -232,7 +257,7 @@ def main():
         build_video(inputs.path, "litclock.avi")
 
     perc_covered = round(len(times)/(60 * 24) * 100)
-    print("File with quotes: {}".format(inputs.filename))
+    print("File with quotes: {}".format(filename))
     msg_fmt = "Number of quotes with unique time: {} ({}%)"
     print(msg_fmt.format(len(times), perc_covered))