Debian Codegraph @

src/timeago/locales/import_locale.py

raw symbols SCIP
1 # coding=utf-8
2 import codecs
3 import glob
4 import json
5
6 from git import Repo
7 import os
8
9 import shutil
10 import re
11
12 from subprocess import call
13
14 # Note that you have to specify path to script
15
16 dest = './jsrepo'
17
18 # Clean dest
19 try:
20 shutil.rmtree(dest)
21 except:
22 pass
23
24 if not os.access(dest, os.F_OK):
25 os.mkdir(dest)
26
27 Repo.clone_from('https://github.com/hustcc/timeago.js.git', dest)
28
29 for filename in glob.glob(os.path.join(dest, 'locales/*.js')):
30 with open(filename) as f:
31 content = f.readlines()
32 # We're doing just the basic syntax and we're not trying to understand locale.js which is the index file
33 if len(content) > 18 or 'locales.js' in filename:
34 print('Seems that %s is a bit too complex for this parser' % filename)
35 continue
36 print('Generating %s\'s JSON Object...' % filename)
37 call(["node", "convert_local_to_json.js", filename])
38
39 override = False
40
41 for filename in glob.glob('./tmp/*.json'):
42 wrong = False
43 pure_filename = filename.split('/')[-1:][0][:-5]
44 if not override:
45 for already_existing in glob.glob('./*.py'):
46 if already_existing[2:-3] == pure_filename:
47 wrong = True
48 break
49 if wrong:
50 continue
51 with open(filename) as g:
52 json_value = json.loads(g.read())
53
54 generated_placeholder = []
55
56 for x in json_value['statements']:
57 generated_placeholder.append(x[0])
58 generated_placeholder.append(x[1])
59
60 txt = \
61 """#!/usr/bin/env python
62 # -*- coding: utf-8 -*-
63 '''
64 Created on 2017-8-30
65
66 @author: generated by @lolobosse script
67 '''
68 LOCALE = [
69 ["%s", "%s"],
70 ["%s", "%s"],
71 ["%s", "%s"],
72 ["%s", "%s"],
73 ["%s", "%s"],
74 ["%s", "%s"],
75 ["%s", "%s"],
76 ["%s", "%s"],
77 ["%s", "%s"],
78 ["%s", "%s"],
79 ["%s", "%s"],
80 ["%s", "%s"],
81 ["%s", "%s"],
82 ["%s", "%s"]
83 ]
84 """ % tuple(generated_placeholder)
85 with codecs.open(pure_filename+'.py', 'w+', "utf-8") as h:
86 h.write(txt)
87 h.close()
88
89
90 # Clean
91 shutil.rmtree('./tmp')
92 shutil.rmtree(dest)
93