Пример #1
0
 def test_matches(self):
     name = "2018-04-27.trades.doc.csv"
     result = expand_file(name)
     self.assertEqual(result.date, datetime.datetime(year=2018, month=4, day=27))
     self.assertEqual(result.ext, 'csv')
     self.assertEqual(result.document, 'doc')
     self.assertEqual(result.slug, 'trades')
Пример #2
0
 def identify(self, file: FileMemo) -> bool:
     try:
         fd = expand_file(file.name)
         if fd.slug:
             return True
     except:
         return False
Пример #3
0
 def test_matche3(self):
     name = "2020-01-03-aviator.pdf"
     result = expand_file(name)
     self.assertEqual(
         result.date,
         datetime.datetime(year=2020, month=1, day=3))
     self.assertEqual(result.ext, 'pdf')
     self.assertEqual(result.slug, 'aviator')
     self.assertEqual(result.document, None)
Пример #4
0
 def test_matche4(self):
     name = "2018-09-27-ledger-USD-deposit.csv"
     result = expand_file(name)
     self.assertEqual(
         result.date,
         datetime.datetime(year=2018, month=9, day=27)
     )
     self.assertEqual(result.ext, 'csv')
     self.assertEqual(result.slug, 'ledger-usd-deposit')
     self.assertEqual(result.document, None)
Пример #5
0
 def test_since_date_seq(self):
     file = pathlib.Path("2018-09-27.s2018-09-01.nbi-1857.export.2.csv")
     result = expand_file(file)
     self.assertEqual(
         result,
         FileDetails(
             file_name=file.name,
             date=datetime.datetime(year=2018, month=9, day=27),
             from_date=datetime.datetime(year=2018, month=9, day=1),
             slug="nbi-1857",
             ext="csv",
             file=file,
             document='export',
             seq=2
         )
     )
Пример #6
0
 def test_with_match_seq(self):
     return
     file = pathlib.Path("2020-01-20.paraiso-meralco.statment.pdf")
     result = expand_file(file)
     assert (
         result ==
         FileDetails(
             file_name=file.name,
             date=datetime.datetime(year=2020, month=1, day=20),
             from_date=None,
             slug="paraiso-meralco",
             ext="pdf",
             file=file,
             document='statement',
             seq=0
         )
     )
     self.assertEqual(file.name, result.make_name)
Пример #7
0
    def file_name(self, file):
        """Returns the name without the date portion"""
        logger.debug(f"file_name({file})")
        fd = expand_file(file)

        return f"{fd.slug}.{fd.document}.{fd.ext}".lower()
Пример #8
0
 def file_date(self, file) -> datetime.datetime:
     logger.debug(f"file_date({file}")
     fd = expand_file(file)
     return fd.date
Пример #9
0
 def file_account(self, file):
     logger.debug(f"file_account({file}")
     fd = expand_file(file)
     if fd is None:
         return None
     return self.slugs[fd.slug]
Пример #10
0
def filing_handler(source_directories: typing.List[pathlib.Path],
                   destination: pathlib.Path,
                   slugs: typing.Dict[str, str],
                   dry_run=False):
    """Recurse through a list of source directories looking for filing matching a regular expression format:

    Args:
        source_directories (list): Source Folders to search
        destination: Single target folder, we will create Assests/Liabilities under this
        slugs: slug dict to use.
        dry_run: if True we dont' do any real work, just print out details

    Returns:
        None -- Moves files instead
    """

    for folder in source_directories:
        for file in folder.rglob("*"):
            match = expand_file(file)
            if match is None:
                continue

            account = slugs.get(match.slug, None)
            if not account:
                account = slugs.get(match.slug.replace('-', ''), None)
            if not account:
                logger.info(
                    f"Unable to find matching account for slug {match.slug}. [{match.file}]"
                    f"\n{pprint.pformat(slugs)}")
                continue

            sub_directory = account.replace(':', '/')
            target_directory = destination.joinpath(sub_directory)

            # Just incase
            target_file = target_directory.joinpath(match.make_name)
            count = 0

            if target_file == file:
                # noop
                continue

            # Rename duplicate files
            possible_target: pathlib.Path = target_file
            sep = '.'
            while possible_target.exists():
                print(f"Found matching file {possible_target}")
                count += 1
                possible_target = target_file.parent.joinpath(
                    target_file.stem + sep + str(count) + target_file.suffix)

            if not dry_run:
                if not (target_directory.exists()
                        and target_directory.is_dir()):
                    print(f"mkdir {target_directory}")
                target_directory.mkdir(parents=True, exist_ok=True)
                file.rename(possible_target)
                print(f"MOVED {file} -> {possible_target}")
            else:
                if not (target_directory.exists()
                        and target_directory.is_dir()):
                    logger.info(f"DRY: mkdir {target_directory}")
                    print(f"DRY: mkdir {target_directory}")
                logger.info(f"DRY: mv {file} -> {possible_target}")
                print(f"mv {file} -> {possible_target}")