text: '''
  Utility functions for generating filestructure for newspaper research
  '''
  # %%
  import os
  import datetime
  
  from mdutils.mdutils import MdUtils
  
  from .util import resolve_path
  
  # %%
  # class for making the
  
  
  class FileStructure:
      def __init__(self, parent_dir=None, work_dir=None, custom_name=False) -> None:
          '''[summary]
  
          Parameters
          ----------
          parent_dir : str, optional
              Parent folder for `work_dir`, must exist.
              Optional if existing path is specified in `work_dir`
  
          work_dir : str, optional
              Can be 
              a) absolute path to existing work_dir
              b) name of folder to create (`if custom_work_dir`)
              c) tag to include in autogenerated folder name that will be created
  
          custom_work_dir : bool, optional
              Use `work_dir` as name of the folder if True.
              Completely autogenerated folder name is made if False.
              Autogenerated name that includes `work_dir` if work_dir and not custom_work dir.
          '''
  
          if not parent_dir:
              # if user already has a work_dir, just validate and save the path
              if os.path.exists(work_dir):
                  self.work_dir_path = work_dir
  
          elif parent_dir:
              # if user wants a work_dir to be made
              # assumes you already have a parent_dir
              self.parent_dir = resolve_path(parent_dir)
  
              if custom_name:
                  # user wants custom work_dir name
                  work_dir_name = work_dir
  
              else:
                  # user wants an autogenerated name ...
  
                  if work_dir:
                      # ... with work_dir as tag
                      yymmdd = datetime.datetime.now().strftime('%y%m%d')
                      work_dir_name = yymmdd + '_' + work_dir
  
                  else:
                      # ... that is completely autogenerated
                      work_dir_name = datetime.datetime.now().strftime('%y%m%d%H%M')
  
              # find path
              work_dir_path = os.path.join(self.parent_dir, work_dir_name)
              if not os.path.exists(work_dir_path):
                  self.work_dir_path = work_dir_path
              else:
                  raise FileExistsError(
                      '{} already exists!'.format(work_dir_path))
  
      def gendir_work(self) -> None:
          if not os.path.exists(self.work_dir_path):
              os.mkdir(self.work_dir_path)
  
      @staticmethod
      def gendir_sub(parent_path, child_name) -> None:
          # generate a child directory in word_dir
          subdir_path = os.path.join(parent_path, child_name)
          os.mkdir(subdir_path)
  
      def create_readme(self):
          '''saves a .md file with descritption of a maximal setup
  
          TODO
          minimal readme just for subset making
              - n articles
              - regex patterns used for subsetting
          '''
  
          readme = MdUtils(
              file_name=os.path.join(self.work_dir_path, 'README'),
              title='Subset readme'
          )
  
          # produce content
          date_generated = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  
          # SECTION 1: DATE GENERATED
          readme.new_paragraph()
          readme.new_header(level=1, title='Date', style='setext')
          readme.new_line(date_generated)
  
          # SECTION 2: FOLDER STRUCTURE SCPECIFICATION
          readme.new_paragraph()
          readme.new_header(level=1, title='File structure', style='setext')
          # top folder: workdir
          readme.new_line('```')
          readme.new_line('{}/'.format(os.path.basename(self.work_dir_path)))
          readme.new_line('├── data_subset/  #raw data used for this analysis')
          readme.new_line('│   └── *.ndjson')
          readme.new_line('├── preprocessed/  #preprocessed data')
          readme.new_line('│   └── *.ndjson')
          readme.new_line('├── model_input/  #dataset used for topic modeling')
          readme.new_line('│   └── *.ndjson')
          readme.new_line('├── mdl_lda/  #outputs of LDA')
          readme.new_line('│   ├── model_states/  #serialized trained LDAs')
          readme.new_line('│   │   └── *.pkl')
          readme.new_line('│   ├── topic_overviews/  #top terms per topic')
          readme.new_line('│   │   └── *.txt')
          readme.new_line('│   ├── doc_top_mat/  #document-topic matrices')
          readme.new_line('│   │   └── *.ndjson')
          readme.new_line('│   ├── pyldavis/  #pyldavis plots')
          readme.new_line('│   │   └── *.html')
          readme.new_line(
              '│   └── model_comparison.png  #elbow plot of n topics and coherence')
          readme.new_line(
              '└── mdl_ntr/  #outputs of Novelty, Transience, Resonance')
          readme.new_line('    └── w{number}/  #results at window n')
          readme.new_line('        ├── *.csv')
          readme.new_line('        └── fig/  #plots of results at that window')
          readme.new_line('            └── *.png')
          readme.new_line('```')
          readme.new_line()
  
          readme.create_md_file()
  
      def gendir_maximal_setup(self) -> None:
          '''creates a bunch of folders for the whole analysis
          '''
          # create a working directory
          self.gendir_work()
  
          # generate top level childs
          top_childs = ['data_subset', 'preprocessed',
                        'model_input', 'mdl_lda', 'mdl_ntr']
          for folder in top_childs:
              self.gendir_sub(
                  parent_path=self.work_dir_path,
                  child_name=folder
              )
  
          # generate mdl_lda childs
          mdl_lda_childs = ['model_states',
                            'topic_overviews', 'doc_top_mat', 'pyldavis']
          for folder in mdl_lda_childs:
              self.gendir_sub(
                  parent_path=os.path.join(self.work_dir_path, 'mdl_lda'),
                  child_name=folder
              )
  
          self.create_readme()
  
      def gendir_minimal_setip(self) -> None:
          # transfer minimal folders and readme from querymaking.py
          pass
  
      # # UNFINNISHED BULLSHIT
      # def gendir_subset_manyfiles(self):
      #     # dir to host output of Query in multiple files
      #     pass
  
      # def gendir_subset_singlefile(self):
      #     # dir to host output of Query in a single file
      #     pass
  
      # def gendir_preprocessing(self):
      #     # dir to host output of preprocessing
      #     pass
  
      # def gendir_grouped_files(self):
      #     # dir to host input data for preprocessing
      #     pass
  
      # def gendir_topics(self):
      #     # dir to host topic modeling outputs
      #     pass
  
      # def gendir_infodynamics(self):
      #     # dir to host infodynamics singal files
      #     pass
  
      # def gendir_plots(self):
      #     pass
  
      # def gen_driver_py(self):
      #     # generate a python script, for the analysis,
      #     # where user can play around with the parameters
      #     pass
  