def load(self, input_directory, languages): for lc in zip(*languages)[0]: if os.path.exists(os.path.join(input_directory, lc, self.name + '.po')): out = os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po') makedirs(os.path.dirname(out)) copy_f(os.path.join(input_directory, lc, self.name + '.po'), out) self.compile()
def load(self, input_directory, languages): for lc in zip(*languages)[0]: if os.path.exists( os.path.join(input_directory, lc, self.name + '.po')): out = os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po') makedirs(os.path.dirname(out)) copy_f(os.path.join(input_directory, lc, self.name + '.po'), out) self.compile()
def download_pictures(self, remote_gallery_url): gallery_path = self.gallery_path() # delete previous files, so we don't include old files in ebooks if os.path.isdir(gallery_path): for filename in os.listdir(gallery_path): file_path = os.path.join(gallery_path, filename) os.unlink(file_path) ilustr_elements = list(self.wldocument().edoc.findall('//ilustr')) if ilustr_elements: makedirs(gallery_path) for ilustr in ilustr_elements: ilustr_src = ilustr.get('src') ilustr_path = os.path.join(gallery_path, ilustr_src) urllib.urlretrieve('%s/%s' % (remote_gallery_url, ilustr_src), ilustr_path)
def render_to_csv(output_path, template, context=None, add_files=None): """Renders a TeXML document into a PDF file. :param str output_path: is where the PDF file should go :param str template: is a TeXML template path :param context: is context for rendering the template :param dict add_files: a dictionary of additional files XeTeX will need """ from django.template.loader import render_to_string makedirs(os.path.dirname(output_path)) rendered = render_to_string(template, context) with open(output_path, 'w') as csv_file: csv_file.write(rendered.encode('utf-8'))
def render_to_pdf(output_path, template, context=None, add_files=None): """Renders a TeXML document into a PDF file. :param str output_path: is where the PDF file should go :param str template: is a TeXML template path :param context: is context for rendering the template :param dict add_files: a dictionary of additional files XeTeX will need """ from StringIO import StringIO import shutil from tempfile import mkdtemp import subprocess import Texml.processor from django.template.loader import render_to_string rendered = render_to_string(template, context) texml = StringIO(rendered.encode('utf-8')) tempdir = mkdtemp(prefix="render_to_pdf-") tex_path = os.path.join(tempdir, "doc.tex") with open(tex_path, 'w') as tex_file: Texml.processor.process(texml, tex_file, encoding="utf-8") if add_files: for add_name, src_file in add_files.items(): add_path = os.path.join(tempdir, add_name) if hasattr(src_file, "read"): with open(add_path, 'w') as add_file: add_file.write(add_file.read()) else: shutil.copy(src_file, add_path) cwd = os.getcwd() os.chdir(tempdir) try: subprocess.check_call( ['xelatex', '-interaction=batchmode', tex_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) makedirs(os.path.dirname(output_path)) shutil.move(os.path.join(tempdir, "doc.pdf"), output_path) finally: os.chdir(cwd) shutil.rmtree(tempdir)
def render_to_pdf(output_path, template, context=None, add_files=None): """Renders a TeXML document into a PDF file. :param str output_path: is where the PDF file should go :param str template: is a TeXML template path :param context: is context for rendering the template :param dict add_files: a dictionary of additional files XeTeX will need """ from StringIO import StringIO import shutil from tempfile import mkdtemp import subprocess import Texml.processor from django.template.loader import render_to_string rendered = render_to_string(template, context) texml = StringIO(rendered.encode('utf-8')) tempdir = mkdtemp(prefix="render_to_pdf-") tex_path = os.path.join(tempdir, "doc.tex") with open(tex_path, 'w') as tex_file: Texml.processor.process(texml, tex_file, encoding="utf-8") if add_files: for add_name, src_file in add_files.items(): add_path = os.path.join(tempdir, add_name) if hasattr(src_file, "read"): with open(add_path, 'w') as add_file: add_file.write(add_file.read()) else: shutil.copy(src_file, add_path) cwd = os.getcwd() os.chdir(tempdir) try: subprocess.check_call(['xelatex', '-interaction=batchmode', tex_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) makedirs(os.path.dirname(output_path)) shutil.move(os.path.join(tempdir, "doc.pdf"), output_path) finally: os.chdir(cwd) shutil.rmtree(tempdir)
def init_db(last_checked): makedirs(MOBILE_INIT_DB) db = sqlite3.connect( os.path.join(MOBILE_INIT_DB, 'initial.db-%d' % last_checked)) schema = """ CREATE TABLE book ( id INTEGER PRIMARY KEY, title VARCHAR, cover VARCHAR, html_file VARCHAR, html_file_size INTEGER, parent INTEGER, parent_number INTEGER, sort_key VARCHAR, pretty_size VARCHAR, authors VARCHAR, _local BOOLEAN ); CREATE INDEX IF NOT EXISTS book_title_index ON book (sort_key); CREATE INDEX IF NOT EXISTS book_title_index ON book (title); CREATE INDEX IF NOT EXISTS book_parent_index ON book (parent); CREATE TABLE tag ( id INTEGER PRIMARY KEY, name VARCHAR, category VARCHAR, sort_key VARCHAR, books VARCHAR); CREATE INDEX IF NOT EXISTS tag_name_index ON tag (name); CREATE INDEX IF NOT EXISTS tag_category_index ON tag (category); CREATE INDEX IF NOT EXISTS tag_sort_key_index ON tag (sort_key); CREATE TABLE state (last_checked INTEGER); """ db.executescript(schema) db.execute("INSERT INTO state VALUES (:last_checked)", {'last_checked': last_checked}) return db
def init_db(last_checked): makedirs(MOBILE_INIT_DB) db = sqlite3.connect(os.path.join(MOBILE_INIT_DB, 'initial.db-%d' % last_checked)) schema = """ CREATE TABLE book ( id INTEGER PRIMARY KEY, title VARCHAR, cover VARCHAR, html_file VARCHAR, html_file_size INTEGER, parent INTEGER, parent_number INTEGER, sort_key VARCHAR, pretty_size VARCHAR, authors VARCHAR, _local BOOLEAN ); CREATE INDEX IF NOT EXISTS book_title_index ON book (sort_key); CREATE INDEX IF NOT EXISTS book_title_index ON book (title); CREATE INDEX IF NOT EXISTS book_parent_index ON book (parent); CREATE TABLE tag ( id INTEGER PRIMARY KEY, name VARCHAR, category VARCHAR, sort_key VARCHAR, books VARCHAR); CREATE INDEX IF NOT EXISTS tag_name_index ON tag (name); CREATE INDEX IF NOT EXISTS tag_category_index ON tag (category); CREATE INDEX IF NOT EXISTS tag_sort_key_index ON tag (sort_key); CREATE TABLE state (last_checked INTEGER); """ db.executescript(schema) db.execute("INSERT INTO state VALUES (:last_checked)", {'last_checked': last_checked}) return db
def copy_f(frm, to): makedirs(os.path.dirname(to)) shutil.copyfile(frm, to)
def __init__(self, book_id, revision=None): makedirs(os.path.join(settings.SEARCH_INDEX, self.SNIPPET_DIR)) self.book_id = book_id self.revision = revision self.file = None self.position = None
def handle(self, **options): makedirs(settings.VAR_DIR) update_counters()
def handle(self, appname, **options): if not options['poname']: options['poname'] = appname app = __import__(appname) if options['load']: objects = {} modmod = {} for md, opts in self.get_models(app): if md.__name__ not in objects: objects[md.__name__] = {} modmod['model'] = md languages = get_languages(options['lang']) for lng in zip(*languages)[0]: pofile = os.path.join(options['directory'], lng, options['poname'] + '.po') if not os.path.exists(pofile): if options['keep_running']: continue else: raise OSError('%s po file: %s not found' % (appname, pofile)) po = polib.pofile(pofile) for entry in po: loc, _ignored = entry.occurrences[0] _appname, modelname, fieldname, pk = loc.split('/') try: obj = objects[modelname][pk] except KeyError: obj = modmod['model'].objects.get(pk=pk) objects[modelname][pk] = obj setattr(obj, fieldname, entry.msgstr) for mod, objcs in objects.items(): for o in objcs.values(): o.save() else: pofiles = {} for md, opts in self.get_models(app): for obj in md.objects.all().order_by('pk'): for fld, locflds in opts.local_fields.items(): k = getattr(obj, '%s_%s' % (fld, settings.LANGUAGE_CODE)) or '' for locfld in locflds: cur_lang = locfld.language try: po = pofiles[cur_lang] except KeyError: po = make_po(cur_lang) pofiles[cur_lang] = po v = locfld.value_from_object(obj) or '' entry = polib.POEntry( msgid=k, msgstr=v, occurrences=[('%s/%s/%s/%s' % (appname, md.__name__, locfld.name, str(obj.pk)), 0)]) po.append(entry) directory = options['directory'] for lng, po in pofiles.items(): makedirs(os.path.join(directory, lng)) po.save( os.path.join(directory, lng, '%s.po' % options['poname']))
def handle(self, appname, **options): if not options['poname']: options['poname'] = appname app = __import__(appname) if options['load']: objects = {} modmod = {} for md, opts in self.get_models(app): if md.__name__ not in objects: objects[md.__name__] = {} modmod['model'] = md languages = get_languages(options['lang']) for lng in zip(*languages)[0]: pofile = os.path.join(options['directory'], lng, options['poname'] + '.po') if not os.path.exists(pofile): if options['keep_running']: continue else: raise OSError('%s po file: %s not found' % (appname, pofile)) po = polib.pofile(pofile) for entry in po: loc, _ignored = entry.occurrences[0] _appname, modelname, fieldname, pk = loc.split('/') try: obj = objects[modelname][pk] except KeyError: obj = modmod['model'].objects.get(pk=pk) objects[modelname][pk] = obj setattr(obj, fieldname, entry.msgstr) for mod, objcs in objects.items(): for o in objcs.values(): o.save() else: pofiles = {} for md, opts in self.get_models(app): for obj in md.objects.all().order_by('pk'): for fld, locflds in opts.local_fields.items(): k = getattr(obj, '%s_%s' % (fld, settings.LANGUAGE_CODE)) or '' for locfld in locflds: cur_lang = locfld.language try: po = pofiles[cur_lang] except KeyError: po = make_po(cur_lang) pofiles[cur_lang] = po v = locfld.value_from_object(obj) or '' entry = polib.POEntry( msgid=k, msgstr=v, occurrences=[('%s/%s/%s/%s' % (appname, md.__name__, locfld.name, str(obj.pk)), 0)]) po.append(entry) directory = options['directory'] for lng, po in pofiles.items(): makedirs(os.path.join(directory, lng)) po.save(os.path.join(directory, lng, '%s.po' % options['poname']))