From c8aa617c8e80013d792b70fd256fcb5370a67687 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 29 Oct 2015 04:29:33 +0100 Subject: [PATCH 1/7] Locale codes with country names are also accepted E.g. 'en' and 'en_US' define the american english language --- patacrep/latex/__init__.py | 84 +++++++++++++++++++++--- test/test_compilation/subdir.tex.control | 1 + 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/patacrep/latex/__init__.py b/patacrep/latex/__init__.py index d01cfbe5..f863def0 100644 --- a/patacrep/latex/__init__.py +++ b/patacrep/latex/__init__.py @@ -13,19 +13,87 @@ from patacrep.latex.syntax import tex2plain, parse_song LOGGER = logging.getLogger(__name__) BABEL_LANGUAGES = OrderedDict(( - ('fr', 'french'), - ('en', 'english'), - ('de', 'german'), - ('es', 'spanish'), - ('it', 'italian'), - ('pt', 'portuguese'), + ('de_de', 'german'), + ('de_at', 'austrian'), + ('eo_uy', 'esperanto'), + ('en_us', 'english'), # USenglish, american + ('en_gb', 'british'), # UKenglish + ('en_ca', 'canadian'), + ('en_au', 'australian'), + ('en_nz', 'newzealand'), + ('es_es', 'spanish'), + ('fr_fr', 'french'), # francais + ('fr_ca', 'canadien'), + ('it_it', 'italian'), + ('la_LA', 'latin'), + ('pt_pt', 'portuguese'), # portuges + ('pt_br', 'brazilian'), # brazil + + ## Remaining Babel language codes + # ('??_??', 'afrikaans'), + # ('??_??', 'bahasa'), + # ('??_??', 'indonesian'), + # ('??_??', 'indon'), + # ('??_??', 'bahasai'), + # ('??_??', 'bahasam'), + # ('??_??', 'malay'), + # ('??_??', 'melayu'), + # ('??_??', 'basque'), + # ('??_??', 'breton'), + # ('??_??', 'bulgarian'), + # ('??_??', 'catalan'), + # ('??_??', 'croatian'), + # ('??_??', 'czech'), + # ('??_??', 'danish'), + # ('??_??', 'dutch'), + # ('??_??', 'estonian'), + # ('??_??', 'finnish'), + # ('??_??', 'acadian'), + # ('??_??', 'galician'), + # ('??_??', 'germanb'), + # ('??_??', 'ngerman'), + # ('??_??', 'naustrian'), + # ('??_??', 'greek'), + # ('??_??', 'polutonikogreek'), + # ('??_??', 'hebrew'), + # ('??_??', 'icelandic'), + # ('??_??', 'interlingua'), + # ('??_??', 'irish'), + # ('??_??', 'lowersorbian'), + # ('??_??', 'samin'), + # ('??_??', 'norsk'), + # ('??_??', 'nynorsk'), + # ('??_??', 'polish'), + # ('??_??', 'romanian'), + # ('??_??', 'russian'), + # ('??_??', 'scottish'), + # ('??_??', 'slovak'), + # ('??_??', 'slovene'), + # ('??_??', 'swedish'), + # ('??_??', 'serbian'), + # ('??_??', 'turkish'), + # ('??_??', 'ukrainian'), + # ('??_??', 'uppersorbian'), + # ('??_??', 'welsh'), )) def lang2babel(lang): """Return the language used by babel, corresponding to the language code""" try: - return BABEL_LANGUAGES[lang] + # Exact match + if lang.lower() in BABEL_LANGUAGES: + return BABEL_LANGUAGES[lang.lower()] + # Only language code is provided (e.g. 'fr') + for babel in BABEL_LANGUAGES: + if babel.startswith(lang.lower()): + return BABEL_LANGUAGES[babel] + # A non existent country code is provided (e.g. 'fr_CD'). + language = lang.lower().split("_")[0] + for babel in BABEL_LANGUAGES: + if babel.startswith(language): + LOGGER.error("Unknown country code '{}'. Using default '{}' instead.".format(lang, babel)) + return BABEL_LANGUAGES[babel] except KeyError: available = ", ".join(BABEL_LANGUAGES.keys()) - LOGGER.error('Unknown lang code: ' + lang + '. Supported: ' + available) + LOGGER.error("Unknown language code '{}' (supported: {}). Using default 'english' instead.".format(lang, available)) return 'english' diff --git a/test/test_compilation/subdir.tex.control b/test/test_compilation/subdir.tex.control index bd9ac649..7c816f7e 100644 --- a/test/test_compilation/subdir.tex.control +++ b/test/test_compilation/subdir.tex.control @@ -34,6 +34,7 @@ guitar, \usepackage{lmodern} +\PassOptionsToPackage{english}{babel} \PassOptionsToPackage{english}{babel} \usepackage[english]{babel} \lang{english} From 0731ab973d2c0fbd0d622c00ed2b8035499f714c Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 29 Oct 2015 04:36:46 +0100 Subject: [PATCH 2/7] pylint --- patacrep/latex/__init__.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/patacrep/latex/__init__.py b/patacrep/latex/__init__.py index f863def0..78251f4e 100644 --- a/patacrep/latex/__init__.py +++ b/patacrep/latex/__init__.py @@ -91,9 +91,19 @@ def lang2babel(lang): language = lang.lower().split("_")[0] for babel in BABEL_LANGUAGES: if babel.startswith(language): - LOGGER.error("Unknown country code '{}'. Using default '{}' instead.".format(lang, babel)) + LOGGER.error( + "Unknown country code '{}'. Using default '{}' instead.".format( + lang, + babel + ) + ) return BABEL_LANGUAGES[babel] except KeyError: available = ", ".join(BABEL_LANGUAGES.keys()) - LOGGER.error("Unknown language code '{}' (supported: {}). Using default 'english' instead.".format(lang, available)) + LOGGER.error( + "Unknown language code '{}' (supported: {}). Using default 'english' instead.".format( + lang, + available + ) + ) return 'english' From b2b80ef172aaba152d42c19854c19c12a63561f8 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 29 Oct 2015 09:20:30 +0100 Subject: [PATCH 3/7] Exeption was never raised --- patacrep/latex/__init__.py | 49 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/patacrep/latex/__init__.py b/patacrep/latex/__init__.py index 78251f4e..d596abae 100644 --- a/patacrep/latex/__init__.py +++ b/patacrep/latex/__init__.py @@ -79,31 +79,30 @@ BABEL_LANGUAGES = OrderedDict(( def lang2babel(lang): """Return the language used by babel, corresponding to the language code""" - try: - # Exact match - if lang.lower() in BABEL_LANGUAGES: - return BABEL_LANGUAGES[lang.lower()] - # Only language code is provided (e.g. 'fr') - for babel in BABEL_LANGUAGES: - if babel.startswith(lang.lower()): - return BABEL_LANGUAGES[babel] - # A non existent country code is provided (e.g. 'fr_CD'). - language = lang.lower().split("_")[0] - for babel in BABEL_LANGUAGES: - if babel.startswith(language): - LOGGER.error( - "Unknown country code '{}'. Using default '{}' instead.".format( - lang, - babel - ) + # Exact match + if lang.lower() in BABEL_LANGUAGES: + return BABEL_LANGUAGES[lang.lower()] + # Only language code is provided (e.g. 'fr') + for babel in BABEL_LANGUAGES: + if babel.startswith(lang.lower()): + return BABEL_LANGUAGES[babel] + # A non existent country code is provided (e.g. 'fr_CD'). + language = lang.lower().split("_")[0] + for babel in BABEL_LANGUAGES: + if babel.startswith(language): + LOGGER.error( + "Unknown country code '{}'. Using default '{}' instead.".format( + lang, + babel ) - return BABEL_LANGUAGES[babel] - except KeyError: - available = ", ".join(BABEL_LANGUAGES.keys()) - LOGGER.error( - "Unknown language code '{}' (supported: {}). Using default 'english' instead.".format( - lang, - available ) + return BABEL_LANGUAGES[babel] + # Error: no (exact or approximate) match found + available = ", ".join(BABEL_LANGUAGES.keys()) + LOGGER.error( + "Unknown language code '{}' (supported: {}). Using default 'english' instead.".format( + lang, + available ) - return 'english' + ) + return 'english' From eff9c749b24b88cc685c5a3aea585c47c845daf7 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 29 Oct 2015 14:02:49 +0100 Subject: [PATCH 4/7] [test] Add tests for language selection --- test/test_compilation/languages.sb | 3 + test/test_compilation/languages.tex.control | 195 ++++++++++++++++++ .../languages_datadir/songs/language.sgc | 4 + .../songs/language_location.sgc | 4 + .../languages_datadir/songs/no_language.sgc | 3 + .../songs/wrong_language.sgc | 4 + .../songs/wrong_location.sgc | 4 + 7 files changed, 217 insertions(+) create mode 100644 test/test_compilation/languages.sb create mode 100644 test/test_compilation/languages.tex.control create mode 100644 test/test_compilation/languages_datadir/songs/language.sgc create mode 100644 test/test_compilation/languages_datadir/songs/language_location.sgc create mode 100644 test/test_compilation/languages_datadir/songs/no_language.sgc create mode 100644 test/test_compilation/languages_datadir/songs/wrong_language.sgc create mode 100644 test/test_compilation/languages_datadir/songs/wrong_location.sgc diff --git a/test/test_compilation/languages.sb b/test/test_compilation/languages.sb new file mode 100644 index 00000000..e0c55f92 --- /dev/null +++ b/test/test_compilation/languages.sb @@ -0,0 +1,3 @@ +{ +"datadir": ["languages_datadir"] +} diff --git a/test/test_compilation/languages.tex.control b/test/test_compilation/languages.tex.control new file mode 100644 index 00000000..ae172dae --- /dev/null +++ b/test/test_compilation/languages.tex.control @@ -0,0 +1,195 @@ + + + + + + +%% Automatically generated document. +%% You may edit this file but all changes will be overwritten. +%% If you want to change this document, have a look at +%% the templating system. +%% +%% Generated using Songbook + +\makeatletter +\def\input@path{ % + {/home/louis/projets/patacrep/patacrep/test/test_compilation/languages_datadir/latex/} % + {/home/louis/projets/patacrep/patacrep/test/test_compilation/latex/} % + {/home/louis/projets/patacrep/patacrep/patacrep/data/latex/} % +} +\makeatother + +\documentclass[ + ]{article} + +\usepackage[ + chorded, +diagram, +pictures, +guitar, + ]{patacrep} + +\usepackage[utf8]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{lmodern} + + +\PassOptionsToPackage{english}{babel} +\PassOptionsToPackage{spanish}{babel} +\PassOptionsToPackage{french}{babel} +\PassOptionsToPackage{english}{babel} +\usepackage[english]{babel} +\lang{english} + +\usepackage{graphicx} +\graphicspath{ % + {/home/louis/projets/patacrep/patacrep/test/test_compilation/languages_datadir/} % + {/home/louis/projets/patacrep/patacrep/test/test_compilation/} % + {/home/louis/projets/patacrep/patacrep/patacrep/data/} % +} + + +\makeatletter +\@ifpackageloaded{hyperref}{}{ + \usepackage{url} + \newcommand{\phantomsection}{} + \newcommand{\hyperlink}[2]{#2} + \newcommand{\href}[2]{\expandafter\url\expandafter{#1}} +} +\makeatother + + +\usepackage{chords} + +\title{Guitar songbook} +\author{The Patacrep Team} + +\newindex{titleidx}{languages_title} +\newauthorindex{authidx}{languages_auth} + + +\notenamesout{A}{B}{C}{D}{E}{F}{G} + + +\begin{document} + +\maketitle + + +\showindex{\songindexname}{titleidx} +\showindex{\authorindexname}{authidx} + +% list of chords +\ifchorded + \ifdiagram + \phantomsection + \addcontentsline{toc}{section}{\chordlistname} + \chords + \fi +\fi + +\phantomsection +\addcontentsline{toc}{section}{\songlistname} + +\begin{songs}{titleidx,authidx} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% songs/./language.sgc + +\selectlanguage{english} + +\beginsong{Language only}[ + by={ + }, +] + + + + +\begin{verse} + Foo +\end{verse} + +\endsong + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% songs/./language_location.sgc + +\selectlanguage{french} + +\beginsong{Full language specification}[ + by={ + }, +] + + + + +\begin{verse} + Foo +\end{verse} + +\endsong + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% songs/./no_language.sgc + +\selectlanguage{english} + +\beginsong{No language}[ + by={ + }, +] + + + + +\begin{verse} + Foo +\end{verse} + +\endsong + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% songs/./wrong_language.sgc + +\selectlanguage{english} + +\beginsong{Wrong language}[ + by={ + }, +] + + + + +\begin{verse} + Foo +\end{verse} + +\endsong + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% songs/./wrong_location.sgc + +\selectlanguage{spanish} + +\beginsong{Wrong location}[ + by={ + }, +] + + + + +\begin{verse} + Foo +\end{verse} + +\endsong + +\end{songs} + + + + +\end{document} \ No newline at end of file diff --git a/test/test_compilation/languages_datadir/songs/language.sgc b/test/test_compilation/languages_datadir/songs/language.sgc new file mode 100644 index 00000000..88dc3b23 --- /dev/null +++ b/test/test_compilation/languages_datadir/songs/language.sgc @@ -0,0 +1,4 @@ +{title : Language only} +{lang: en} + +Foo diff --git a/test/test_compilation/languages_datadir/songs/language_location.sgc b/test/test_compilation/languages_datadir/songs/language_location.sgc new file mode 100644 index 00000000..93fa6b52 --- /dev/null +++ b/test/test_compilation/languages_datadir/songs/language_location.sgc @@ -0,0 +1,4 @@ +{title : Full language specification} +{lang: fr_FR} + +Foo diff --git a/test/test_compilation/languages_datadir/songs/no_language.sgc b/test/test_compilation/languages_datadir/songs/no_language.sgc new file mode 100644 index 00000000..1f996c6d --- /dev/null +++ b/test/test_compilation/languages_datadir/songs/no_language.sgc @@ -0,0 +1,3 @@ +{title : No language} + +Foo diff --git a/test/test_compilation/languages_datadir/songs/wrong_language.sgc b/test/test_compilation/languages_datadir/songs/wrong_language.sgc new file mode 100644 index 00000000..a196c14f --- /dev/null +++ b/test/test_compilation/languages_datadir/songs/wrong_language.sgc @@ -0,0 +1,4 @@ +{title : Wrong language} +{lang: xx_XX} + +Foo diff --git a/test/test_compilation/languages_datadir/songs/wrong_location.sgc b/test/test_compilation/languages_datadir/songs/wrong_location.sgc new file mode 100644 index 00000000..c0c3d9e9 --- /dev/null +++ b/test/test_compilation/languages_datadir/songs/wrong_location.sgc @@ -0,0 +1,4 @@ +{title : Wrong location} +{lang: es_XX} + +Foo From ca012bfa1b18439ac6f0a644dbedcd14ad0c7151 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 29 Oct 2015 14:18:25 +0100 Subject: [PATCH 5/7] [test] Fix test path --- test/test_compilation/languages.tex.control | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/test_compilation/languages.tex.control b/test/test_compilation/languages.tex.control index ae172dae..770b40bb 100644 --- a/test/test_compilation/languages.tex.control +++ b/test/test_compilation/languages.tex.control @@ -13,9 +13,9 @@ \makeatletter \def\input@path{ % - {/home/louis/projets/patacrep/patacrep/test/test_compilation/languages_datadir/latex/} % - {/home/louis/projets/patacrep/patacrep/test/test_compilation/latex/} % - {/home/louis/projets/patacrep/patacrep/patacrep/data/latex/} % + {@TEST_FOLDER@/languages_datadir/latex/} % + {@TEST_FOLDER@/latex/} % + {@DATA_FOLDER@/latex/} % } \makeatother @@ -43,9 +43,9 @@ guitar, \usepackage{graphicx} \graphicspath{ % - {/home/louis/projets/patacrep/patacrep/test/test_compilation/languages_datadir/} % - {/home/louis/projets/patacrep/patacrep/test/test_compilation/} % - {/home/louis/projets/patacrep/patacrep/patacrep/data/} % + {@TEST_FOLDER@/languages_datadir/} % + {@TEST_FOLDER@/} % + {@DATA_FOLDER@/} % } @@ -192,4 +192,4 @@ guitar, -\end{document} \ No newline at end of file +\end{document} From 446b79154176da077b9ae34446bedad059fdab77 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 30 Oct 2015 22:22:07 +0100 Subject: [PATCH 6/7] Fix tests --- test/test_compilation/datadir.tex.control | 1 - test/test_compilation/languages.tex.control | 2 -- 2 files changed, 3 deletions(-) diff --git a/test/test_compilation/datadir.tex.control b/test/test_compilation/datadir.tex.control index 4f6c8521..90c8d34b 100644 --- a/test/test_compilation/datadir.tex.control +++ b/test/test_compilation/datadir.tex.control @@ -32,7 +32,6 @@ guitar, \usepackage{lmodern} -\PassOptionsToPackage{english}{babel} \PassOptionsToPackage{english}{babel} \usepackage[english]{babel} \lang{english} diff --git a/test/test_compilation/languages.tex.control b/test/test_compilation/languages.tex.control index 770b40bb..59c233a7 100644 --- a/test/test_compilation/languages.tex.control +++ b/test/test_compilation/languages.tex.control @@ -29,8 +29,6 @@ pictures, guitar, ]{patacrep} -\usepackage[utf8]{inputenc} -\usepackage[T1]{fontenc} \usepackage{lmodern} From e4228beafca488f6c0de88474e3123f5171a1e50 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 30 Oct 2015 22:29:17 +0100 Subject: [PATCH 7/7] Fix tests --- test/test_compilation/datadir.tex.control | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_compilation/datadir.tex.control b/test/test_compilation/datadir.tex.control index 90c8d34b..4f6c8521 100644 --- a/test/test_compilation/datadir.tex.control +++ b/test/test_compilation/datadir.tex.control @@ -32,6 +32,7 @@ guitar, \usepackage{lmodern} +\PassOptionsToPackage{english}{babel} \PassOptionsToPackage{english}{babel} \usepackage[english]{babel} \lang{english}