Exemplo n.º 1
0
def fetch_spacex_last_launch(images_dir: Path) -> None:
    response: Response = requests.get(
        'https://api.spacexdata.com/v4/launches/latest')
    response.raise_for_status()
    images: list = response.json()['links']['flickr']['original']
    for image_number, image_url in enumerate(images):
        ext: str = get_file_extension(image_url)
        save_pictures('spacex{}.{}'.format(image_number, ext), image_url,
                      images_dir)
Exemplo n.º 2
0
def get_hubble_image(foto_id: int, images_dir: Path) -> None:
    response: Response = requests.get(
        'http://hubblesite.org/api/v3/image/{}'.format(foto_id))
    response.raise_for_status()
    picture_link: str = response.json()['image_files'][-1]['file_url']
    picture_link = picture_link.replace('//', 'https://')
    ext: str = get_file_extension(picture_link)
    filename: str = 'hubble{}.{}'.format(foto_id, ext)
    save_pictures(filename, picture_link, images_dir)
Exemplo n.º 3
0
def analyze_file_code_convention(filename=''):
    '''
    This RESTful API analyze file
    '''
    # TODO!!!!!!
    if util.get_file_extension(filename) == 'py':
        log_path = util.py_file_code_convention_analysis('test.py')
        fp_log = open(log_path, 'r')
        content = fp_log.read()
        fp_log.close()
        return content
Exemplo n.º 4
0
Arquivo: tasks.py Projeto: quru/qis
def burst_pdf(**kwargs):
    """
    A task that creates a sub-folder next to a PDF file and extracts all
    pages from the PDF as PNG files into the sub-folder.
    """
    from flask_app import app
    from filesystem_manager import get_abs_path, get_burst_path, get_file_data
    from filesystem_manager import delete_dir, make_dirs, path_exists
    from filesystem_sync import delete_folder
    from imagemagick import imagemagick_burst_pdf
    from models import Folder
    from util import get_file_extension

    (src,) = _extract_parameters(["src"], **kwargs)
    burst_folder_rel = get_burst_path(src)

    # Ensure src is a PDF
    if get_file_extension(src) not in app.config["PDF_FILE_TYPES"]:
        app.log.warn("Cannot burst non-PDF file: " + src)
        return

    # See if the burst folder already exists (in the database and on disk)
    db_folder = app.data_engine.get_folder(folder_path=burst_folder_rel)
    if db_folder is not None and db_folder.status == Folder.STATUS_ACTIVE:
        # Wipe the folder, old images, data, and uncache the old images
        delete_folder(db_folder, None, app.data_engine, None, app.log)
        deleted_ids = app.data_engine.list_image_ids(db_folder)
        for image_id in deleted_ids:
            app.image_engine._uncache_image_id(image_id)

    # See if the burst folder already exists (just on disk)
    if path_exists(burst_folder_rel, require_directory=True):
        # Wipe the folder and old images
        delete_dir(burst_folder_rel, recursive=True)

    # Create the burst folder and burst
    pdf_data = get_file_data(src)
    if pdf_data is not None:
        make_dirs(burst_folder_rel)
        burst_folder_abs = get_abs_path(burst_folder_rel)
        if not imagemagick_burst_pdf(pdf_data, burst_folder_abs, app.config["PDF_BURST_DPI"]):
            app.log.warn("Failed to burst PDF: " + src)
    else:
        app.log.warn("Cannot burst PDF, file not found: " + src)
Exemplo n.º 5
0
 def __init__(self, src, db_id=-1, page=None,
              iformat=None, template=None, width=None, height=None,
              align_h=None, align_v=None, rotation=None, flip=None,
              top=None, left=None, bottom=None, right=None, crop_fit=None,
              size_fit=None, fill=None, quality=None, sharpen=None,
              overlay_src=None, overlay_size=None, overlay_pos=None, overlay_opacity=None,
              icc_profile=None, icc_intent=None, icc_bpc=None, colorspace=None,
              strip=None, dpi=None, tile_spec=None):
     self._version = self.__class__.class_version
     self._filename = filepath_normalize(src)
     self._filename_ext = get_file_extension(self._filename)
     self._db_id = db_id
     self._page = page
     self._format = self._no_blank(iformat)
     self._template = self._no_blank(template)
     self._width = width
     self._height = height
     self._align_h = self._no_blank(align_h)
     self._align_v = self._no_blank(align_v)
     self._rotation = rotation
     self._flip = self._no_blank(flip)
     self._top = top
     self._left = left
     self._bottom = bottom
     self._right = right
     self._crop_fit = crop_fit
     self._size_fit = size_fit
     self._fill = self._no_blank(fill)
     self._quality = quality
     self._sharpen = sharpen
     self._overlay_src = self._no_blank(overlay_src)
     self._overlay_size = overlay_size
     self._overlay_pos = self._no_blank(overlay_pos)
     self._overlay_opacity = overlay_opacity
     self._icc_profile = self._no_blank(icc_profile)
     self._icc_intent = self._no_blank(icc_intent)
     self._icc_bpc = icc_bpc
     self._colorspace = self._no_blank(colorspace)
     self._strip = strip
     self._dpi_x = dpi
     self._dpi_y = dpi
     self._tile = tile_spec
     self._round_floats()
    def get_track(self, track) -> None:
        tracks = self.urlService.get_track_request(track)
        models = tracks.json()['models']
        for i in models:
            chal_slug = i.get('slug')
            sub_domain = i.get('track').get('slug')
            if chal_slug is None:
                raise Exception("Chal_slug:" + str(chal_slug))

            sub_domain_string = "Domain: " + sub_domain
            print(track + " " + sub_domain_string +
                  chal_slug.rjust(70 - len(sub_domain_string)))

            sub_id = self.get_submissions(chal_slug)
            code = False
            if sub_id:
                result = self.get_code(chal_slug, sub_id)
                code = result['code']
                lang = result['language']

            if code:
                ext = get_file_extension(track, lang)
                self.create_code_file(track, sub_domain, chal_slug, code, ext)
Exemplo n.º 7
0
def file_extension_filter(filename):
    """
    A template filter to return the file extension of a file name.
    """
    return get_file_extension(filename)
Exemplo n.º 8
0
def auto_sync_existing_file(rel_path, data_manager, task_manager,
                            anon_history=True, burst_pdf='auto', _db_session=None):
    """
    Returns the database record for an image file that is known to exist,
    creating a new record or un-deleting an old record if required,
    and always returning a value.

    This method creates anonymous image history entries when anon_history is
    True. If the current user should be recorded against an action, the caller
    should set anon_history to False and manually add a history record.

    The bursting of PDF files is also initiated here. By default, a PDF file
    will be burst if no burst folder already exists. Setting burst_pdf to False
    disables this, or setting burst_pdf to True will force it to be burst again.

    Raises a DoesNotExistError if the image path is in fact invalid.
    Raises a SecurityError if the image path is outside of IMAGES_BASE_DIR.
    Raises a DBError if the database record cannot be created.
    """
    db_own = (_db_session is None)
    db_session = _db_session or data_manager.db_get_session()
    db_error = False
    try:
        # Get (or create) db record for the file
        on_create = on_image_db_create_anon_history if anon_history \
            else on_image_db_create
        db_image = data_manager.get_or_create_image(
            rel_path, on_create, _db_session=db_session
        )
        if not db_image:
            # Not expected
            raise DBError('Failed to add image to database: ' + rel_path)

        # Burst PDF if we need to
        # TODO This would be better in on_image_db_create if we can get a task_manager without
        #      importing the one from flask_app. Needs to be compatible with the task server.
        if burst_pdf and app.config['PDF_BURST_TO_PNG']:
            can_burst = get_file_extension(rel_path) in app.config['PDF_FILE_TYPES']
            if can_burst:
                if burst_pdf == 'auto':
                    burst_pdf = not path_exists(
                        get_burst_path(rel_path),
                        require_directory=True
                    )
                if burst_pdf:
                    burst_pdf_file(rel_path, task_manager)

        if db_image.status == Image.STATUS_ACTIVE:
            # The normal case
            return db_image
        else:
            # We need to undelete the database record
            db_image.status = Image.STATUS_ACTIVE
            if anon_history:
                on_image_db_create_anon_history(db_image)
            else:
                on_image_db_create(db_image)

            # Check whether the file's folder needs to be undeleted too
            if db_image.folder.status == Folder.STATUS_DELETED:
                auto_sync_existing_folder(
                    db_image.folder.path,
                    data_manager,
                    _db_session=db_session
                )
            return db_image
    except:
        db_error = True
        raise
    finally:
        if db_own:
            try:
                if db_error:
                    db_session.rollback()
                else:
                    db_session.commit()
            finally:
                db_session.close()