示例#1
0
def mainPage(request):
    session = get_current_session()
    access_token_key = session['access_token_key']
    access_token_secret = session['access_token_secret']
    oauth_verifier = request.GET.get('oauth_verifier', '')
    get_absolute_path(request)
    if not access_token_key:
        #params['test'] = reverse('sinaweibo.views.login', args=[], kwargs={})#, current_app=context.current_app)        
        login_url = reverse('sinaweibo.views.login', args=[], kwargs={})
        #return shortcuts.render_to_response('test.html', params)
        return http.HttpResponseRedirect(login_url)
    else:
        auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
        auth.setToken(access_token_key, access_token_secret)
        api = API(auth)
        
        #myself = api.get_user(id=1894001933)
        #screen_name = myself. __getattribute__('screen_name')
        myweibo = []
        myweibo_obj = api.user_timeline(count=20, page=1)
        for weibo in myweibo_obj:
            myweibo.append({'text': weibo.text, 
                            'created_at': weibo.created_at, 
                            'retweeted_status': hasattr(weibo, 'retweeted_status') and weibo.retweeted_status or None,
                            'source': weibo.source})
        wrapper__at(myweibo)
        params = {}
        
        params['user'] = api.verify_credentials()
        params['result'] = myweibo
        template = get_template_uri(appContext, 'weibo.html')
        return shortcuts.render_to_response(template, params)
def add_custom_collxml_to_variants_catalogs(collection_dir, result_dir, variants):
    collection_file = utils.get_absolute_path(collection_dir, utils.COLLECTION_XML)
    doCopyModules = 1 if collection_dir != result_dir else 0
    if os.path.isfile(collection_file):
        collection_file_tmp = collection_file + '-tmp'
        shutil.copyfile(collection_file, collection_file_tmp)
        strCmd = ['java', '-jar',
                  utils.SAXON_JAR,
                  '-s:' + collection_file_tmp,
                  '-xsl:' + utils.get_absolute_path(utils.DEFAULT_XSL_DIRECTORY, EPCOLLXML2VARIANTS_MODIFY_COLLXML),
                  'resultDir=' + result_dir.replace('\\', '/'),
                  'doCopyModules=' + str(doCopyModules),
                  'path_to_files_epxml_of_modules_in_col=' + os.path.abspath(collection_dir),
                  'variants=' + variants
        ]
        proc = subprocess.Popen(strCmd, stdout=subprocess.PIPE)
        proc.communicate()
        os.remove(collection_file_tmp)
        exit_code = proc.wait()
        if exit_code > 0:
            print "[PY_XSLT_ERR] Error in transformation process [error_code=%s]." % str(exit_code)
            return -1
        print "[collxml2variants] Transformation completed"
    else:
        print "[PY_ERR][collxml2variants] Error: could not find: " + collection_dir
def generate_mapping_file_mathml_digest(input_dir_with_epxhtml, output):
    print "[Py] \tProcessing module - create file with mapping digest<->mathml"
    MODULE_ID_XPATH = etree.XPath('//col:module/@document', namespaces=utils.NAMESPACES)

    try:
        collxml_file = utils.get_absolute_path(input_dir_with_epxhtml, 'collection.xml')

        if os.path.isfile(collxml_file):
            collxml_xml = etree.parse(collxml_file)
            if collxml_xml != "":
                if not os.path.exists(output):
                    os.makedirs(output)

                file_all_mathml_digest = open(os.path.join(output, 'mathml_digest.xml'), "w", encoding='utf-8')
                file_all_mathml_digest.write(unicode('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>', "utf-8"))
                file_all_mathml_digest.write(unicode("<root>", "utf-8"))

                number_of_modules = len(MODULE_ID_XPATH(collxml_xml))
                for module_position, module_id in enumerate(MODULE_ID_XPATH(collxml_xml)):
                    input_module_file = utils.get_absolute_path(input_dir_with_epxhtml, module_id+'/index.epxml')
                    if os.path.isfile(input_module_file):
                        modulexml = etree.parse(input_module_file)
                        if modulexml != "":
                            mathmls = modulexml.findall('//mml:math', namespaces=utils.NAMESPACES)
                            for math_position, mathml in enumerate(mathmls):
                                mathContent = copy(mathml)

                                math_content_value = re.sub('\s+', ' ', etree.tostring(mathContent, with_tail=False, encoding='UTF-8')).strip()

                                mmd5 = hashlib.md5()
                                mmd5.update(math_content_value)
                                mathDigest = mmd5.hexdigest()

                                alt_text = generate_alt_text(mathContent, mathDigest)

                                file_all_mathml_digest.write(u"<mathElement id=\"%s\">" % (mathDigest))
                                file_all_mathml_digest.write(unicode(math_content_value, "utf-8"))
                                file_all_mathml_digest.write(u"</mathElement>")

                                addMathElementAroundMathmlInModuleContent(mathml, mathDigest, alt_text)

                            if not os.path.exists(os.path.join(output,module_id)):
                                os.makedirs(os.path.join(output,module_id))
                            with open(os.path.join(output,module_id,'index.epxml'), 'w', encoding='utf-8') as f:
                                f.write(etree.tostring(modulexml, pretty_print=True, encoding='unicode'))

                file_all_mathml_digest.write(unicode("</root>", "utf-8"))
    except Exception as e:
        print "[PY_ERR] error (generate_mapping_file_mathml_digest) %s" % e
        sys.exit(1)
    finally:
        try:
            file_all_mathml_digest.close()
        except Exception as errorFin:
            print "[PY_ERR] error (generate_mapping_file_mathml_digest) in finally %s" % (errorFin)
            sys.exit(1)
def processTocXmlToJson(result_dir):
    try:
        f = open(utils.get_absolute_path(result_dir, 'toc.xml'), 'r', encoding='utf-8')
        tocData = f.read()
        f.close()

        tocData = tocData.replace('<!DOCTYPE HTML>\n', '').replace('&nbsp;', ' ')
        tree = etree.fromstring(tocData)

        f = open(os.path.join(result_dir, 'toc.json'), 'w', encoding='utf-8')
        f.write(unicode(json.dumps(toc_create_main_file(tree)['toc'], indent=4, encoding="utf-8")))
        f.close()

        f = open(os.path.join(result_dir, 'pages.json'), 'w', encoding='utf-8')
        f.write(unicode(json.dumps(toc_create_pages_file(tree)['pages'], indent=4, encoding="utf-8")))
        f.close()

        os.remove(utils.get_absolute_path(result_dir, 'toc.xml'))
    except Exception as e:
        print "[PY_ERR] error in fun 'processTocXmlToJson' - "+str(e)
        sys.exit(1)
示例#5
0
 def get_media_id(self):
     delta = datetime.datetime.utcnow() - self.get_update_time()
     if delta.days >= 3:
         url = self.appitem.get_weixin_upload_api('voice')
         if self.is_done and self.file_url:
             filename = get_absolute_path(self.file_url)
             dict_data = post_file_get_media_id(filename, url)
             if dict_data:
                 self.media_id = dict_data.get('media_id')
                 self.update_time_str = dict_data.get('created_at')
                 self.save()
     return self.media_id
示例#6
0
    def prepare_directories(self):
        relative_base_path = 'results'
        relative_user_path = f"{relative_base_path}/{self.user_setup.profile_name}"
        relative_scrapping_path = f"{relative_user_path}/{time.strftime('%Y.%m.%d-%H%M%S')}"
        relative_posts_path = f"{relative_scrapping_path}/posts"
        relative_tagged_posts_path = f"{relative_scrapping_path}/tagged-posts"
        relative_fetched_data_path = f"{relative_scrapping_path}/FETCHED_DATA"

        results_path = get_absolute_path(relative_base_path)
        user_path = get_absolute_path(relative_user_path)
        scrapping_path = get_absolute_path(relative_scrapping_path)
        posts_path = get_absolute_path(relative_posts_path)
        tagged_posts_path = get_absolute_path(relative_tagged_posts_path)
        fetched_data_path = get_absolute_path(relative_fetched_data_path)

        paths = {
            'results_path': results_path,
            'user_pathu': user_path,
            'scrapping_path': scrapping_path,
            'posts_path': posts_path,
            'tagged_posts_path': tagged_posts_path,
            'fetched_data_path': fetched_data_path,
        }
        self.__paths = paths
        return paths
示例#7
0
    def path_to_argument(self, key, value, gen_arg):
        res = 0

        if value == None:
            abs_path = os.getcwd()
        else:
            value = value.replace("\ ", " ")
            abs_path = utils.get_absolute_path(value)
        if os.path.exists(abs_path):
            gen_arg.add_path(key, str(abs_path))
        else:
            print "Value error: local path <", value, "> doesn't exist"
            res = -1
        return res
示例#8
0
    def path_to_argument(self, key, value, gen_arg):
        res = 0

        if value == None:
            abs_path = os.getcwd()
        else:
            value = value.replace("\ ", " ")
            abs_path = utils.get_absolute_path(value)
        if os.path.exists(abs_path):   
             gen_arg.add_path(key, str(abs_path))
        else:
            print "Value error: local path <", value, "> doesn't exist"
            res = -1
        return res
def transform_epxml_preprocessing(collection_dir, result_dir):
    collection_file = utils.get_absolute_path(collection_dir, utils.COLLECTION_XML)
    doCopyModules = 1 if collection_dir != result_dir else 0
    if os.path.isfile(collection_file):
        strCmd = ['java', '-jar',
                  utils.SAXON_JAR,
                  '-s:' + collection_file,
                  '-xsl:' + utils.get_absolute_path(utils.DEFAULT_XSL_DIRECTORY, EPXML_PREPROCESSING),
                  'resultDir=' + result_dir.replace('\\', '/'),
                  'doCopyModules=' + str(doCopyModules),
                  'path_to_files_epxml_of_modules_in_col=' + os.path.abspath(collection_dir)
        ]
        proc = subprocess.Popen(strCmd, stdout=subprocess.PIPE)
        proc.communicate()
        exit_code = proc.wait()
        if exit_code > 0:
            print "[PY_XSLT_ERR] Error in transformation process [error_code=%s]." % str(exit_code)
            return -1

        if not os.path.exists(os.path.join(result_dir, utils.COLLECTION_XML)):
            shutil.copy(collection_file, utils.get_absolute_path(result_dir, utils.COLLECTION_XML))

    else:
        print "[PY_EPXML_PREPROC] Error: could not find: " + collection_dir
def getCurrSubject(path_collxml):
    XPATH_STYLESHEET = etree.XPath('//col:metadata/ep:e-textbook/ep:stylesheet/text()', namespaces=utils.NAMESPACES)
    XPATH_SUBJECT = etree.XPath('//col:metadata/md:subjectlist/md:subject/text()', namespaces=utils.NAMESPACES)
    currSubjects=""

    collxml_file = utils.get_absolute_path(path_collxml, 'collection.xml')
    if os.path.isfile(collxml_file):
        collxml_xml = etree.parse(collxml_file)
        if collxml_xml != "":
            xpath_result = XPATH_SUBJECT(collxml_xml)
            if len(xpath_result) > 0:
                for i in xpath_result:
                    currSubjects += i.encode('UTF-8').lower()+','

    print "[py] currSubjects="+currSubjects
    return currSubjects
示例#11
0
    def before(self, request, args, kwargs):
        """Override this method to add common functionality to all HTTP method handlers.

        args and kwargs are passed as regular arguments so you can add/remove arguments:
            def before(self, request, args, kwargs):
                kwargs['article'] = get_object_or_404(Article, id=kwargs.pop('article_id')
            def GET(self, request, article): # <== 'article' instead of 'article_id'
                ...
            def post(delf, request, article): # <== 'article' instead of 'article_id'
                ...
        """
        self.request = request
        self.response = HttpResponse()
        self.response.headers = Setter(self.response)
        self.response.out = Writer(self.response)
        self.request.uri = get_absolute_path(request)
        self.request.str_cookies = request.COOKIES
        self.request.remote_addr = request.META["REMOTE_ADDR"]
        self.request.get_all = lambda x: request.REQUEST.getlist(x)
示例#12
0
    def path_to_argument(self, key, value, gen_arg):
        res = 0

        if value == None:
            abs_path = os.getcwd()
        else:
            value = value.replace("\ ", " ")
            abs_path = utils.get_absolute_path(value)
#XXX print ?
        #print abs_path
        if os.path.exists(abs_path):   
#            path = self.api.Path(str(abs_path))
#            path.thisown = False
#            gen_arg.add_path(key, path)
             gen_arg.add_path(key, str(abs_path))
        else:
            print "Value error: local path <", value, "> doesn't exist"
            res = -1
        return res
示例#13
0
    def before(self, request, args, kwargs):
        """Override this method to add common functionality to all HTTP method handlers.

        args and kwargs are passed as regular arguments so you can add/remove arguments:
            def before(self, request, args, kwargs):
                kwargs['article'] = get_object_or_404(Article, id=kwargs.pop('article_id')
            def GET(self, request, article): # <== 'article' instead of 'article_id'
                ...
            def post(delf, request, article): # <== 'article' instead of 'article_id'
                ...
        """
        self.request = request
        self.response = HttpResponse()
        self.response.headers = Setter(self.response)
        self.response.out = Writer(self.response)
        self.request.uri = get_absolute_path(request)
        self.request.str_cookies = request.COOKIES
        self.request.remote_addr = request.META['REMOTE_ADDR']
        self.request.get_all = lambda x: request.REQUEST.getlist(x)
import os
from tempfile import mkdtemp
from lxml import etree
import subprocess
from StringIO import StringIO
import zipfile
import time
import shutil
import zipfile
from qrcode import *

import utils
import collection2epcollxml
import collection2epxhtml

EPXHTML2PDF = utils.get_absolute_path(utils.DEFAULT_XSL_DIRECTORY, 'collection2pdf.xsl')
COLLECTION_HTML = 'collection.html'
QR_CODE_XPATH = etree.XPath('//x:div[@class=\'qr-code\' or @class=\'qr-code-gallery\']/x:img', namespaces=utils.NAMESPACES)
PDF_JS = 'css/pdf.js'

CONVERT_BIN = 'convert'

def _create_temp_directories(temp_dir):
    dirs = {}
    dirs['root'] = mkdtemp(suffix='-ep2pdf')
    if temp_dir:
        shutil.rmtree(dirs['root'], ignore_errors=True)
        dirs['root'] = temp_dir

    dirs['womi'] = os.path.join(dirs['root'], 'womi')
    if temp_dir:
def generate_mathml_md5_map(input_dir_with_epxhtml, output, dirs):
    try:
        print "[Py] \tProcessing module - create md5 from mathml"

        collxml_file = utils.get_absolute_path(input_dir_with_epxhtml, 'collection.xml')
        tmp_dir = os.path.abspath(dirs['content'])

        if os.path.isfile(collxml_file):
            collxml_xml = etree.parse(collxml_file)
            if collxml_xml != "":
                with open(os.path.join(output, 'collection.xml'), 'w', encoding='utf-8') as f:
                    f.write(etree.tostring(collxml_xml, pretty_print=True, encoding='unicode'))

                if os.path.exists(os.path.join(input_dir_with_epxhtml, "mappingGlossary.xml")) and os.path.exists(os.path.join(input_dir_with_epxhtml, "mappingGlossary.xml")):
                    shutil.copy(os.path.join(input_dir_with_epxhtml, "mappingGlossary.xml"), os.path.join(output, "mappingGlossary.xml"))

                all_svg_file = open(os.path.join(tmp_dir, 'svg.html'), "w", encoding='utf-8')
                all_svg_file.write(unicode("<svgs>", "utf-8"))

                svg_output= StringIO()
                math_handler = getMathHandler(svg_output)
                svg_output_value=''

                start = time.time()
                number_of_modules = len(MODULE_ID_XPATH(collxml_xml))
                math_flag = 0;
                for module_position, module_id in enumerate(MODULE_ID_XPATH(collxml_xml)):
                    if '_about_' in module_id and '_about_licenses' not in module_id:
                        module_filename = module_id + '_mobile_app.xhtml'
                    else:
                        module_filename = module_id + '.xhtml'
                    input_module_file = utils.get_absolute_path(input_dir_with_epxhtml, module_filename)
                    if os.path.isfile(input_module_file):
                        modulexml = etree.parse(input_module_file)
                        if modulexml != "":
                            mathmls = modulexml.findall('//mml:math', namespaces=utils.NAMESPACES)
                            number_of_mathmls = len(mathmls)
                            module_range = 100.0 / float(number_of_modules);
                            if number_of_mathmls > 0 and math_flag == 0:
                                sys.stdout.write("[mobile-app] \tProcessing mathmls to svgs: ")
                                math_flag = 1
                            display_progress(int((float(module_position + 1) / float(number_of_modules)) * 100))
                            for math_position, mathml in enumerate(mathmls):
                                display_progress(int((float(module_position + 1) / float(number_of_modules)) * 100) + int((float(math_position + 1) / float(number_of_mathmls)) * module_range))

                                mathml_string = etree.tostring(mathml, with_tail=False)
                                md5_id=create_md5_from_mathml(mathml_string)
                                isValidMathml=math2svg(math_handler, mathml_string, md5_id,module_id,'')

                                svg_output_value=svg_output.getvalue()
                                all_svg_file.write(unicode("<mathElement id=\"%s\">" % md5_id, "utf-8"))
                                if isValidMathml:
                                    all_svg_file.write(unicode(svg_output_value, "utf-8"))
                                else:
                                    all_svg_file.write(unicode("Wystąpił błąd w przetwarzaniu wzoru.", "utf-8"))

                                all_svg_file.write(unicode("</mathElement>", "utf-8"))
                                svg_output.seek(0)
                                svg_output.truncate(0)

                                addMathElement(mathml, md5_id, svg_output_value,isValidMathml)

                            with open(os.path.join(output, module_filename), 'w', encoding='utf-8') as f:
                                f.write(etree.tostring(modulexml, pretty_print=True, encoding='unicode'))

                svg_output.close()
                all_svg_file.write(unicode("</svgs>", "utf-8"))
                all_svg_file.close()
                sys.stdout.write("\nTime math2svg: ")
                print(time.time() - start)
    except Exception as e:
        print "[PY_ERR] error in fun 'generate_mathml_md5_map' in module_id=%s, with err=%s, mathml_string=[%s]" % (module_id,str(e),mathml_string)
        sys.exit(1)
from xml import sax
from io import open

sys.path.insert(0, os.getcwd() +'/SVGMath')
import math2svg
import xml_corrector
from svgmath.mathhandler import MathHandler
from svgmath.tools.saxtools import XMLGenerator

import utils
import collection2epcollxml
import collection2epxhtml
import importlib

DIR_PACKAGE_MOBILE_APP=utils.get_absolute_path_dir('package_mobile_app')
EPXHTML2MOBILEAPP = utils.get_absolute_path(utils.DEFAULT_XSL_DIRECTORY, 'collection2mobile_app.xsl')
SVGMATH_CONFIG = "SVGMath/svgmath.xml"
CONVERT_BIN = 'convert'

MATH_XPATH = etree.XPath('//mml:math', namespaces=utils.NAMESPACES)
SVG_XPATH = etree.XPath('//svg:svg', namespaces=utils.NAMESPACES)
SVG_HEIGHT_XPATH = etree.XPath('@height', namespaces=utils.NAMESPACES)
SVG_WIDTH_XPATH = etree.XPath('@width', namespaces=utils.NAMESPACES)
XPATH_NODE = etree.XPath('//node')

SVG_MATH_ID_XPATH = etree.XPath('parent::node()/@id', namespaces=utils.NAMESPACES)
MODULE_ID_XPATH = etree.XPath('//col:module/@document', namespaces=utils.NAMESPACES)
PMML_METADATA_XPATH = etree.XPath('svg:metadata/pmml2svg:baseline-shift/text()', namespaces=utils.NAMESPACES)

ADDITIONAL_FILES_MAIN = ['icons/awesome-icons.zip', 'icons/nav.zip', 'css/css.css', 'css/ep.modern.css', 'icons/img.zip',
                         'tooltips/showDlgModeInfo.html', 'js/js.zip', 'womi/briefcase.svg']
import argparse
import sys
import os
import subprocess
import utils
import shutil
import collection2epcollxml
import collection2epxhtml
from tempfile import mkdtemp

EPXHTML2HTML = utils.get_absolute_path(utils.DEFAULT_XSL_DIRECTORY, 'collection2html.xsl')

def _check_arguments(args):
    if not os.path.exists(args.input_dir) or not os.path.isfile(os.path.join(args.input_dir, utils.COLLECTION_XML)):
        print >> sys.stderr, "[PY_ERR][PY_HTML] -i input_dir must be directory containing collection.xml file"
        exit(1)
              
    if not os.path.exists(args.womi_dir):
        print >> sys.stderr, "[PY_ERR][PY_HTML] -w womi_dir: \"%s\" does not exist" % womi_dir
        exit(1)

def _check_exists_of_output_dir(output_dir):
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

def transform_epxhtml_to_html(input_dir_with_epxhtml, result_dir, womi_dir, variant):
    input_dir = os.path.abspath(input_dir_with_epxhtml)
    result_dir = os.path.abspath(result_dir)
    str_cmd = ['java', '-jar',
               utils.SAXON_JAR,
               '-s:' + input_dir + "/collection.xml",